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