forked from jeala/DEPW
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Character-Player.cs
120 lines (109 loc) · 3.16 KB
/
Character-Player.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace MathInfection
{
class Player : ICharacter
{
private readonly Texture2D texture;
private Vector2 position;
private Vector2 velocity;
private Vector2 characterSize;
private Vector2 windowSize;
private int health;
private bool wasHit;
private bool startBoost;
public Player(Texture2D tex, Vector2 pos, Vector2 vel, Vector2 cSize, Vector2 wSize)
{
texture = tex;
position = pos;
velocity = vel;
characterSize = cSize;
windowSize = wSize;
health = 100;
wasHit = false;
startBoost = false;
}
public Vector2 CharacterSize
{
get
{
return characterSize;
}
}
public Vector2 WindowSize
{
get
{
return windowSize;
}
}
public bool StartBoost
{
set
{
startBoost = value;
}
}
public bool IsAlive()
{
return health > 0;
}
public void update()
{
if (wasHit)
{
health--;
wasHit = false;
}
if(IsAlive())
{
Vector2 speed = velocity;
if(startBoost)
{
speed = velocity * 2;
}
if(Keyboard.GetState().IsKeyDown(Keys.W) || GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Y > .25)
{
position.Y -= speed.Y;
}
if(Keyboard.GetState().IsKeyDown(Keys.S) || GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Y < -.25)
{
position.Y += speed.Y;
}
if(Keyboard.GetState().IsKeyDown(Keys.A) || GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X < -.25)
{
position.X -= speed.X;
}
if(Keyboard.GetState().IsKeyDown(Keys.D) || GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X > .25)
{
position.X += speed.X;
}
StopEdge();
}
}
public void draw(SpriteBatch sb)
{
sb.Draw(texture, position, Color.White);
}
private void StopEdge()
{
if(position.X < 0)
{
position.X = 0;
}
if(position.Y < 0)
{
position.Y = 0;
}
if(position.X + characterSize.X > windowSize.X)
{
position.X = windowSize.X - characterSize.X;
}
if(position.Y + characterSize.Y > windowSize.Y)
{
position.Y = windowSize.Y - characterSize.Y;
}
}
}
}