-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPushableBox.cpp
317 lines (280 loc) · 8.09 KB
/
PushableBox.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
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 "PushableBox.h"
#include "fileTypeDefines.h"
#include "Level.h"
#include "MusicCache.h"
#include "BasePlayer.h"
#include "MyGame.h"
#define PUSHING_SPEED 1.0f
PushableBox::PushableBox(Level* newParent) : BaseUnit(newParent)
{
rect.w = 32;
rect.h = 32;
col = BLACK;
sizeTimer.x = 0;
sizeTimer.y = 0;
dynSize.x = 0;
dynSize.y = 0;
isBeingPushed = false;
if (pushSound.loadSound(parent->chapterPath + "sounds/box.wav") != PENJIN_OK)
{
pushSound.loadSound("sounds/box.wav");
}
pushSound.setVolume(MUSIC_CACHE->getSoundVolume());
//pushSound.setSimultaneousPlay(true);
stringToOrder["size"] = boSize;
}
PushableBox::~PushableBox()
{
//
}
/// ---public---
bool PushableBox::processParameter(const PARAMETER_TYPE& value)
{
// first ensure backwards compatibility by passing the value to BaseUnit
// (also avoids having to copy that code)
if (BaseUnit::processParameter(value))
return true;
// if the specific parameter is not caught by BaseUnit is has to be special
// to this unit (or erroneous), so custom implementation here, following the
// same structure as BaseUnit::processParameter
bool parsed = true;
switch (stringToProp[value.first])
{
case BaseUnit::upSize:
{
vector<string> token;
StringUtility::tokenize(value.second,token,DELIMIT_STRING);
if (token.size() != 2)
{
parsed = false;
break;
}
rect.w = StringUtility::stringToInt(token[0]);
rect.h = StringUtility::stringToInt(token[1]);
break;
}
default:
parsed = false;
}
return parsed;
}
void PushableBox::reset()
{
rect.w = 32;
rect.h = 32;
pushSound.stop();
BaseUnit::reset();
}
int PushableBox::getHeight() const
{
return rect.h;
}
int PushableBox::getWidth() const
{
return rect.w;
}
Vector2df PushableBox::getPixel(const SimpleDirection& dir) const
{
switch (dir.value)
{
case diTOPLEFT:
return position + Vector2df(1,0);
case diTOPRIGHT:
return Vector2df(position.x + getWidth() - 2, position.y);
case diBOTTOMLEFT:
return Vector2df(position.x+1, position.y + getHeight() - 1);
case diBOTTOMRIGHT:
return position + getSize() - Vector2df(2,1);
default:
return BaseUnit::getPixel(dir);
}
}
void PushableBox::update()
{
if (velocity.x == 0)
isBeingPushed = false; // stop sound if box not actually moving
if (isBeingPushed == false && pushSound.isPlaying())
pushSound.pause();
// stop sound on next frame if not being pushed
// if box is being pushed hitUnit will be called and this set to true again before next update
isBeingPushed = false;
BaseUnit::update();
}
void PushableBox::updateScreenPosition(const Vector2di& offset)
{
rect.x = position.x - offset.x;
rect.y = position.y - offset.y;
}
void PushableBox::render(SDL_Surface* surf)
{
// SDL_FillRect might sometimes change the passed rect which we don't want
int w = rect.w;
int h = rect.h;
SDL_FillRect(surf,&rect,col.getSDL_Uint32Colour(surf));
rect.w = w;
rect.h = h;
BaseUnit::render(surf);
}
void PushableBox::hitUnit(const UnitCollisionEntry& entry)
{
if (velocity.y < 4 && entry.unit->isPlayer) // if not falling
{
if (entry.overlap.y > entry.unit->velocity.y && entry.overlap.y > entry.overlap.x
&& entry.unit->velocity.x != 0.0f )
{
// set fixed speed to avoid jittery animations (yup, this is very ugly and I am very lazy, but it works, so fuck it all)
if (((BasePlayer*)entry.unit)->activelyMoving)
entry.unit->velocity.x = PUSHING_SPEED * NumberUtility::sign( entry.unit->velocity.x );
// only move by full pixels (else the collision detection fucks up, due
// to differences in hitUnit and hitMap... maybe I should fix that...)
// TODO: Fix that in Physics (see above)
if (entry.unit->velocity.x >= 0)
velocity.x = ceil(entry.unit->velocity.x);
else
velocity.x = floor(entry.unit->velocity.x);
// snap unit to box
if ( velocity.x > 0.0f )
entry.unit->collisionInfo.positionCorrection.x += position.x - entry.unit->position.x - entry.unit->getWidth();
else
entry.unit->collisionInfo.positionCorrection.x -= entry.unit->position.x - position.x - getWidth();
if (entry.unit->direction > 0)
entry.unit->setSpriteState("pushright");
else
entry.unit->setSpriteState("pushleft");
if (!pushSound.isPlaying() || pushSound.isPaused())
{
pushSound.play();
pushSound.setVolume(MUSIC_CACHE->getSoundVolume());
}
isBeingPushed = true;
}
}
// I have no real clue what this code does (years after writing and not documenting it)
// might have to do with slopes (which aren't in the main game really and not
// tested very much), too lazy to find out
if (entry.overlap.x > entry.overlap.y && entry.unit->position.y < position.y)
{
if (collisionInfo.positionCorrection.x != 0)
entry.unit->collisionInfo.positionCorrection.x += collisionInfo.positionCorrection.x;
if (velocity.y != 0.0f || collisionInfo.positionCorrection.y != 0.0f)
entry.unit->collisionInfo.positionCorrection.y -= entry.overlap.y - NumberUtility::sign(entry.overlap.y);
}
}
void PushableBox::explode()
{
if (parent)
{
Vector2df vel(0,0);
int time = 0;
int inc;
switch (ENGINE->settings->getParticleDensity())
{
case Settings::pdOff:
MUSIC_CACHE->playSound("sounds/die.wav",parent->chapterPath);
toBeRemoved = true;
return;
case Settings::pdFew:
inc = round(max((float)(getWidth() + getHeight()) / 32.0f,4.0f));
break;
case Settings::pdMany:
inc = round(max((float)(getWidth() + getHeight()) / 64.0f,2.0f));
break;
case Settings::pdTooMany:
inc = 1;
break;
default:
inc = round(max((float)(getWidth() + getHeight()) / 64.0f,2.0f));
break;
}
for (int X = getWidth()-1; X >= 0; X-=inc)
{
for (int Y = getHeight()-1; Y >= 0; Y-=inc)
{
vel.x = Random::nextFloat(-5,5);
vel.y = Random::nextFloat(-8,-3);
time = Random::nextInt(45,75);
parent->addParticle(this,col,position + Vector2df(X,Y),vel,time);
}
}
MUSIC_CACHE->playSound("sounds/die.wav",parent->chapterPath);
}
toBeRemoved = true;
}
/// ---protected---
bool PushableBox::processOrder(Order& next)
{
bool parsed = true;
switch (next.key)
{
case boSize:
{
if (next.params.size() < 3)
{
string temp = StringUtility::combine(next.params, DELIMIT_STRING);
printf("ERROR: Bad order parameter \"%s\"in order #%i on unit id \"%s\"\n", temp.c_str(), currentOrder, id.c_str());
orderList.erase(orderList.begin() + currentOrder);
orderTimer = 1; // process next order in next cycle
return false;
}
Vector2di destSize;
destSize.x = StringUtility::stringToInt(next.params[1]);
destSize.y = StringUtility::stringToInt(next.params[2]);
sizeTimer.x = (float)(destSize.x - rect.w) / (float)next.ticks;
sizeTimer.y = (float)(destSize.y - rect.h) / (float)next.ticks;
dynSize.x = rect.w;
dynSize.y = rect.h;
break;
}
default:
parsed = false;
}
if (parsed == false)
return BaseUnit::processOrder(next);
else
{
orderTimer = next.ticks;
orderRunning = true;
}
return parsed;
}
bool PushableBox::updateOrder(const Order& curr)
{
if (BaseUnit::updateOrder(curr))
return true;
bool parsed = true;
switch (curr.key)
{
case boSize:
{
dynSize += sizeTimer;
rect.w = round(dynSize.x);
rect.h = round(dynSize.y);
break;
}
default:
parsed = false;
}
return parsed;
}
void PushableBox::move()
{
BaseUnit::move();
velocity.x = 0; // without an implementation of friction, this avoids sliding
}