added badguy Nolok_01, as he may appear as the boss of world 1 (still very basic).
[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 SHOOT_TIME 2.5
8 #define IDLE_TIME 0.4
9 #define JUMP_TIME 0.3
10
11 static const float WALKSPEED = 80;
12
13 //TODO: Create sprite, give multiple hitpoints, limit max number of snowballs
14 //      Can only be killed when jumping, no idea why
15 Nolok_01::Nolok_01(LispReader& reader)
16 {
17   reader.read_float("x", start_position.x);
18   reader.read_float("y", start_position.y);
19   bbox.set_size(31.8, 31.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, 31.8);
28   sprite = sprite_manager->create("dummyguy");
29 }
30
31 void
32 Nolok_01::write(LispWriter& writer)
33 {
34   writer.start_list("nolok01");
35
36   writer.write_float("x", get_pos().x);
37   writer.write_float("y", get_pos().y);
38
39   writer.end_list("nolok01");
40 }
41
42 void
43 Nolok_01::activate()
44 {
45   physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
46   sprite->set_action(dir == LEFT ? "left" : "right");
47   action_timer.start(SHOOT_TIME, true);
48 }
49
50 void
51 Nolok_01::active_action(float elapsed_time)
52 {
53    movement = physic.get_movement(elapsed_time);
54    if (action_timer.check()) {
55       physic.set_velocity_y(700);
56       jump_timer.start(JUMP_TIME);
57    }
58    if (jump_timer.check()) {
59       sprite->set_action("throw");
60       idle_timer.start(IDLE_TIME);
61    }
62    if (idle_timer.check()) {
63       Sector::current()->add_object(new BouncingSnowball(get_pos().x-32, get_pos().y, LEFT));
64       Sector::current()->add_object(new BouncingSnowball(get_pos().x, get_pos().y, RIGHT));
65       physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
66       sprite->set_action(dir == LEFT ? "left" : "right");
67    }
68 }
69
70 bool
71 Nolok_01::collision_squished(Player& player)
72 {
73   sprite->set_action("dead"); 
74   kill_squished(player);
75   Sector::current()->add_object(new Door((int)get_pos().x+32, 512, "sector1", "main2"));
76   return true;
77 }
78
79 HitResponse
80 Nolok_01::collision_solid(GameObject& , const CollisionHit& hit)
81 {
82   if(fabsf(hit.normal.y) > .5) { // hit floor or roof?
83     //physic.set_velocity_y(0);
84   } else { // hit right or left
85     dir = dir == LEFT ? RIGHT : LEFT;
86     sprite->set_action(dir == LEFT ? "left" : "right");
87     physic.set_velocity_x(-physic.get_velocity_x());
88   }
89
90   return CONTINUE;
91 }
92