3 // AngryStone - A spiked block that charges towards the player
4 // Copyright (C) 2006 Christoph Sommer <supertux@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
23 #include "angrystone.hpp"
25 static const float SPEED = 240;
27 static const float CHARGE_TIME = .5;
28 static const float ATTACK_TIME = 1;
29 static const float RECOVER_TIME = .5;
31 AngryStone::AngryStone(const lisp::Lisp& reader)
32 : BadGuy(reader, "images/creatures/angrystone/angrystone.sprite"), state(IDLE)
37 AngryStone::write(lisp::Writer& writer)
39 writer.start_list("angrystone");
41 writer.write_float("x", start_position.x);
42 writer.write_float("y", start_position.y);
44 writer.end_list("angrystone");
48 AngryStone::activate()
50 physic.set_velocity_x(0);
51 physic.set_velocity_y(0);
52 physic.enable_gravity(true);
53 sprite->set_action("idle");
57 AngryStone::collision_solid(const CollisionHit& hit)
62 if ((state == ATTACKING) &&
63 (hit.normal.x == -attackDirection.x) && (hit.normal.y == attackDirection.y)) {
65 sprite->set_action("idle");
66 physic.set_velocity_x(0);
67 physic.set_velocity_y(0);
68 physic.enable_gravity(true);
69 oldWallDirection.x = attackDirection.x;
70 oldWallDirection.y = attackDirection.y;
76 AngryStone::kill_fall()
78 //prevents AngryStone from getting killed by other enemies or the player
82 AngryStone::collision_badguy(BadGuy& badguy, const CollisionHit& )
84 if (state == ATTACKING) {
93 AngryStone::active_update(float elapsed_time) {
94 BadGuy::active_update(elapsed_time);
97 MovingObject* player = this->get_nearest_player();
98 MovingObject* badguy = this;
99 const Vector playerPos = player->get_pos();
100 const Vector badguyPos = badguy->get_pos();
101 float dx = (playerPos.x - badguyPos.x);
102 float dy = (playerPos.y - badguyPos.y);
104 float playerHeight = player->get_bbox().p2.y - player->get_bbox().p1.y;
105 float badguyHeight = badguy->get_bbox().p2.y - badguy->get_bbox().p1.y;
107 float playerWidth = player->get_bbox().p2.x - player->get_bbox().p1.x;
108 float badguyWidth = badguy->get_bbox().p2.x - badguy->get_bbox().p1.x;
110 if ((dx > -playerWidth) && (dx < badguyWidth)) {
112 attackDirection.x = 0;
113 attackDirection.y = 1;
115 attackDirection.x = 0;
116 attackDirection.y = -1;
118 if ((attackDirection.x != oldWallDirection.x) || (attackDirection.y != oldWallDirection.y)) {
119 sprite->set_action("charging");
120 timer.start(CHARGE_TIME);
124 if ((dy > -playerHeight) && (dy < badguyHeight)) {
126 attackDirection.x = 1;
127 attackDirection.y = 0;
129 attackDirection.x = -1;
130 attackDirection.y = 0;
132 if ((attackDirection.x != oldWallDirection.x) || (attackDirection.y != oldWallDirection.y)) {
133 sprite->set_action("charging");
134 timer.start(CHARGE_TIME);
141 if (state == CHARGING) {
143 sprite->set_action("attacking");
144 timer.start(ATTACK_TIME);
146 physic.enable_gravity(false);
147 physic.set_velocity_x(SPEED * attackDirection.x);
148 physic.set_velocity_y(SPEED * attackDirection.y);
149 oldWallDirection.x = 0;
150 oldWallDirection.y = 0;
154 if (state == ATTACKING) {
156 timer.start(RECOVER_TIME);
158 sprite->set_action("idle");
159 physic.enable_gravity(true);
160 physic.set_velocity_x(0);
161 physic.set_velocity_y(0);
165 if (state == RECOVERING) {
168 sprite->set_action("idle");
169 physic.enable_gravity(true);
170 physic.set_velocity_x(0);
171 physic.set_velocity_y(0);
177 IMPLEMENT_FACTORY(AngryStone, "angrystone")