-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cs
More file actions
224 lines (199 loc) · 6.12 KB
/
Player.cs
File metadata and controls
224 lines (199 loc) · 6.12 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace ProgGame
{
public class Player
{
private const float MaxJumpTime = 0.4f;
private const float JumpForce = -1500.0f;
private const float GravityAcceleration = 3400.0f;
private const float MaxVerticalSpeed = 400.0f;
private const float HorizontalPowerInJump = 0.15f;
private const float MoveAcceleration = 10000.0f;
private const float MaxHorizontalSpeed = 1750.0f;
private const float GroundDrag = 0.5f;
private const float AirDrag = 0.57f;
private Texture2D textureToDraw;
private Texture2D normalTexture;
private Texture2D deadTexture;
public Vector2 position;
public Vector2 velocity;
private bool wantsJump;
private bool wasJumping;
public bool grounded;
private float jumpTime;
private float movement;
private float previousBottom;
public bool isAlive;
public bool onPlatform;
private GameLevel level;
private Rectangle localBounds;
private SpriteEffects spriteEffects;
public Rectangle BoundingRectangle
{
get
{
int left = (int)Math.Round(position.X - (normalTexture.Height / 2)) + localBounds.X;
int top = (int)Math.Round(position.Y - normalTexture.Height) + localBounds.Y;
return new Rectangle(left, top, localBounds.Width, localBounds.Height);
}
}
public Player(GameLevel level, Vector2 position)
{
this.position = position;
this.level = level;
normalTexture = level.content.Load<Texture2D>("player");
deadTexture = level.content.Load<Texture2D>("player_dead");
int width = (int)(normalTexture.Width * 0.4);
int left = (normalTexture.Width - width) / 2;
int height = (int)(normalTexture.Width * 0.8);
int top = normalTexture.Height - height;
localBounds = new Rectangle(left, top, width, height);
wantsJump = false;
wasJumping = false;
isAlive = true;
grounded = false;
}
public void Draw(SpriteBatch spriteBatch)
{
if (velocity.X > 0)
spriteEffects = SpriteEffects.None;
else if (velocity.X < 0)
spriteEffects = SpriteEffects.FlipHorizontally;
textureToDraw = isAlive ? normalTexture : deadTexture;
spriteBatch.Draw(textureToDraw, position, null, Color.White, 0.0f, new Vector2(textureToDraw.Width / 2, textureToDraw.Height), 1.0f, spriteEffects, 0.0f);
}
public void Update(GameTime gameTime)
{
ProcessInput();
DoPhysics(gameTime);
movement = 0;
wantsJump = false;
}
public void ProcessInput()
{
if (!isAlive)
return;
KeyboardState state = Keyboard.GetState();
if (state.IsKeyDown(Keys.A) || state.IsKeyDown(Keys.Left))
movement = -1.0f;
if (state.IsKeyDown(Keys.D) || state.IsKeyDown(Keys.Right))
movement = 1.0f;
if (state.IsKeyDown(Keys.W) || state.IsKeyDown(Keys.Space) || state.IsKeyDown(Keys.Up))
wantsJump = true;
}
public void OnKilled()
{
isAlive = false;
}
public void Respawn(Vector2 position)
{
isAlive = true;
this.position = position;
velocity = Vector2.Zero;
}
public void DoPhysics(GameTime gameTime)
{
Vector2 positionBefore = position;
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
velocity.X += movement * MoveAcceleration * elapsed;
velocity.Y = MathHelper.Clamp(velocity.Y + GravityAcceleration * elapsed, -MaxVerticalSpeed, MaxVerticalSpeed);
velocity.Y = Jump(velocity.Y, gameTime);
if (grounded)
velocity.X *= GroundDrag;
else
velocity.X *= AirDrag;
velocity.X = MathHelper.Clamp(velocity.X, -MaxHorizontalSpeed, MaxHorizontalSpeed);
if (onPlatform)
{
position = new Vector2(position.X, position.Y - MovingPlatform.movingSpeed);
if (!wantsJump || jumpTime == 0)
velocity.Y = 0;
}
position += velocity * elapsed;
position = new Vector2((float)Math.Round(position.X), (float)Math.Round(position.Y));
ProcessCollision();
if (position.X == positionBefore.X)
velocity.X = 0;
if (position.Y == positionBefore.Y)
velocity.Y = 0;
}
public float Jump(float velocity, GameTime gameTime)
{
if (wantsJump)
{
if ((!wasJumping && (grounded || onPlatform)) || jumpTime > 0.0f)
{
if (jumpTime == 0.0f)
{
// TODO: звук прыжка
}
jumpTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
}
if (jumpTime > 0.0f && jumpTime <= MaxJumpTime)
{
velocity = JumpForce * (1.0f - (float)Math.Pow(jumpTime / MaxJumpTime, HorizontalPowerInJump));
}
else
jumpTime = 0;
}
else
jumpTime = 0;
wasJumping = wantsJump;
return velocity;
}
private void ProcessCollision()
{
Rectangle bounds = BoundingRectangle;
int leftTile = (int)Math.Floor((float)bounds.Left / Tile.width);
int rightTile = (int)Math.Ceiling((float)bounds.Right / Tile.width) - 1;
int topTile = (int)Math.Floor((float)bounds.Top / Tile.height);
int bottomTile = (int)Math.Ceiling((float)bounds.Bottom / Tile.height) - 1;
grounded = false;
for (int y = topTile; y <= bottomTile; ++y)
{
for (int x = leftTile; x <= rightTile; ++x)
{
CollisionType collision = level.GetCollisionType(x, y);
if (collision != CollisionType.Air)
{
Rectangle tileBounds = level.GetTileBounds(x, y);
Vector2 depth = Extentions.GetIntersectionDepth(bounds, tileBounds);
if (depth != Vector2.Zero)
{
if (collision == CollisionType.FakeSolid)
{
level.tiles[x, y].texture = null;
continue;
}
float absDepthX = Math.Abs(depth.X);
float absDepthY = Math.Abs(depth.Y);
if (absDepthY < absDepthX)
{
if (previousBottom <= tileBounds.Top)
grounded = true;
if (collision == CollisionType.Solid || grounded)
{
position = new Vector2(position.X, position.Y + depth.Y);
bounds = BoundingRectangle;
}
}
else if (collision == CollisionType.Solid)
{
position = new Vector2(position.X + depth.X, position.Y);
bounds = BoundingRectangle;
}
}
}
}
}
previousBottom = bounds.Bottom;
}
}
}