-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.cs
75 lines (63 loc) · 2.81 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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System;
namespace Mono3D
{
public class Player
{
private const float Pi = (float)Math.PI;
private Vector3 position; // Player position
private Vector2 angle; // Player angle
private Vector3 direction; // Current player direction
private Vector2 rotation; // Current player rotation
private const float Speed = 3; // Player movement speed
private const float Spin = 1; // Player rotation speed
public Vector3 GetPosition() => position;
public Vector2 GetAngle() => angle;
public Player() { }
public void Update(GameTime gameTime, Game1 game)
{
float delta = (float)gameTime.ElapsedGameTime.TotalSeconds; // Get time delta
ProcessKeyboardState(game); // Process keyboard state
MovePlayer(delta); // Move player by delta
}
private void ProcessKeyboardState(Game1 game)
{
KeyboardState state = game.KeyboardState;
// Get movement direction
if (state.IsKeyDown(Keys.W)) direction.X = 1;
else if (state.IsKeyDown(Keys.S)) direction.X = -1;
else direction.X = 0;
if (state.IsKeyDown(Keys.D)) direction.Z = 1;
else if (state.IsKeyDown(Keys.A)) direction.Z = -1;
else direction.Z = 0;
if (state.IsKeyDown(Keys.LeftShift)) direction.Y = 1;
else if (state.IsKeyDown(Keys.Space)) direction.Y = -1;
else direction.Y = 0;
// Get movement rotation
if (state.IsKeyDown(Keys.Up)) rotation.Y = -1;
else if (state.IsKeyDown(Keys.Down)) rotation.Y = 1;
else rotation.Y = 0;
if (state.IsKeyDown(Keys.Right)) rotation.X = 1;
else if (state.IsKeyDown(Keys.Left)) rotation.X = -1;
else rotation.X = 0;
}
// Moves player based on given delta
private void MovePlayer(float delta)
{
// Update angle
angle += rotation * Spin * delta;
// Clamp angle Y
angle.Y = Math.Clamp(angle.Y, Pi / -2, Pi / 2);
// Update position frontways
position.X += direction.X * (float)Math.Cos(angle.X) * (float)Math.Cos(angle.Y) * Speed * delta;
position.Y += direction.X * (float)Math.Sin(angle.Y) * Speed * delta;
position.Z += direction.X * (float)Math.Sin(angle.X) * (float)Math.Cos(angle.Y) * Speed * delta;
// Update position sideways
position.X -= direction.Z * (float)Math.Sin(angle.X) * Speed * delta;
position.Z += direction.Z * (float)Math.Cos(angle.X) * Speed * delta;
// Update position vertically
position.Y += direction.Y * Speed * delta;
}
}
}