2 // Copyright (C) 2006 Matthias Braun <matze@braunis.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/stumpy.hpp"
19 #include "audio/sound_manager.hpp"
20 #include "math/random_generator.hpp"
21 #include "object/player.hpp"
22 #include "object/sprite_particle.hpp"
23 #include "supertux/object_factory.hpp"
24 #include "supertux/sector.hpp"
28 static const float WALKSPEED = 120;
29 static const float INVINCIBLE_TIME = 1;
31 Stumpy::Stumpy(const Reader& reader) :
32 WalkingBadguy(reader, "images/creatures/mr_tree/stumpy.sprite","left","right"),
33 mystate(STATE_NORMAL),
36 walk_speed = WALKSPEED;
38 sound_manager->preload("sounds/mr_treehit.ogg");
41 Stumpy::Stumpy(const Vector& pos, Direction d) :
42 WalkingBadguy(pos, d, "images/creatures/mr_tree/stumpy.sprite","left","right"),
43 mystate(STATE_INVINCIBLE),
46 walk_speed = WALKSPEED;
48 sound_manager->preload("sounds/mr_treehit.ogg");
49 invincible_timer.start(INVINCIBLE_TIME);
56 case STATE_INVINCIBLE:
57 sprite->set_action(dir == LEFT ? "dizzy-left" : "dizzy-right");
58 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
59 physic.set_velocity_x(0);
62 WalkingBadguy::initialize();
68 Stumpy::active_update(float elapsed_time)
71 case STATE_INVINCIBLE:
72 if (invincible_timer.check()) {
73 mystate = STATE_NORMAL;
74 WalkingBadguy::initialize();
76 BadGuy::active_update(elapsed_time);
79 WalkingBadguy::active_update(elapsed_time);
85 Stumpy::collision_squished(GameObject& object)
88 // if we're still invincible, we ignore the hit
89 if (mystate == STATE_INVINCIBLE) {
90 sound_manager->play("sounds/mr_treehit.ogg", get_pos());
91 Player* player = dynamic_cast<Player*>(&object);
92 if (player) player->bounce(*this);
96 // if we can die, we do
97 if (mystate == STATE_NORMAL) {
98 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
99 set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
100 kill_squished(object);
101 // spawn some particles
102 // TODO: provide convenience function in MovingSprite or MovingObject?
103 for (int i = 0; i < 25; i++) {
104 Vector ppos = bbox.get_middle();
105 float angle = systemRandom.randf(-M_PI_2, M_PI_2);
106 float velocity = systemRandom.randf(45, 90);
107 float vx = sin(angle)*velocity;
108 float vy = -cos(angle)*velocity;
109 Vector pspeed = Vector(vx, vy);
110 Vector paccel = Vector(0, 100);
111 Sector::current()->add_object(new SpriteParticle("images/objects/particles/bark.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
123 Stumpy::collision_solid(const CollisionHit& hit)
125 update_on_ground_flag(hit);
128 case STATE_INVINCIBLE:
129 if(hit.top || hit.bottom) {
130 physic.set_velocity_y(0);
132 if(hit.left || hit.right) {
133 physic.set_velocity_x(0);
137 WalkingBadguy::collision_solid(hit);
143 Stumpy::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
146 case STATE_INVINCIBLE:
147 if(hit.top || hit.bottom) {
148 physic.set_velocity_y(0);
150 if(hit.left || hit.right) {
151 physic.set_velocity_x(0);
156 return WalkingBadguy::collision_badguy(badguy, hit);
162 IMPLEMENT_FACTORY(Stumpy, "stumpy");