GhostTree Class Reference

#include <ghosttree.hpp>

Inherits BadGuy.

List of all members.

Public Member Functions

 GhostTree (const Reader &lisp)
 ~GhostTree ()
virtual bool is_flammable () const
 Returns whether to call ignite() when a badguy gets hit by a fire bullet.
virtual bool is_freezable () const
virtual void kill_fall ()
 Set the badguy to kill/falling state, which makes him falling of the screen (his sprite is turned upside-down).
void activate ()
 called when the badguy has been activated.
void active_update (float elapsed_time)
 called each frame when the badguy is activated.
void willowisp_died (TreeWillOWisp *willowisp)
virtual void draw (DrawingContext &context)
 Called when the badguy is drawn.
virtual bool collides (GameObject &other, const CollisionHit &hit)
 when 2 objects collided, we will first call the pre_collision_check functions of both objects that can decide on how to react to the collision.
virtual HitResponse collision (GameObject &other, const CollisionHit &hit)
 Called when a collision with another object occurred.
void die ()

Private Types

enum  MyState { STATE_IDLE, STATE_SUCKING, STATE_SWALLOWING, STATE_DYING }

Private Member Functions

bool is_color_deadly (Color color) const
void spawn_lantern ()
 GhostTree (const GhostTree &)
GhostTreeoperator= (const GhostTree &)

Private Attributes

MyState mystate
Timer willowisp_timer
float willo_spawn_y
float willo_radius
float willo_speed
int willo_color
SpritePtr glow_sprite
Timer colorchange_timer
Timer suck_timer
Timer root_timer
int treecolor
Color suck_lantern_color
Lanternsuck_lantern
 Lantern that is currently being sucked in.
std::vector< TreeWillOWisp * > willowisps


Detailed Description

Definition at line 25 of file ghosttree.hpp.


Member Enumeration Documentation

enum GhostTree::MyState [private]

Enumerator:
STATE_IDLE 
STATE_SUCKING 
STATE_SWALLOWING 
STATE_DYING 

Definition at line 46 of file ghosttree.hpp.


Constructor & Destructor Documentation

GhostTree::GhostTree ( const Reader lisp  ) 

Definition at line 39 of file ghosttree.cpp.

References COLGROUP_TOUCHABLE, SpriteManager::create(), glow_sprite, SoundManager::preload(), BadGuy::set_colgroup_active(), sound_manager, and sprite_manager.

00039                                        :
00040   BadGuy(lisp, "images/creatures/ghosttree/ghosttree.sprite", LAYER_OBJECTS - 10), 
00041   mystate(STATE_IDLE),
00042   willowisp_timer(),
00043   willo_spawn_y(0),
00044   willo_radius(200), 
00045   willo_speed(1.8f), 
00046   willo_color(0),
00047   glow_sprite(),
00048   colorchange_timer(),
00049   suck_timer(),
00050   root_timer(),
00051   treecolor(0), 
00052   suck_lantern_color(),
00053   suck_lantern(0),
00054   willowisps()
00055 {
00056   glow_sprite = sprite_manager->create("images/creatures/ghosttree/ghosttree-glow.sprite");
00057   set_colgroup_active(COLGROUP_TOUCHABLE);
00058   sound_manager->preload("sounds/tree_howling.ogg");
00059   sound_manager->preload("sounds/tree_suck.ogg");
00060 }

GhostTree::~GhostTree (  ) 

Definition at line 62 of file ghosttree.cpp.

00063 {
00064 }

GhostTree::GhostTree ( const GhostTree  )  [private]


Member Function Documentation

virtual bool GhostTree::is_flammable (  )  const [inline, virtual]

Returns whether to call ignite() when a badguy gets hit by a fire bullet.

Reimplemented from BadGuy.

Definition at line 31 of file ghosttree.hpp.

00031 { return false; }

virtual bool GhostTree::is_freezable (  )  const [inline, virtual]

Reimplemented from BadGuy.

Definition at line 32 of file ghosttree.hpp.

00032 { return false; }

virtual void GhostTree::kill_fall (  )  [inline, virtual]

Set the badguy to kill/falling state, which makes him falling of the screen (his sprite is turned upside-down).

Reimplemented from BadGuy.

Definition at line 33 of file ghosttree.hpp.

00033 { }

void GhostTree::activate (  )  [virtual]

called when the badguy has been activated.

