-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamestates.lua2p
132 lines (112 loc) · 3.15 KB
/
gamestates.lua2p
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
local Concord = require("modules.concord.concord")
local Log = require("modules.log.log")
local Cache = require("cache")
local ECS = require("ecs")
local Preloader = require("preloader")
local Resources = require("resources")
local GameStates = {
type_id = "GameStates",
is_ready = false,
current_id = nil,
world = nil,
prev_id = nil,
prev_world = nil,
}
function GameStates.preload()
local resources = {
atlas = {},
images = {},
image_data = {},
array_images = {},
fonts = {},
}
local list = Resources.get_meta(GameStates.current_id)
!if _CACHED_PRELOAD then
Cache.manage_resources(resources, list, Resources.data)
!end
Resources.clean()
!if _CACHED_PRELOAD and _GLSL_NORMALS then
Resources.copy_array_images(resources)
!end
Log.info("Preloading: " .. GameStates.current_id)
Preloader.start(list, resources, function()
GameStates.start(resources)
end)
end
function GameStates.start(resources)
@@profb("gs_start")
Resources.set_resources(resources)
GameStates.is_ready = true
GameStates.world = Concord.world()
GameStates.world.current_id = GameStates.current_id
ECS.load_systems(GameStates.current_id, GameStates.world, GameStates.prev_id)
@@profb("state_setup")
GameStates.world:emit("state_setup")
@@profe("state_setup")
@@profb("state_init")
GameStates.world:emit("state_init")
@@profe("state_init")
!if _DEV then
local sc = ECS.get_state_class(GameStates.current_id)
local DevTools = require("devtools")
DevTools.camera = GameStates.world:getSystem(sc).camera
!end
@@profe("gs_start")
end
function GameStates.switch(next_id)
@@assert(type(next_id) == "string")
@@profb("gs_switch")
GameStates.is_ready = false
if GameStates.world then
GameStates.prev_world = GameStates.world
GameStates.prev_id = GameStates.current_id
GameStates.exit()
end
Log.info("Switching to:", next_id)
GameStates.current_id = next_id
@@profb("preload")
GameStates.preload()
@@profe("preload")
@@profe("gs_switch")
end
function GameStates.switch_to_previous()
GameStates.switch(GameStates.prev_id)
end
function GameStates.update(dt)
if not GameStates.is_ready then return end
GameStates.world:emit("state_update", dt)
end
function GameStates.draw()
if not GameStates.is_ready then return end
GameStates.world:emit("state_draw")
end
function GameStates.keypressed(key)
if not GameStates.is_ready then return end
GameStates.world:emit("state_keypressed", key)
end
function GameStates.keyreleased(key)
if not GameStates.is_ready then return end
GameStates.world:emit("state_keyreleased", key)
end
!if _DEV then
function GameStates.mousemoved(mx, my, dx, dy)
if not GameStates.is_ready then return end
GameStates.world:emit("state_mousemoved", mx, my, dx, dy)
end
function GameStates.mousepressed(mx, my, mb)
if not GameStates.is_ready then return end
GameStates.world:emit("state_mousepressed", mx, my, mb)
end
function GameStates.mousereleased(mx, my, mb)
if not GameStates.is_ready then return end
GameStates.world:emit("state_mousereleased", mx, my, mb)
end
!end
function GameStates.exit()
GameStates.world:emit("cleanup")
GameStates.world:clear()
for _, e in ipairs(GameStates.world:getEntities()) do
e:destroy()
end
end
return GameStates