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)
33 reader.get("x", start_position.x);
34 reader.get("y", start_position.y);
35 sprite = sprite_manager->create("images/creatures/angrystone/angrystone.sprite");
36 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
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::activate()
54 physic.set_velocity_x(0);
55 physic.set_velocity_y(0);
56 physic.enable_gravity(true);
57 sprite->set_action("idle");
61 AngryStone::collision_solid(GameObject& , const CollisionHit& hit)
63 if ((state == ATTACKING) && (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;
77 AngryStone::kill_fall()
79 //prevents AngryStone from getting killed by other enemies or the player
83 AngryStone::collision_badguy(BadGuy& badguy, const CollisionHit& )
85 if (state == ATTACKING) {
94 AngryStone::active_update(float elapsed_time) {
95 BadGuy::active_update(elapsed_time);
98 MovingObject* player = this->get_nearest_player();
99 MovingObject* badguy = this;
100 const Vector playerPos = player->get_pos();
101 const Vector badguyPos = badguy->get_pos();
102 float dx = (playerPos.x - badguyPos.x);
103 float dy = (playerPos.y - badguyPos.y);
105 float playerHeight = player->get_bbox().p2.y - player->get_bbox().p1.y;
106 float badguyHeight = badguy->get_bbox().p2.y - badguy->get_bbox().p1.y;
108 float playerWidth = player->get_bbox().p2.x - player->get_bbox().p1.x;
109 float badguyWidth = badguy->get_bbox().p2.x - badguy->get_bbox().p1.x;
111 if ((dx > -playerWidth) && (dx < badguyWidth)) {
113 attackDirection.x = 0;
114 attackDirection.y = -1;
116 attackDirection.x = 0;
117 attackDirection.y = 1;
119 if ((attackDirection.x != oldWallDirection.x) || (attackDirection.y != oldWallDirection.y)) {
120 sprite->set_action("charging");
121 timer.start(CHARGE_TIME);
125 if ((dy > -playerHeight) && (dy < badguyHeight)) {
127 attackDirection.x = 1;
128 attackDirection.y = 0;
130 attackDirection.x = -1;
131 attackDirection.y = 0;
133 if ((attackDirection.x != oldWallDirection.x) || (attackDirection.y != oldWallDirection.y)) {
134 sprite->set_action("charging");
135 timer.start(CHARGE_TIME);
142 if (state == CHARGING) {
144 sprite->set_action("attacking");
145 timer.start(ATTACK_TIME);
147 physic.enable_gravity(false);
148 physic.set_velocity_x(SPEED * attackDirection.x);
149 physic.set_velocity_y(SPEED * attackDirection.y);
150 oldWallDirection.x = 0;
151 oldWallDirection.y = 0;
155 if (state == ATTACKING) {
157 timer.start(RECOVER_TIME);
159 sprite->set_action("idle");
160 physic.enable_gravity(true);
161 physic.set_velocity_x(0);
162 physic.set_velocity_y(0);
166 if (state == RECOVERING) {
169 sprite->set_action("idle");
170 physic.enable_gravity(true);
171 physic.set_velocity_x(0);
172 physic.set_velocity_y(0);
178 IMPLEMENT_FACTORY(AngryStone, "angrystone")