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());
43 if (carrying) carrying->jump_off();
44 if (carried_by) jump_off();
48 Totem::write(lisp::Writer& writer)
50 writer.start_list("totem");
52 writer.write_float("x", start_position.x);
53 writer.write_float("y", start_position.y);
55 writer.end_list("totem");
62 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
63 sprite->set_action(dir == LEFT ? "walking-left" : "walking-right");
66 synchronize_with(carried_by);
67 sprite->set_action(dir == LEFT ? "stacked-left" : "stacked-right");
73 Totem::active_update(float elapsed_time)
75 BadGuy::active_update(elapsed_time);
80 dir = (dir == LEFT ? RIGHT : LEFT);
84 Sector* s = Sector::current();
86 // jump a bit if we find a suitable totem
87 for (std::vector<MovingObject*>::iterator i = s->moving_objects.begin(); i != s->moving_objects.end(); i++) {
88 Totem* t = dynamic_cast<Totem*>(*i);
91 // skip if we are not approaching each other
92 if (!((this->dir == LEFT) && (t->dir == RIGHT))) continue;
94 Vector p1 = this->get_pos();
95 Vector p2 = t->get_pos();
97 // skip if not on same height
98 float dy = (p1.y - p2.y);
99 if (fabsf(dy - 0) > 2) continue;
101 // skip if too far away
102 float dx = (p1.x - p2.x);
103 if (fabsf(dx - 128) > 2) continue;
105 physic.set_velocity_y(JUMP_ON_SPEED_Y);
114 this->synchronize_with(carried_by);
118 carrying->synchronize_with(this);
124 Totem::collision_squished(Player& player)
126 if (carrying) carrying->jump_off();
128 player.bounce(*this);
132 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
133 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
135 kill_squished(player);
140 Totem::collision_solid(GameObject& object, const CollisionHit& hit)
142 // if we are being carried around, pass event to bottom of stack and ignore it
144 carried_by->collision_solid(object, hit);
148 // If we hit something from above or below: stop moving in this direction
149 if (hit.normal.y != 0) {
150 physic.set_velocity_y(0);
153 // If we are hit from the direction we are facing: turn around
154 if ((hit.normal.x > .8) && (dir == LEFT)) {
158 if ((hit.normal.x < -.8) && (dir == RIGHT)) {
167 Totem::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
169 // if we are being carried around, pass event to bottom of stack and ignore it
171 carried_by->collision_badguy(badguy, hit);
175 // if we hit a Totem that is not from our stack: have our base jump on its top
176 Totem* totem = dynamic_cast<Totem*>(&badguy);
178 Totem* thisBase = this; while (thisBase->carried_by) thisBase=thisBase->carried_by;
179 Totem* srcBase = totem; while (srcBase->carried_by) srcBase=srcBase->carried_by;
180 Totem* thisTop = this; while (thisTop->carrying) thisTop=thisTop->carrying;
181 if (srcBase != thisBase) {
182 srcBase->jump_on(thisTop);
186 // If we are hit from the direction we are facing: turn around
187 if ((hit.normal.x > .8) && (dir == LEFT)) {
191 if ((hit.normal.x < -.8) && (dir == RIGHT)) {
202 if (carrying) carrying->jump_off();
203 if (carried_by) jump_off();
209 Totem::jump_on(Totem* target)
211 if (target->carrying) {
212 log_warning << "target is already carrying someone" << std::endl;
216 target->carrying = this;
218 this->carried_by = target;
220 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
222 this->synchronize_with(target);
228 log_warning << "not carried by anyone" << std::endl;
232 carried_by->carrying = 0;
234 this->carried_by = 0;
237 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
240 physic.set_velocity_y(JUMP_OFF_SPEED_Y);
244 Totem::synchronize_with(Totem* base)
247 if (dir != base->dir) {
249 sprite->set_action(dir == LEFT ? "stacked-left" : "stacked-right");
252 Vector pos = base->get_pos();
253 pos.y -= sprite->get_current_hitbox_height();
256 physic.set_velocity_x(base->physic.get_velocity_x());
257 physic.set_velocity_y(base->physic.get_velocity_y());
261 IMPLEMENT_FACTORY(Totem, "totem")