1 // $Id: stumpy.cpp 3980 2006-07-10 19:55:56Z sommer $
4 // Copyright (C) 2006 Matthias Braun <matze@braunis.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 02111-1307, USA.
23 #include "poisonivy.hpp"
24 #include "random_generator.hpp"
25 #include "object/sprite_particle.hpp"
27 static const float WALKSPEED = 120;
28 static const float INVINCIBLE_TIME = 1;
30 Stumpy::Stumpy(const lisp::Lisp& reader)
31 : WalkingBadguy(reader, "images/creatures/mr_tree/stumpy.sprite","left","right"), mystate(STATE_NORMAL)
33 walk_speed = WALKSPEED;
35 sound_manager->preload("sounds/mr_tree.ogg");
36 sound_manager->preload("sounds/mr_treehit.ogg");
39 Stumpy::Stumpy(const Vector& pos, Direction d)
40 : WalkingBadguy(pos, d, "images/creatures/mr_tree/stumpy.sprite","left","right"), mystate(STATE_INVINCIBLE)
42 walk_speed = WALKSPEED;
44 sound_manager->preload("sounds/mr_treehit.ogg");
45 invincible_timer.start(INVINCIBLE_TIME);
50 Stumpy::write(lisp::Writer& writer)
52 writer.start_list("stumpy");
53 WalkingBadguy::write(writer);
54 writer.end_list("stumpy");
61 case STATE_INVINCIBLE:
62 WalkingBadguy::activate();
63 physic.set_velocity_x(0);
66 WalkingBadguy::activate();
72 Stumpy::active_update(float elapsed_time)
75 case STATE_INVINCIBLE:
76 if (invincible_timer.check()) {
77 mystate = STATE_NORMAL;
78 WalkingBadguy::activate();
80 BadGuy::active_update(elapsed_time);
83 WalkingBadguy::active_update(elapsed_time);
89 Stumpy::collision_squished(Player& player)
92 // if we're still invincible, we ignore the hit
93 if (mystate == STATE_INVINCIBLE) {
94 sound_manager->play("sounds/mr_treehit.ogg", get_pos());
99 // if we can die, we do
100 if (mystate == STATE_NORMAL) {
101 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
102 set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
103 kill_squished(player);
104 // spawn some particles
105 // TODO: provide convenience function in MovingSprite or MovingObject?
106 for (int i = 0; i < 25; i++) {
107 Vector ppos = bbox.get_middle();
108 float angle = systemRandom.randf(-M_PI_2, M_PI_2);
109 float velocity = systemRandom.randf(45, 90);
110 float vx = sin(angle)*velocity;
111 float vy = -cos(angle)*velocity;
112 Vector pspeed = Vector(vx, vy);
113 Vector paccel = Vector(0, 100);
114 Sector::current()->add_object(new SpriteParticle("images/objects/particles/bark.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
126 Stumpy::collision_solid(const CollisionHit& hit)
128 update_on_ground_flag(hit);
131 case STATE_INVINCIBLE:
132 if(hit.top || hit.bottom) {
133 physic.set_velocity_y(0);
135 if(hit.left || hit.right) {
136 physic.set_velocity_x(0);
140 WalkingBadguy::collision_solid(hit);
146 Stumpy::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
149 case STATE_INVINCIBLE:
150 if(hit.top || hit.bottom) {
151 physic.set_velocity_y(0);
153 if(hit.left || hit.right) {
154 physic.set_velocity_x(0);
159 return WalkingBadguy::collision_badguy(badguy, hit);
165 IMPLEMENT_FACTORY(Stumpy, "stumpy")