fixed a couple of nolok's minor bugs
[supertux.git] / src / badguy / nolok_01.cpp
1 #include <config.h>
2
3 #include "nolok_01.h"
4 #include "badguy/bouncing_snowball.h"
5 #include "trigger/door.h"
6
7 #define WALK_TIME 2.5
8 #define SHOOT_TIME 0.4
9 #define JUMP_TIME 0.3
10
11 static const float WALKSPEED = 90;
12
13 //TODO: Create sprite, give multiple hitpoints, limit max number of snowballs
14 //      Can only be killed when jumping, no idea why
15 //      Stop actions when pause button is hit (probably a general problem of timers)
16 Nolok_01::Nolok_01(LispReader& reader)
17 {
18   reader.read_float("x", start_position.x);
19   reader.read_float("y", start_position.y);
20   bbox.set_size(31.8, 63.8);
21   sprite = sprite_manager->create("dummyguy");
22 }
23
24 Nolok_01::Nolok_01(float pos_x, float pos_y)
25 {
26   start_position.x = pos_x;
27   start_position.y = pos_y;
28   bbox.set_size(31.8, 63.8);
29   sprite = sprite_manager->create("dummyguy");
30 }
31
32 void
33 Nolok_01::write(LispWriter& writer)
34 {
35   writer.start_list("nolok01");
36
37   writer.write_float("x", get_pos().x);
38   writer.write_float("y", get_pos().y);
39
40   writer.end_list("nolok01");
41 }
42
43 void
44 Nolok_01::activate()
45 {
46   physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
47   sprite->set_action(dir == LEFT ? "left" : "right");
48   action = WALKING;
49   action_timer.start(WALK_TIME);
50 }
51
52 void
53 Nolok_01::active_action(float elapsed_time)
54 {
55    movement = physic.get_movement(elapsed_time);
56    if (action_timer.check()) {
57      if (action == WALKING) {
58         physic.set_velocity_y(700);
59         action = JUMPING;
60         action_timer.start(JUMP_TIME);
61      }
62      else if (action == JUMPING) {
63         sprite->set_action("throw");
64         action = SHOOTING;
65         action_timer.start(SHOOT_TIME);
66      }
67      else if (action == SHOOTING) {
68         Sector::current()->add_object(new BouncingSnowball(get_pos().x - 64, get_pos().y, LEFT));
69         Sector::current()->add_object(new BouncingSnowball(get_pos().x + 64, get_pos().y, RIGHT));
70         physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
71         sprite->set_action(dir == LEFT ? "left" : "right");
72         action = WALKING;
73         action_timer.start(WALK_TIME);
74      }
75    }
76 }
77
78 bool
79 Nolok_01::collision_squished(Player& player)
80 {
81   sprite->set_action("dead"); 
82   kill_squished(player);
83   Sector::current()->add_object(new Door((int)get_pos().x+32, 512, "sector1", "main2"));
84   return true;
85 }
86
87 HitResponse
88 Nolok_01::collision_solid(GameObject& , const CollisionHit& hit)
89 {
90   if(fabsf(hit.normal.y) > .5) { // hit floor or roof?
91     //physic.set_velocity_y(0);
92   } else { // hit right or left
93     dir = dir == LEFT ? RIGHT : LEFT;
94     sprite->set_action(dir == LEFT ? "left" : "right");
95     physic.set_velocity_x(-physic.get_velocity_x());
96   }
97
98   return CONTINUE;
99 }
100