Merge branch 'lua' of nslu34:disk/projects/SDLBotor into lua
[sdlbotor.git] / Robot.cpp
blob3e6dd8309bb20e90be7d99fd454a68b161b56f8a
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 if( Game::getPlayer()->Walking() )
89 // if( rand()%3 )
90 // {
91 Game::getPlayer()->Die();
92 // }
94 else
95 Game::getPlayer()->Die();
97 if( rand()%4 == 0 )
98 Die();
101 MakeNewDir();
104 void Robot::MakeNewDir()
106 vX = sign( Game::getPlayer()->getMapX() - mapX );
107 vY = sign( Game::getPlayer()->getMapY() - mapY );
109 if( !isWalkable( mapX+vX, mapY+vY ) )
111 if( isWalkable( mapX, mapY+vY ) )
112 vX = 0;
113 else if( isWalkable( mapX+vX, mapY ) )
114 vY = 0;
115 else
118 for( Uint8 y = mapY-1; y <= mapY+1; y++ )
120 for( Uint8 x = mapX-1; x <= mapX+1; x++ )
122 if( isWalkable( x, y ) )
124 vX = sign( x - mapX );
125 vY = sign( y - mapY );
126 return;
130 Die();