6 #include "globalfunc.h"
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
)
34 FILE *in
= fopen( fmap
, "r" );
38 fscanf( in
, "%s", (char*)tilefile
);
40 if( !tileset
.load( tilefile
) )
43 for( unsigned int y
= 0; y
< MAP_HEIGHT
; y
++ )
45 for( unsigned int x
= 0; x
< MAP_WIDTH
; x
++ )
51 fscanf( in
, "%d:%d ", &temp
.tileID
, &temp
.typeMask
);
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
];
80 Map::transformM2S( x
, y
, sx
, sy
);
81 if( dst( t
.movementX
, t
.movementY
, (float)sx
, (float)sy
) < 0.5 )
89 dirX
= sign( sx
- t
.movementX
) * Player::PLAYER_SPEED
;
90 dirY
= sign( sy
- t
.movementY
) * Player::PLAYER_SPEED
;
99 if( time(0) > t
.timer
)
111 for( unsigned int i
= 0; i
< robots
.size(); i
++ )
113 Robot
*r
= robots
[i
];
119 robots
.erase( robots
.begin() + i
);
124 for( unsigned int i
= 0; i
< objects
.size(); i
++ )
126 Object
*o
= objects
[i
];
130 if( o
->getLocation() != Object::LOC_GROUND
)
132 objects
.erase( objects
.begin() + i
);
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
];
152 tileset
.Draw( (Sint16
)t
.movementX
, (Sint16
)t
.movementY
, t
.tileID
);
154 else if( t
.acid
> 0 )
157 Map::transformM2S( (Uint8
)x
, (Uint8
)y
, sx
, sy
);
159 tileset
.Draw( sx
, sy
, 5+(4-t
.acid
) );
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
)
179 for( std::vector
<Robot
*>::iterator r
= robots
.begin(); r
!= robots
.end(); r
++ )
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
;
200 if( isBitSet( tiles
[toX
][toY
].typeMask
, Tile::TILE_NOTPUSHABLE
) ||
201 isBitSet( tiles
[toX
][toY
].typeMask
, Tile::TILE_WALL
) ||
202 isRobotOn( toX
, toY
) )
207 if( isBitSet( tiles
[toX
][toY
].typeMask
, Tile::TILE_PUSHABLE
) )
209 if(!PushPushable( toX
, toY
, dirX
, dirY
) )
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
);
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
)
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
)
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
) &&
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
)
284 void Map::insertObject( Object
*o
, Uint8 X
, Uint8 Y
)
286 o
->setLocation( Object::LOC_GROUND
);
289 objects
.push_back( o
);