-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cs
260 lines (246 loc) · 7.96 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Slip.Puzzles;
namespace Slip
{
public class Player
{
public static Texture2D[] textures = new Texture2D[(int)Direction.Count];
public static Texture2D slashTexture;
public const int size = 20;
public const int attackDistance = 20;
public Vector2 position;
public Direction direction = Direction.Down;
private int attackTimer = 0;
public int life = 1;
public int maxLife = 1;
public int exp = 0;
public int invincible = 0;
public bool debug = false;
public Checkpoint lastCheckpoint;
public int silverKeys = 0;
public Vector2 TopLeft
{
get
{
return position - 0.5f * new Vector2(size, size);
}
set
{
position = value + 0.5f * new Vector2(size, size);
}
}
public Hitbox hitbox
{
get
{
return new Hitbox(TopLeft, size, size);
}
}
public float radius
{
get
{
return size / 2f;
}
}
public bool Attacking
{
get
{
return attackTimer > 0;
}
}
public bool AttackCanDamage
{
get
{
return attackTimer > 5;
}
}
public void Update(Room room)
{
if (Main.IsControlPressed(KeyControl.Debug))
{
debug = !debug;
}
if (Attacking)
{
attackTimer--;
}
if (!Attacking && Main.IsControlPressed(KeyControl.Action))
{
Action(room);
}
if (Attacking)
{
Attack(room);
}
else
{
Move(room);
}
Hitbox box = hitbox;
int left = (int)Math.Floor(box.topLeft.X / Tile.tileSize);
int right = (int)Math.Ceiling(box.topRight.X / Tile.tileSize);
int top = (int)Math.Floor(box.topLeft.Y / Tile.tileSize);
int bottom = (int)Math.Ceiling(box.bottomLeft.Y / Tile.tileSize);
for (int x = left; x < right; x++)
{
for (int y = top; y < bottom; y++)
{
Puzzle puzzle = room.tiles[x, y].puzzle;
if (puzzle != null)
{
puzzle.OnPlayerCollide(room, x, y, this);
}
}
}
if (invincible > 0)
{
invincible--;
}
}
public void Move(Room room)
{
Vector2 velocity = Vector2.Zero;
if (Main.IsControlHeld(KeyControl.Up))
{
velocity.Y -= 1f;
}
if (Main.IsControlHeld(KeyControl.Down))
{
velocity.Y += 1f;
}
if (Main.IsControlHeld(KeyControl.Left))
{
velocity.X -= 1f;
}
if (Main.IsControlHeld(KeyControl.Right))
{
velocity.X += 1f;
}
if (velocity != Vector2.Zero)
{
velocity.Normalize();
if (Main.IsControlHeld(KeyControl.Focus))
{
velocity *= 1.5f;
}
else
{
velocity *= 4f;
}
Helper.GetDirection(velocity, ref direction);
bool collided;
TopLeft = Collision.MovePos(TopLeft, size, size, velocity, room, out collided);
}
}
public void Action(Room room)
{
Vector2 checkDirection = Helper.DirectionToVector2(direction);
float checkPosOffset = size;
if (checkDirection.X != 0f && checkDirection.Y != 0f)
{
checkPosOffset *= (float)Math.Sqrt(2);
}
Vector2 checkPos = position + checkPosOffset * checkDirection;
checkPos /= Tile.tileSize;
if (checkDirection.X > 0f && checkPos.X == (float)Math.Floor(checkPos.X))
{
checkPos.X -= 1f;
}
if (checkDirection.Y > 0f && checkPos.Y == (float)Math.Floor(checkPos.Y))
{
checkPos.Y -= 1f;
}
int x = (int)checkPos.X;
int y = (int)checkPos.Y;
Puzzle puzzle = room.tiles[x, y].puzzle;
if (puzzle != null && puzzle.PlayerInteraction(room, x, y, this))
{
return;
}
attackTimer = 15;
}
public void Attack(Room room)
{
if (!AttackCanDamage)
{
return;
}
float attackDir = Helper.DirectionToRotation(direction);
float angle1 = attackDir - 0.25f * (float)Math.PI;
float angle2 = attackDir + 0.25f * (float)Math.PI;
float reach = radius + attackDistance;
int i = 0;
while (i < room.enemies.Count)
{
Enemy enemy = room.enemies[i];
if (Collision.SectorCollides(position, reach, angle1, angle2, enemy.position, enemy.radius))
{
if (enemy.TakeDamage(1, room, this))
{
i--;
}
}
i++;
}
}
public void TakeDamage(int damage)
{
if (invincible <= 0 && !debug)
{
life -= damage;
if (life < 0)
{
life = 0;
}
invincible = 60;
}
}
public void Revive(GameScreen screen)
{
life = maxLife;
attackTimer = 0;
screen.ChangeRoom(lastCheckpoint.room, lastCheckpoint.position);
}
public void Draw(GameScreen screen, Main main)
{
Color color = Color.White;
if (invincible > 0)
{
color *= 0.75f;
}
Texture2D texture = textures[(int)direction];
main.spriteBatch.Draw(texture, screen.DrawPos(main, position), null, color, texture.Center());
if (Attacking)
{
color = Color.White;
if (!AttackCanDamage)
{
color *= 0.5f;
}
float rotation = Helper.DirectionToRotation(direction);
main.spriteBatch.Draw(slashTexture, screen.DrawPos(main, position), null, color, rotation,
slashTexture.Center(), 1f, SpriteEffects.None, 1f);
}
}
public static void LoadContent(ContentManager loader)
{
textures[(int)Direction.Down] = loader.Load<Texture2D>("Player/Player_Down");
textures[(int)Direction.DownLeft] = loader.Load<Texture2D>("Player/Player_DownLeft");
textures[(int)Direction.Left] = loader.Load<Texture2D>("Player/Player_Left");
textures[(int)Direction.UpLeft] = loader.Load<Texture2D>("Player/Player_UpLeft");
textures[(int)Direction.Up] = loader.Load<Texture2D>("Player/Player_Up");
textures[(int)Direction.UpRight] = loader.Load<Texture2D>("Player/Player_UpRight");
textures[(int)Direction.Right] = loader.Load<Texture2D>("Player/Player_Right");
textures[(int)Direction.DownRight] = loader.Load<Texture2D>("Player/Player_DownRight");
slashTexture = loader.Load<Texture2D>("Slash");
}
}
}