d9404c5570252b9bb295054a7226afbc953a5506
[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.5
10 #define INITIAL_HITPOINTS 3
11
12 static const float WALKSPEED = 90;
13
14 //TODO: Create sprite, limit max number of snowballs
15 Nolok_01::Nolok_01(const lisp::Lisp& reader)
16 {
17   reader.get("x", start_position.x);
18   reader.get("y", start_position.y);
19   bbox.set_size(31.8, 63.8);
20   sprite = sprite_manager->create("dummyguy");
21 }
22
23 Nolok_01::Nolok_01(float pos_x, float pos_y)
24 {
25   start_position.x = pos_x;
26   start_position.y = pos_y;
27   bbox.set_size(31.8, 63.8);
28   sprite = sprite_manager->create("dummyguy");
29 }
30
31 void
32 Nolok_01::write(lisp::Writer& writer)
33 {
34   writer.start_list("nolok_01");
35
36   writer.write_float("x", start_position.x);
37   writer.write_float("y", start_position.y);
38
39   writer.end_list("nolok_01");
40 }
41
42 void
43 Nolok_01::activate()
44 {
45   hitpoints = INITIAL_HITPOINTS;
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    if (action_timer.check()) {
56      switch (action) {       
57        case WALKING:
58         {
59          sprite->set_action("jump");
60          physic.set_velocity_y(700);
61          action = JUMPING;
62          action_timer.start(JUMP_TIME);
63          break;
64         }
65        case JUMPING:
66        {
67         sprite->set_action("throw");
68         action = SHOOTING;
69         action_timer.start(SHOOT_TIME);
70         break;
71        }
72        case SHOOTING:
73        {
74         Sector::current()->add_object(new BouncingSnowball(get_pos().x - 64, get_pos().y, LEFT));
75         Sector::current()->add_object(new BouncingSnowball(get_pos().x + 64, get_pos().y, RIGHT));
76         physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
77         sprite->set_action(dir == LEFT ? "left" : "right");
78         action = WALKING;
79         action_timer.start(WALK_TIME);
80         break;
81        }
82      }
83    }
84    movement = physic.get_movement(elapsed_time);
85 }
86
87 bool
88 Nolok_01::collision_squished(Player& player)
89 {
90   bool result = false;
91   player.bounce(*this);
92   if (hitpoints <= 0) {
93     sprite->set_action("dead"); 
94     kill_squished(player);
95     Sector::current()->add_object(new Door((int)get_pos().x+32, 512, "sector1", "main2"));
96     result = true;
97   }
98   return result;
99 }
100
101 HitResponse
102 Nolok_01::collision_solid(GameObject& , const CollisionHit& hit)
103 {
104   if(fabsf(hit.normal.y) > .5){ // hit floor or roof?
105     if (action != JUMPING) physic.set_velocity_y(0);
106   } else { // hit right or left
107     dir = dir == LEFT ? RIGHT : LEFT;
108     sprite->set_action(dir == LEFT ? "left" : "right");
109     physic.set_velocity_x(-physic.get_velocity_x());
110   }
111
112   return CONTINUE;
113 }
114
115 //TODO: Hitpoint count incorrect when combining squishing and shooting
116 void
117 Nolok_01::kill_fall()
118 {
119   hitpoints--;
120   if (hitpoints <= 0) {
121    SoundManager::get()->play_sound(IDToSound(SND_FALL), this,
122          Sector::current()->player->get_pos());
123    physic.set_velocity_y(0);
124    physic.enable_gravity(true);
125    set_state(STATE_FALLING);
126    Sector::current()->add_object(new Door((int)get_pos().x+32, 512, "sector1", "main2"));
127   }
128 }
129
130 IMPLEMENT_FACTORY(Nolok_01, "nolok_01")