(As a side effect the dir variable might have been changed so that it faces towards the player.

Reimplemented from BadGuy.

Definition at line 81 of file ghosttree.cpp.

References colorchange_timer, root_timer, Timer::start(), and willowisp_timer.

00082 {
00083   willowisp_timer.start(1.0f, true);
00084   colorchange_timer.start(13, true);
00085   root_timer.start(5, true);
00086 }

void GhostTree::active_update ( float  elapsed_time  )  [virtual]

called each frame when the badguy is activated.

Reimplemented from BadGuy.

Definition at line 89 of file ghosttree.cpp.

References Sector::add_object(), MovingObject::bbox, Timer::check(), colorchange_timer, Sector::current(), die(), BadGuy::dir, gameRandom, MovingObject::get_bbox(), Rectf::get_bottom(), Rectf::get_height(), Rectf::get_middle(), BadGuy::get_nearest_player(), MovingObject::get_pos(), Rectf::get_width(), glow_sprite, is_color_deadly(), mystate, Vector::norm(), SoundManager::play(), RandomGenerator::randf(), RIGHT, root_timer, ROOT_TOP_OFFSET, TreeWillOWisp::set_color(), sound_manager, spawn_lantern(), MovingSprite::sprite, Timer::start(), STATE_IDLE, STATE_SUCKING, STATE_SWALLOWING, suck_lantern, suck_lantern_color, SUCK_TARGET_OFFSET, SUCK_TARGET_SPREAD, suck_timer, treecolor, Vector::unit(), willo_color, willo_radius, willo_spawn_y, willo_speed, WILLOWISP_COUNT, willowisp_timer, WILLOWISP_TOP_OFFSET, and willowisps.

00090 {
00091   (void) elapsed_time;
00092 
00093   if (mystate == STATE_IDLE) {
00094     if(colorchange_timer.check()) {
00095       sound_manager->play("sounds/tree_howling.ogg", get_pos());
00096       suck_timer.start(3);
00097       treecolor = (treecolor + 1) % 3;
00098 
00099       Color col;
00100       switch(treecolor) {
00101         case 0: col = Color(1, 0, 0); break;
00102         case 1: col = Color(0, 1, 0); break;
00103         case 2: col = Color(0, 0, 1); break;
00104         case 3: col = Color(1, 1, 0); break;
00105         case 4: col = Color(1, 0, 1); break;
00106         case 5: col = Color(0, 1, 1); break;
00107         default: assert(false);
00108       }
00109       glow_sprite->set_color(col);
00110     }
00111 
00112     if(suck_timer.check()) {
00113       Color col = glow_sprite->get_color();
00114       sound_manager->play("sounds/tree_suck.ogg", get_pos());
00115       std::vector<TreeWillOWisp*>::iterator iter;
00116       for(iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
00117         TreeWillOWisp *willo = *iter;
00118         if(willo->get_color() == col) {
00119           willo->start_sucking(get_bbox().get_middle() + SUCK_TARGET_OFFSET + Vector(gameRandom.randf(-SUCK_TARGET_SPREAD, SUCK_TARGET_SPREAD), gameRandom.randf(-SUCK_TARGET_SPREAD, SUCK_TARGET_SPREAD)));
00120         }
00121       }
00122       mystate = STATE_SUCKING;
00123     }
00124 
00125     if(willowisp_timer.check()) {
00126       if(willowisps.size() < WILLOWISP_COUNT) {
00127         Vector pos = Vector(bbox.get_width() / 2, bbox.get_height() / 2 + willo_spawn_y + WILLOWISP_TOP_OFFSET);
00128         TreeWillOWisp *willowisp 
00129           = new TreeWillOWisp(this, pos, 200 + willo_radius, willo_speed);
00130 
00131         Sector::current()->add_object(willowisp);
00132         willowisps.push_back(willowisp);
00133 
00134         willo_spawn_y -= 40;
00135         if(willo_spawn_y < -160)
00136           willo_spawn_y = 0;
00137 
00138         willo_radius += 20;
00139         if(willo_radius > 120)
00140           willo_radius = 0;
00141 
00142         if(willo_speed == 1.8f) {
00143           willo_speed = 1.5f;
00144         } else {
00145           willo_speed = 1.8f;
00146         }
00147 
00148         do {
00149           willo_color = (willo_color + 1) % 3;
00150         } while(willo_color == treecolor);
00151 
00152         switch(willo_color) {
00153           case 0: willowisp->set_color(Color(1, 0, 0)); break;
00154           case 1: willowisp->set_color(Color(0, 1, 0)); break;
00155           case 2: willowisp->set_color(Color(0, 0, 1)); break;
00156           case 3: willowisp->set_color(Color(1, 1, 0)); break;
00157           case 4: willowisp->set_color(Color(1, 0, 1)); break;
00158           case 5: willowisp->set_color(Color(0, 1, 1)); break;
00159           default: assert(false);
00160         }
00161       }
00162     }
00163 
00164     if(root_timer.check()) {
00165       /* TODO indicate root with an animation */
00166       Player* player = get_nearest_player();
00167       if (player) {
00168         Root* root = new Root(Vector(player->get_bbox().get_left(), get_bbox().get_bottom()+ROOT_TOP_OFFSET));
00169         Sector::current()->add_object(root);
00170       }
00171     }
00172   } else if (mystate == STATE_SWALLOWING) {
00173     if (suck_lantern) {
00174       // suck in lantern
00175       assert (suck_lantern);
00176       Vector pos = suck_lantern->get_pos();
00177       Vector delta = get_bbox().get_middle() + SUCK_TARGET_OFFSET - pos;
00178       Vector dir = delta.unit();
00179       if (delta.norm() < 1) {
00180         dir = delta;
00181         suck_lantern->ungrab(*this, RIGHT);
00182         suck_lantern->remove_me();
00183         suck_lantern = 0;
00184         sprite->set_action("swallow", 1); 
00185       } else {
00186         pos += dir;
00187         suck_lantern->grab(*this, pos, RIGHT);
00188       }
00189     } else {
00190       // wait until lantern is swallowed
00191       if (sprite->animation_done()) {
00192         if (is_color_deadly(suck_lantern_color)) {
00193           die();
00194         } else {
00195           sprite->set_action("default");
00196           mystate = STATE_IDLE;
00197           spawn_lantern();
00198         }
00199       }
00200     }
00201   }
00202 }

void GhostTree::willowisp_died ( TreeWillOWisp willowisp  ) 

Definition at line 212 of file ghosttree.cpp.

References mystate, STATE_IDLE, STATE_SUCKING, TreeWillOWisp::was_sucked, and willowisps.

Referenced by TreeWillOWisp::active_update().

00213 {
00214   if ((mystate == STATE_SUCKING) && (willowisp->was_sucked)) {
00215     mystate = STATE_IDLE;
00216   }
00217   willowisps.erase(std::find(willowisps.begin(), willowisps.end(), willowisp));
00218 }

void GhostTree::draw ( DrawingContext context  )  [virtual]

Called when the badguy is drawn.

The default implementation simply draws the badguy sprite on screen

Reimplemented from BadGuy.

Definition at line 221 of file ghosttree.cpp.

References BadGuy::draw(), game_time, MovingObject::get_pos(), glow_sprite, MovingSprite::layer, DrawingContext::LIGHTMAP, mystate, DrawingContext::pop_target(), DrawingContext::pop_transform(), DrawingContext::push_target(), DrawingContext::push_transform(), DrawingContext::set_alpha(), DrawingContext::set_target(), and STATE_SUCKING.

00222 {
00223   BadGuy::draw(context);
00224 
00225   context.push_target();
00226   context.push_transform();
00227   context.set_target(DrawingContext::LIGHTMAP);
00228   if (mystate == STATE_SUCKING) {
00229     context.set_alpha(0.5 + fmodf(game_time, 0.5));
00230   } else {
00231     context.set_alpha(0.5);
00232   }
00233   glow_sprite->draw(context, get_pos(), layer);
00234   context.pop_transform();
00235   context.pop_target();
00236 }

bool GhostTree::collides ( GameObject other,
const CollisionHit hit 
) [virtual]

when 2 objects collided, we will first call the pre_collision_check functions of both objects that can decide on how to react to the collision.

Reimplemented from MovingObject.

Definition at line 239 of file ghosttree.cpp.

References mystate, and STATE_SUCKING.

00239                                                            {
00240   if (mystate != STATE_SUCKING) return false;
00241   if (dynamic_cast<Lantern*>(&other)) return true;
00242   if (dynamic_cast<Player*>(&other)) return true;
00243   return false;
00244 }

HitResponse GhostTree::collision ( GameObject other,
const CollisionHit hit 
) [virtual]

Called when a collision with another object occurred.

The default implementation calls collision_player, collision_solid, collision_badguy and collision_squished

Reimplemented from BadGuy.

Definition at line 247 of file ghosttree.cpp.

References ABORT_MOVE, Lantern::get_color(), MovingObject::get_pos(), Lantern::grab(), Player::kill(), mystate, RIGHT, STATE_SUCKING, STATE_SWALLOWING, suck_lantern, and suck_lantern_color.

00247                                                             {
00248   if(mystate != STATE_SUCKING) return ABORT_MOVE;
00249 
00250   Player* player = dynamic_cast<Player*>(&other);
00251   if (player) {
00252     player->kill(false);
00253   }
00254 
00255   Lantern* lantern = dynamic_cast<Lantern*>(&other);
00256   if (lantern) {
00257     suck_lantern = lantern;
00258     suck_lantern->grab(*this, suck_lantern->get_pos(), RIGHT);
00259     suck_lantern_color = lantern->get_color();
00260     mystate = STATE_SWALLOWING;
00261   }
00262 
00263   return ABORT_MOVE;
00264 }

void GhostTree::die (  ) 

Definition at line 67 of file ghosttree.cpp.

References glow_sprite, mystate, MovingSprite::sprite, STATE_DYING, and willowisps.

Referenced by active_update().

00068 {
00069   mystate = STATE_DYING;
00070   sprite->set_action("dying", 1); 
00071   glow_sprite->set_action("dying", 1); 
00072 
00073   std::vector<TreeWillOWisp*>::iterator iter;
00074   for(iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
00075     TreeWillOWisp *willo = *iter;
00076     willo->vanish();
00077   }
00078 }

bool GhostTree::is_color_deadly ( Color  color  )  const [private]

Definition at line 205 of file ghosttree.cpp.

References Color::blue, glow_sprite, Color::green, and Color::red.

Referenced by active_update().

00205                                             {
00206   if (color == Color(0,0,0)) return false;
00207   Color my_color = glow_sprite->get_color();
00208   return ((my_color.red != color.red) || (my_color.green != color.green) || (my_color.blue != color.blue));
00209 }

void GhostTree::spawn_lantern (  )  [private]

Definition at line 267 of file ghosttree.cpp.

References Sector::add_object(), Sector::current(), MovingObject::get_bbox(), and SUCK_TARGET_OFFSET.

Referenced by active_update().

00267                          {
00268   Lantern* lantern = new Lantern(get_bbox().get_middle() + SUCK_TARGET_OFFSET);
00269   Sector::current()->add_object(lantern);
00270 }

GhostTree& GhostTree::operator= ( const GhostTree  )  [private]


Member Data Documentation

MyState GhostTree::mystate [private]

Definition at line 49 of file ghosttree.hpp.

Referenced by active_update(), collides(), collision(), die(), draw(), and willowisp_died().

Timer GhostTree::willowisp_timer [private]

Definition at line 50 of file ghosttree.hpp.

Referenced by activate(), and active_update().

float GhostTree::willo_spawn_y [private]

Definition at line 51 of file ghosttree.hpp.

Referenced by active_update().

float GhostTree::willo_radius [private]

Definition at line 52 of file ghosttree.hpp.

Referenced by active_update().

float GhostTree::willo_speed [private]

Definition at line 53 of file ghosttree.hpp.

Referenced by active_update().

int GhostTree::willo_color [private]

Definition at line 54 of file ghosttree.hpp.

Referenced by active_update().

SpritePtr GhostTree::glow_sprite [private]

Definition at line 56 of file ghosttree.hpp.

Referenced by active_update(), die(), draw(), GhostTree(), and is_color_deadly().

Timer GhostTree::colorchange_timer [private]

Definition at line 57 of file ghosttree.hpp.

Referenced by activate(), and active_update().

Timer GhostTree::suck_timer [private]

Definition at line 58 of file ghosttree.hpp.

Referenced by active_update().

Timer GhostTree::root_timer [private]

Definition at line 59 of file ghosttree.hpp.

Referenced by activate(), and active_update().

int GhostTree::treecolor [private]

Definition at line 60 of file ghosttree.hpp.

Referenced by active_update().

Color GhostTree::suck_lantern_color [private]

Definition at line 61 of file ghosttree.hpp.

Referenced by active_update(), and collision().

Lantern* GhostTree::suck_lantern [private]

Lantern that is currently being sucked in.

Definition at line 63 of file ghosttree.hpp.

Referenced by active_update(), and collision().

std::vector<TreeWillOWisp*> GhostTree::willowisps [private]

Definition at line 65 of file ghosttree.hpp.

Referenced by active_update(), die(), and willowisp_died().


The documentation for this class was generated from the following files:
Generated on Mon Apr 14 03:38:35 2014 for SuperTux by  doxygen 1.5.1