worldmap::Tux Class Reference

#include <tux.hpp>

Inherits GameObject.

List of all members.

Public Member Functions

 Tux (WorldMap *worldmap_)
 ~Tux ()
void setup ()
 called prior to first update
void draw (DrawingContext &context)
 The GameObject should draw itself onto the provided DrawingContext if this function is called.
void update (float elapsed_time)
 This function is called once per frame and allows the object to update it's state.
void set_direction (Direction dir)
void set_ghost_mode (bool enabled)
bool get_ghost_mode ()
bool is_moving () const
Vector get_pos ()
Vector get_tile_pos () const
void set_tile_pos (Vector p)

Public Attributes

Direction back_direction

Private Member Functions

void stop ()
bool canWalk (int tile_data, Direction dir)
 check if we can leave a tile (with given "tile_data") in direction "dir"
void updateInputDirection ()
 if controller was pressed, update input_direction
void tryStartWalking ()
 try starting to walk in input_direction
void tryContinueWalking (float elapsed_time)
 try to continue walking in current direction
 Tux (const Tux &)
Tuxoperator= (const Tux &)

Private Attributes

WorldMapworldmap
SpritePtr sprite
Controllercontroller
Direction input_direction
Direction direction
Vector tile_pos
float offset
 Length by which tux is away from its current tile, length is in input_direction direction.
bool moving
bool ghost_mode


Detailed Description

Definition at line 29 of file tux.hpp.


Constructor & Destructor Documentation

worldmap::Tux::Tux ( WorldMap worldmap_  ) 

Definition at line 33 of file tux.cpp.

References SpriteManager::create(), worldmap::D_NONE, direction, ghost_mode, input_direction, moving, offset, sprite, and sprite_manager.

00033                             :
00034   back_direction(),
00035   worldmap(worldmap_),
00036   sprite(),
00037   controller(),
00038   input_direction(),
00039   direction(),
00040   tile_pos(),
00041   offset(),
00042   moving(),
00043   ghost_mode()
00044 {
00045   sprite = sprite_manager->create("images/worldmap/common/tux.sprite");
00046 
00047   offset = 0;
00048   moving = false;
00049   direction = D_NONE;
00050   input_direction = D_NONE;
00051 
00052   ghost_mode = false;
00053 }

worldmap::Tux::~Tux (  ) 

Definition at line 55 of file tux.cpp.

00056 {
00057 }

worldmap::Tux::Tux ( const Tux  )  [private]


Member Function Documentation

void worldmap::Tux::stop (  )  [private]

Definition at line 109 of file tux.cpp.

References worldmap::D_NONE, direction, input_direction, moving, and offset.

Referenced by tryContinueWalking().

00110 {
00111   offset = 0;
00112   direction = D_NONE;
00113   input_direction = D_NONE;
00114   moving = false;
00115 }

bool worldmap::Tux::canWalk ( int  tile_data,
Direction  dir 
) [private]

check if we can leave a tile (with given "tile_data") in direction "dir"

Definition at line 162 of file tux.cpp.

References worldmap::D_EAST, worldmap::D_NORTH, worldmap::D_SOUTH, worldmap::D_WEST, ghost_mode, Tile::WORLDMAP_EAST, Tile::WORLDMAP_NORTH, Tile::WORLDMAP_SOUTH, and Tile::WORLDMAP_WEST.

Referenced by tryContinueWalking().

00163 {
00164   return ghost_mode || 
00165     ((tile_data & Tile::WORLDMAP_NORTH && dir == D_NORTH) ||
00166      (tile_data & Tile::WORLDMAP_SOUTH && dir == D_SOUTH) ||
00167      (tile_data & Tile::WORLDMAP_EAST  && dir == D_EAST) ||
00168      (tile_data & Tile::WORLDMAP_WEST  && dir == D_WEST));
00169 }

void worldmap::Tux::updateInputDirection (  )  [private]

if controller was pressed, update input_direction

Definition at line 288 of file tux.cpp.

References controller, worldmap::D_EAST, worldmap::D_NORTH, worldmap::D_SOUTH, worldmap::D_WEST, Controller::DOWN, g_jk_controller, JoystickKeyboardController::get_main_controller(), Controller::hold(), input_direction, Controller::LEFT, Controller::RIGHT, and Controller::UP.

Referenced by update().

void worldmap::Tux::tryStartWalking (  )  [private]

try starting to walk in input_direction

Definition at line 136 of file tux.cpp.

References worldmap::WorldMap::at_level(), back_direction, worldmap::D_NONE, direction, worldmap::WorldMap::get_next_tile(), ghost_mode, input_direction, moving, worldmap::WorldMap::path_ok(), worldmap::reverse_dir(), worldmap::LevelTile::solved, tile_pos, and worldmap.

