3 // AngryStone - A spiked block that charges towards the player
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
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)
34 physic.set_velocity_x(0);
35 physic.set_velocity_y(0);
36 physic.enable_gravity(true);
37 sprite->set_action("idle");
41 AngryStone::write(lisp::Writer& writer)
43 writer.start_list("angrystone");
45 writer.write_float("x", start_position.x);
46 writer.write_float("y", start_position.y);
48 writer.end_list("angrystone");
52 AngryStone::collision_solid(const CollisionHit& hit)
57 if ((state == ATTACKING) &&
58 (hit.normal.x == -attackDirection.x) && (hit.normal.y == attackDirection.y)) {
60 sprite->set_action("idle");
61 physic.set_velocity_x(0);
62 physic.set_velocity_y(0);
63 physic.enable_gravity(true);
64 oldWallDirection.x = attackDirection.x;
65 oldWallDirection.y = attackDirection.y;
71 AngryStone::kill_fall()
73 //prevents AngryStone from getting killed by other enemies or the player
77 AngryStone::collision_badguy(BadGuy& badguy, const CollisionHit& )
79 if (state == ATTACKING) {
88 AngryStone::active_update(float elapsed_time) {
89 BadGuy::active_update(elapsed_time);
92 MovingObject* player = this->get_nearest_player();
93 MovingObject* badguy = this;
94 const Vector playerPos = player->get_pos();
95 const Vector badguyPos = badguy->get_pos();
96 float dx = (playerPos.x - badguyPos.x);
97 float dy = (playerPos.y - badguyPos.y);
99 float playerHeight = player->get_bbox().p2.y - player->get_bbox().p1.y;
100 float badguyHeight = badguy->get_bbox().p2.y - badguy->get_bbox().p1.y;
102 float playerWidth = player->get_bbox().p2.x - player->get_bbox().p1.x;
103 float badguyWidth = badguy->get_bbox().p2.x - badguy->get_bbox().p1.x;
105 if ((dx > -playerWidth) && (dx < badguyWidth)) {
107 attackDirection.x = 0;
108 attackDirection.y = 1;
110 attackDirection.x = 0;
111 attackDirection.y = -1;
113 if ((attackDirection.x != oldWallDirection.x) || (attackDirection.y != oldWallDirection.y)) {
114 sprite->set_action("charging");
115 timer.start(CHARGE_TIME);
119 if ((dy > -playerHeight) && (dy < badguyHeight)) {
121 attackDirection.x = 1;
122 attackDirection.y = 0;
124 attackDirection.x = -1;
125 attackDirection.y = 0;
127 if ((attackDirection.x != oldWallDirection.x) || (attackDirection.y != oldWallDirection.y)) {
128 sprite->set_action("charging");
129 timer.start(CHARGE_TIME);
136 if (state == CHARGING) {
138 sprite->set_action("attacking");
139 timer.start(ATTACK_TIME);
141 physic.enable_gravity(false);
142 physic.set_velocity_x(SPEED * attackDirection.x);
143 physic.set_velocity_y(SPEED * attackDirection.y);
144 oldWallDirection.x = 0;
145 oldWallDirection.y = 0;
149 if (state == ATTACKING) {
151 timer.start(RECOVER_TIME);
153 sprite->set_action("idle");
154 physic.enable_gravity(true);
155 physic.set_velocity_x(0);
156 physic.set_velocity_y(0);
160 if (state == RECOVERING) {
163 sprite->set_action("idle");
164 physic.enable_gravity(true);
165 physic.set_velocity_x(0);
166 physic.set_velocity_y(0);
172 IMPLEMENT_FACTORY(AngryStone, "angrystone")