3
* Copyright (C) Remigiusz Dybka 2009 <remigiusz.dybka@gmail.com>
5
Zaz is free software: you can redistribute it and/or modify it
6
under the terms of the GNU General Public License as published by the
7
Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
10
Zaz is distributed in the hope that it will be useful, but
11
WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
See the GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License along
16
with this program. If not, see <http://www.gnu.org/licenses/>.
23
Player::Player(Bezier path, BallPath **ballPaths, bool pathLooped, bool invert, bool drawPath)
24
: tex(LoadTexture("player.png")), path(path), drawPath(drawPath), pathLooped(pathLooped), invert(invert),
25
ballPaths(ballPaths), pickedCol(-1), pickedBonus(BONUS_NONE), picking(false), shooting(false), score(0),
28
BezierPoint p0 = path.points[0];
30
p0.cx = p0.x + (p0.x - p0.cx);
31
p0.cy = p0.y + (p0.y - p0.cy);
34
path.points.push_back(p0);
36
pts = path.GenerateUniform(0.05);
38
ptsLen = pts.size() -1;
42
double mouseSens = atof(settings.get("mouseSensivity", "5").c_str());
46
mouseDiv = (mouseSens / 10.0) / 500;
51
glDeleteTextures(1, &tex);
54
void Player::UpdateRotation()
56
XY pt1 = pts[currPt - 1];
57
XY pt2 = pts[currPt + 1];
73
rot = atan2(y, x) * (180.0 / PI);
76
void Player::Move(double t)
82
while ( currPos < 0.0)
97
currPt = (int)((ptsLen - 2) * currPos);
102
if (currPt > ptsLen - 2)
108
void Player::Logic(Scenes::FrameEvents events)
111
while (ballPaths[p] != NULL)
113
if (ballPaths[p]->state.accuracyShotTriggered)
116
accuracyShotTime = accuracyShotTimeout;
117
ballPaths[p]->state.accuracyShotTriggered = false;
126
if (accuracyShotTime < 0)
127
accuracyShot = false;
134
ShotAddr shotBall(-1);
138
while (ballPaths[p] != NULL)
140
nBall = ballPaths[p]->PickShot(shootPoint.x, shootPoint.y);
144
double bx = ballPaths[p]->ballPath[(uint)ballPaths[p]->balls[nBall.pos].pos].x;
145
double by = ballPaths[p]->ballPath[(uint)ballPaths[p]->balls[nBall.pos].pos].y;
147
double dist = sqrt(pow(fabs(bx - shootPoint.x), 2.0) + pow(fabs(by - shootPoint.y), 2.0));
158
// is this the first ball ?
159
if (shotBall.pos == 0)
161
// do we have space for a ball before that one ?
162
if (ballPaths[shotPath]->balls[shotBall.pos].pos >= BallPath::stepsPerBall)
164
// if yes.... was the shot on the right side ?
165
double bx = ballPaths[shotPath]->ballPath[(uint)ballPaths[shotPath]->balls[shotBall.pos].pos].x;
166
double by = ballPaths[shotPath]->ballPath[(uint)ballPaths[shotPath]->balls[shotBall.pos].pos].y;
168
double distBall = sqrt(pow(fabs(bx - shootPoint.x), 2.0) + pow(fabs(by - shootPoint.y), 2.0));
170
bx = ballPaths[shotPath]->ballPath[(uint)ballPaths[shotPath]->balls[shotBall.pos].pos - BallPath::stepsPerBall].x;
171
by = ballPaths[shotPath]->ballPath[(uint)ballPaths[shotPath]->balls[shotBall.pos].pos - BallPath::stepsPerBall].y;
173
double distFirst = sqrt(pow(fabs(bx - shootPoint.x), 2.0) + pow(fabs(by - shootPoint.y), 2.0));
175
if (distFirst < distBall)
177
ballPaths[shotPath]->InsertBall(ShotAddr(-1), shootCol, shootBonus);
185
if (shotBall.pos != -1)
187
ballPaths[shotPath]->InsertBall(shotBall, shootCol, shootBonus);
192
if (shootPoint.x > 120 || shootPoint.x < -20 || shootPoint.y > 120 || shootPoint.y < -20)
194
mix->EnqueueSample(sfx_ouch, sfxVol);
201
shootPoint.x = shootPoint.x + shootVel.x * shootingSpeed;
202
shootPoint.y = shootPoint.y + shootVel.y * shootingSpeed;
216
Move(double(events.relmouseX) * mouseDiv);
219
Move(double(-1 * events.relmouseX) * mouseDiv);
221
if (events.buttDown[0] && picking && !shooting)
223
shootCol = pickedCol;
224
shootTex = pickedTex;
225
shootBonus = pickedBonus;
226
shootBonusTex = pickedBonusTex;
228
shootVel.x = -sin(rot / (180.0 / PI));
229
shootVel.y = cos(rot / (180.0 / PI));
231
shootPoint.x = pts[currPt].x + holdingDist * shootVel.x;
232
shootPoint.y = pts[currPt].y + holdingDist * shootVel.y;
238
mix->EnqueueSample(sfx_push, sfxVol);
242
if (events.buttDown[0] && !shooting && !picking)
243
{ // we're shoootin :)
245
XY pt1 = pts[currPt - 1];
246
XY pt2 = pts[currPt + 1];
248
double vx = -sin(rot / (180.0 / PI));
249
double vy = cos(rot / (180.0 / PI));
251
double x = pts[currPt].x;
252
double y = pts[currPt].y;
256
double ldist = 200.0;
259
while (ballPaths[p] != NULL)
261
nBall = ballPaths[p]->Pick(x, y, vx, vy);
265
double bx = ballPaths[p]->ballPath[(uint)ballPaths[p]->balls[nBall].pos].x;
266
double by = ballPaths[p]->ballPath[(uint)ballPaths[p]->balls[nBall].pos].y;
267
double dist = sqrt(pow(fabs(bx - x), 2.0) + pow(fabs(by - y), 2.0));
280
if (pickedBall != -1)
282
pickedCol = ballPaths[pickedPath]->balls[pickedBall].col;
283
pickedBonus = ballPaths[pickedPath]->balls[pickedBall].bonus;
284
pickedTex = ballPaths[pickedPath]->tex[pickedCol];
285
pickedBonusTex = ballPaths[pickedPath]->tex[NBALLCOLORS + (pickedBonus) - 1];
286
pickPoint.x = ballPaths[pickedPath]->ballPath[(uint)ballPaths[pickedPath]->balls[pickedBall].pos].x;
287
pickPoint.y = ballPaths[pickedPath]->ballPath[(uint)ballPaths[pickedPath]->balls[pickedBall].pos].y;
292
pickV = 1.0 / (double)pickSpeed;
294
deque<Ball>::iterator i;
295
i = ballPaths[pickedPath]->balls.begin();
298
ballPaths[pickedPath]->balls.erase(i);
299
ballPaths[pickedPath]->state.comboCnt = 0;
301
mix->EnqueueSample(sfx_pull, sfxVol);
305
if (events.buttUp[0])
307
if (picking && !shooting && !reshooting)
309
shootCol = pickedCol;
310
shootTex = pickedTex;
311
shootBonus = pickedBonus;
312
shootBonusTex = pickedBonusTex;
314
shootVel.x = -sin(rot / (180.0 / PI));
315
shootVel.y = cos(rot / (180.0 / PI));
317
shootPoint.x = pts[currPt].x + holdingDist * shootVel.x;
318
shootPoint.y = pts[currPt].y + holdingDist * shootVel.y;
323
mix->EnqueueSample(sfx_push, sfxVol);
329
void Player::Render()
331
double ts = ((BallPath::ballOneTextureSize * 10.0)/ double(BallPath::ballTextureSize)) * 0.1; // size of one ball in texture coordinates
335
glEnable(GL_TEXTURE_2D);
336
glTranslated(pts[currPt].x, pts[currPt].y, 40);
337
glBindTexture(GL_TEXTURE_2D, tex);
338
glScaled(10, 10, 10);
339
glRotated(rot, 0, 0, 1.0);
342
glVertex3d(-0.5, 0.5, 0);
344
glVertex3d(-0.5, -0.5, 0);
346
glVertex3d(0.5, -0.5, 0);
348
glVertex3d(0.5, 0.5, 0);
350
glDisable(GL_TEXTURE_2D);
355
glEnable(GL_TEXTURE_2D);
356
glTranslated(shootPoint.x, shootPoint.y, 40);
357
glBindTexture(GL_TEXTURE_2D, shootTex);
360
glTexCoord2d(0.0, 0.0);
361
glVertex3d(-0.5, 0.5, 0);
362
glTexCoord2d(0.0, ts);
363
glVertex3d(-0.5, -0.5, 0);
364
glTexCoord2d(ts, ts);
365
glVertex3d(0.5, -0.5, 0);
366
glTexCoord2d(ts, 0.0);
367
glVertex3d(0.5, 0.5, 0);
373
glTranslated(shootPoint.x, shootPoint.y, 45);
374
glBindTexture(GL_TEXTURE_2D, shootBonusTex);
377
glTexCoord2d(0.0, 0.0);
378
glVertex3d(-0.5, 0.5, 0);
379
glTexCoord2d(0.0, 1);
380
glVertex3d(-0.5, -0.5, 0);
382
glVertex3d(0.5, -0.5, 0);
383
glTexCoord2d(1, 0.0);
384
glVertex3d(0.5, 0.5, 0);
388
glDisable(GL_TEXTURE_2D);
393
double bx = pts[currPt].x + holdingDist * -sin(rot / (180.0 / PI));
394
double by = pts[currPt].y + holdingDist * cos(rot / (180.0 / PI));
395
bx = bx + (pickPoint.x - bx) * (1.0 - pickT);
396
by = by + (pickPoint.y - by) * (1.0 - pickT);
398
double bps = (double)BallPath::ballSize / 1.5;
399
double bs = (double)BallPath::ballSize - (((double)BallPath::ballSize - bps) * pickT);
402
glEnable(GL_TEXTURE_2D);
403
glTranslated(bx, by, 40);
404
glRotated(rot, 0, 0, 1.0);
405
glBindTexture(GL_TEXTURE_2D, pickedTex);
406
glScaled(bs, bs, bs);
408
glTexCoord2d(0.0, 0.0);
409
glVertex3d(-0.5, 0.5, 0);
410
glTexCoord2d(0.0, ts);
411
glVertex3d(-0.5, -0.5, 0);
412
glTexCoord2d(ts, ts);
413
glVertex3d(0.5, -0.5, 0);
414
glTexCoord2d(ts, 0.0);
415
glVertex3d(0.5, 0.5, 0);
421
glTranslated(bx, by, 45);
422
glRotated(rot, 0, 0, 1.0);
423
glBindTexture(GL_TEXTURE_2D, pickedBonusTex);
424
glScaled(bs, bs, bs);
427
glVertex3d(-0.5, 0.5, 0);
429
glVertex3d(-0.5, -0.5, 0);
431
glVertex3d(0.5, -0.5, 0);
433
glVertex3d(0.5, 0.5, 0);
437
glDisable(GL_TEXTURE_2D);
444
glTranslated(pts[currPt].x, pts[currPt].y, 30);
445
glRotated(rot, 0, 0, 1.0);
446
glTranslatef(0, 5, 0);
447
glBegin(GL_TRIANGLES);
449
glColor4d(1, 0, 0, 0.5 * double(accuracyShotTime) / double(accuracyShotTimeout));
452
glVertex3d(0, 100, 0);
454
glColor4d(0, 0, 0, 0);