Badguys now inherit from MovingSprite
[supertux.git] / src / badguy / nolok_01.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "nolok_01.hpp"
23 #include "badguy/snail.hpp"
24 #include "trigger/door.hpp"
25
26 #define WALK_TIME 2.5
27 #define SHOOT_TIME 0.4
28 #define JUMP_TIME 0.5
29 #define INITIAL_HITPOINTS 3
30 #define INITIAL_BULLET_HP 10
31
32 static const float WALKSPEED = 90;
33
34 //TODO: Create sprite, limit max number of snowballs
35 Nolok_01::Nolok_01(const lisp::Lisp& reader)
36         : BadGuy(reader, "images/creatures/nolok/nolok.sprite")
37 {
38   countMe = false;
39 }
40
41 Nolok_01::Nolok_01(const Vector& pos)
42         : BadGuy(pos, "images/creatures/nolok/nolok.sprite")
43 {
44   countMe = false;
45 }
46
47 void
48 Nolok_01::write(lisp::Writer& writer)
49 {
50   writer.start_list("nolok_01");
51
52   writer.write_float("x", start_position.x);
53   writer.write_float("y", start_position.y);
54
55   writer.end_list("nolok_01");
56 }
57
58 void
59 Nolok_01::activate()
60 {
61   //hitpoints = INITIAL_HITPOINTS;
62   //bullet_hitpoints = INITIAL_BULLET_HP;
63   physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
64   sprite->set_action(dir == LEFT ? "left" : "right");
65   action = WALKING;
66   action_timer.start(WALK_TIME);
67 }
68
69 void
70 Nolok_01::active_update(float elapsed_time)
71 {
72    if (action_timer.check()) {
73      switch (action) {       
74        case WALKING:
75         {
76          sprite->set_action("jump");
77          physic.set_velocity_y(700);
78          action = JUMPING;
79          action_timer.start(JUMP_TIME);
80          break;
81         }
82        case JUMPING:
83        {
84         sprite->set_action("throw");
85         action = SHOOTING;
86         action_timer.start(SHOOT_TIME);
87         break;
88        }
89        case SHOOTING:
90        {
91         Sector::current()->add_object(new Snail(Vector(get_pos().x - 64, get_pos().y), LEFT));
92         Sector::current()->add_object(new Snail(Vector(get_pos().x + 64, get_pos().y), RIGHT));
93         physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
94         sprite->set_action(dir == LEFT ? "left" : "right");
95         action = WALKING;
96         action_timer.start(WALK_TIME);
97         break;
98        }
99      }
100    }
101    movement = physic.get_movement(elapsed_time);
102 }
103
104 bool
105 Nolok_01::collision_squished(Player& player)
106 {
107   bool result = false;
108   player.bounce(*this);
109 #if 0
110   if (hitpoints <= 0) {
111     bullet_hitpoints = 0;
112     sprite->set_action("dead"); 
113     kill_squished(player);
114     Sector::current()->add_object(new Door((int)get_pos().x+32, 512, "sector1", "main2"));
115     result = true;
116   }
117 #endif
118   return result;
119 }
120
121 HitResponse
122 Nolok_01::collision_solid(GameObject& , const CollisionHit& hit)
123 {
124   if(fabsf(hit.normal.y) > .5){ // hit floor or roof?
125     if (action != JUMPING) physic.set_velocity_y(0);
126   } else { // hit right or left
127     dir = dir == LEFT ? RIGHT : LEFT;
128     sprite->set_action(dir == LEFT ? "left" : "right");
129     physic.set_velocity_x(-physic.get_velocity_x());
130   }
131
132   return CONTINUE;
133 }
134
135 //TODO: Hitpoint count incorrect when combining squishing and shooting
136 void
137 Nolok_01::kill_fall()
138 {
139 #if 0
140   bullet_hitpoints--;
141   if (bullet_hitpoints <= 0) {
142    hitpoints = 0;
143    sound_manager->play("sounds/fall.wav", this,
144                              this->get_pos());
145    physic.set_velocity_y(0);
146    physic.enable_gravity(true);
147    set_state(STATE_FALLING);
148    Sector::current()->add_object(new Door((int)get_pos().x+32, 512, "sector1", "main2"));
149   }
150 #endif
151 }
152
153 IMPLEMENT_FACTORY(Nolok_01, "nolok_01")