Referenced by update().

00137 {
00138   if (moving)
00139     return;
00140   if (input_direction == D_NONE)
00141     return;
00142 
00143   LevelTile* level = worldmap->at_level();
00144 
00145   // We got a new direction, so lets start walking when possible
00146   Vector next_tile;
00147   if ((!level || level->solved)
00148       && worldmap->path_ok(input_direction, tile_pos, &next_tile)) {
00149     tile_pos = next_tile;
00150     moving = true;
00151     direction = input_direction;
00152     back_direction = reverse_dir(direction);
00153   } else if (ghost_mode || (input_direction == back_direction)) {
00154     moving = true;
00155     direction = input_direction;
00156     tile_pos = worldmap->get_next_tile(tile_pos, direction);
00157     back_direction = reverse_dir(direction);
00158   }
00159 }

void worldmap::Tux::tryContinueWalking ( float  elapsed_time  )  [private]

try to continue walking in current direction

Definition at line 172 of file tux.cpp.

References worldmap::SpecialTile::apply_action_east, worldmap::SpecialTile::apply_action_north, worldmap::SpecialTile::apply_action_south, worldmap::SpecialTile::apply_action_west, worldmap::WorldMap::at_level(), worldmap::WorldMap::at_special_tile(), worldmap::WorldMap::at_sprite_change(), worldmap::WorldMap::at_teleporter(), back_direction, canWalk(), worldmap::SpriteChange::change_on_touch, worldmap::SpriteChange::clear_stay_action(), worldmap::D_EAST, worldmap::D_NONE, worldmap::D_NORTH, worldmap::D_SOUTH, worldmap::D_WEST, direction, ghost_mode, input_direction, log_debug, log_warning, worldmap::SpecialTile::map_message, worldmap::map_message_TIME, moving, offset, worldmap::WorldMap::passive_message, worldmap::SpecialTile::passive_message, worldmap::WorldMap::passive_message_timer, worldmap::WorldMap::path_ok(), worldmap::reverse_dir(), worldmap::WorldMap::run_script(), worldmap::SpecialTile::script, worldmap::SpriteChange::set_stay_action(), worldmap::SpriteChange::sprite, sprite, Timer::start(), stop(), worldmap::WorldMap::tile_data_at(), tile_pos, worldmap::TUXSPEED, worldmap, Tile::WORLDMAP_EAST, Tile::WORLDMAP_NORTH, Tile::WORLDMAP_SOUTH, Tile::WORLDMAP_STOP, and Tile::WORLDMAP_WEST.

Referenced by update().

