objekt hihi
[sdlbotor.git] / Robot.cpp
blob3574ce3ca21ba78b1e95f0cc5d5cabe1eebdc9e0
1 #include "Robot.h"
2 #include "globalfunc.h"
3 #include "Map.h"
4 #include "Game.h"
5 #include "DTile.h"
7 #include <cstdio>
8 #include <cstdlib>
10 namespace botor
13 Tileset Robot::ROBOTS_TILESET( "robots.png" );
15 Robot::Robot( Uint8 X, Uint8 Y ) : LivingMapObject( X, Y ),
16 ani_walking( 0, 3, 10 )
18 ani_player = new DAnimation( &ROBOTS_TILESET );
19 graphic = ani_player;
20 ani_player->load( &ani_walking );
22 walking = true;
25 MakeNewDir();
26 StartMovement();
29 float Robot::SPEED() { return ROBOT_SPEED; }
31 void Robot::StartMovement()
33 Sint16 sx,sy;
34 Map::transformM2S( mapX, mapY, sx, sy );
35 movementX = (float)sx;
36 movementY = (float)sy;
38 if( !isWalkable( mapX+vX, mapY+vY ) )
39 MakeNewDir();
41 mapX += vX;
42 mapY += vY;
43 if( isBitSet( Game::getMap()->tileAt( mapX, mapY )->typeMask, Tile::TILE_SLOW ) )
44 speed = SPEED() * 0.5f;
45 else
46 speed = SPEED();
48 moving = true;
52 bool Robot::isWalkable( Uint8 X, Uint8 Y )
54 Tile *t = Game::getMap()->tileAt(X, Y);
55 if(
56 (isBitSet( t->typeMask, Tile::TILE_WALKABLE ) ||
57 isBitSet( t->typeMask, Tile::TILE_EFENCE )) &&
58 !Game::getMap()->isRobotOn(X, Y)
61 if( isBitSet( t->typeMask, Tile::TILE_EFENCE ) )
63 return rand()%4 == 0;
66 return true;
68 else
69 return false;
72 void Robot::OnTile( Tile *t )
74 if( isBitSet( t->typeMask, Tile::TILE_EFENCE ) )
76 Game::getMap()->RemoveTile( mapX, mapY );
77 Die();
80 if( mapX == Game::getPlayer()->getMapX() &&
81 mapY == Game::getPlayer()->getMapY() )
83 if( Game::getPlayer()->Walking() )
85 // if( rand()%3 )
86 // {
87 Game::getPlayer()->Die();
88 // }
90 else
91 Game::getPlayer()->Die();
93 if( rand()%4 == 0 )
94 Die();
97 MakeNewDir();
100 void Robot::MakeNewDir()
102 vX = sign( Game::getPlayer()->getMapX() - mapX );
103 vY = sign( Game::getPlayer()->getMapY() - mapY );
105 if( !isWalkable( mapX+vX, mapY+vY ) )
107 if( isWalkable( mapX, mapY+vY ) )
108 vX = 0;
109 else if( isWalkable( mapX+vX, mapY ) )
110 vY = 0;
111 else
114 for( Uint8 y = mapY-1; y <= mapY+1; y++ )
116 for( Uint8 x = mapX-1; x <= mapX+1; x++ )
118 if( isWalkable( x, y ) )
120 vX = sign( x - mapX );
121 vY = sign( y - mapY );
122 return;
126 Die();