MagicBlock Class Reference

#include <magicblock.hpp>

Inherits MovingSprite.

List of all members.

Public Member Functions

 MagicBlock (const Reader &reader)
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.
HitResponse collision (GameObject &other, const CollisionHit &hit)
 this function is called when the object collided with any other object
void update (float elapsed_time)
 This function is called once per frame and allows the object to update it's state.
void draw (DrawingContext &context)
 The GameObject should draw itself onto the provided DrawingContext if this function is called.

Private Attributes

bool is_solid
float trigger_red
float trigger_green
float trigger_blue
float solid_time
float switch_delay
 seconds until switching solidity
Color color
Color light
Vector center
bool black


Detailed Description

Definition at line 29 of file magicblock.hpp.


Constructor & Destructor Documentation

MagicBlock::MagicBlock ( const Reader reader  ) 

Definition at line 39 of file magicblock.cpp.

References Color::alpha, ALPHA_SOLID, black, Color::blue, center, COLGROUP_STATIC, color, lisp::Lisp::get(), MovingObject::get_bbox(), Rectf::get_middle(), Color::green, MIN_INTENSITY, Color::red, MovingObject::set_group(), trigger_blue, trigger_green, and trigger_red.

00039                                          :
00040   MovingSprite(lisp, "images/objects/magicblock/magicblock.sprite"),
00041   is_solid(false), 
00042   trigger_red(),
00043   trigger_green(),
00044   trigger_blue(),
00045   solid_time(0), 
00046   switch_delay(0),
00047   color(),
00048   light(1.0f,1.0f,1.0f),
00049   center(),
00050   black()
00051 {
00052   set_group(COLGROUP_STATIC);
00053   //get color from lisp
00054   std::vector<float> vColor;
00055   lisp.get("color", vColor );
00056   color = Color( vColor );
00057 
00058   //all alpha to make the sprite still visible
00059   color.alpha = ALPHA_SOLID;
00060 
00061   //set trigger
00062   if(color.red == 0 && color.green == 0 && color.blue == 0) { //is it black?
00063     black = true;
00064     trigger_red = MIN_INTENSITY;
00065     trigger_green = MIN_INTENSITY;
00066     trigger_blue = MIN_INTENSITY;
00067   } else {
00068     black = false;
00069     trigger_red = (color.red == 1.0f ? MIN_INTENSITY : 0);
00070     trigger_green = (color.green == 1.0f ? MIN_INTENSITY : 0);
00071     trigger_blue = (color.blue == 1.0f ? MIN_INTENSITY : 0);
00072   }
00073 
00074   center = get_bbox().get_middle();
00075 }


Member Function Documentation

bool MagicBlock::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 154 of file magicblock.cpp.

References is_solid.

00155 {
00156   return is_solid;
00157 }

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

this function is called when the object collided with any other object

Implements MovingObject.

Definition at line 160 of file magicblock.cpp.

References FORCE_MOVE.

00161 {
00162   return FORCE_MOVE;
00163 }

void MagicBlock::update ( float  elapsed_time  )  [virtual]

This function is called once per frame and allows the object to update it's state.