00173 {
00174   if (!moving)
00175     return;
00176 
00177   // Let tux walk
00178   offset += TUXSPEED * elapsed_time;
00179 
00180   // Do nothing if we have not yet reached the next tile
00181   if (offset <= 32)
00182     return;
00183 
00184   offset -= 32;
00185 
00186   SpriteChange* sprite_change = worldmap->at_sprite_change(tile_pos);
00187   if(sprite_change != NULL) {
00188     sprite = sprite_change->sprite->clone();
00189     sprite_change->clear_stay_action();
00190   }
00191 
00192   // if this is a special_tile with passive_message, display it
00193   SpecialTile* special_tile = worldmap->at_special_tile();
00194   if(special_tile)
00195   {
00196     // direction and the apply_action_ are opposites, since they "see"
00197     // directions in a different way
00198     if((direction == D_NORTH && special_tile->apply_action_south) ||
00199        (direction == D_SOUTH && special_tile->apply_action_north) ||
00200        (direction == D_WEST && special_tile->apply_action_east) ||
00201        (direction == D_EAST && special_tile->apply_action_west))
00202     {
00203       if(special_tile->passive_message) {
00204         worldmap->passive_message = special_tile->map_message;
00205         worldmap->passive_message_timer.start(map_message_TIME);
00206       } else if(special_tile->script != "") {
00207         try {
00208           std::istringstream in(special_tile->script);
00209           worldmap->run_script(in, "specialtile");
00210         } catch(std::exception& e) {
00211           log_warning << "Couldn't execute special tile script: " << e.what()
00212                       << std::endl;
00213         }
00214       }
00215     }
00216   }
00217 
00218   // check if we are at a Teleporter
00219   Teleporter* teleporter = worldmap->at_teleporter(tile_pos);
00220 
00221   // stop if we reached a level, a WORLDMAP_STOP tile, a teleporter or a special tile without a passive_message
00222   if ((worldmap->at_level())
00223       || (worldmap->tile_data_at(tile_pos) & Tile::WORLDMAP_STOP)
00224       || (special_tile && !special_tile->passive_message
00225           && special_tile->script == "")
00226       || (teleporter) || ghost_mode) {
00227     if(special_tile && !special_tile->map_message.empty()
00228        && !special_tile->passive_message)
00229       worldmap->passive_message_timer.start(0);
00230     stop();
00231     return;
00232   }
00233 
00234   // if user wants to change direction, try changing, else guess the direction in which to walk next
00235   const int tile_data = worldmap->tile_data_at(tile_pos);
00236   if ((direction != input_direction) && canWalk(tile_data, input_direction)) {
00237     direction = input_direction;
00238     back_direction = reverse_dir(direction);
00239   } else {
00240     Direction dir = D_NONE;
00241     if (tile_data & Tile::WORLDMAP_NORTH && back_direction != D_NORTH)
00242       dir = D_NORTH;
00243     else if (tile_data & Tile::WORLDMAP_SOUTH && back_direction != D_SOUTH)
00244       dir = D_SOUTH;
00245     else if (tile_data & Tile::WORLDMAP_EAST && back_direction != D_EAST)
00246       dir = D_EAST;
00247     else if (tile_data & Tile::WORLDMAP_WEST && back_direction != D_WEST)
00248       dir = D_WEST;
00249 
00250     if (dir == D_NONE) {
00251       // Should never be reached if tiledata is good
00252       log_warning << "Could not determine where to walk next" << std::endl;
00253       stop();
00254       return;
00255     }
00256 
00257     direction = dir;
00258     input_direction = direction;
00259     back_direction = reverse_dir(direction);
00260   }
00261 
00262   // Walk automatically to the next tile
00263   if(direction == D_NONE)
00264     return;
00265 
00266   Vector next_tile;
00267   if (!ghost_mode && !worldmap->path_ok(direction, tile_pos, &next_tile)) {
00268     log_debug << "Tilemap data is buggy" << std::endl;
00269     stop();
00270     return;
00271   }
00272 
00273   SpriteChange* next_sprite = worldmap->at_sprite_change(next_tile);
00274   if(next_sprite != NULL && next_sprite->change_on_touch) {
00275     sprite = next_sprite->sprite->clone();
00276     next_sprite->clear_stay_action();
00277   }
00278   SpriteChange* last_sprite = worldmap->at_sprite_change(tile_pos);
00279   if(last_sprite != NULL && next_sprite != NULL) {
00280     log_debug << "Old: " << tile_pos << " New: " << next_tile << std::endl;
00281     last_sprite->set_stay_action();
00282   }
00283 
00284   tile_pos = next_tile;
00285 }

void worldmap::Tux::setup (  ) 

called prior to first update

Definition at line 312 of file tux.cpp.

References worldmap::WorldMap::at_sprite_change(), worldmap::SpriteChange::clear_stay_action(), worldmap::SpriteChange::sprite, sprite, tile_pos, and worldmap.

Referenced by worldmap::WorldMap::setup().

00313 {
00314   // check if we already touch a SpriteChange object
00315   SpriteChange* sprite_change = worldmap->at_sprite_change(tile_pos);
00316   if(sprite_change != NULL) {
00317     sprite = sprite_change->sprite->clone();
00318     sprite_change->clear_stay_action();
00319   }
00320 }

void worldmap::Tux::draw ( DrawingContext context  )  [virtual]

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

Implements GameObject.

Definition at line 60 of file tux.cpp.

References PlayerStatus::bonus, FIRE_BONUS, worldmap::WorldMap::get_player_status(), get_pos(), GROWUP_BONUS, LAYER_OBJECTS, log_debug, moving, NO_BONUS, sprite, and worldmap.

00061 {
00062   switch (worldmap->get_player_status()->bonus) {
00063     case GROWUP_BONUS:
00064       sprite->set_action(moving ? "large-walking" : "large-stop");
00065       break;
00066     case FIRE_BONUS:
00067       sprite->set_action(moving ? "fire-walking" : "fire-stop");
00068       break;
00069     case NO_BONUS:
00070       sprite->set_action(moving ? "small-walking" : "small-stop");
00071       break;
00072     default:
00073       log_debug << "Bonus type not handled in worldmap." << std::endl;
00074       sprite->set_action("large-stop");
00075       break;
00076   }
00077 
00078   sprite->draw(context, get_pos(), LAYER_OBJECTS);
00079 }

void worldmap::Tux::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)

Implements GameObject.

Definition at line 302 of file tux.cpp.

References moving, tryContinueWalking(), tryStartWalking(), and updateInputDirection().

00303 {
00304   updateInputDirection();
00305   if (moving)
00306     tryContinueWalking(elapsed_time);
00307   else
00308     tryStartWalking();
00309 }

void worldmap::Tux::set_direction ( Direction  dir  ) 

Definition at line 118 of file tux.cpp.

References input_direction.

