1 // AngryStone - A spiked block that charges towards the player
2 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.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 "badguy/angrystone.hpp"
19 #include "object/player.hpp"
20 #include "sprite/sprite.hpp"
21 #include "supertux/object_factory.hpp"
23 static const float SPEED = 240;
25 static const float CHARGE_TIME = .5;
26 static const float ATTACK_TIME = 1;
27 static const float RECOVER_TIME = .5;
29 AngryStone::AngryStone(const Reader& reader) :
30 BadGuy(reader, "images/creatures/angrystone/angrystone.sprite"),
36 physic.set_velocity_x(0);
37 physic.set_velocity_y(0);
38 physic.enable_gravity(true);
39 sprite->set_action("idle");
43 AngryStone::collision_solid(const CollisionHit& hit)
48 if ((state == ATTACKING) &&
49 (hit.normal.x == -attackDirection.x) && (hit.normal.y == attackDirection.y)) {
51 sprite->set_action("idle");
52 physic.set_velocity_x(0);
53 physic.set_velocity_y(0);
54 physic.enable_gravity(true);
55 oldWallDirection.x = attackDirection.x;
56 oldWallDirection.y = attackDirection.y;
62 AngryStone::kill_fall()
64 //prevents AngryStone from getting killed by other enemies or the player
68 AngryStone::collision_badguy(BadGuy& badguy, const CollisionHit& )
70 if (state == ATTACKING) {
79 AngryStone::active_update(float elapsed_time) {
80 BadGuy::active_update(elapsed_time);
83 MovingObject* player = this->get_nearest_player();
85 MovingObject* badguy = this;
86 const Vector playerPos = player->get_pos();
87 const Vector badguyPos = badguy->get_pos();
88 float dx = (playerPos.x - badguyPos.x);
89 float dy = (playerPos.y - badguyPos.y);
91 float playerHeight = player->get_bbox().p2.y - player->get_bbox().p1.y;
92 float badguyHeight = badguy->get_bbox().p2.y - badguy->get_bbox().p1.y;
94 float playerWidth = player->get_bbox().p2.x - player->get_bbox().p1.x;
95 float badguyWidth = badguy->get_bbox().p2.x - badguy->get_bbox().p1.x;
97 if ((dx > -playerWidth) && (dx < badguyWidth)) {
99 attackDirection.x = 0;
100 attackDirection.y = 1;
102 attackDirection.x = 0;
103 attackDirection.y = -1;
105 if ((attackDirection.x != oldWallDirection.x) || (attackDirection.y != oldWallDirection.y)) {
106 sprite->set_action("charging");
107 timer.start(CHARGE_TIME);
111 if ((dy > -playerHeight) && (dy < badguyHeight)) {
113 attackDirection.x = 1;
114 attackDirection.y = 0;
116 attackDirection.x = -1;
117 attackDirection.y = 0;
119 if ((attackDirection.x != oldWallDirection.x) || (attackDirection.y != oldWallDirection.y)) {
120 sprite->set_action("charging");
121 timer.start(CHARGE_TIME);
129 if (state == CHARGING) {
131 sprite->set_action("attacking");
132 timer.start(ATTACK_TIME);
134 physic.enable_gravity(false);
135 physic.set_velocity_x(SPEED * attackDirection.x);
136 physic.set_velocity_y(SPEED * attackDirection.y);
137 oldWallDirection.x = 0;
138 oldWallDirection.y = 0;
142 if (state == ATTACKING) {
144 timer.start(RECOVER_TIME);
146 sprite->set_action("idle");
147 physic.enable_gravity(true);
148 physic.set_velocity_x(0);
149 physic.set_velocity_y(0);
153 if (state == RECOVERING) {
156 sprite->set_action("idle");
157 physic.enable_gravity(true);
158 physic.set_velocity_x(0);
159 physic.set_velocity_y(0);
165 IMPLEMENT_FACTORY(AngryStone, "angrystone");