Edited map, new efence tile
[sdlbotor.git] / Map.cpp
blobbcdd65e91ac1b65cfaede7e70ac74f87e5be3610
1 #include <cstdio>
3 #include "Map.h"
4 #include "Game.h"
5 #include "Tile.h"
6 #include "globalfunc.h"
7 #include "Objects.h"
9 namespace botor
12 Map::Map() : tiles( MAP_WIDTH, std::vector<Tile>( MAP_HEIGHT ) ), robots()
15 void Map::Initialize()
17 for( int i = 0; i < 10; i++ )
19 robots.push_back( new Robot( i+3, 12 ) );
21 robots.push_back( new Robot( 19, 14 ) );
23 objects.push_back( new OAcid( 5, 2 ) );
24 objects.push_back( new OAcid( 6, 2 ) );
25 objects.push_back( new OAcid( 7, 2 ) );
26 objects.push_back( new OAcid( 5, 3 ) );
28 objects.push_back( new OBlueman( 5,4 ) );
31 bool Map::load( const char *fmap )
33 char tilefile[255];
34 FILE *in = fopen( fmap, "r" );
35 if( !in )
36 return false;
38 fscanf( in, "%s", (char*)tilefile );
40 if( !tileset.load( tilefile ) )
41 return false;
43 for( unsigned int y = 0; y < MAP_HEIGHT; y++ )
45 for( unsigned int x = 0; x < MAP_WIDTH; x++ )
47 Tile temp;
48 temp.moving = false;
49 temp.acid = 0;
51 fscanf( in, "%d:%d ", &temp.tileID, &temp.typeMask );
53 tiles[x][y] = temp;
55 fscanf( in, "\n" );
58 fclose( in );
60 return true;
64 void Map::unload()
66 tileset.unload();
69 void Map::Update()
71 for( unsigned int x = 0; x < MAP_WIDTH; x++ )
73 for( unsigned int y = 0; y < MAP_HEIGHT; y++ )
75 Tile &t = tiles[x][y];
76 if( t.moving )
79 Sint16 sx,sy;
80 Map::transformM2S( x, y, sx, sy );
81 if( dst( t.movementX, t.movementY, (float)sx, (float)sy ) < 0.5 )
83 t.moving = false;
85 else
87 float dirX, dirY;
89 dirX = sign( sx - t.movementX ) * Player::PLAYER_SPEED;
90 dirY = sign( sy - t.movementY ) * Player::PLAYER_SPEED;
92 t.movementX += dirX;
93 t.movementY += dirY;
97 if( t.acid > 0 )
99 if( time(0) > t.timer )
101 t.acid--;
102 if(!t.acid)
103 RemoveTile(x,y);
104 t.timer = time(0)+1;
111 for( unsigned int i = 0; i < robots.size(); i++ )
113 Robot *r = robots[i];
115 r->Update();
117 if( !r->isAlive() )
119 robots.erase( robots.begin() + i );
120 delete r;
124 for( unsigned int i = 0; i < objects.size(); i++ )
126 Object *o = objects[i];
128 o->Update();
130 if( o->getLocation() != Object::LOC_GROUND )
132 objects.erase( objects.begin() + i );
138 void Map::Draw( )
141 for( unsigned int x = 0; x < MAP_WIDTH; x++ )
143 for( unsigned int y = 0; y < MAP_HEIGHT; y++ )
145 Tile &t = tiles[x][y];
147 if( t.tileID == -1 )
148 continue;
150 if( t.moving )
152 tileset.Draw( (Sint16)t.movementX, (Sint16)t.movementY, t.tileID );
154 else if( t.acid > 0 )
156 Sint16 sx,sy;
157 Map::transformM2S( (Uint8)x, (Uint8)y, sx, sy );
159 tileset.Draw( sx, sy, 5+(4-t.acid) );
162 else
164 Sint16 sx,sy;
165 Map::transformM2S( (Uint8)x, (Uint8)y, sx, sy );
166 tileset.Draw( sx, sy, tiles[x][y].tileID );
173 for( std::vector<Object*>::iterator o = objects.begin(); o != objects.end(); o++ )
175 if((*o)->getLocation() == Object::LOC_GROUND )
176 (*o)->Draw();
179 for( std::vector<Robot*>::iterator r = robots.begin(); r != robots.end(); r++ )
181 (*r)->Draw();
188 Tile *Map::tileAt( Uint8 mapX, Uint8 mapY )
190 return &tiles[mapX][mapY];
193 bool Map::PushPushable( Uint8 fromX, Uint8 fromY, Uint8 dirX, Uint8 dirY )
195 Uint8 toX = fromX+dirX;
196 Uint8 toY = fromY+dirY;
198 Sint16 posX, posY;
200 if( isBitSet( tiles[toX][toY].typeMask, Tile::TILE_NOTPUSHABLE ) ||
201 isBitSet( tiles[toX][toY].typeMask, Tile::TILE_WALL ) ||
202 isRobotOn( toX, toY ) )
204 return false;
207 if( isBitSet( tiles[toX][toY].typeMask, Tile::TILE_PUSHABLE ) )
209 if(!PushPushable( toX, toY, dirX, dirY ) )
211 return false;
215 Map::transformM2S( fromX, fromY, posX, posY );
217 tiles[toX][toY] = tiles[fromX][fromY];
218 tiles[toX][toY].moving = true;
219 tiles[toX][toY].movementX = (float)posX;
220 tiles[toX][toY].movementY = (float)posY;
222 RemoveTile( fromX, fromY );
224 return true;
228 void Map::RemoveTile( Uint8 mapX, Uint8 mapY )
230 tiles[mapX][mapY].typeMask = Tile::TILE_WALKABLE;
231 tiles[mapX][mapY].tileID = -1;
232 tiles[mapX][mapY].moving = false;
233 tiles[mapX][mapY].acid = 0;
236 void Map::transformM2S( Uint8 mapX, Uint8 mapY, Sint16 &x, Sint16 &y )
238 x = mapX*20;
239 y = mapY*20 + MAP_SHIFT;
242 bool Map::isRobotOn( Uint8 mapX, Uint8 mapY )
244 for( std::vector<Robot*>::iterator r = robots.begin(); r != robots.end(); r++ )
246 if( (*r)->getMapX() == mapX && (*r)->getMapY() == mapY )
247 return true;
249 return false;
252 void Map::Acid( Uint8 cX, Uint8 cY )
254 for( Uint8 y = cY-1; y <= cY+1; y++ )
256 for( Uint8 x = cX-1; x <= cX+1; x++ )
258 Tile &t = tiles[x][y];
259 if( isBitSet( t.typeMask, Tile::TILE_WALL ) &&
260 !isBitSet( t.typeMask, Tile::TILE_SOLID) &&
261 !t.acid )
263 t.acid = 4;
264 t.timer = time(0)+1;
270 Object *Map::getObject( Uint8 X, Uint8 Y )
272 for( std::vector<Object*>::iterator o = objects.begin(); o != objects.end(); o++ )
274 if((*o)->getLocation() == Object::LOC_GROUND )
276 if( (*o)->getMapX() == X && (*o)->getMapY() == Y )
277 return *o;
281 return 0;
284 void Map::insertObject( Object *o, Uint8 X, Uint8 Y )
286 o->setLocation( Object::LOC_GROUND );
287 o->Teleport( X, Y );
289 objects.push_back( o );