/
player.cpp
1659 lines (1464 loc) · 43.7 KB
/
player.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SuperTux
// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
//
// This program 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 2
// of the License, or (at your option) any later version.
//
// This program 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, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "object/player.hpp"
#include "audio/sound_manager.hpp"
#include "badguy/badguy.hpp"
#include "control/input_manager.hpp"
#include "math/random_generator.hpp"
#include "object/bullet.hpp"
#include "object/camera.hpp"
#include "object/display_effect.hpp"
#include "object/falling_coin.hpp"
#include "object/particles.hpp"
#include "object/portable.hpp"
#include "object/sprite_particle.hpp"
#include "scripting/squirrel_util.hpp"
#include "supertux/game_session.hpp"
#include "supertux/globals.hpp"
#include "supertux/sector.hpp"
#include "supertux/tile.hpp"
#include "trigger/climbable.hpp"
#include <math.h>
//#define SWIMMING
namespace {
static const float BUTTJUMP_MIN_VELOCITY_Y = 400.0f;
static const float SHOOTING_TIME = .150f;
static const float GLIDE_TIME_PER_FLOWER = 0.5f;
/** number of idle stages, including standing */
static const unsigned int IDLE_STAGE_COUNT = 5;
/**
* how long to play each idle animation in milliseconds
* '0' means the sprite action is played once before moving onto the next
* animation
*/
static const int IDLE_TIME[] = { 5000, 0, 2500, 0, 2500 };
/** idle stages */
static const std::string IDLE_STAGES[] =
{ "stand",
"idle",
"stand",
"idle",
"stand" };
/** acceleration in horizontal direction when walking
* (all accelerations are in pixel/s^2) */
static const float WALK_ACCELERATION_X = 300;
/** acceleration in horizontal direction when running */
static const float RUN_ACCELERATION_X = 400;
/** acceleration when skidding */
static const float SKID_XM = 200;
/** time of skidding in seconds */
static const float SKID_TIME = .3f;
/** maximum walk velocity (pixel/s) */
static const float MAX_WALK_XM = 230;
/** maximum run velocity (pixel/s) */
static const float MAX_RUN_XM = 320;
/** bonus run velocity addition (pixel/s) */
static const float BONUS_RUN_XM = 80;
/** maximum horizontal climb velocity */
static const float MAX_CLIMB_XM = 96;
/** maximum vertical climb velocity */
static const float MAX_CLIMB_YM = 128;
/** maximum vertical glide velocity */
static const float MAX_GLIDE_YM = 128;
/** instant velocity when tux starts to walk */
static const float WALK_SPEED = 100;
/** multiplied by WALK_ACCELERATION to give friction */
static const float NORMAL_FRICTION_MULTIPLIER = 1.5f;
/** multiplied by WALK_ACCELERATION to give friction */
static const float ICE_FRICTION_MULTIPLIER = 0.1f;
static const float ICE_ACCELERATION_MULTIPLIER = 0.25f;
/** time of the kick (kicking mriceblock) animation */
static const float KICK_TIME = .3f;
/** if Tux cannot unduck for this long, he will get hurt */
static const float UNDUCK_HURT_TIME = 0.25f;
/** gravity is higher after the jump key is released before
the apex of the jump is reached */
static const float JUMP_EARLY_APEX_FACTOR = 3.0;
static const float JUMP_GRACE_TIME = 0.25f; /**< time before hitting the ground that the jump button may be pressed (and still trigger a jump) */
/* Tux's collision rectangle */
static const float TUX_WIDTH = 31.8f;
static const float RUNNING_TUX_WIDTH = 34;
static const float SMALL_TUX_HEIGHT = 30.8f;
static const float BIG_TUX_HEIGHT = 62.8f;
static const float DUCKED_TUX_HEIGHT = 31.8f;
bool no_water = true;
}
Player::Player(PlayerStatus* _player_status, const std::string& name_) :
deactivated(),
controller(),
scripting_controller(),
player_status(_player_status),
duck(),
dead(),
dying(),
winning(),
backflipping(),
backflip_direction(),
peekingX(),
peekingY(),
glide_time(),
stone(),
swimming(),
speedlimit(),
scripting_controller_old(0),
jump_early_apex(),
on_ice(),
ice_this_frame(),
lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-tiny.sprite")),
dir(),
old_dir(),
last_ground_y(),
fall_mode(),
on_ground_flag(),
jumping(),
can_jump(),
jump_button_timer(),
wants_buttjump(),
does_buttjump(),
invincible_timer(),
skidding_timer(),
safe_timer(),
kick_timer(),
shooting_timer(),
ability_timer(),
cooldown_timer(),
dying_timer(),
growing(),
backflip_timer(),
physic(),
visible(),
grabbed_object(NULL),
sprite(),
airarrow(),
floor_normal(),
ghost_mode(false),
edit_mode(false),
unduck_hurt_timer(),
idle_timer(),
idle_stage(0),
climbing(0)
{
this->name = name_;
controller = InputManager::current()->get_controller();
scripting_controller.reset(new CodeController());
// if/when we have complete penny gfx, we can
// load those instead of Tux's sprite in the
// constructor
sprite = SpriteManager::current()->create("images/creatures/tux/tux.sprite");
airarrow = Surface::create("images/engine/hud/airarrow.png");
idle_timer.start(IDLE_TIME[0]/1000.0f);
SoundManager::current()->preload("sounds/bigjump.wav");
SoundManager::current()->preload("sounds/jump.wav");
SoundManager::current()->preload("sounds/hurt.wav");
SoundManager::current()->preload("sounds/kill.wav");
SoundManager::current()->preload("sounds/skid.wav");
SoundManager::current()->preload("sounds/flip.wav");
SoundManager::current()->preload("sounds/invincible_start.ogg");
SoundManager::current()->preload("sounds/splash.ogg");
init();
}
Player::~Player()
{
if (climbing) stop_climbing(*climbing);
}
void
Player::init()
{
if(is_big())
set_size(TUX_WIDTH, BIG_TUX_HEIGHT);
else
set_size(TUX_WIDTH, SMALL_TUX_HEIGHT);
dir = RIGHT;
old_dir = dir;
duck = false;
dead = false;
dying = false;
winning = false;
peekingX = AUTO;
peekingY = AUTO;
last_ground_y = 0;
fall_mode = ON_GROUND;
jumping = false;
jump_early_apex = false;
can_jump = true;
wants_buttjump = false;
does_buttjump = false;
growing = false;
deactivated = false;
backflipping = false;
backflip_direction = 0;
sprite->set_angle(0.0f);
visible = true;
glide_time = 0;
stone = false;
swimming = false;
on_ice = false;
ice_this_frame = false;
speedlimit = 0; //no special limit
lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
on_ground_flag = false;
grabbed_object = NULL;
climbing = 0;
physic.reset();
}
void
Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
{
if (name.empty())
return;
scripting::expose_object(vm, table_idx, dynamic_cast<scripting::Player *>(this), name, false);
}
void
Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
{
if (name.empty())
return;
scripting::unexpose_object(vm, table_idx, name);
}
float
Player::get_speedlimit()
{
return speedlimit;
}
void
Player::set_speedlimit(float newlimit)
{
speedlimit=newlimit;
}
void
Player::set_controller(Controller* controller_)
{
this->controller = controller_;
}
void
Player::set_winning()
{
if( ! is_winning() ){
winning = true;
invincible_timer.start(10000.0f);
}
}
void
Player::use_scripting_controller(bool use_or_release)
{
if ((use_or_release == true) && (controller != scripting_controller.get())) {
scripting_controller_old = get_controller();
set_controller(scripting_controller.get());
}
if ((use_or_release == false) && (controller == scripting_controller.get())) {
set_controller(scripting_controller_old);
scripting_controller_old = 0;
}
}
void
Player::do_scripting_controller(std::string control, bool pressed)
{
for(int i = 0; Controller::controlNames[i] != 0; ++i) {
if(control == std::string(Controller::controlNames[i])) {
scripting_controller->press(Controller::Control(i), pressed);
}
}
}
bool
Player::adjust_height(float new_height)
{
Rectf bbox2 = bbox;
bbox2.move(Vector(0, bbox.get_height() - new_height));
bbox2.set_height(new_height);
if(new_height > bbox.get_height()) {
Rectf additional_space = bbox2;
additional_space.set_height(new_height - bbox.get_height());
if(!Sector::current()->is_free_of_statics(additional_space, this, true))
return false;
}
// adjust bbox accordingly
// note that we use members of moving_object for this, so we can run this during CD, too
set_pos(bbox2.p1);
set_size(bbox2.get_width(), bbox2.get_height());
return true;
}
void
Player::trigger_sequence(std::string sequence_name)
{
if (climbing) stop_climbing(*climbing);
backflipping = false;
backflip_direction = 0;
sprite->set_angle(0.0f);
GameSession::current()->start_sequence(sequence_name);
}
void
Player::update(float elapsed_time)
{
if( no_water ){
swimming = false;
}
no_water = true;
if(dying && dying_timer.check()) {
set_bonus(NO_BONUS, true);
dead = true;
return;
}
if(!dying && !deactivated)
handle_input();
/*
// handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
if (deactivated)
apply_friction();
*/
// extend/shrink tux collision rectangle so that we fall through/walk over 1
// tile holes
if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
set_width(RUNNING_TUX_WIDTH);
} else {
set_width(TUX_WIDTH);
}
// on downward slopes, adjust vertical velocity so tux walks smoothly down
if (on_ground() && !dying) {
if(floor_normal.y != 0) {
if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
physic.set_velocity_y(250);
}
}
}
// handle backflipping
if (backflipping && !dying) {
//prevent player from changing direction when backflipping
dir = (backflip_direction == 1) ? LEFT : RIGHT;
if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
//rotate sprite during flip
sprite->set_angle(sprite->get_angle() + (dir==LEFT?1:-1) * elapsed_time * (360.0f / 0.5f));
}
// set fall mode...
if(on_ground()) {
fall_mode = ON_GROUND;
last_ground_y = get_pos().y;
} else {
if(get_pos().y > last_ground_y)
fall_mode = FALLING;
else if(fall_mode == ON_GROUND)
fall_mode = JUMPING;
}
// check if we landed
if(on_ground()) {
jumping = false;
if (backflipping && (backflip_timer.get_timegone() > 0.15f)) {
backflipping = false;
backflip_direction = 0;
sprite->set_angle(0.0f);
// if controls are currently deactivated, we take care of standing up ourselves
if (deactivated)
do_standup();
}
if (player_status->bonus == AIR_BONUS)
glide_time = player_status->max_air_time * GLIDE_TIME_PER_FLOWER;
}
// calculate movement for this frame
movement = physic.get_movement(elapsed_time);
if(grabbed_object != NULL && !dying) {
position_grabbed_object();
}
if(grabbed_object != NULL && dying){
grabbed_object->ungrab(*this, dir);
grabbed_object = NULL;
}
if(!ice_this_frame && on_ground())
on_ice = false;
on_ground_flag = false;
ice_this_frame = false;
// when invincible, spawn particles
if (invincible_timer.started())
{
if (graphicsRandom.rand(0, 2) == 0) {
float px = graphicsRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
float py = graphicsRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
Vector ppos = Vector(px, py);
Vector pspeed = Vector(0, 0);
Vector paccel = Vector(0, 0);
Sector::current()->add_object(std::make_shared<SpriteParticle>(
"images/objects/particles/sparkle.sprite",
// draw bright sparkle when there is lots of time left,
// dark sparkle when invincibility is about to end
(invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) ?
// make every other a longer sparkle to make trail a bit fuzzy
(size_t(game_time*20)%2) ? "small" : "medium"
:
"dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
}
}
if (growing) {
if (sprite->animation_done()) growing = false;
}
// when climbing animate only while moving
if(climbing){
if((physic.get_velocity_x()==0)&&(physic.get_velocity_y()==0))
sprite->stop_animation();
else
sprite->set_animation_loops(-1);
}
}
bool
Player::on_ground()
{
return on_ground_flag;
}
bool
Player::is_big()
{
if(player_status->bonus == NO_BONUS)
return false;
return true;
}
void
Player::apply_friction()
{
if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
physic.set_velocity_x(0);
physic.set_acceleration_x(0);
} else {
float friction = WALK_ACCELERATION_X * (on_ice ? ICE_FRICTION_MULTIPLIER : NORMAL_FRICTION_MULTIPLIER);
if(physic.get_velocity_x() < 0) {
physic.set_acceleration_x(friction);
} else if(physic.get_velocity_x() > 0) {
physic.set_acceleration_x(-friction);
} // no friction for physic.get_velocity_x() == 0
}
}
void
Player::handle_horizontal_input()
{
float vx = physic.get_velocity_x();
float vy = physic.get_velocity_y();
float ax = physic.get_acceleration_x();
float ay = physic.get_acceleration_y();
float dirsign = 0;
if(!duck || physic.get_velocity_y() != 0) {
if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
old_dir = dir;
dir = LEFT;
dirsign = -1;
} else if(!controller->hold(Controller::LEFT)
&& controller->hold(Controller::RIGHT)) {
old_dir = dir;
dir = RIGHT;
dirsign = 1;
}
}
// do not run if we're holding something which slows us down
if ( grabbed_object && grabbed_object->is_hampering() ) {
ax = dirsign * WALK_ACCELERATION_X;
// limit speed
if(vx >= MAX_WALK_XM && dirsign > 0) {
vx = MAX_WALK_XM;
ax = 0;
} else if(vx <= -MAX_WALK_XM && dirsign < 0) {
vx = -MAX_WALK_XM;
ax = 0;
}
} else {
if( vx * dirsign < MAX_WALK_XM ) {
ax = dirsign * WALK_ACCELERATION_X;
} else {
ax = dirsign * RUN_ACCELERATION_X;
}
// limit speed
if(vx >= MAX_RUN_XM + BONUS_RUN_XM *((player_status->bonus == AIR_BONUS) ? 1 : 0) && dirsign > 0) {
vx = MAX_RUN_XM + BONUS_RUN_XM *((player_status->bonus == AIR_BONUS) ? 1 : 0);
ax = 0;
} else if(vx <= -MAX_RUN_XM - BONUS_RUN_XM *((player_status->bonus == AIR_BONUS) ? 1 : 0) && dirsign < 0) {
vx = -MAX_RUN_XM - BONUS_RUN_XM *((player_status->bonus == AIR_BONUS) ? 1 : 0);
ax = 0;
}
}
// we can reach WALK_SPEED without any acceleration
if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
vx = dirsign * WALK_SPEED;
}
//Check speedlimit.
if( speedlimit > 0 && vx * dirsign >= speedlimit ) {
vx = dirsign * speedlimit;
ax = 0;
}
// changing directions?
if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
// let's skid!
if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
skidding_timer.start(SKID_TIME);
SoundManager::current()->play("sounds/skid.wav");
// dust some particles
Sector::current()->add_object(
std::make_shared<Particles>(
Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
LAYER_OBJECTS+1));
ax *= 2.5;
} else {
ax *= 2;
}
}
if(on_ice) {
ax *= ICE_ACCELERATION_MULTIPLIER;
}
physic.set_velocity(vx, vy);
physic.set_acceleration(ax, ay);
// we get slower when not pressing any keys
if(dirsign == 0) {
apply_friction();
}
}
void
Player::do_cheer()
{
do_duck();
do_backflip();
do_standup();
}
void
Player::do_duck() {
if (duck)
return;
if (!is_big())
return;
if (physic.get_velocity_y() != 0)
return;
if (!on_ground())
return;
if (does_buttjump)
return;
if (adjust_height(DUCKED_TUX_HEIGHT)) {
duck = true;
growing = false;
unduck_hurt_timer.stop();
} else {
// FIXME: what now?
}
}
void
Player::do_standup() {
if (!duck)
return;
if (!is_big())
return;
if (backflipping)
return;
if (adjust_height(BIG_TUX_HEIGHT)) {
duck = false;
unduck_hurt_timer.stop();
} else {
// if timer is not already running, start it.
if (unduck_hurt_timer.get_period() == 0) {
unduck_hurt_timer.start(UNDUCK_HURT_TIME);
}
else if (unduck_hurt_timer.check()) {
kill(false);
}
}
}
void
Player::do_backflip() {
if (!duck)
return;
if (!on_ground())
return;
backflip_direction = (dir == LEFT)?(+1):(-1);
backflipping = true;
do_jump((player_status->bonus == AIR_BONUS) ? -720 : -580);
SoundManager::current()->play("sounds/flip.wav");
backflip_timer.start(TUX_BACKFLIP_TIME);
}
void
Player::do_jump(float yspeed) {
if (!on_ground())
return;
physic.set_velocity_y(yspeed);
//bbox.move(Vector(0, -1));
jumping = true;
on_ground_flag = false;
can_jump = false;
// play sound
if (is_big()) {
SoundManager::current()->play("sounds/bigjump.wav");
} else {
SoundManager::current()->play("sounds/jump.wav");
}
}
void
Player::early_jump_apex()
{
if (!jump_early_apex)
{
jump_early_apex = true;
physic.set_gravity_modifier(JUMP_EARLY_APEX_FACTOR);
}
}
void
Player::do_jump_apex()
{
if (jump_early_apex)
{
jump_early_apex = false;
physic.set_gravity_modifier(1.0f);
}
}
void
Player::handle_vertical_input()
{
// Press jump key
if(controller->pressed(Controller::JUMP)) jump_button_timer.start(JUMP_GRACE_TIME);
if(controller->hold(Controller::JUMP) && jump_button_timer.started() && can_jump) {
jump_button_timer.stop();
if (duck) {
// when running, only jump a little bit; else do a backflip
if ((physic.get_velocity_x() != 0) ||
(controller->hold(Controller::LEFT)) ||
(controller->hold(Controller::RIGHT)))
{
do_jump(-300);
}
else
{
do_backflip();
}
} else {
// airflower allows for higher jumps-
// jump a bit higher if we are running; else do a normal jump
if(player_status->bonus == AIR_BONUS)
do_jump((fabs(physic.get_velocity_x()) > MAX_WALK_XM) ? -620 : -580);
else
do_jump((fabs(physic.get_velocity_x()) > MAX_WALK_XM) ? -580 : -520);
}
// airflower glide only when holding jump key
} else if (controller->hold(Controller::JUMP) && player_status->bonus == AIR_BONUS && physic.get_velocity_y() > MAX_GLIDE_YM) {
if (glide_time > 0 && !ability_timer.started())
ability_timer.start(glide_time);
else if (ability_timer.started()) {
log_debug << ability_timer.get_timeleft() << std::endl;
if (ability_timer.get_timeleft() <= 0.05f) {
glide_time = 0;
ability_timer.stop();
} else {
physic.set_velocity_y(MAX_GLIDE_YM);
physic.set_acceleration_y(0);
}
}
}
/*ability_timer.started() ? gliding = true : ability_timer.start(player_status->max_air_time);*/
//}
// Let go of jump key
else if(!controller->hold(Controller::JUMP)) {
if (!backflipping && jumping && physic.get_velocity_y() < 0) {
jumping = false;
early_jump_apex();
}
if (player_status->bonus == AIR_BONUS && ability_timer.started()){
glide_time = ability_timer.get_timeleft();
ability_timer.stop();
}
}
if(jump_early_apex && physic.get_velocity_y() >= 0) {
do_jump_apex();
}
/* In case the player has pressed Down while in a certain range of air,
enable butt jump action */
if (controller->hold(Controller::DOWN) && !duck && is_big() && !on_ground()) {
wants_buttjump = true;
if (physic.get_velocity_y() >= BUTTJUMP_MIN_VELOCITY_Y) does_buttjump = true;
}
/* When Down is not held anymore, disable butt jump */
if(!controller->hold(Controller::DOWN)) {
wants_buttjump = false;
does_buttjump = false;
}
// swimming
physic.set_acceleration_y(0);
#ifdef SWIMMING
if (swimming) {
if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
physic.set_acceleration_y(-2000);
physic.set_velocity_y(physic.get_velocity_y() * 0.94);
}
#endif
}
void
Player::handle_input()
{
if (ghost_mode) {
handle_input_ghost();
return;
}
if (climbing) {
handle_input_climbing();
return;
}
/* Peeking */
if( controller->released( Controller::PEEK_LEFT ) || controller->released( Controller::PEEK_RIGHT ) ) {
peekingX = AUTO;
}
if( controller->released( Controller::PEEK_UP ) || controller->released( Controller::PEEK_DOWN ) ) {
peekingY = AUTO;
}
if( controller->pressed( Controller::PEEK_LEFT ) ) {
peekingX = LEFT;
}
if( controller->pressed( Controller::PEEK_RIGHT ) ) {
peekingX = RIGHT;
}
if(!backflipping && !jumping && on_ground()) {
if( controller->pressed( Controller::PEEK_UP ) ) {
peekingY = UP;
} else if( controller->pressed( Controller::PEEK_DOWN ) ) {
peekingY = DOWN;
}
}
/* Handle horizontal movement: */
if (!backflipping) handle_horizontal_input();
/* Jump/jumping? */
if (on_ground())
can_jump = true;
/* Handle vertical movement: */
handle_vertical_input();
/* Shoot! */
if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
if(Sector::current()->add_bullet(
get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
: Vector(32, bbox.get_height()/2)),
player_status,
physic.get_velocity_x(), dir))
shooting_timer.start(SHOOTING_TIME);
}
/* Duck or Standup! */
if (controller->hold(Controller::DOWN)) {
do_duck();
} else {
do_standup();
}
/* grabbing */
try_grab();
if(!controller->hold(Controller::ACTION) && grabbed_object) {
MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
if(moving_object) {
// move the grabbed object a bit away from tux
Rectf grabbed_bbox = moving_object->get_bbox();
Rectf dest_;
dest_.p2.y = bbox.get_top() + bbox.get_height()*0.66666;
dest_.p1.y = dest_.p2.y - grabbed_bbox.get_height();
if(dir == LEFT) {
dest_.p2.x = bbox.get_left() - 1;
dest_.p1.x = dest_.p2.x - grabbed_bbox.get_width();
} else {
dest_.p1.x = bbox.get_right() + 1;
dest_.p2.x = dest_.p1.x + grabbed_bbox.get_width();
}
if(Sector::current()->is_free_of_tiles(dest_, true)) {
moving_object->set_pos(dest_.p1);
if(controller->hold(Controller::UP)) {
grabbed_object->ungrab(*this, UP);
} else {
grabbed_object->ungrab(*this, dir);
}
grabbed_object = NULL;
}
} else {
log_debug << "Non MovingObject grabbed?!?" << std::endl;
}
}
/* stop backflipping at will */
if( backflipping && ( !controller->hold(Controller::JUMP) && !backflip_timer.started()) ){
backflipping = false;
backflip_direction = 0;
sprite->set_angle(0.0f);
}
}
void
Player::position_grabbed_object()
{
MovingObject* moving_object = dynamic_cast<MovingObject*>(grabbed_object);
assert(moving_object);
// Position where we will hold the lower-inner corner
Vector pos(get_bbox().get_left() + get_bbox().get_width()/2,
get_bbox().get_top() + get_bbox().get_height()*0.66666);
// Adjust to find the grabbed object's upper-left corner
if (dir == LEFT)
pos.x -= moving_object->get_bbox().get_width();
pos.y -= moving_object->get_bbox().get_height();
grabbed_object->grab(*this, pos, dir);
}
void
Player::try_grab()
{
if(controller->hold(Controller::ACTION) && !grabbed_object
&& !duck) {
Sector* sector = Sector::current();
Vector pos;
if(dir == LEFT) {
pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
} else {
pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
}
for(Sector::Portables::iterator i = sector->portables.begin();
i != sector->portables.end(); ++i) {
Portable* portable = *i;
if(!portable->is_portable())
continue;
// make sure the Portable is a MovingObject
MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
assert(moving_object);
if(moving_object == NULL)
continue;
// make sure the Portable isn't currently non-solid
if(moving_object->get_group() == COLGROUP_DISABLED) continue;
// check if we are within reach
if(moving_object->get_bbox().contains(pos)) {
if (climbing) stop_climbing(*climbing);
grabbed_object = portable;
position_grabbed_object();
break;
}
}
}
}
void
Player::handle_input_ghost()
{
float vx = 0;
float vy = 0;
if (controller->hold(Controller::LEFT)) {
dir = LEFT;
vx -= MAX_RUN_XM * 2;
}
if (controller->hold(Controller::RIGHT)) {
dir = RIGHT;
vx += MAX_RUN_XM * 2;
}
if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
vy -= MAX_RUN_XM * 2;
}
if (controller->hold(Controller::DOWN)) {
vy += MAX_RUN_XM * 2;
}
if (controller->hold(Controller::ACTION)) {
set_ghost_mode(false);
}
physic.set_velocity(vx, vy);
physic.set_acceleration(0, 0);
}
void
Player::add_coins(int count)
{
player_status->add_coins(count);
}
int
Player::get_coins()
{
return player_status->coins;
}
bool
Player::add_bonus(const std::string& bonustype)
{
BonusType type = NO_BONUS;
if(bonustype == "grow") {
type = GROWUP_BONUS;
} else if(bonustype == "fireflower") {
type = FIRE_BONUS;
} else if(bonustype == "iceflower") {
type = ICE_BONUS;
} else if(bonustype == "airflower") {
type = AIR_BONUS;
} else if(bonustype == "earthflower") {
type = EARTH_BONUS;
} else if(bonustype == "none") {
type = NO_BONUS;
} else {