-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cs
152 lines (138 loc) · 4.88 KB
/
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RPG
{
class Player
{
// fields
private Vector2 position = new Vector2(100, 100);
private int speed = 200;
private Dir direction = Dir.Down;
private bool isMoving = false;
public AnimatedSprite anim;
public AnimatedSprite[] animations = new AnimatedSprite[4]; // 4 to hold the 4 enums
private KeyboardState kStateOld = Keyboard.GetState();
public int health = 3;
private int radius = 20;
private float healthTimer = 0f;
// properties
public float HealthTimer
{
get { return healthTimer; }
set { healthTimer = value; }
}
public int Radius
{
get { return radius; }
}
public int Health
{
get { return health; }
set { health = value; }
}
// using a set for a Vector2 is tricker and is done below
public Vector2 Position {
get{ return position; }
}
public void setX(float newX)
{
position.X = newX;
}
public void setY(float newY)
{
position.Y = newY;
}
// Methods
// player movement
public void Update(GameTime gameTime, int mapW, int mapH) // int mapW, int mapH is for the right and bottom camera edges
{
KeyboardState kstate = Keyboard.GetState();
float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
// so players health doesnt decline too fast when hit
if (healthTimer > 0)
{
healthTimer -= dt;
}
// set the animation based on direction player is facing
anim = animations[(int)direction];
// this cycles through the player animations
if (isMoving)
anim.Update(gameTime);
else
anim.setFrame(1); // return animation to standing still frame
// this means every frame the player stops moving
isMoving = false;
// however if the keys are held, player will move
if (kstate.IsKeyDown(Keys.Right))
{
direction = Dir.Right;
isMoving = true;
}
if (kstate.IsKeyDown(Keys.Left))
{
direction = Dir.Left;
isMoving = true;
}
if (kstate.IsKeyDown(Keys.Up))
{
direction = Dir.Up;
isMoving = true;
}
if (kstate.IsKeyDown(Keys.Down))
{
direction = Dir.Down;
isMoving = true;
}
// movement speed
if (isMoving)
{
Vector2 tempPos = position; // for obstable collision
switch (direction)
{
case Dir.Down:
tempPos.Y += speed * dt; // put next move into variable and pass to Obstacle.didCollide
if (!Obstacle.didCollide(tempPos, radius) && tempPos.Y < mapH) // if collision is NOT made and not at edge of screen
{
position.Y += speed * dt; // do the actual movement
}
break;
case Dir.Up:
tempPos.Y -= speed * dt;
if (!Obstacle.didCollide(tempPos, radius) && tempPos.Y > 0)
{
position.Y -= speed * dt;
}
break;
case Dir.Left:
tempPos.X -= speed * dt;
if (!Obstacle.didCollide(tempPos, radius) && tempPos.X > 0)
{
position.X -= speed * dt;
}
break;
case Dir.Right:
tempPos.X += speed * dt;
if (!Obstacle.didCollide(tempPos, radius) && tempPos.X < mapW)
{
position.X += speed * dt;
}
break;
default:
break;
}
}
// when spacebar is pressed, create ONE projectile
if (kstate.IsKeyDown(Keys.Space) && kStateOld.IsKeyUp(Keys.Space))
{
Projectile.projectiles.Add(new Projectile(position, direction));
MySounds.projectileSound.Play(1f, 0.5f, 0f);
}
kStateOld = kstate;
}
}
}