-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel.lua
More file actions
130 lines (107 loc) · 2.8 KB
/
Copy pathlevel.lua
File metadata and controls
130 lines (107 loc) · 2.8 KB
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
local ldtk = require 'lib.ldtk'
local World = require("core.World")
---@class Level
---@field loaded boolean
---@field world World|nil
---@field entities table<Entity>
---@field ui_elements table<any>
---@field player Player
---@field LayerGround ldtk.Layer|nil
local level = {
world = nil,
entities = {},
ui_elements = {},
loaded = false,
LayerGround = nil,
}
function ldtk.onEntity(entity)
local id = entity.id
local entityClassName = "entities." .. id
local status, entityClass = pcall(require, entityClassName)
if status and entityClass then
local e = entityClass:new(entity, entity.x, entity.y)
if id == "Player" then
level.player = e
end
table.insert(level.entities, e)
else
print("Unknown entity type: " .. id)
end
end
function ldtk.onLayer(layer)
-- A new layer is created.
if layer.id == "Ground" then
level.LayerGround = layer
level.world:addGroundLayer(layer)
elseif layer.id == "Background" then
layer.color = { 0.6, 0.6, 0.6, 1 }
level.LayerBackground = layer
else
print("Unknown layer type: " .. layer.id)
end
end
function ldtk.onLevelLoaded(ldtkLevel)
-- Current level is about to be changed.
level.loaded = false
level.entities = {}
level.ui_elements = {}
level.player = nil
level.world = World:new(ldtkLevel)
end
function ldtk.onLevelCreated(ldtkLevel)
-- Current level has been created and is ready to be used.
print("Level created: " .. ldtkLevel.id)
table.insert(level.ui_elements, require("ui.HUD"):new(level.player))
level.loaded = true
end
function level:Draw()
if self.LayerGround then
self.LayerGround:draw()
end
for _, e in ipairs(self.entities) do
-- Draw non-player entities first
if e ~= self.player and e.draw then
e:draw()
end
end
-- Draw player last so it appears on top
if self.player and self.player.draw then
self.player:draw()
end
Debug:draw(self.world.physics)
for _, ui in ipairs(self.ui_elements) do
if ui.draw then
ui:draw()
end
end
end
function level:Update(dt)
if self.world == nil then
return
end
for _, entity in ipairs(self.entities) do
entity:update(dt)
end
for _, ui in ipairs(self.ui_elements) do
if ui.update then
ui:update(dt)
end
end
self.world.physics:update(dt)
Debug:update(dt)
end
function level:keypressed(key)
for _, entity in ipairs(self.entities) do
if entity.keypressed then
entity:keypressed(key)
end
end
end
function level:load(levelName)
Helpers.codeLeft = true
ldtk:level(levelName)
end
function level:reload()
ldtk:reload()
end
return level