-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotspawnrule.cpp
41 lines (35 loc) · 884 Bytes
/
dotspawnrule.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <rainbowduino.h>
#include "coordinate.h"
#include "dot.h"
#include "dotspawnrule.h"
#include "game.h"
#include "snake.h"
#include "world.h"
DotSpawnRule::DotSpawnRule() : m_spawn_delay(32)
{
}
void DotSpawnRule::execute(Game* game)
{
if (game->dot.is_spawned() || game->state != Game::Running) {
return;
}
if (m_spawn_delay == 0) {
m_spawn_delay = random(64, 256);
place_dot(game);
}
m_spawn_delay--;
}
/**
* Places a new dot on an unoccupied space.
*/
void DotSpawnRule::place_dot(Game* game)
{
Coordinate possible_dot;
do {
possible_dot.z = random(0, 4);
possible_dot.x = random(0, 4);
possible_dot.y = random(0, 4);
} while(game->world.get_entity(possible_dot) != World::Empty);
game->dot.spawn(possible_dot);
game->world.set_entity(possible_dot, World::Berry);
}