Screenshots with F9.
[sdlbotor.git] / MapObject.cpp
blobc0275635a3bf3abc15498951181f86550c5b5a1a
1 #include "MapObject.h"
2 #include "Map.h"
3 #include "Game.h"
4 #include "Tile.h"
7 namespace botor
9 void MapObject::StartMovement()
11 Sint16 sx,sy;
12 Map::transformM2S( mapX, mapY, sx, sy );
13 movementX = (float)sx;
14 movementY = (float)sy;
16 if( isWalkable( mapX+vX, mapY+vY ) )
18 mapX +=vX;
19 mapY +=vY;
20 if( isBitSet( Game::getMap()->tileAt( mapX, mapY )->typeMask, Tile::TILE_SLOW ) )
21 speed = SPEED() * 0.5f;
22 else
23 speed = SPEED();
25 moving = true;
29 bool MapObject::isWalkable( Uint8 X, Uint8 Y )
31 Tile *t = Game::getMap()->tileAt(X, Y);
33 if( isBitSet( t->typeMask, Tile::TILE_WALKABLE ) )
35 return true;
37 else
38 return false;
41 MapObject::MapObject( Uint8 X, Uint8 Y, Drawable *graphic ) :
42 mapX(0), mapY(0), vX(0), vY(0), movementX(0), movementY(0), speed(0), walking(false), moving(false)
44 this->graphic = graphic;
45 Teleport(X, Y);
48 MapObject::~MapObject()
50 delete graphic;
53 void MapObject::UpdatePosition()
55 Sint16 sx,sy;
56 Map::transformM2S( mapX, mapY, sx, sy );
57 if( dst( movementX, movementY, (float)sx, (float)sy ) < 0.5 )
59 OnTile( Game::getMap()->tileAt( mapX, mapY ) );
61 if( walking )
62 StartMovement();
63 else
64 moving = false;
67 else
69 float dirX, dirY;
71 dirX = sign( sx - movementX ) * speed;
72 dirY = sign( sy - movementY ) * speed;
74 movementX += dirX;
75 movementY += dirY;
80 void MapObject::Initialize() {}
81 void MapObject::DeInitialize() {}
83 void MapObject::Update()
85 if( moving )
87 UpdatePosition();
91 void MapObject::Draw()
93 if( !graphic )
94 return;
96 Sint16 sx,sy;
97 if( moving )
99 sx = (Sint16)movementX;
100 sy = (Sint16)movementY;
102 else
104 Map::transformM2S( mapX, mapY, sx, sy );
107 graphic->Draw( sx, sy );
110 void MapObject::OnTile( Tile *t ) {}
113 void MapObject::Teleport( Uint8 mapX, Uint8 mapY, Uint16 room )
115 this->mapX = mapX;
116 this->mapY = mapY;
117 moving = false;
120 Uint8 MapObject::getMapX() { return mapX; }
121 Uint8 MapObject::getMapY() { return mapY; }
123 bool MapObject::Walking() { return walking; }
125 void MapObject::setGraphic( Drawable *graphic )
127 this->graphic = graphic;
130 Drawable *MapObject::getGraphic( )
132 return graphic;