forked from flavius-st/TheAdventure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerObject.cs
More file actions
153 lines (125 loc) · 4.21 KB
/
Copy pathPlayerObject.cs
File metadata and controls
153 lines (125 loc) · 4.21 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
using System.Formats.Asn1;
using Silk.NET.Maths;
using TheAdventure;
namespace TheAdventure.Models;
public class PlayerObject : RenderableGameObject
{
public enum PlayerStateDirection{
None = 0,
Down,
Up,
Left,
Right,
}
public enum PlayerState{
None = 0,
Idle,
Move,
Attack,
GameOver
}
private bool _isSprinting = false;
public bool IsSprinting => _isSprinting;
private const int SprintSpeed = 300;
private int _pixelsPerSecond = 192;
private Input _input;
public (PlayerState State, PlayerStateDirection Direction) State{ get; private set; }
public PlayerObject(SpriteSheet spriteSheet, int x, int y, Input input) : base(spriteSheet, (x, y))
{
_input = input; // Ini?ializeaz? input-ul
SetState(PlayerState.Idle, PlayerStateDirection.Down);
}
public void ToggleSprint()
{
_isSprinting = !_isSprinting;
}
public void SetState(PlayerState state, PlayerStateDirection direction)
{
if(State.State == PlayerState.GameOver) return;
if(State.State == state && State.Direction == direction){
return;
}
else if(state == PlayerState.None && direction == PlayerStateDirection.None){
SpriteSheet.ActivateAnimation(null);
}
else if(state == PlayerState.GameOver){
SpriteSheet.ActivateAnimation(Enum.GetName(state));
}
else{
var animationName = Enum.GetName<PlayerState>(state) + Enum.GetName<PlayerStateDirection>(direction);
SpriteSheet.ActivateAnimation(animationName);
}
State = (state, direction);
}
public void GameOver(){
SetState(PlayerState.GameOver, PlayerStateDirection.None);
}
public void Attack(bool up, bool down, bool left, bool right)
{
if(State.State == PlayerState.GameOver) return;
var direction = State.Direction;
if(up){
direction = PlayerStateDirection.Up;
}
else if (down)
{
direction = PlayerStateDirection.Down;
}
else if (right)
{
direction = PlayerStateDirection.Right;
}
else if (left){
direction = PlayerStateDirection.Left;
}
SetState(PlayerState.Attack, direction);
}
public void UpdatePlayerPosition(double up, double down, double left, double right, int width, int height, double time)
{
if (State.State == PlayerState.GameOver) return;
if (up <= double.Epsilon &&
down <= double.Epsilon &&
left <= double.Epsilon &&
right <= double.Epsilon &&
State.State == PlayerState.Idle)
{
return;
}
bool isWPressed = _input.IsWPressed();
_pixelsPerSecond = isWPressed ? SprintSpeed : _pixelsPerSecond;
var deltaX = (right - left) * time * _pixelsPerSecond;
var deltaY = (down - up) * time * _pixelsPerSecond;
var pixelsToMove = time * _pixelsPerSecond;
var x = Position.X + (int)(right * pixelsToMove);
x -= (int)(left * pixelsToMove);
var y = Position.Y - (int)(up * pixelsToMove);
y += (int)(down * pixelsToMove);
x = Math.Clamp(x, 10, width - 10);
y = Math.Clamp(y, 24, height - 6);
if (y < Position.Y)
{
SetState(PlayerState.Move, PlayerStateDirection.Up);
}
if (y > Position.Y)
{
SetState(PlayerState.Move, PlayerStateDirection.Down);
}
if (x > Position.X)
{
SetState(PlayerState.Move, PlayerStateDirection.Right);
}
if (x < Position.X)
{
SetState(PlayerState.Move, PlayerStateDirection.Left);
}
if (x == Position.X && y == Position.Y)
{
SetState(PlayerState.Idle, State.Direction);
}
Position = (x, y);
}
public void SetSprint(bool isSprinting)
{
_pixelsPerSecond = isSprinting ? SprintSpeed : _pixelsPerSecond;
}
}