Skip to content

Commit 95491b3

Browse files
committed
Update reset_position method in class Entity.
Now it can be added to the middle of a tile. However, there is still a tiny chance that it may be added to a position outside the window.
1 parent 00a10d1 commit 95491b3

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

BombingAdventure/Classes/Entity.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ void Entity::bind_sprite(Sprite *sprite) {
3030
}
3131
void Entity::reset_position() {
3232
if(get_sprite() != NULL) {
33-
setPosition(Vec2(CCRANDOM_0_1() * 960, CCRANDOM_0_1() * 640));
33+
/* Generate a position in unit Tile */
34+
int target_tile_x = (int) floor(CCRANDOM_0_1() * MAP_SIZE.width);
35+
int target_tile_y = (int) floor(CCRANDOM_0_1() * MAP_SIZE.height);
36+
/* Set to the position in unit pixel, did not add robust part */
37+
setPosition(Vec2(target_tile_x * TILE_SIZE.width + TILE_SIZE.width / 2, target_tile_y * TILE_SIZE.height + TILE_SIZE.height / 2));
3438
}
3539
}
3640
Vec2 Entity::tileCoordFromPosition(Vec2 position)

0 commit comments

Comments
 (0)