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)
32 stay_on_platform = false;
35 bbox.set_size(48, 49);
37 reader.get("x", start_position.x);
38 reader.get("y", start_position.y);
39 reader.get("stay-on-platform", stay_on_platform);
40 sprite = sprite_manager->create("images/creatures/totem/totem.sprite");
45 if (carrying) carrying->jump_off();
46 if (carried_by) jump_off();
50 Totem::write(lisp::Writer& writer)
52 writer.start_list("totem");
54 writer.write_float("x", start_position.x);
55 writer.write_float("y", start_position.y);
56 writer.write_bool("stay-on-platform", stay_on_platform);
58 writer.end_list("totem");
65 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
66 sprite->set_action(dir == LEFT ? "walking-left" : "walking-right");
69 synchronize_with(carried_by);
70 sprite->set_action(dir == LEFT ? "stacked-left" : "stacked-right");
76 Totem::active_update(float elapsed_time)
78 BadGuy::active_update(elapsed_time);
81 if (stay_on_platform && may_fall_off_platform())
83 dir = (dir == LEFT ? RIGHT : LEFT);
87 Sector* s = Sector::current();
89 // jump a bit if we find a suitable totem
90 for (std::vector<MovingObject*>::iterator i = s->moving_objects.begin(); i != s->moving_objects.end(); i++) {
91 Totem* t = dynamic_cast<Totem*>(*i);
94 // skip if we are not approaching each other
95 if (!((this->dir == LEFT) && (t->dir == RIGHT))) continue;
97 Vector p1 = this->get_pos();
98 Vector p2 = t->get_pos();
100 // skip if not on same height
101 float dy = (p1.y - p2.y);
102 if (fabsf(dy - 0) > 2) continue;
104 // skip if too far away
105 float dx = (p1.x - p2.x);
106 if (fabsf(dx - 128) > 2) continue;
108 physic.set_velocity_y(JUMP_ON_SPEED_Y);
117 this->synchronize_with(carried_by);
121 carrying->synchronize_with(this);
127 Totem::collision_squished(Player& player)
129 if (carrying) carrying->jump_off();
131 player.bounce(*this);
135 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
136 this->bbox.set_size(48, 45);
137 kill_squished(player);
142 Totem::collision_solid(GameObject& object, const CollisionHit& hit)
144 // if we are being carried around, pass event to bottom of stack and ignore it
146 carried_by->collision_solid(object, hit);
150 // If we hit something from above or below: stop moving in this direction
151 if (hit.normal.y != 0) {
152 physic.set_velocity_y(0);
155 // If we are hit from the direction we are facing: turn around
156 if ((hit.normal.x > .8) && (dir == LEFT)) {
160 if ((hit.normal.x < -.8) && (dir == RIGHT)) {
169 Totem::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
171 // if we are being carried around, pass event to bottom of stack and ignore it
173 carried_by->collision_badguy(badguy, hit);
177 // if we hit a Totem that is not from our stack: have our base jump on its top
178 Totem* totem = dynamic_cast<Totem*>(&badguy);
180 Totem* thisBase = this; while (thisBase->carried_by) thisBase=thisBase->carried_by;
181 Totem* srcBase = totem; while (srcBase->carried_by) srcBase=srcBase->carried_by;
182 Totem* thisTop = this; while (thisTop->carrying) thisTop=thisTop->carrying;
183 if (srcBase != thisBase) {
184 srcBase->jump_on(thisTop);
188 // If we are hit from the direction we are facing: turn around
189 if ((hit.normal.x > .8) && (dir == LEFT)) {
193 if ((hit.normal.x < -.8) && (dir == RIGHT)) {
204 if (carrying) carrying->jump_off();
205 if (carried_by) jump_off();
211 Totem::jump_on(Totem* target)
213 if (target->carrying) {
214 log_warning << "target is already carrying someone" << std::endl;
218 target->carrying = this;
220 this->carried_by = target;
221 this->bbox.set_size(48, 45);
224 this->synchronize_with(target);
230 log_warning << "not carried by anyone" << std::endl;
234 carried_by->carrying = 0;
236 this->carried_by = 0;
237 this->bbox.set_size(48, 49);
241 physic.set_velocity_y(JUMP_OFF_SPEED_Y);
245 Totem::synchronize_with(Totem* base)
248 if (dir != base->dir) {
250 sprite->set_action(dir == LEFT ? "stacked-left" : "stacked-right");
253 Vector pos = base->get_pos();
257 physic.set_velocity_x(base->physic.get_velocity_x());
258 physic.set_velocity_y(base->physic.get_velocity_y());
262 IMPLEMENT_FACTORY(Totem, "totem")