forked from rlguy/boids
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboids_main.lua
199 lines (153 loc) · 5.66 KB
/
boids_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
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
math.randomseed(os.time())
for i=1,5 do math.random() end
local scrnum = 0
function love.keypressed(key, unicode)
if key == "escape" then
love.event.push('quit')
end
if (DEBUG or true) and key == '1' then
FREEZE = not FREEZE
end
BOIDS:keypressed(key)
if key == "p" then
local screenshot = love.graphics.newScreenshot()
screenshot:encode(tostring(scrnum)..".png")
scrnum = scrnum + 1
end
if key == "backspace" then
BOIDS:load_previous_state()
end
end
function love.keyreleased(key)
BOIDS:keyreleased(key)
end
function love.mousepressed(x, y, button)
local mpos = MOUSE_INPUT:get_position()
BOIDS:mousepressed(mpos.x, mpos.y, button)
if button == "l" and STATES.continue_button.bbox:contains_coordinate(mpos.x, mpos.y) then
love.keypressed("return")
end
end
function love.mousereleased(x, y, button)
local mpos = MOUSE_INPUT:get_position()
BOIDS:mousereleased(mpos.x, mpos.y, button)
end
function love.load(args)
-- GLOBALS -------------------------------------------------------------------
lg = love.graphics
lw = love.window
lf = love.filesystem
lk = love.keyboard
li = love.image
ARGS = args
DEBUG = true
FREEZE = false
SCR_WIDTH = args[1]
SCR_HEIGHT = args[2]
FULLSCREEN = args[3]
MOUSE_INPUT = nil
TILE_WIDTH = 16
TILE_HEIGHT = 16
CELL_WIDTH = 64 -- collider cell width
CELL_HEIGHT = 64
MAX_IMAGE_WIDTH = 2048 -- in pixels
MAX_IMAGE_HEIGHT = 2048
ACTIVE_AREA_WIDTH = 2920
ACTIVE_AREA_HEIGHT= 2080
RED, GREEN, BLUE, ALPHA = 1, 2, 3, 4
-- global assets
require("boids_utils")
require("boids_math")
require("table_utils")
local object_loader = require("object_loader")
object_loader.load_objects()
FONTS = require("font_loader")
MASTER_TIMER = master_timer:new()
MOUSE_INPUT = mouse_input:new()
MOUSE_INPUT:init()
-- construct state machine
local states = require('state_loader')
STATES = states
BOIDS = state_manager:new()
BOIDS:add_state(states.main_screen_load_state, "main_screen_load_state")
BOIDS:add_state(states.main_screen_state, "main_screen_state")
BOIDS:add_state(states.overview_screen_state, "overview_screen_state")
BOIDS:add_state(states.flockmates_screen_state, "flockmates_screen_state")
BOIDS:add_state(states.flockmates_demo_load_state, "flockmates_demo_load_state")
BOIDS:add_state(states.flockmates_demo_state, "flockmates_demo_state")
BOIDS:add_state(states.query_screen_state, "query_screen_state")
BOIDS:add_state(states.query_demo_load_state, "query_demo_load_state")
BOIDS:add_state(states.query_demo_state, "query_demo_state")
BOIDS:add_state(states.rules_screen_state, "rules_screen_state")
BOIDS:add_state(states.rule_alignment_screen_state, "rule_alignment_screen_state")
BOIDS:add_state(states.rule_cohesion_screen_state, "rule_cohesion_screen_state")
BOIDS:add_state(states.rule_separation_screen_state, "rule_separation_screen_state")
BOIDS:add_state(states.rules_demo_load_state, "rules_demo_load_state")
BOIDS:add_state(states.rules_demo_state, "rules_demo_state")
BOIDS:add_state(states.obstacle_screen_state, "obstacle_screen_state")
BOIDS:add_state(states.obstacle_demo_load_state, "obstacle_demo_load_state")
BOIDS:add_state(states.obstacle_demo_state, "obstacle_demo_state")
BOIDS:add_state(states.food_screen_state, "food_screen_state")
BOIDS:add_state(states.food_demo_load_state, "food_demo_load_state")
BOIDS:add_state(states.food_demo_state, "food_demo_state")
BOIDS:add_state(states.emitter_screen_state, "emitter_screen_state")
BOIDS:add_state(states.emitter_demo_load_state, "emitter_demo_load_state")
BOIDS:add_state(states.emitter_demo_state, "emitter_demo_state")
BOIDS:add_state(states.graph_screen_state, "graph_screen_state")
BOIDS:add_state(states.graph_demo_load_state, "graph_demo_load_state")
BOIDS:add_state(states.graph_demo_state, "graph_demo_state")
BOIDS:add_state(states.animation_screen_state, "animation_screen_state")
BOIDS:add_state(states.animation_demo_load_state, "animation_demo_load_state")
BOIDS:add_state(states.animation_demo_state, "animation_demo_state")
BOIDS:add_state(states.exit_screen_state, "exit_screen_state")
BOIDS:load_state("main_screen_load_state")
love.mouse.setVisible(false)
lg.setFont(FONTS.bebas_smallest)
local text = "Press [ enter ] to continue"
local tw, th = FONTS.bebas_smallest:getWidth(text), FONTS.bebas_smallest:getHeight(text)
local pad = 5
local x, y = SCR_WIDTH - tw - pad, SCR_HEIGHT - th - pad
lg.setColor(255, 255, 255, 100)
lg.print(text, x, y)
STATES.continue_button = {font = FONTS.bebas_smallest,
text = text,
bbox = bbox:new(x, y, tw+2*pad, th+2*pad),
color = {255, 255, 255, 255}}
end
function love.update(dt)
if FREEZE then
return
end
if love.keyboard.isDown('z') then dt = dt / 16 end
if love.keyboard.isDown('x') then dt = dt * 3 end
dt = math.min(dt, 1/20)
MASTER_TIMER:update(dt)
MOUSE_INPUT:update(dt)
BOIDS:update(dt)
local b = STATES.continue_button
local mx, my = MOUSE_INPUT:get_position():get_vals()
if b.bbox:contains_coordinate(mx, my) then
b.color[4] = 255
else
b.color[4] = 100
end
if lk.isDown('`') then -- tilde
DEBUG = true
else
DEBUG = false
end
end
function love.draw()
lg.setPointStyle("rough")
BOIDS:draw()
MOUSE_INPUT:draw()
lg.setFont(FONTS.courier_small)
if DEBUG then
lg.setColor(255, 0, 0, 255)
lg.print("FPS "..love.timer.getFPS(), 0, 0)
end
local b = STATES.continue_button
lg.setFont(b.font)
lg.setColor(b.color)
lg.print(b.text, b.bbox.x, b.bbox.y)
end