1 // IceCrusher - A block to stand on, which can drop down to crush the player
2 // Copyright (C) 2008 Christoph Sommer <christoph.sommer@2008.expires.deltadevelopment.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "object/icecrusher.hpp"
19 #include "badguy/badguy.hpp"
20 #include "sprite/sprite.hpp"
21 #include "object/player.hpp"
22 #include "supertux/object_factory.hpp"
23 #include "supertux/sector.hpp"
26 const float DROP_SPEED = 500;
27 const float RECOVER_SPEED = 200;
30 IceCrusher::IceCrusher(const Reader& reader) :
31 MovingSprite(reader, "images/creatures/icecrusher/icecrusher.sprite", LAYER_OBJECTS, COLGROUP_STATIC),
36 start_position = get_bbox().p1;
37 set_state(state, true);
41 IceCrusher::IceCrusher(const IceCrusher& other)
42 : MovingSprite(other),
43 state(other.state), speed(other.speed)
45 start_position = get_bbox().p1;
46 set_state(state, true);
50 IceCrusher::set_state(IceCrusherState state, bool force)
52 if ((this->state == state) && (!force)) return;
55 set_group(COLGROUP_STATIC);
57 sprite->set_action("idle");
60 set_group(COLGROUP_MOVING_STATIC);
61 speed=Vector(0, DROP_SPEED);
62 sprite->set_action("idle");
65 set_group(COLGROUP_MOVING_STATIC);
66 speed=Vector(0, -RECOVER_SPEED);
67 sprite->set_action("idle");
70 log_debug << "IceCrusher in invalid state" << std::endl;
77 IceCrusher::collision(GameObject& other, const CollisionHit& hit)
79 Player* player = dynamic_cast<Player*>(&other);
80 if (player && hit.bottom) {
81 if(player->is_invincible()) {
82 if (state == CRUSHING) set_state(RECOVERING);
86 if (state == CRUSHING) set_state(RECOVERING);
89 BadGuy* badguy = dynamic_cast<BadGuy*>(&other);
97 IceCrusher::collision_solid(const CollisionHit& )
103 set_state(RECOVERING);
108 log_debug << "IceCrusher in invalid state" << std::endl;
114 IceCrusher::update(float elapsed_time)
118 if (found_victim()) set_state(CRUSHING);
123 if (get_bbox().p1.y <= start_position.y+1) {
124 set_pos(start_position);
129 log_debug << "IceCrusher in invalid state" << std::endl;
132 movement = speed * elapsed_time;
136 IceCrusher::get_nearest_player()
138 // FIXME: does not really return nearest player
140 std::vector<Player*> players = Sector::current()->get_players();
141 for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
142 Player* player = *playerIter;
143 if (player->is_dying() || player->is_dead()) continue;
151 IceCrusher::found_victim()
153 Player* player = this->get_nearest_player();
154 if (!player) return false;
156 const Rect& pr = player->get_bbox();
157 const Rect& br = get_bbox();
158 if ((pr.p2.x > br.p1.x) && (pr.p1.x < br.p2.x) && (pr.p1.y >= br.p2.y)) {
164 IMPLEMENT_FACTORY(IceCrusher, "icecrusher");