-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.lua
106 lines (88 loc) · 2.73 KB
/
main.lua
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
-- Author : Wilhelm Chung - http://webjazz.blogspot.com
love.filesystem.require 'food.lua'
love.filesystem.require 'boid.lua'
love.filesystem.require 'roommates.lua'
love.filesystem.require 'status.lua'
function load()
math.randomseed(os.time())
num_boids = 25
num_foodstuff = 1
boids = {}
foodstuffs = {}
love.graphics.setLineWidth(2)
font = love.graphics.newFont(love.default_font, 14)
love.graphics.setFont(font)
for i = 1, num_boids do
boids[i] = Boid:new(math.random() * love.graphics.getWidth(),
math.random() * love.graphics.getHeight(),
math.random(30) - 15,
math.random(30) - 15)
end
for i = 1, num_foodstuff do
foodstuffs[i] = Food:new()
end
spatial_db = Roommates:new(love.graphics.getWidth(), love.graphics.getHeight(), 10, 10)
end
function update(dt)
-- updates the spatial data structure
for _, boid in ipairs(boids) do
--spatial_db:update(boid, boid.position.x, boid.position.y)
end
-- updates vectors for each boid
for _, boid in ipairs(boids) do
neighbors = boids -- spatial_db:neighbors(boid.position.x, boid.position.y, Boid.ATTRACTION_RADIUS)
boid:navigate(neighbors, foodstuffs)
boid:move(dt)
boid:animate(dt)
-- wrap boids in torus
torus(boid)
-- see if each food is isEaten
for _, food in ipairs(foodstuffs) do
food:isEaten(boid)
end
end
end
function draw()
draw_world_debug()
-- draw each food
for _, food in ipairs(foodstuffs) do
food:draw()
end
-- draw each boid
for i, boid in ipairs(boids) do
draw_boids_radius(boid)
boid:draw()
if i == 1 then
boid:draw_debug()
end
end
draw_boids_status(10, 20)
draw_radius_status(10, 600)
end
function mousepressed(x, y, button)
if button == love.mouse_left then
table.insert(foodstuffs, Food:new(x, y))
end
end
function keyreleased(key)
toggle_status_keys(key)
end
function toggle(variable)
if (variable == true) then
return false
else
return true
end
end
-- breaks encapsulation
function torus(boid)
if boid.position.x >= love.graphics.getWidth() then
boid.position = Vector:new(boid.position.x - love.graphics.getWidth(), boid.position.y)
elseif boid.position.x <= 0 then
boid.position = Vector:new(love.graphics.getWidth() + boid.position.x, boid.position.y)
elseif boid.position.y >= love.graphics.getHeight() then
boid.position = Vector:new(boid.position.x, boid.position.y - love.graphics.getHeight())
elseif boid.position.y <= 0 then
boid.position = Vector:new(boid.position.x, love.graphics.getHeight() + boid.position.y)
end
end