merged new collision detection branch back into mainline
[supertux.git] / src / badguy / totem.cpp
1 //  $Id$
2 // 
3 //  SuperTux - "Totem" Badguy
4 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.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
21 #include <config.h>
22
23 #include "totem.hpp"
24 #include "log.hpp"
25
26 static const float WALKSPEED = 100;
27 static const float JUMP_ON_SPEED_Y = -400;
28 static const float JUMP_OFF_SPEED_Y = -500;
29
30 Totem::Totem(const lisp::Lisp& reader)
31         : BadGuy(reader, "images/creatures/totem/totem.sprite")
32 {
33   carrying = 0;
34   carried_by = 0;
35 }
36
37 Totem::Totem(const Totem& other)
38         : BadGuy(other), carrying(other.carrying), carried_by(other.carried_by)
39 {
40 }
41
42 Totem::~Totem() 
43 {
44   if (carrying) carrying->jump_off();
45   if (carried_by) jump_off();
46 }
47
48 bool 
49 Totem::updatePointers(const GameObject* from_object, GameObject* to_object)
50 {
51   if (from_object == carrying) {
52     carrying = dynamic_cast<Totem*>(to_object);
53     return true;
54   }
55   if (from_object == carried_by) {
56     carried_by = dynamic_cast<Totem*>(to_object);
57     return true;
58   }
59   return false;
60 }
61
62 void
63 Totem::write(lisp::Writer& writer)
64 {
65   writer.start_list("totem");
66
67   writer.write_float("x", start_position.x);
68   writer.write_float("y", start_position.y);
69
70   writer.end_list("totem");
71 }
72
73 void
74 Totem::activate()
75 {
76   if (!carried_by) {
77     physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
78     sprite->set_action(dir == LEFT ? "walking-left" : "walking-right");
79     return;
80   } else {
81     synchronize_with(carried_by);
82     sprite->set_action(dir == LEFT ? "stacked-left" : "stacked-right");
83     return;
84   }
85 }
86
87 void
88 Totem::active_update(float elapsed_time)
89 {
90   BadGuy::active_update(elapsed_time);
91
92   if (!carried_by) {
93     if (might_fall())
94     {
95       dir = (dir == LEFT ? RIGHT : LEFT);
96       activate();
97     }
98
99     Sector* s = Sector::current();
100     if (s) {
101       // jump a bit if we find a suitable totem 
102       for (std::vector<MovingObject*>::iterator i = s->moving_objects.begin(); i != s->moving_objects.end(); i++) {
103         Totem* t = dynamic_cast<Totem*>(*i);
104         if (!t) continue;
105         
106         // skip if we are not approaching each other
107         if (!((this->dir == LEFT) && (t->dir == RIGHT))) continue;
108         
109         Vector p1 = this->get_pos();
110         Vector p2 = t->get_pos();
111
112         // skip if not on same height
113         float dy = (p1.y - p2.y);
114         if (fabsf(dy - 0) > 2) continue;
115
116         // skip if too far away
117         float dx = (p1.x - p2.x);
118         if (fabsf(dx - 128) > 2) continue;
119
120         physic.set_velocity_y(JUMP_ON_SPEED_Y);
121         p1.y -= 1;
122         this->set_pos(p1);
123         break;
124       }
125     }
126   }
127
128   if (carried_by) {
129     this->synchronize_with(carried_by);
130   }
131
132   if (carrying) {
133     carrying->synchronize_with(this);
134   }
135
136 }
137
138 bool
139 Totem::collision_squished(Player& player)
140 {
141   if (carrying) carrying->jump_off();
142   if (carried_by) {
143     player.bounce(*this);
144     jump_off();
145   }
146
147   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
148   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
149
150   kill_squished(player);
151   return true;
152 }
153
154 void
155 Totem::collision_solid(const CollisionHit& hit)
156 {
157   // if we are being carried around, pass event to bottom of stack and ignore it
158   if (carried_by) {
159     carried_by->collision_solid(hit);
160     return;
161   }
162
163   // If we hit something from above or below: stop moving in this direction 
164   if (hit.top || hit.bottom) {
165     physic.set_velocity_y(0);
166   }
167
168   // If we are hit from the direction we are facing: turn around
169   if (hit.left && (dir == LEFT)) {
170     dir = RIGHT;
171     activate();
172   }
173   if (hit.right && (dir == RIGHT)) {
174     dir = LEFT;
175     activate();
176   }
177 }
178
179 HitResponse
180 Totem::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
181 {
182   // if we are being carried around, pass event to bottom of stack and ignore it
183   if (carried_by) {
184     carried_by->collision_badguy(badguy, hit);
185     return CONTINUE;
186   }
187  
188   // if we hit a Totem that is not from our stack: have our base jump on its top
189   Totem* totem = dynamic_cast<Totem*>(&badguy);
190   if (totem) {
191     Totem* thisBase = this; while (thisBase->carried_by) thisBase=thisBase->carried_by;
192     Totem* srcBase = totem; while (srcBase->carried_by)  srcBase=srcBase->carried_by;
193     Totem* thisTop = this;  while (thisTop->carrying)    thisTop=thisTop->carrying;
194     if (srcBase != thisBase) {
195       srcBase->jump_on(thisTop);
196     }
197   }
198
199   // If we are hit from the direction we are facing: turn around
200   if(hit.left && (dir == LEFT)) {
201     dir = RIGHT;
202     activate();
203   }
204   if(hit.right && (dir == RIGHT)) {
205     dir = LEFT;
206     activate();
207   }
208
209   return CONTINUE;
210 }
211
212 void
213 Totem::kill_fall()
214 {
215   if (carrying) carrying->jump_off();
216   if (carried_by) jump_off();
217
218   BadGuy::kill_fall();
219 }
220
221 void 
222 Totem::jump_on(Totem* target)
223 {
224   if (target->carrying) {
225     log_warning << "target is already carrying someone" << std::endl;
226     return;
227   }
228   
229   target->carrying = this;
230
231   this->carried_by = target;
232   this->activate();
233   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
234   
235   this->synchronize_with(target);
236 }
237
238 void
239 Totem::jump_off() {
240   if (!carried_by) {
241     log_warning << "not carried by anyone" << std::endl;
242     return;
243   }
244
245   carried_by->carrying = 0;
246
247   this->carried_by = 0;
248
249   this->activate();
250   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
251
252
253   physic.set_velocity_y(JUMP_OFF_SPEED_Y);
254 }
255
256 void 
257 Totem::synchronize_with(Totem* base)
258 {
259
260   if (dir != base->dir) {
261     dir = base->dir;
262     sprite->set_action(dir == LEFT ? "stacked-left" : "stacked-right");
263   }
264   
265   Vector pos = base->get_pos();
266   pos.y -= sprite->get_current_hitbox_height();
267   set_pos(pos);
268
269   physic.set_velocity_x(base->physic.get_velocity_x());
270   physic.set_velocity_y(base->physic.get_velocity_y());
271 }
272
273
274 IMPLEMENT_FACTORY(Totem, "totem")
275