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)
35 reader.get("x", start_position.x);
36 reader.get("y", start_position.y);
37 sprite = sprite_manager->create("images/creatures/totem/totem.sprite");
38 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
41 Totem::Totem(const Totem& other)
42 : BadGuy(other), carrying(other.carrying), carried_by(other.carried_by)
48 if (carrying) carrying->jump_off();
49 if (carried_by) jump_off();
53 Totem::updatePointers(const GameObject* from_object, GameObject* to_object)
55 if (from_object == carrying) {
56 carrying = dynamic_cast<Totem*>(to_object);
59 if (from_object == carried_by) {
60 carried_by = dynamic_cast<Totem*>(to_object);
67 Totem::write(lisp::Writer& writer)
69 writer.start_list("totem");
71 writer.write_float("x", start_position.x);
72 writer.write_float("y", start_position.y);
74 writer.end_list("totem");
81 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
82 sprite->set_action(dir == LEFT ? "walking-left" : "walking-right");
85 synchronize_with(carried_by);
86 sprite->set_action(dir == LEFT ? "stacked-left" : "stacked-right");
92 Totem::active_update(float elapsed_time)
94 BadGuy::active_update(elapsed_time);
99 dir = (dir == LEFT ? RIGHT : LEFT);
103 Sector* s = Sector::current();
105 // jump a bit if we find a suitable totem
106 for (std::vector<MovingObject*>::iterator i = s->moving_objects.begin(); i != s->moving_objects.end(); i++) {
107 Totem* t = dynamic_cast<Totem*>(*i);
110 // skip if we are not approaching each other
111 if (!((this->dir == LEFT) && (t->dir == RIGHT))) continue;
113 Vector p1 = this->get_pos();
114 Vector p2 = t->get_pos();
116 // skip if not on same height
117 float dy = (p1.y - p2.y);
118 if (fabsf(dy - 0) > 2) continue;
120 // skip if too far away
121 float dx = (p1.x - p2.x);
122 if (fabsf(dx - 128) > 2) continue;
124 physic.set_velocity_y(JUMP_ON_SPEED_Y);
133 this->synchronize_with(carried_by);
137 carrying->synchronize_with(this);
143 Totem::collision_squished(Player& player)
145 if (carrying) carrying->jump_off();
147 player.bounce(*this);
151 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
152 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
154 kill_squished(player);
159 Totem::collision_solid(GameObject& object, const CollisionHit& hit)
161 // if we are being carried around, pass event to bottom of stack and ignore it
163 carried_by->collision_solid(object, hit);
167 // If we hit something from above or below: stop moving in this direction
168 if (hit.normal.y != 0) {
169 physic.set_velocity_y(0);
172 // If we are hit from the direction we are facing: turn around
173 if ((hit.normal.x > .8) && (dir == LEFT)) {
177 if ((hit.normal.x < -.8) && (dir == RIGHT)) {
186 Totem::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
188 // if we are being carried around, pass event to bottom of stack and ignore it
190 carried_by->collision_badguy(badguy, hit);
194 // if we hit a Totem that is not from our stack: have our base jump on its top
195 Totem* totem = dynamic_cast<Totem*>(&badguy);
197 Totem* thisBase = this; while (thisBase->carried_by) thisBase=thisBase->carried_by;
198 Totem* srcBase = totem; while (srcBase->carried_by) srcBase=srcBase->carried_by;
199 Totem* thisTop = this; while (thisTop->carrying) thisTop=thisTop->carrying;
200 if (srcBase != thisBase) {
201 srcBase->jump_on(thisTop);
205 // If we are hit from the direction we are facing: turn around
206 if ((hit.normal.x > .8) && (dir == LEFT)) {
210 if ((hit.normal.x < -.8) && (dir == RIGHT)) {
221 if (carrying) carrying->jump_off();
222 if (carried_by) jump_off();
228 Totem::jump_on(Totem* target)
230 if (target->carrying) {
231 log_warning << "target is already carrying someone" << std::endl;
235 target->carrying = this;
237 this->carried_by = target;
239 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
241 this->synchronize_with(target);
247 log_warning << "not carried by anyone" << std::endl;
251 carried_by->carrying = 0;
253 this->carried_by = 0;
256 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
259 physic.set_velocity_y(JUMP_OFF_SPEED_Y);
263 Totem::synchronize_with(Totem* base)
266 if (dir != base->dir) {
268 sprite->set_action(dir == LEFT ? "stacked-left" : "stacked-right");
271 Vector pos = base->get_pos();
272 pos.y -= sprite->get_current_hitbox_height();
275 physic.set_velocity_x(base->physic.get_velocity_x());
276 physic.set_velocity_y(base->physic.get_velocity_y());
280 IMPLEMENT_FACTORY(Totem, "totem")