3 #include "globalfunc.h"
15 Map::Map() : tiles( MAP_WIDTH
, std::vector
<Tile
>( MAP_HEIGHT
) ), robots(), objects()
18 void Map::Initialize()
21 lua_State
*L
= lua_newthread( Game::LUA
);
23 int s
= luaL_loadfile( L
, "map.lua" );
27 s
= lua_pcall( L
, 0, 0, 0 ); //run file
30 std::cerr
<< "Error loading Map:" << std::endl
31 << lua_tostring( L
, -1 ) << std::endl
;
37 std::cerr
<< "Error loading file: map.lua:" << std::endl
;
43 bool Map::load( const char *fmap
)
46 FILE *in
= fopen( fmap
, "r" );
50 fscanf( in
, "%s", (char*)tilefile
);
52 if( !tileset
.load( tilefile
) )
55 for( unsigned int y
= 0; y
< MAP_HEIGHT
; y
++ )
57 for( unsigned int x
= 0; x
< MAP_WIDTH
; x
++ )
63 fscanf( in
, "%d:%d ", &temp
.tileID
, &temp
.typeMask
);
83 for( unsigned int x
= 0; x
< MAP_WIDTH
; x
++ )
85 for( unsigned int y
= 0; y
< MAP_HEIGHT
; y
++ )
87 Tile
&t
= tiles
[x
][y
];
92 Map::transformM2S( x
, y
, sx
, sy
);
93 if( dst( t
.movementX
, t
.movementY
, (float)sx
, (float)sy
) < 0.5 )
101 dirX
= sign( sx
- t
.movementX
) * Player::PLAYER_SPEED
;
102 dirY
= sign( sy
- t
.movementY
) * Player::PLAYER_SPEED
;
111 if( time(0) > t
.timer
)
123 for( unsigned int i
= 0; i
< robots
.size(); i
++ )
125 Robot
*r
= robots
[i
];
131 robots
.erase( robots
.begin() + i
);
136 for( unsigned int i
= 0; i
< objects
.size(); i
++ )
138 Object
*o
= objects
[i
];
142 if( o
->getLocation() != Object::LOC_GROUND
)
144 objects
.erase( objects
.begin() + i
);
153 for( unsigned int x
= 0; x
< MAP_WIDTH
; x
++ )
155 for( unsigned int y
= 0; y
< MAP_HEIGHT
; y
++ )
157 Tile
&t
= tiles
[x
][y
];
164 tileset
.Draw( (Sint16
)t
.movementX
, (Sint16
)t
.movementY
, t
.tileID
);
166 else if( t
.acid
> 0 )
169 Map::transformM2S( (Uint8
)x
, (Uint8
)y
, sx
, sy
);
171 tileset
.Draw( sx
, sy
, 5+(4-t
.acid
) );
177 Map::transformM2S( (Uint8
)x
, (Uint8
)y
, sx
, sy
);
178 tileset
.Draw( sx
, sy
, tiles
[x
][y
].tileID
);
185 for( std::vector
<Object
*>::iterator o
= objects
.begin(); o
!= objects
.end(); o
++ )
190 for( std::vector
<Robot
*>::iterator r
= robots
.begin(); r
!= robots
.end(); r
++ )
199 Tile
*Map::tileAt( Uint8 mapX
, Uint8 mapY
)
201 return &tiles
[mapX
][mapY
];
204 bool Map::PushPushable( Uint8 fromX
, Uint8 fromY
, Uint8 dirX
, Uint8 dirY
)
206 Uint8 toX
= fromX
+dirX
;
207 Uint8 toY
= fromY
+dirY
;
211 if( isBitSet( tiles
[toX
][toY
].typeMask
, Tile::TILE_NOTPUSHABLE
) ||
212 isBitSet( tiles
[toX
][toY
].typeMask
, Tile::TILE_WALL
) ||
213 isRobotOn( toX
, toY
) ||
214 getObject( toX
, toY
) )
219 if( isBitSet( tiles
[toX
][toY
].typeMask
, Tile::TILE_PUSHABLE
) )
221 if(!PushPushable( toX
, toY
, dirX
, dirY
) )
227 Map::transformM2S( fromX
, fromY
, posX
, posY
);
229 tiles
[toX
][toY
] = tiles
[fromX
][fromY
];
230 tiles
[toX
][toY
].moving
= true;
231 tiles
[toX
][toY
].movementX
= (float)posX
;
232 tiles
[toX
][toY
].movementY
= (float)posY
;
234 RemoveTile( fromX
, fromY
);
240 void Map::RemoveTile( Uint8 mapX
, Uint8 mapY
)
242 tiles
[mapX
][mapY
].typeMask
= Tile::TILE_WALKABLE
;
243 tiles
[mapX
][mapY
].tileID
= -1;
244 tiles
[mapX
][mapY
].moving
= false;
245 tiles
[mapX
][mapY
].acid
= 0;
248 void Map::transformM2S( Uint8 mapX
, Uint8 mapY
, Sint16
&x
, Sint16
&y
)
251 y
= mapY
*20 + MAP_SHIFT
;
254 bool Map::isRobotOn( Uint8 mapX
, Uint8 mapY
)
256 for( std::vector
<Robot
*>::iterator r
= robots
.begin(); r
!= robots
.end(); r
++ )
258 if( (*r
)->getMapX() == mapX
&& (*r
)->getMapY() == mapY
)
264 void Map::Acid( Uint8 cX
, Uint8 cY
)
266 for( Uint8 y
= cY
-1; y
<= cY
+1; y
++ )
268 for( Uint8 x
= cX
-1; x
<= cX
+1; x
++ )
270 Tile
&t
= tiles
[x
][y
];
271 if( isBitSet( t
.typeMask
, Tile::TILE_WALL
) &&
272 !isBitSet( t
.typeMask
, Tile::TILE_SOLID
) &&
282 Object
*Map::getObject( Uint8 X
, Uint8 Y
)
284 for( std::vector
<Object
*>::iterator o
= objects
.begin(); o
!= objects
.end(); o
++ )
286 if((*o
)->getLocation() == Object::LOC_GROUND
)
288 if( (*o
)->getMapX() == X
&& (*o
)->getMapY() == Y
)
296 void Map::insertObject( Object
*o
, Uint8 X
, Uint8 Y
)
298 o
->setLocation( Object::LOC_GROUND
);
301 objects
.push_back( o
);
304 void Map::insertRobot( Uint8 X
, Uint8 Y
)
306 robots
.push_back( new Robot( X
, Y
) );