-
Notifications
You must be signed in to change notification settings - Fork 1
/
DinoStates.h
164 lines (120 loc) · 3.54 KB
/
DinoStates.h
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
#pragma once
#include "States.h"
#include "Sprites.h"
#include "SoundManager.h"
#include "Game.h"
class RunningState : public DinoState {
void enter(Dino& dino) override {
dino.setSrcRect(0, 0, 32, 32);
Collider& rCollider = dino.getCollider();
TransformComponent& rTransform = dino.getTransform();
rTransform.mPosition.y = static_cast<float>(263);
rCollider.resize(1);
rCollider.setColliderRect(0, rTransform.mPosition.x, rTransform.mPosition.y + 30, 121, 113);
if (Game::mSpeedToggled) {
dino.setAnimation(0, 2, 150);
}
else {
dino.setAnimation(0, 2, 200);
}
dino.startRun();
}
void update(Dino& dino) override {
SDL_Rect& rSrcRect = dino.getSrcRect();
rSrcRect.x = rSrcRect.w * static_cast<int>((SDL_GetTicks() / dino.getFramesSpeed()) % dino.getFramesCount());
rSrcRect.y = dino.getSheetIndex() * rSrcRect.h;
}
void leave(Dino& dino) override {
dino.stopRun();
}
};
class DuckingState : public DinoState {
void enter(Dino& dino) override {
dino.setSrcRect(0, 64, 32, 32);
Collider& rCollider = dino.getCollider();
TransformComponent& rTransform = dino.getTransform();
rTransform.mPosition.y = static_cast<float>(263);
rCollider.resize(1);
rCollider.setColliderRect(0, rTransform.mPosition.x, rTransform.mPosition.y + 68, 140, 128);
if (Game::mSpeedToggled) {
dino.setAnimation(2, 2, 150);
}
else {
dino.setAnimation(2, 2, 200);
}
dino.startDuck();
}
void update(Dino& dino) override {
SDL_Rect& rSrcRect = dino.getSrcRect();
rSrcRect.x = rSrcRect.w * static_cast<int>((SDL_GetTicks() / dino.getFramesSpeed()) % dino.getFramesCount());
rSrcRect.y = dino.getSheetIndex() * rSrcRect.h;
}
void leave(Dino& dino) override {
dino.stopDuck();
}
};
class JumpingState : public DinoState {
bool mCharging = false;
int mChargeTime = 0;
int mMAX_CHARGETIME = 5;
bool mLanding = false;
int mLandTime = 0;
int mMAX_LANDTIME = 13;
bool mJumping = false;
int mJumpTime = 0;
int mMAX_JUMPTIME = 30;
public:
void enter(Dino& dino) override {
dino.setSrcRect(0, 32, 32, 32);
TransformComponent& rTransform = dino.getTransform();
Collider& rCollider = dino.getCollider();
rCollider.resize(1);
rCollider.setColliderRect(0, rTransform.mPosition.x + 4, rTransform.mPosition.y + 38, 130, 96);
mCharging = true;
dino.startJump();
}
void update(Dino& dino) override {
if (mCharging) {
mChargeTime++;
if (mChargeTime >= mMAX_CHARGETIME) {
mChargeTime = 0;
mCharging = false;
mJumping = true;
TransformComponent& rTransform = dino.getTransform();
Collider& rCollider = dino.getCollider();
dino.setSrcRect(32, 32, 32, 32);
rTransform.mPosition.y -= dino.mVerticalJumpDistance;
rCollider.resize(1);
rCollider.setColliderRect(0, rTransform.mPosition.x, rTransform.mPosition.y + 32, 124, 68);
}
}
if (mJumping) {
mJumpTime++;
if (mJumpTime >= mMAX_JUMPTIME) {
mJumpTime = 0;
mJumping = false;
mLanding = true;
dino.setSrcRect(64, 32, 32, 32);
TransformComponent& rTransform = dino.getTransform();
Collider& rCollider = dino.getCollider();
rTransform.mPosition.y += dino.mVerticalJumpDistance;
rCollider.resize(1);
rCollider.setColliderRect(0, rTransform.mPosition.x, rTransform.mPosition.y + 157, 155, 19);
}
}
if (mLanding) {
mLandTime++;
if (mLandTime >= mMAX_LANDTIME) {
mLanding = false;
mLandTime = 0;
leave(dino);
}
}
}
void leave(Dino& dino) override {
dino.stopJump();
dino.clearState();
dino.setState(new RunningState());
dino.getState()->enter(dino);
}
};