The elapsed_time is the time since the last frame in seconds and should be the base for all timed calculations (don't use SDL_GetTicks directly as this will fail in pause mode)

Reimplemented from MovingSprite.

Definition at line 78 of file magicblock.cpp.

References Color::alpha, ALPHA_NONSOLID, ALPHA_SOLID, black, Color::blue, Sector::camera, center, COLGROUP_DISABLED, COLGROUP_STATIC, color, Sector::current(), MovingObject::get_bbox(), Camera::get_translation(), Color::green, is_solid, light, MIN_SOLIDTIME, Color::red, SCREEN_HEIGHT, SCREEN_WIDTH, MovingObject::set_group(), solid_time, MovingSprite::sprite, SWITCH_DELAY, switch_delay, trigger_blue, trigger_green, trigger_red, Vector::x, and Vector::y.

00079 {
00080   //Check if center of this block is on screen.
00081   //Don't update if not, because there is no light off screen.
00082   float screen_left = Sector::current()->camera->get_translation().x;
00083   float screen_top = Sector::current()->camera->get_translation().y;
00084   float screen_right = screen_left+ SCREEN_WIDTH;
00085   float screen_bottom = screen_top + SCREEN_HEIGHT;
00086   if((center.x > screen_right ) || ( center.y > screen_bottom) ||
00087      ( center.x < screen_left) || ( center.y < screen_top)) {
00088     switch_delay = SWITCH_DELAY;
00089     return;
00090   }
00091 
00092   bool lighting_ok;
00093   if(black) {
00094     lighting_ok = (light.red >= trigger_red || light.green >= trigger_green
00095                    || light.blue >= trigger_blue);
00096   }else{
00097     lighting_ok = (light.red >= trigger_red && light.green >= trigger_green
00098                    && light.blue >= trigger_blue);
00099   }
00100 
00101   // overrule lighting_ok if switch_delay has not yet passed
00102   if (lighting_ok == is_solid) {
00103     switch_delay = SWITCH_DELAY;
00104   } else {
00105     if (switch_delay > 0) {
00106       lighting_ok = is_solid;
00107       switch_delay -= elapsed_time;
00108     }
00109   }
00110 
00111   if (lighting_ok) {
00112     // lighting suggests going solid
00113 
00114     if (!is_solid) {
00115       if (Sector::current()->is_free_of_movingstatics(get_bbox(), this)) {
00116         is_solid = true;
00117         solid_time = 0;
00118         switch_delay = SWITCH_DELAY;
00119       }
00120     }
00121   } else {
00122     // lighting suggests going nonsolid
00123 
00124     if( solid_time >= MIN_SOLIDTIME ){
00125       is_solid = false;
00126     }
00127   }
00128 
00129   //Update Sprite.
00130   if(is_solid) {
00131     solid_time+=elapsed_time;
00132     color.alpha = ALPHA_SOLID;
00133     sprite->set_action("solid");
00134     set_group(COLGROUP_STATIC);
00135   } else {
00136     color.alpha = ALPHA_NONSOLID;
00137     sprite->set_action("normal");
00138     set_group(COLGROUP_DISABLED);
00139   }
00140 }

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

The GameObject should draw itself onto the provided DrawingContext if this function is called.

Reimplemented from MovingSprite.

Definition at line 143 of file magicblock.cpp.

References center, color, MovingSprite::draw(), DrawingContext::draw_filled_rect(), MovingObject::get_bbox(), DrawingContext::get_light(), MovingSprite::layer, and light.

00143                                        {
00144   //Ask for update about lightmap at center of this block
00145   context.get_light( center, &light );
00146 
00147   //Draw the Sprite.
00148   MovingSprite::draw(context);
00149   //Add the color.
00150   context.draw_filled_rect( get_bbox(), color, layer);
00151 }


Member Data Documentation

bool MagicBlock::is_solid [private]

Definition at line 40 of file magicblock.hpp.

Referenced by collides(), and update().

float MagicBlock::trigger_red [private]

Definition at line 41 of file magicblock.hpp.

Referenced by MagicBlock(), and update().

float MagicBlock::trigger_green [private]

Definition at line 42 of file magicblock.hpp.

Referenced by MagicBlock(), and update().

float MagicBlock::trigger_blue [private]

Definition at line 43 of file magicblock.hpp.

Referenced by MagicBlock(), and update().

float MagicBlock::solid_time [private]

Definition at line 44 of file magicblock.hpp.

Referenced by update().

float MagicBlock::switch_delay [private]

seconds until switching solidity

Definition at line 45 of file magicblock.hpp.

Referenced by update().

Color MagicBlock::color [private]

Definition at line 46 of file magicblock.hpp.

Referenced by draw(), MagicBlock(), and update().

Color MagicBlock::light [private]

Definition at line 47 of file magicblock.hpp.

Referenced by draw(), and update().

Vector MagicBlock::center [private]

Definition at line 48 of file magicblock.hpp.

Referenced by draw(), MagicBlock(), and update().

bool MagicBlock::black [private]

Definition at line 49 of file magicblock.hpp.

Referenced by MagicBlock(), and update().


The documentation for this class was generated from the following files:
Generated on Mon Jun 9 03:38:32 2014 for SuperTux by  doxygen 1.5.1