00001 #ifndef H_BOY
00002 #define H_BOY
00003
00004 #include "common.h"
00005
00006 class PickupEffect;
00007
00008 typedef std::pair<PickupEffect*,Bitmap> epair;
00009 typedef std::list<epair> effect_list;
00010
00011 class JungleBoy : public AnimatedSprite
00012 {
00013 bool m_LastRight;
00014 int m_OnGround;
00015 bool m_Dying;
00016 SoundClip m_EatSound;
00017 SoundClip m_AaahhSound;
00018 SoundClip m_DeathSound;
00019 int m_DeathDuration;
00020 effect_list m_Effects;
00021 int m_EffectPress;
00022 int m_Shield;
00023 bool m_ShieldBlink;
00024 Joystick* m_Joystick;
00025
00026 void activate_effect();
00027 public:
00028 JungleBoy()
00029 : AnimatedSprite("rsc/boy.xml"),
00030 m_EatSound("rsc/eat2.wav"),
00031 m_AaahhSound("rsc/aaahh.wav"),
00032 m_DeathSound("rsc/death.wav"),
00033 m_LastRight(true),
00034 m_OnGround(0),
00035 m_Dying(false),
00036 m_DeathDuration(0),
00037 m_EffectPress(0),
00038 m_Shield(0),
00039 m_ShieldBlink(true),
00040 m_Joystick(0)
00041 {
00042 set("Boy","YES");
00043 set_acceleration(dVec2(0,200));
00044 if (EventManager::instance()->get_joystick_count()>0)
00045 {
00046 m_Joystick=&EventManager::instance()->get_joystick(0);
00047 }
00048 }
00049
00050 ~JungleBoy();
00051
00052 void reset()
00053 {
00054 set_velocity(dVec2(0,0));
00055 add_animation_object(this);
00056 set_active_sequence(1);
00057 m_DeathDuration=0;
00058 m_Dying=false;
00059 m_LastRight=true;
00060 m_OnGround=0;
00061 m_EffectPress=0;
00062 }
00063
00064 int get_death_duration() const { return m_DeathDuration; }
00065
00066 void take_pickup(PickupEffect* effect, Bitmap image)
00067 {
00068 m_EatSound.play();
00069 m_Effects.push_back(std::make_pair(effect,image));
00070 }
00071
00072 void eat_food()
00073 {
00074 g_score+=10;
00075 m_EatSound.play();
00076 if (--g_food_left==0) g_next_screen=true;
00077 }
00078
00079 void add_shield(int n)
00080 {
00081 m_Shield+=n;
00082 }
00083
00084 void die()
00085 {
00086 if (!m_Dying)
00087 {
00088 m_DeathSound.play();
00089 set_active_sequence(4);
00090 m_Dying=true;
00091 m_DeathDuration=0;
00092 }
00093 }
00094
00095 bool is_vulnerable() const { return m_Shield==0; }
00096
00097 virtual void handle_collision(RigidBody2D* o)
00098 {
00099 if (o->get("Floor")=="YES")
00100 {
00101 iRect2 r1=get_rect();
00102 iRect2 r2=o->get_rect();
00103 iRect2 res=r1.overlap(r2);
00104 if (res.get_height()<10 && get_velocity().y>0 && res.br.y==r1.br.y)
00105 {
00106 offset_position(dVec2(0,-res.get_height()));
00107 set_velocity(dVec2(get_velocity().x,0));
00108 m_OnGround=15;
00109 }
00110 }
00111 else
00112 {
00113 o->handle_collision(this);
00114 }
00115 }
00116
00117 virtual void render(GameView& gv)
00118 {
00119 if (m_Shield==0 || !m_ShieldBlink)
00120 AnimatedSprite::render(gv);
00121 if (!m_Effects.empty())
00122 {
00123 Font& f=game_font();
00124 int x=300;
00125 effect_list::iterator b=m_Effects.begin(),e=m_Effects.end();
00126 for(int i=1;b!=e;++b,++i)
00127 {
00128 std::ostringstream os; os << i;
00129 Bitmap bmp=b->second;
00130 x+=f.draw(x,5,os.str(),MapRGB(255,0,255)).x;
00131 bmp.draw(x,5);
00132 x+=bmp.get_width()+5;
00133 }
00134 }
00135 }
00136
00137 bool is_joy(double x, double y)
00138 {
00139 if (!m_Joystick) return false;
00140 double X=m_Joystick->axes[0];
00141 double Y=m_Joystick->axes[1];
00142 return ((x!=0 && udiff(x,X)<0.5) || (y!=0 && udiff(y,Y)<0.5));
00143 }
00144
00145 virtual bool advance(int dt)
00146 {
00147 --m_OnGround;
00148 AnimatedSprite::advance(dt);
00149 if (m_Joystick) m_Joystick->update();
00150 if (m_Shield>0)
00151 {
00152 m_Shield=Max(0,m_Shield-dt);
00153 m_ShieldBlink=!m_ShieldBlink;
00154 }
00155 if (get_position().y>450 && !m_Dying)
00156 {
00157 m_AaahhSound.play();
00158 m_Dying=true;
00159 }
00160 if (m_Dying)
00161 {
00162 m_DeathDuration+=dt;
00163 if (get_current_frame()==11) set_position(iVec2(100,500));
00164 else return true;
00165 }
00166 if (m_EffectPress>0)
00167 {
00168 if (!is_pressed(SDLKey(SDLK_0+m_EffectPress)))
00169 activate_effect();
00170 }
00171 else
00172 {
00173 for(int i=0;i<int(m_Effects.size());++i)
00174 if (is_pressed(SDLKey(SDLK_1+i))) m_EffectPress=i+1;
00175 }
00176 int act_seq=get_active_sequence();
00177 if ((is_pressed(SDLK_UP) || is_pressed(SDLK_KP8) || is_joy(0,-1)) && m_OnGround>0)
00178 {
00179 set_velocity(dVec2(get_velocity().x,-200));
00180 set_acceleration(dVec2(0,get_acceleration().y));
00181 m_OnGround=0;
00182 }
00183 if ((is_pressed(SDLK_DOWN) || is_pressed(SDLK_KP2) || is_joy(0,1)) && m_OnGround>0 && (act_seq==1 || act_seq==3 || act_seq>4))
00184 {
00185 if (act_seq<4)
00186 set_active_sequence(act_seq==1?5:6);
00187 }
00188 else
00189 if ((is_pressed(SDLK_RIGHT) || is_pressed(SDLK_KP6) || is_joy(1,0)) && m_OnGround>0)
00190 {
00191 m_LastRight=true;
00192 set_active_sequence(0);
00193 dVec2 v=get_velocity();
00194 if (v.x<200)
00195 set_acceleration(dVec2(300,get_acceleration().y));
00196 else
00197 set_acceleration(dVec2(0,get_acceleration().y));
00198 }
00199 else
00200 if ((is_pressed(SDLK_LEFT) || is_pressed(SDLK_KP4) || is_joy(-1,0)) && m_OnGround>0)
00201 {
00202 m_LastRight=false;
00203 set_active_sequence(2);
00204 dVec2 v=get_velocity();
00205 if (v.x>-200)
00206 set_acceleration(dVec2(-300,get_acceleration().y));
00207 else
00208 set_acceleration(dVec2(0,get_acceleration().y));
00209 }
00210 else
00211 {
00212 dVec2 v=get_velocity();
00213 if (fabs(v.x)>1)
00214 {
00215 if (m_OnGround>0)
00216 {
00217 double ax=100;
00218 dVec2 a=get_acceleration();
00219 a.x=(v.x>0 ? -ax : ax);
00220 set_acceleration(a);
00221 }
00222 else
00223 {
00224 dVec2 a=get_acceleration();
00225 a.x=(v.x>0 ? -20 : 20);
00226 set_acceleration(a);
00227 }
00228 }
00229 else
00230 {
00231 set_velocity(dVec2(0,v.y));
00232 set_acceleration(dVec2(0,get_acceleration().y));
00233 set_active_sequence(m_LastRight?1:3);
00234 }
00235 }
00236 return true;
00237 }
00238 };
00239
00240 #endif H_BOY
00241
00242