Referenced by worldmap::WorldMap::finished_level(), worldmap::WorldMap::move_to_spawnpoint(), and worldmap::WorldMap::on_escape_press().

00119 {
00120   input_direction = dir;
00121 }

void worldmap::Tux::set_ghost_mode ( bool  enabled  ) 

Definition at line 124 of file tux.cpp.

References ghost_mode.

00125 {
00126   ghost_mode = enabled;
00127 }

bool worldmap::Tux::get_ghost_mode (  ) 

Definition at line 130 of file tux.cpp.

References ghost_mode.

00131 {
00132   return ghost_mode;
00133 }

bool worldmap::Tux::is_moving (  )  const [inline]

Definition at line 68 of file tux.hpp.

References moving.

Referenced by worldmap::WorldMap::draw_status(), and worldmap::WorldMap::update().

00068 { return moving; }

Vector worldmap::Tux::get_pos (  ) 

Definition at line 82 of file tux.cpp.

References worldmap::D_EAST, worldmap::D_NONE, worldmap::D_NORTH, worldmap::D_SOUTH, worldmap::D_WEST, direction, offset, tile_pos, Vector::x, and Vector::y.

Referenced by draw(), and worldmap::WorldMap::get_camera_pos_for_tux().

00083 {
00084   float x = tile_pos.x * 32;
00085   float y = tile_pos.y * 32;
00086 
00087   switch(direction)
00088   {
00089     case D_WEST:
00090       x -= offset - 32;
00091       break;
00092     case D_EAST:
00093       x += offset - 32;
00094       break;
00095     case D_NORTH:
00096       y -= offset - 32;
00097       break;
00098     case D_SOUTH:
00099       y += offset - 32;
00100       break;
00101     case D_NONE:
00102       break;
00103   }
00104 
00105   return Vector(x, y);
00106 }

Vector worldmap::Tux::get_tile_pos (  )  const [inline]

Definition at line 70 of file tux.hpp.

References tile_pos.

Referenced by worldmap::WorldMap::at_level(), worldmap::WorldMap::at_special_tile(), worldmap::WorldMap::draw_status(), worldmap::WorldMap::finished_level(), worldmap::WorldMap::save_state(), and worldmap::WorldMap::update().

00070 { return tile_pos; }

void worldmap::Tux::set_tile_pos ( Vector  p  )  [inline]

Definition at line 71 of file tux.hpp.

References tile_pos.

Referenced by worldmap::WorldMap::load_state(), and worldmap::WorldMap::move_to_spawnpoint().

00071 { tile_pos = p; }

Tux& worldmap::Tux::operator= ( const Tux  )  [private]


Member Data Documentation

Direction worldmap::Tux::back_direction

Definition at line 32 of file tux.hpp.

Referenced by worldmap::WorldMap::finished_level(), worldmap::WorldMap::load_state(), worldmap::WorldMap::save_state(), tryContinueWalking(), tryStartWalking(), and worldmap::WorldMap::update().

WorldMap* worldmap::Tux::worldmap [private]

Definition at line 34 of file tux.hpp.

Referenced by draw(), setup(), tryContinueWalking(), and tryStartWalking().

SpritePtr worldmap::Tux::sprite [private]

Definition at line 35 of file tux.hpp.

Referenced by draw(), setup(), tryContinueWalking(), and Tux().

Controller* worldmap::Tux::controller [private]

Definition at line 36 of file tux.hpp.

Referenced by updateInputDirection().

Direction worldmap::Tux::input_direction [private]

Definition at line 38 of file tux.hpp.

Referenced by set_direction(), stop(), tryContinueWalking(), tryStartWalking(), Tux(), and updateInputDirection().

Direction worldmap::Tux::direction [private]

Definition at line 39 of file tux.hpp.

Referenced by get_pos(), stop(), tryContinueWalking(), tryStartWalking(), and Tux().

Vector worldmap::Tux::tile_pos [private]

Definition at line 40 of file tux.hpp.

Referenced by get_pos(), get_tile_pos(), set_tile_pos(), setup(), tryContinueWalking(), and tryStartWalking().

float worldmap::Tux::offset [private]

Length by which tux is away from its current tile, length is in input_direction direction.

Definition at line 43 of file tux.hpp.

Referenced by get_pos(), stop(), tryContinueWalking(), and Tux().

bool worldmap::Tux::moving [private]

Definition at line 44 of file tux.hpp.

Referenced by draw(), is_moving(), stop(), tryContinueWalking(), tryStartWalking(), Tux(), and update().

bool worldmap::Tux::ghost_mode [private]

Definition at line 46 of file tux.hpp.

Referenced by canWalk(), get_ghost_mode(), set_ghost_mode(), tryContinueWalking(), tryStartWalking(), and Tux().


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