3 // IceCrusher - A block to stand on, which can drop down to crush the player
4 // Copyright (C) 2008 Christoph Sommer <christoph.sommer@2008.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
23 #include "icecrusher.hpp"
27 #include "video/drawing_context.hpp"
28 #include "resources.hpp"
29 #include "badguy/badguy.hpp"
30 #include "sprite/sprite.hpp"
31 #include "lisp/lisp.hpp"
32 #include "object_factory.hpp"
36 const float DROP_SPEED = 500;
37 const float RECOVER_SPEED = 200;
40 IceCrusher::IceCrusher(const lisp::Lisp& reader)
41 : MovingSprite(reader, "images/creatures/icecrusher/icecrusher.sprite", LAYER_OBJECTS, COLGROUP_STATIC),
42 state(IDLE), speed(Vector(0,0))
44 start_position = get_bbox().p1;
45 set_state(state, true);
48 IceCrusher::IceCrusher(const IceCrusher& other)
49 : MovingSprite(other),
50 state(other.state), speed(other.speed)
52 start_position = get_bbox().p1;
53 set_state(state, true);
57 IceCrusher::set_state(IceCrusherState state, bool force)
59 if ((this->state == state) && (!force)) return;
62 set_group(COLGROUP_STATIC);
64 sprite->set_action("idle");
67 set_group(COLGROUP_MOVING_STATIC);
68 speed=Vector(0, DROP_SPEED);
69 sprite->set_action("idle");
72 set_group(COLGROUP_MOVING_STATIC);
73 speed=Vector(0, -RECOVER_SPEED);
74 sprite->set_action("idle");
77 log_debug << "IceCrusher in invalid state" << std::endl;
84 IceCrusher::collision(GameObject& other, const CollisionHit& hit)
86 Player* player = dynamic_cast<Player*>(&other);
87 if (player && hit.bottom) {
88 if(player->is_invincible()) {
89 if (state == CRUSHING) set_state(RECOVERING);
93 if (state == CRUSHING) set_state(RECOVERING);
96 BadGuy* badguy = dynamic_cast<BadGuy*>(&other);
104 IceCrusher::collision_solid(const CollisionHit& )
110 set_state(RECOVERING);
115 log_debug << "IceCrusher in invalid state" << std::endl;
121 IceCrusher::update(float elapsed_time)
125 if (found_victim()) set_state(CRUSHING);
130 if (get_bbox().p1.y <= start_position.y+1) {
131 set_pos(start_position);
136 log_debug << "IceCrusher in invalid state" << std::endl;
139 movement = speed * elapsed_time;
143 IceCrusher::get_nearest_player()
145 // FIXME: does not really return nearest player
147 std::vector<Player*> players = Sector::current()->get_players();
148 for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
149 Player* player = *playerIter;
150 if (player->is_dying() || player->is_dead()) continue;
158 IceCrusher::found_victim()
160 Player* player = this->get_nearest_player();
161 if (!player) return false;
163 const Rect& pr = player->get_bbox();
164 const Rect& br = get_bbox();
165 if ((pr.p2.x > br.p1.x) && (pr.p1.x < br.p2.x) && (pr.p1.y >= br.p2.y)) {
171 IMPLEMENT_FACTORY(IceCrusher, "icecrusher");