Added potion. reverted inventory lala
[sdlbotor.git] / Robot.cpp
blob8a3bcd281a58b2237efa21542815daed8e2e38ee
1 #include <cstdio>
2 #include <cstdlib>
4 #include "globalfunc.h"
6 #include "Robot.h"
7 #include "Map.h"
8 #include "Game.h"
9 #include "DTile.h"
10 #include "Player.h"
13 namespace botor
16 Tileset Robot::ROBOTS_TILESET( "robots.png" );
18 Robot::Robot( Uint8 X, Uint8 Y ) : LivingMapObject( X, Y ),
19 ani_walking( 0, 3, 10 )
21 ani_player = new DAnimation( &ROBOTS_TILESET );
22 graphic = ani_player;
23 ani_player->load( &ani_walking );
25 walking = true;
28 MakeNewDir();
29 StartMovement();
32 float Robot::SPEED() { return ROBOT_SPEED; }
34 void Robot::StartMovement()
36 Sint16 sx,sy;
37 Map::transformM2S( mapX, mapY, sx, sy );
38 movementX = (float)sx;
39 movementY = (float)sy;
41 if( !isWalkable( mapX+vX, mapY+vY ) )
42 MakeNewDir();
44 mapX += vX;
45 mapY += vY;
46 if( isBitSet( Game::getMap()->tileAt( mapX, mapY )->typeMask, Tile::TILE_SLOW ) )
47 speed = SPEED() * 0.5f;
48 else
49 speed = SPEED();
51 moving = true;
55 bool Robot::isWalkable( Uint8 X, Uint8 Y )
57 Tile *t = Game::getMap()->tileAt(X, Y);
58 if(
59 (isBitSet( t->typeMask, Tile::TILE_WALKABLE ) ||
60 isBitSet( t->typeMask, Tile::TILE_EFENCE )) &&
61 !Game::getMap()->isRobotOn(X, Y) &&
62 !Game::getMap()->getObject( X, Y )
65 if( isBitSet( t->typeMask, Tile::TILE_EFENCE ) )
67 return rand()%4 == 0;
70 return true;
72 else
73 return false;
76 void Robot::OnTile( Tile *t )
78 if( isBitSet( t->typeMask, Tile::TILE_EFENCE ) )
80 Game::getMap()->RemoveTile( mapX, mapY );
81 Die();
84 if( mapX == Game::getPlayer()->getMapX() &&
85 mapY == Game::getPlayer()->getMapY() )
87 Game::getPlayer()->Die();
89 if( rand()%4 == 0 )
90 Die();
93 MakeNewDir();
96 void Robot::MakeNewDir()
98 vX = sign( Game::getPlayer()->getMapX() - mapX );
99 vY = sign( Game::getPlayer()->getMapY() - mapY );
101 if( !isWalkable( mapX+vX, mapY+vY ) )
103 if( isWalkable( mapX, mapY+vY ) )
104 vX = 0;
105 else if( isWalkable( mapX+vX, mapY ) )
106 vY = 0;
107 else
110 for( Uint8 y = mapY-1; y <= mapY+1; y++ )
112 for( Uint8 x = mapX-1; x <= mapX+1; x++ )
114 if( isWalkable( x, y ) )
116 vX = sign( x - mapX );
117 vY = sign( y - mapY );
118 return;
122 Die();