Debian Codegraph @

dokidoki/actor_scene.lua

raw symbols SCIP
1 require "dokidoki.module" [[make_actor_scene]]
2
3 require 'glfw'
4 import(require 'gl')
5 import(require 'glu')
6
7 local kernel = require "dokidoki.kernel"
8 local graphics = require "dokidoki.graphics"
9 local v2 = require "dokidoki.v2"
10 import(require "dokidoki.base")
11
12 ---- Core Game Behaviour ------------------------------------------------------
13
14 function make_actor_scene (update_methods, draw_methods, init)
15 local actor_interface
16
17 local actors_by_tag = {}
18 local actors_by_method
19 local actor_timings = {}
20
21 local paused = false
22
23 local key_states = {}
24 local old_key_states = {}
25
26 ---- Actor Interface --------------------------------------------------------
27
28 -- actors have access to these functions
29
30 local function add_actor (actor)
31 for method, t in pairs(actors_by_method) do
32 if actor[method] then t[#t+1] = actor end
33 end
34 for _, tag in ipairs(actor.tags or {}) do
35 actors_by_tag[tag] = actors_by_tag[tag] or {}
36 table.insert(actors_by_tag[tag], actor)
37 end
38 actor_timings[actor] = 0
39 end
40
41 local function is_key_down (key)
42 return not not key_states[key]
43 end
44
45 local function was_key_pressed (key)
46 return key_states[key] and not old_key_states[key]
47 end
48
49 local function get_actors_by_tag (tag)
50 return actors_by_tag[tag] or {}
51 end
52
53 local function get_actor_timing_information ()
54 return actor_timings
55 end
56
57 actor_interface =
58 {
59 add_actor = add_actor,
60 is_key_down = is_key_down,
61 was_key_pressed = was_key_pressed,
62 get_actors_by_tag = get_actors_by_tag,
63 }
64
65 ---- Event Handling ---------------------------------------------------------
66
67 local function handle_event (event)
68 -- Quit
69 if event.type == 'quit' or
70 event.type == 'key' and event.is_down and event.key == glfw.KEY_ESC then
71 kernel.abort_main_loop()
72 for k, v in pairs(actor_timings) do
73 log.log_message(v .. '\t' .. k .. '\t' .. k.pos)
74 end
75 -- Other key events
76 elseif event.type == 'key' and event.is_down and
77 event.key == ('P'):byte() then
78 paused = not paused
79 elseif event.type == 'key' then
80 key_states[event.key] = event.is_down or nil
81 end
82 end
83
84 ---- Game Updates -----------------------------------------------------------
85
86 local function update (dt)
87 if init then
88 init(actor_interface)
89 init = false
90 end
91
92 if not paused then
93 -- update all actors
94 for _, update_type in ipairs(update_methods) do
95 for _, a in ipairs(actors_by_method[update_type]) do
96 local time = glfw.GetTime()
97 if not a.is_dead then a[update_type]() end
98 actor_timings[a] = actor_timings[a] + glfw.GetTime() - time
99 end
100 end
101
102 for k, _ in pairs(actors_by_method) do
103 actors_by_method[k] =
104 ifilter(function (a) return not a.is_dead end, actors_by_method[k])
105 end
106 for k, _ in pairs(actors_by_tag) do
107 actors_by_tag[k] =
108 ifilter(function (a) return not a.is_dead end, actors_by_tag[k])
109 end
110 for k, _ in pairs(actor_timings) do
111 if k.is_dead then actor_timings[k] = nil end
112 end
113
114 old_key_states = key_states
115 key_states = copy(old_key_states)
116 end
117 end
118
119 ---- Drawing ----------------------------------------------------------------
120
121 local function draw ()
122 for _, draw_type in ipairs(draw_methods) do
123 for _, a in ipairs(actors_by_method[draw_type]) do
124 assert(not a.is_dead)
125 local do_transform = a.pos
126 if do_transform then
127 glPushMatrix()
128 glTranslated(a.pos.x, a.pos.y, 0)
129 end
130 a[draw_type]()
131 if do_transform then
132 glPopMatrix()
133 end
134 end
135 end
136 end
137
138 ---- Init -------------------------------------------------------------------
139
140 actors_by_method = {}
141 for _, method in ipairs(iconcat(update_methods, draw_methods)) do
142 actors_by_method[method] = {}
143 end
144 -- scene interface for main loop
145 return { handle_event = handle_event, update = update, draw = draw }
146 end
147
148 return get_module_exports()