-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBasePlayer.cpp
238 lines (211 loc) · 4.87 KB
/
BasePlayer.cpp
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
/*
Greyout - a colourful platformer about love
Greyout is Copyright (c)2011-2014 Janek Schäfer
This file is part of Greyout.
Greyout is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Greyout is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Please direct any feedback, questions or comments to
Janek Schäfer (foxblock), foxblock_at_gmail_dot_com
*/
#include "BasePlayer.h"
#include "Level.h"
#define JUMP_TO_FALL_FRAMES 30
#define WALK_TO_FALL_FRAMES 10
BasePlayer::BasePlayer(Level* newParent) : ControlUnit(newParent)
{
canJump = false;
isJumping = false;
flags.addFlag(ufMissionObjective);
fallCounter = 0;
activelyMoving = false;
}
BasePlayer::~BasePlayer()
{
//
}
///---public---
// Custom load implementation to ensure the unit is initialized properly after loading
bool BasePlayer::load(list<PARAMETER_TYPE >& params)
{
bool result = BaseUnit::load(params);
if (imageOverwrite[0] == 0)
{
imageOverwrite = "images/player/black_big.png";
}
if (startingState[0] == 0 || startingState == "default")
{
if (takesControl)
startingState = "wave";
else
startingState = "stand";
}
setSpriteState(startingState,true);
return result;
}
void BasePlayer::resetTemporary()
{
BaseUnit::resetTemporary();
if (currentSprite->getLoops() == -1 || currentSprite->hasFinished())
{
setSpriteState("stand");
}
}
void BasePlayer::update()
{
if (collisionInfo.correction.y < 0) // on the ground
{
if (currentState == "stand")
{
if (velocity.x < 0.0f)
setSpriteState("runleft");
else if (velocity.x > 0.0f)
setSpriteState("runright");
}
}
else // air
{
if ( fallCounter > 0 )
--fallCounter;
if (currentState == "stand")
{
if (fallCounter == 0)
{
if (direction < 0)
setSpriteState("fallleft");
else
setSpriteState("fallright");
}
else
{
if (isJumping)
{
if (direction < 0)
setSpriteState("flyleft");
else
setSpriteState("flyright");
}
else
{
if (velocity.x < 0.0f)
setSpriteState("runleft");
else if (velocity.x > 0.0f)
setSpriteState("runright");
}
}
}
}
BaseUnit::update();
if ((int)velocity.y != 0)
canJump = false;
}
void BasePlayer::hitMap(const Vector2df& correctionOverride)
{
BaseUnit::hitMap(correctionOverride);
if (correctionOverride.y < 0.0f)
{
isJumping = false;
fallCounter = 0;
}
else if (correctionOverride.y > 0.0f)
{
acceleration[0].y = 0;
acceleration[1].y = 0;
}
else if (lastYCorrection != 0.0f && !isJumping)
{
fallCounter = WALK_TO_FALL_FRAMES;
}
lastYCorrection = correctionOverride.y;
}
void BasePlayer::control(SimpleJoy* input)
{
if (!input)
return;
if (input->isLeft() && !input->isRight())
{
if ((direction > 0 || velocity.x == 0.0f) && ((int)velocity.y == 0))
{
setSpriteState("turnleft",true);
states["runleft"]->rewind(); // reset running anim to match end of turn
}
if (velocity.x > 0.0f)
{
acceleration[0].x = -0.75;
}
else
{
acceleration[0].x = -0.25;
}
acceleration[1].x = -4;
activelyMoving = true;
}
else if (input->isRight() && !input->isLeft())
{
if ((direction < 0 || velocity.x == 0.0f) && ((int)velocity.y == 0))
{
setSpriteState("turnright",true);
states["runright"]->rewind(); // reset running anim to match end of turn
}
if (velocity.x < 0.0f)
{
acceleration[0].x = 0.75;
}
else
{
acceleration[0].x = 0.25;
}
acceleration[1].x = 4;
activelyMoving = true;
}
else
{
activelyMoving = false;
if (canJump && currentState != "wave")
{
setSpriteState("stand");
}
if ((int)collisionInfo.correction.y != 0)
{
acceleration[0].x = NumberUtility::sign(velocity.x) * -0.5f;
acceleration[1].x = 0;
//velocity.x = 0;
}
else
{
acceleration[0].x = (float)NumberUtility::sign(velocity.x) * -0.25f;
acceleration[1].x = 0;
}
}
if ((input->isB() || input->isY()) && canJump && !isJumping)
{
acceleration[0].y = -4;
acceleration[1].y = -9;
//velocity.y = -10;
canJump = false;
isJumping = true;
if (direction < 0)
setSpriteState("jumpleft",true);
else
setSpriteState("jumpright",true);
fallCounter = JUMP_TO_FALL_FRAMES;
}
if (!(input->isB() || input->isY()))
canJump = true;
}
#ifdef _DEBUG
string BasePlayer::debugInfo()
{
string result = BaseUnit::debugInfo();
result += StringUtility::boolToString(activelyMoving) + "\n";
result += StringUtility::intToString(fallCounter) + "\n";
return result;
}
#endif