3 // SuperTux - "Totem" Badguy
4 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
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.
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.
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
26 #include "lisp/writer.hpp"
27 #include "object_factory.hpp"
28 #include "object/player.hpp"
29 #include "audio/sound_manager.hpp"
31 #include "sprite/sprite.hpp"
35 static const float WALKSPEED = 100;
36 static const float JUMP_ON_SPEED_Y = -400;
37 static const float JUMP_OFF_SPEED_Y = -500;
38 static const std::string LAND_ON_TOTEM_SOUND = "sounds/totem.ogg";
40 Totem::Totem(const lisp::Lisp& reader)
41 : BadGuy(reader, "images/creatures/totem/totem.sprite")
45 sound_manager->preload( LAND_ON_TOTEM_SOUND );
48 Totem::Totem(const Totem& other)
49 : BadGuy(other), carrying(other.carrying), carried_by(other.carried_by)
51 sound_manager->preload( LAND_ON_TOTEM_SOUND );
56 if (carrying) carrying->jump_off();
57 if (carried_by) jump_off();
61 Totem::updatePointers(const GameObject* from_object, GameObject* to_object)
63 if (from_object == carrying) {
64 carrying = dynamic_cast<Totem*>(to_object);
67 if (from_object == carried_by) {
68 carried_by = dynamic_cast<Totem*>(to_object);
75 Totem::write(lisp::Writer& writer)
77 writer.start_list("totem");
79 writer.write("x", start_position.x);
80 writer.write("y", start_position.y);
82 writer.end_list("totem");
89 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
90 sprite->set_action(dir == LEFT ? "walking-left" : "walking-right");
93 synchronize_with(carried_by);
94 sprite->set_action(dir == LEFT ? "stacked-left" : "stacked-right");
100 Totem::active_update(float elapsed_time)
102 BadGuy::active_update(elapsed_time);
105 if (on_ground() && might_fall())
107 dir = (dir == LEFT ? RIGHT : LEFT);
111 Sector* s = Sector::current();
113 // jump a bit if we find a suitable totem
114 for (std::vector<MovingObject*>::iterator i = s->moving_objects.begin(); i != s->moving_objects.end(); i++) {
115 Totem* t = dynamic_cast<Totem*>(*i);
118 // skip if we are not approaching each other
119 if (!((this->dir == LEFT) && (t->dir == RIGHT))) continue;
121 Vector p1 = this->get_pos();
122 Vector p2 = t->get_pos();
124 // skip if not on same height
125 float dy = (p1.y - p2.y);
126 if (fabsf(dy - 0) > 2) continue;
128 // skip if too far away
129 float dx = (p1.x - p2.x);
130 if (fabsf(dx - 128) > 2) continue;
132 physic.set_velocity_y(JUMP_ON_SPEED_Y);
141 this->synchronize_with(carried_by);
145 carrying->synchronize_with(this);
151 Totem::collision_squished(GameObject& object)
153 if (carrying) carrying->jump_off();
155 Player* player = dynamic_cast<Player*>(&object);
156 if (player) player->bounce(*this);
160 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
161 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
163 kill_squished(object);
168 Totem::collision_solid(const CollisionHit& hit)
170 update_on_ground_flag(hit);
172 // if we are being carried around, pass event to bottom of stack and ignore it
174 carried_by->collision_solid(hit);
178 // If we hit something from above or below: stop moving in this direction
179 if (hit.top || hit.bottom) {
180 physic.set_velocity_y(0);
183 // If we are hit from the direction we are facing: turn around
184 if (hit.left && (dir == LEFT)) {
188 if (hit.right && (dir == RIGHT)) {
195 Totem::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
197 // if we are being carried around, pass event to bottom of stack and ignore it
199 carried_by->collision_badguy(badguy, hit);
203 // if we hit a Totem that is not from our stack: have our base jump on its top
204 Totem* totem = dynamic_cast<Totem*>(&badguy);
206 Totem* thisBase = this; while (thisBase->carried_by) thisBase=thisBase->carried_by;
207 Totem* srcBase = totem; while (srcBase->carried_by) srcBase=srcBase->carried_by;
208 Totem* thisTop = this; while (thisTop->carrying) thisTop=thisTop->carrying;
209 if (srcBase != thisBase) {
210 srcBase->jump_on(thisTop);
214 // If we are hit from the direction we are facing: turn around
215 if(hit.left && (dir == LEFT)) {
219 if(hit.right && (dir == RIGHT)) {
230 if (carrying) carrying->jump_off();
231 if (carried_by) jump_off();
237 Totem::jump_on(Totem* target)
239 if (target->carrying) {
240 log_warning << "target is already carrying someone" << std::endl;
244 target->carrying = this;
246 this->carried_by = target;
248 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
250 sound_manager->play( LAND_ON_TOTEM_SOUND , get_pos());
253 this->synchronize_with(target);
259 log_warning << "not carried by anyone" << std::endl;
263 carried_by->carrying = 0;
265 this->carried_by = 0;
268 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
271 physic.set_velocity_y(JUMP_OFF_SPEED_Y);
275 Totem::synchronize_with(Totem* base)
278 if (dir != base->dir) {
280 sprite->set_action(dir == LEFT ? "stacked-left" : "stacked-right");
283 Vector pos = base->get_pos();
284 pos.y -= sprite->get_current_hitbox_height();
287 physic.set_velocity_x(base->physic.get_velocity_x());
288 physic.set_velocity_y(base->physic.get_velocity_y());
292 IMPLEMENT_FACTORY(Totem, "totem")