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)
34 bbox.set_size(48, 49);
36 reader.get("x", start_position.x);
37 reader.get("y", start_position.y);
38 sprite = sprite_manager->create("images/creatures/totem/totem.sprite");
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 this->bbox.set_size(48, 45);
134 kill_squished(player);
139 Totem::collision_solid(GameObject& object, const CollisionHit& hit)
141 // if we are being carried around, pass event to bottom of stack and ignore it
143 carried_by->collision_solid(object, hit);
147 // If we hit something from above or below: stop moving in this direction
148 if (hit.normal.y != 0) {
149 physic.set_velocity_y(0);
152 // If we are hit from the direction we are facing: turn around
153 if ((hit.normal.x > .8) && (dir == LEFT)) {
157 if ((hit.normal.x < -.8) && (dir == RIGHT)) {
166 Totem::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
168 // if we are being carried around, pass event to bottom of stack and ignore it
170 carried_by->collision_badguy(badguy, hit);
174 // if we hit a Totem that is not from our stack: have our base jump on its top
175 Totem* totem = dynamic_cast<Totem*>(&badguy);
177 Totem* thisBase = this; while (thisBase->carried_by) thisBase=thisBase->carried_by;
178 Totem* srcBase = totem; while (srcBase->carried_by) srcBase=srcBase->carried_by;
179 Totem* thisTop = this; while (thisTop->carrying) thisTop=thisTop->carrying;
180 if (srcBase != thisBase) {
181 srcBase->jump_on(thisTop);
185 // If we are hit from the direction we are facing: turn around
186 if ((hit.normal.x > .8) && (dir == LEFT)) {
190 if ((hit.normal.x < -.8) && (dir == RIGHT)) {
201 if (carrying) carrying->jump_off();
202 if (carried_by) jump_off();
208 Totem::jump_on(Totem* target)
210 if (target->carrying) {
211 log_warning << "target is already carrying someone" << std::endl;
215 target->carrying = this;
217 this->carried_by = target;
218 this->bbox.set_size(48, 45);
221 this->synchronize_with(target);
227 log_warning << "not carried by anyone" << std::endl;
231 carried_by->carrying = 0;
233 this->carried_by = 0;
234 this->bbox.set_size(48, 49);
238 physic.set_velocity_y(JUMP_OFF_SPEED_Y);
242 Totem::synchronize_with(Totem* base)
245 if (dir != base->dir) {
247 sprite->set_action(dir == LEFT ? "stacked-left" : "stacked-right");
250 Vector pos = base->get_pos();
254 physic.set_velocity_x(base->physic.get_velocity_x());
255 physic.set_velocity_y(base->physic.get_velocity_y());
259 IMPLEMENT_FACTORY(Totem, "totem")