forked from querry43/rgbdisplay-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbug.cpp
More file actions
147 lines (118 loc) · 2.56 KB
/
bug.cpp
File metadata and controls
147 lines (118 loc) · 2.56 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <Arduino.h>
#include "LPD8806.h"
#include "SPI.h"
#include "display.h"
#include "bug.h"
#include "utils.h"
void bug_t::randomize_direction()
{
dx = random(3) - 1;
dy = random(3) - 1;
}
void bug_t::move()
{
if (random(100) >= PCT_TIME_BUG_MOVES_SAME_DIRECTION)
randomize_direction();
x += dx;
y += dy;
if (x >= GRID_SIZE) { x = GRID_SIZE - 1; dx = -1; }
if (y >= GRID_SIZE) { y = GRID_SIZE - 1; dy = -1; }
if (x < 0) { x = 0; dx = 1; }
if (y < 0) { y = 0; dy = 1; }
--satiation;
}
void bug_t::eat()
{
satiation += SATIATION_PROVIDED_BY_PLANT;
}
bool bug_t::is_alive()
{
return satiation > 0;
}
uint32_t bug_t::color()
{
uint32_t brightness = 8 * satiation;
if (brightness > 255)
brightness = 255;
return display.Color(brightness, 0, 0);
}
bug_t bug_t::reproduce()
{
satiation /= 2;
bug_t child = *this;
child.randomize_direction();
return child;
}
environment_t::environment_t()
: num_bugs(0)
{
add_initial_bug();
for (int i = 0; i < 10; ++i)
add_random_vegetation();
}
void environment_t::add_initial_bug()
{
add_bug(3, 3, 30);
}
void environment_t::add_bug(bug_t b)
{
bugs[num_bugs++] = b;
}
void environment_t::add_bug(int x, int y, int s)
{
bug_t b;
b.x = x;
b.y = y;
b.satiation = s;
b.randomize_direction();
add_bug(b);
}
void environment_t::add_random_vegetation()
{
vegetation[random(GRID_SIZE)][random(GRID_SIZE)] = true;
}
void environment_t::advance()
{
for (int i = 0; i < num_bugs; ++i) {
bug_t & bug = bugs[i];
bug.move();
if (vegetation[bug.x][bug.y]) {
vegetation[bug.x][bug.y] = false;
bug.eat();
if (bug.satiation >= BUG_REPRODUCE_AT_SATIATION) {
add_bug(bug.reproduce());
}
}
}
for (int i = 0; i < num_bugs; ++i) {
bug_t & bug = bugs[i];
if (! bug.is_alive()) {
bugs[i] = bugs[--num_bugs];
i = 0;
}
}
if (num_bugs == 0)
add_initial_bug();
if (random(100) <= PCT_TIME_NEW_VEGETATION)
add_random_vegetation();
}
void environment_t::show()
{
uint32_t vegetation_color = display.Color(0, 63, 0);
uint32_t background_color = display.Color(0, 0, 0);
for (int y = 0; y < GRID_SIZE; ++y) {
for (int x = 0; x < GRID_SIZE; ++x) {
uint32_t color = vegetation[x][y] ? vegetation_color : background_color;
for (int b = 0; b < num_bugs; ++b) {
bug_t & bug = bugs[b];
if (bug.x == x && bug.y == y)
color = bug.color();
}
display.setPixelColor(x, y, color);
}
}
display.show();
for (int b = 0; b < num_bugs; ++b) {
bug_t & bug = bugs[b];
}
}