00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "badguy/stumpy.hpp"
00018
00019 #include "audio/sound_manager.hpp"
00020 #include "math/random_generator.hpp"
00021 #include "object/player.hpp"
00022 #include "object/sprite_particle.hpp"
00023 #include "supertux/object_factory.hpp"
00024 #include "supertux/sector.hpp"
00025
00026 #include <math.h>
00027
00028 static const float STUMPY_SPEED = 120;
00029 static const float INVINCIBLE_TIME = 1;
00030
00031 Stumpy::Stumpy(const Reader& reader) :
00032 WalkingBadguy(reader, "images/creatures/mr_tree/stumpy.sprite","left","right"),
00033 mystate(STATE_NORMAL),
00034 invincible_timer()
00035 {
00036 walk_speed = STUMPY_SPEED;
00037 max_drop_height = 16;
00038 sound_manager->preload("sounds/mr_treehit.ogg");
00039 }
00040
00041 Stumpy::Stumpy(const Vector& pos, Direction d) :
00042 WalkingBadguy(pos, d, "images/creatures/mr_tree/stumpy.sprite","left","right"),
00043 mystate(STATE_INVINCIBLE),
00044 invincible_timer()
00045 {
00046 walk_speed = STUMPY_SPEED;
00047 max_drop_height = 16;
00048 sound_manager->preload("sounds/mr_treehit.ogg");
00049 invincible_timer.start(INVINCIBLE_TIME);
00050 }
00051
00052 void
00053 Stumpy::initialize()
00054 {
00055 switch (mystate) {
00056 case STATE_INVINCIBLE:
00057 sprite->set_action(dir == LEFT ? "dizzy-left" : "dizzy-right");
00058 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00059 physic.set_velocity_x(0);
00060 break;
00061 case STATE_NORMAL:
00062 WalkingBadguy::initialize();
00063 break;
00064 }
00065 }
00066
00067 void
00068 Stumpy::active_update(float elapsed_time)
00069 {
00070 switch (mystate) {
00071 case STATE_INVINCIBLE:
00072 if (invincible_timer.check()) {
00073 mystate = STATE_NORMAL;
00074 WalkingBadguy::initialize();
00075 }
00076 BadGuy::active_update(elapsed_time);
00077 break;
00078 case STATE_NORMAL:
00079 WalkingBadguy::active_update(elapsed_time);
00080 break;
00081 }
00082 }
00083
00084 bool
00085 Stumpy::collision_squished(GameObject& object)
00086 {
00087
00088
00089 if (mystate == STATE_INVINCIBLE) {
00090 sound_manager->play("sounds/mr_treehit.ogg", get_pos());
00091 Player* player = dynamic_cast<Player*>(&object);
00092 if (player) player->bounce(*this);
00093 return true;
00094 }
00095
00096
00097 if (mystate == STATE_NORMAL) {
00098 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
00099 set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00100 kill_squished(object);
00101
00102
00103 for (int i = 0; i < 25; i++) {
00104 Vector ppos = bbox.get_middle();
00105 float angle = graphicsRandom.randf(-M_PI_2, M_PI_2);
00106 float velocity = graphicsRandom.randf(45, 90);
00107 float vx = sin(angle)*velocity;
00108 float vy = -cos(angle)*velocity;
00109 Vector pspeed = Vector(vx, vy);
00110 Vector paccel = Vector(0, 100);
00111 Sector::current()->add_object(new SpriteParticle("images/objects/particles/bark.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
00112 }
00113
00114 return true;
00115
00116 }
00117
00118
00119 return true;
00120 }
00121
00122 void
00123 Stumpy::collision_solid(const CollisionHit& hit)
00124 {
00125 update_on_ground_flag(hit);
00126
00127 switch (mystate) {
00128 case STATE_INVINCIBLE:
00129 if(hit.top || hit.bottom) {
00130 physic.set_velocity_y(0);
00131 }
00132 if(hit.left || hit.right) {
00133 physic.set_velocity_x(0);
00134 }
00135 break;
00136 case STATE_NORMAL:
00137 WalkingBadguy::collision_solid(hit);
00138 break;
00139 }
00140 }
00141
00142 HitResponse
00143 Stumpy::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
00144 {
00145 switch (mystate) {
00146 case STATE_INVINCIBLE:
00147 if(hit.top || hit.bottom) {
00148 physic.set_velocity_y(0);
00149 }
00150 if(hit.left || hit.right) {
00151 physic.set_velocity_x(0);
00152 }
00153 return CONTINUE;
00154 break;
00155 case STATE_NORMAL:
00156 return WalkingBadguy::collision_badguy(badguy, hit);
00157 break;
00158 }
00159 return CONTINUE;
00160 }
00161
00162