-
Notifications
You must be signed in to change notification settings - Fork 0
/
creature.go
206 lines (132 loc) · 3.39 KB
/
creature.go
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import "math"
import "math/rand"
const (
offset_x = 0
offset_y = 1
offset_dx = 2
offset_dy = 3
offset_θ = 4
offset_e = 5
offset_r = 6
offset_red = 7
offset_green = 8
offset_blue = 9
)
type Creature struct {
// The fields for this struct are stored in an array instead
// of individual fields so that it is easier to do matrix
// multiplication with them.
//
// Conceptually, this is what is in there:
//
// x,y float64 // Where the creature is.
// dx,dy float64 // The velocity of the creature.
// θ float64 // The angle the creature is facing. (In radians.)
//
// e float64 // The energy.
//
// r float64 // The radius of the creature. (The creature is basically a circle.)
// red,green,blue float64 // The color of the creature.
//
data [10]float64
name string
}
func (me *Creature) X() float64 {
return me.data[offset_x]
}
func (me *Creature) SetX(x float64) {
me.data[offset_x] = x
}
func (me *Creature) Y() float64 {
return me.data[offset_y]
}
func (me *Creature) SetY(y float64) {
me.data[offset_y] = y
}
func (me *Creature) Dx() float64 {
return me.data[offset_dx]
}
func (me *Creature) SetDx(dx float64) {
me.data[offset_dx] = dx
}
func (me *Creature) Dy() float64 {
return me.data[offset_dy]
}
func (me *Creature) SetDy(dy float64) {
me.data[offset_dy] = dy
}
// Speed2 returns the speed squared of the creature.
func (me *Creature) Speed2() float64 {
speed2 := me.Dx() * me.Dx() + me.Dy() * me.Dy()
return speed2
}
func (me *Creature) θ() float64 {
return me.data[offset_θ]
}
func (me *Creature) Setθ(theta float64) {
me.data[offset_θ] = theta
}
func (me *Creature) E() float64 {
return me.data[offset_e]
}
func (me *Creature) R() float64 {
return me.data[offset_r]
}
func (me *Creature) Red() float64 {
return me.data[offset_red]
}
func (me *Creature) Green() float64 {
return me.data[offset_green]
}
func (me *Creature) Blue() float64 {
return me.data[offset_blue]
}
func (me *Creature) Mass() float64 {
mass := me.R() * me.R() * math.Pi
// For the density.
mass *= 3
return mass
}
func NewCreature(name string) (*Creature) {
me := Creature{
name: name,
}
return &me
}
func (me *Creature) Randomize() (*Creature) {
me.data[offset_red] = 0.5 + rand.Float64()*0.5
me.data[offset_green] = 0.5 + rand.Float64()*0.5
me.data[offset_blue] = 0.5 + rand.Float64()*0.5
me.data[offset_r] = 10 + rand.Float64()*30
return me
}
func (me *Creature) RandomlyPlace(width, height float64) (*Creature) {
me.data[offset_x] = rand.Float64()*width
me.data[offset_y] = rand.Float64()*height
me.data[offset_dx] = rand.Float64()*2 - 1
me.data[offset_dy] = rand.Float64()*2 - 1
me.data[offset_θ] = rand.Float64()*2*math.Pi
if 2*math.Pi == me.data[offset_θ] {
me.data[offset_θ] = 0
}
return me
}
func (me *Creature) Next(width, height float64) {
// Update position.
me.data[offset_x] += me.Dx()
me.data[offset_y] += me.Dy()
//@TODO: If the creature is moving fast enough, this won't work!
if (0 > me.data[offset_x]) {
me.data[offset_x] += width
} else if (width < me.data[offset_x]) {
me.data[offset_x] -= width
}
if (0 > me.data[offset_y]) {
me.data[offset_y] += height
} else if (height < me.data[offset_y]) {
me.data[offset_y] -= height
}
// Decrease energy.
me.data[offset_e] -= 1
}