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 static const float WALKSPEED = 100;
27 static const float JUMP_ON_SPEED_Y = -400;
28 static const float JUMP_OFF_SPEED_Y = -500;
30 Totem::Totem(const lisp::Lisp& reader)
31 : BadGuy(reader, "images/creatures/totem/totem.sprite")
37 Totem::Totem(const Totem& other)
38 : BadGuy(other), carrying(other.carrying), carried_by(other.carried_by)
44 if (carrying) carrying->jump_off();
45 if (carried_by) jump_off();
49 Totem::updatePointers(const GameObject* from_object, GameObject* to_object)
51 if (from_object == carrying) {
52 carrying = dynamic_cast<Totem*>(to_object);
55 if (from_object == carried_by) {
56 carried_by = dynamic_cast<Totem*>(to_object);
63 Totem::write(lisp::Writer& writer)
65 writer.start_list("totem");
67 writer.write_float("x", start_position.x);
68 writer.write_float("y", start_position.y);
70 writer.end_list("totem");
77 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
78 sprite->set_action(dir == LEFT ? "walking-left" : "walking-right");
81 synchronize_with(carried_by);
82 sprite->set_action(dir == LEFT ? "stacked-left" : "stacked-right");
88 Totem::active_update(float elapsed_time)
90 BadGuy::active_update(elapsed_time);
95 dir = (dir == LEFT ? RIGHT : LEFT);
99 Sector* s = Sector::current();
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);
106 // skip if we are not approaching each other
107 if (!((this->dir == LEFT) && (t->dir == RIGHT))) continue;
109 Vector p1 = this->get_pos();
110 Vector p2 = t->get_pos();
112 // skip if not on same height
113 float dy = (p1.y - p2.y);
114 if (fabsf(dy - 0) > 2) continue;
116 // skip if too far away
117 float dx = (p1.x - p2.x);
118 if (fabsf(dx - 128) > 2) continue;
120 physic.set_velocity_y(JUMP_ON_SPEED_Y);
129 this->synchronize_with(carried_by);
133 carrying->synchronize_with(this);
139 Totem::collision_squished(Player& player)
141 if (carrying) carrying->jump_off();
143 player.bounce(*this);
147 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
148 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
150 kill_squished(player);
155 Totem::collision_solid(const CollisionHit& hit)
157 // if we are being carried around, pass event to bottom of stack and ignore it
159 carried_by->collision_solid(hit);
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);
168 // If we are hit from the direction we are facing: turn around
169 if (hit.left && (dir == LEFT)) {
173 if (hit.right && (dir == RIGHT)) {
180 Totem::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
182 // if we are being carried around, pass event to bottom of stack and ignore it
184 carried_by->collision_badguy(badguy, hit);
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);
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);
199 // If we are hit from the direction we are facing: turn around
200 if(hit.left && (dir == LEFT)) {
204 if(hit.right && (dir == RIGHT)) {
215 if (carrying) carrying->jump_off();
216 if (carried_by) jump_off();
222 Totem::jump_on(Totem* target)
224 if (target->carrying) {
225 log_warning << "target is already carrying someone" << std::endl;
229 target->carrying = this;
231 this->carried_by = target;
233 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
235 this->synchronize_with(target);
241 log_warning << "not carried by anyone" << std::endl;
245 carried_by->carrying = 0;
247 this->carried_by = 0;
250 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
253 physic.set_velocity_y(JUMP_OFF_SPEED_Y);
257 Totem::synchronize_with(Totem* base)
260 if (dir != base->dir) {
262 sprite->set_action(dir == LEFT ? "stacked-left" : "stacked-right");
265 Vector pos = base->get_pos();
266 pos.y -= sprite->get_current_hitbox_height();
269 physic.set_velocity_x(base->physic.get_velocity_x());
270 physic.set_velocity_y(base->physic.get_velocity_y());
274 IMPLEMENT_FACTORY(Totem, "totem")