moved some sprites to separate files
[supertux.git] / src / badguy / yeti.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 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
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include <float.h>
23 #include <sstream>
24 #include <memory>
25 #include "yeti.hpp"
26 #include "object/camera.hpp"
27 #include "yeti_stalactite.hpp"
28 #include "bouncing_snowball.hpp"
29 #include "game_session.hpp"
30 #include "scripting/script_interpreter.hpp"
31
32 static const float JUMP_VEL1 = 250;
33 static const float JUMP_VEL2 = 700;
34 static const float RUN_SPEED = 350;
35 static const float JUMP_TIME = 1.6;
36 static const float ANGRY_JUMP_WAIT = .5;
37 /// the time we are safe when tux just hit us
38 static const float SAFE_TIME = .5;
39 static const int INITIAL_HITPOINTS = 3;
40
41 Yeti::Yeti(const lisp::Lisp& reader)
42 {
43   reader.get("x", start_position.x);
44   reader.get("y", start_position.y);
45   bbox.set_size(80, 120);
46   sprite = sprite_manager->create("images/creatures/yeti/yeti.sprite");
47   sprite->set_action("right");
48   state = INIT;
49   side = LEFT;
50 #if 0
51   sound_manager->preload_sound("yeti_gna");
52   sound_manager->preload_sound("yeti_roar");
53 #endif
54   hit_points = INITIAL_HITPOINTS;
55   reader.get("dead-script", dead_script);
56   countMe = false;
57 }
58
59 Yeti::~Yeti()
60 {
61 }
62
63 void
64 Yeti::draw(DrawingContext& context)
65 {
66   // we blink when we are safe
67   if(safe_timer.started() && size_t(game_time*40)%2)
68     return;
69
70   BadGuy::draw(context);
71 }
72
73 void
74 Yeti::active_update(float elapsed_time)
75 {
76   switch(state) {
77     case INIT:
78       break;
79     case GO_RIGHT:
80       physic.set_velocity_x(RUN_SPEED);
81       if(timer.check())
82         physic.set_velocity_y(JUMP_VEL2);
83       break;
84     case GO_LEFT:
85       physic.set_velocity_x(-RUN_SPEED);
86       if(timer.check())
87         physic.set_velocity_y(JUMP_VEL2);
88       break;
89     case ANGRY_JUMPING:
90       if(timer.check()) {
91         // jump
92         sound_manager->play("sounds/yeti_gna.wav");
93         physic.set_velocity_y(JUMP_VEL1);
94       }
95       break;
96     default:
97       break;
98   }
99
100   movement = physic.get_movement(elapsed_time);
101 }
102
103 void
104 Yeti::go_right()
105 {
106   // jump and move right
107   physic.set_velocity_y(JUMP_VEL1);
108   physic.set_velocity_x(RUN_SPEED);
109   state = GO_RIGHT;
110   timer.start(JUMP_TIME);
111 }
112
113 void
114 Yeti::go_left()
115 {
116   physic.set_velocity_y(JUMP_VEL1);
117   physic.set_velocity_x(-RUN_SPEED);
118   state = GO_LEFT;
119   timer.start(JUMP_TIME);
120 }
121
122 void
123 Yeti::angry_jumping()
124 {
125   jumpcount = 0;
126   timer.start(ANGRY_JUMP_WAIT);
127   state = ANGRY_JUMPING;
128   physic.set_velocity_x(0);
129 }
130
131 void
132 Yeti::summon_snowball()
133 {
134   Sector::current()->add_object(new BouncingSnowball(get_pos().x+(side == LEFT ? 64 : -64), get_pos().y, (side == LEFT ? RIGHT : LEFT)));
135 }
136
137 bool
138 Yeti::collision_squished(Player& player)
139 {
140   if(safe_timer.started())
141     return true;
142
143   player.bounce(*this);
144   sound_manager->play("sounds/yeti_roar.wav");
145   hit_points--;
146   if(hit_points <= 0) {
147     sprite->set_action("dead");
148     kill_squished(player);
149
150     // start script
151     if(dead_script != "") {
152       ScriptInterpreter::add_script_object(Sector::current(),
153           "Yeti - dead-script", dead_script);
154     }
155   } else {
156     safe_timer.start(SAFE_TIME);
157   }
158   
159   return true;
160 }
161
162 void
163 Yeti::kill_fall()
164 {
165   // shooting bullets or being invincible won't work :)
166 }
167
168 void
169 Yeti::write(lisp::Writer& )
170 {
171 }
172
173 void
174 Yeti::drop_stalactite()
175 {
176   YetiStalactite* nearest = 0;
177   float dist = FLT_MAX;
178
179   Sector* sector = Sector::current();
180   for(Sector::GameObjects::iterator i = sector->gameobjects.begin();
181       i != sector->gameobjects.end(); ++i) {
182     YetiStalactite* stalactite = dynamic_cast<YetiStalactite*> (*i);
183     if(stalactite && stalactite->is_hanging()) {
184       float sdist 
185         = fabsf(stalactite->get_pos().x - sector->player->get_pos().x);
186       if(sdist < dist) {
187         nearest = stalactite;
188         dist = sdist;
189       }
190     }
191   }
192
193   if(nearest)
194     nearest->start_shaking();
195 }
196
197 HitResponse
198 Yeti::collision_solid(GameObject& , const CollisionHit& hit)
199 {
200   if(fabsf(hit.normal.y) > .5) { // hit floor or roof?
201     physic.set_velocity_y(0);
202     if(state == INIT) {
203       go_right();
204     } else if(state == GO_LEFT && !timer.started()) {
205       side = LEFT;
206       summon_snowball();
207       sprite->set_action("right");
208       angry_jumping();
209     } else if(state == GO_RIGHT && !timer.started()) {
210       side = RIGHT;
211       summon_snowball();
212       sprite->set_action("left");
213       angry_jumping();
214     } else if(state == ANGRY_JUMPING) {
215       if(!timer.started()) {
216         // we just landed
217         jumpcount++;
218         // make a stalactite falling down and shake camera a bit
219         Sector::current()->camera->shake(.1, 0, 10);
220         drop_stalactite();
221         
222         // go to other side after 3 jumps
223         if(jumpcount == 3) {
224           if(side == LEFT)
225             go_right();
226           else
227             go_left();
228         } else {
229           // jump again
230           timer.start(ANGRY_JUMP_WAIT);
231         }
232       }
233     }
234   }
235   
236   return CONTINUE;
237 }
238
239 IMPLEMENT_FACTORY(Yeti, "yeti")