-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLevel.lua
More file actions
99 lines (87 loc) · 1.96 KB
/
Copy pathLevel.lua
File metadata and controls
99 lines (87 loc) · 1.96 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
require "oop"
require "physics"
require "render"
require "hud"
require "State"
require "Vector"
require "Tile"
require "Billboard"
Level = subclass(State) do
local base = Level
function base:init(width, height)
self.domain = physics.Domain(width, height)
end
function base:onenter()
love.audio.stop()
Level.current = self
physics.Domain.setcurrent(self.domain)
end
function base:update(dt)
hud:update(dt)
self.domain:update(dt)
if player.dead then
player:update(dt)
end
love.audio.setOrientation(-camera.dir.x, -camera.dir.y, 0, 0, 0, 1)
love.audio.setPosition(camera.pos:xy())
end
function base:draw()
love.graphics.clear()
camera:render()
player:render()
for i = 1, #self.domain.entities do
local entity = self.domain.entities[i]
if not entity:isinactive() and entity.render then
entity:render()
end
end
render:draw()
hud:draw()
end
function base:mousepressed(x, y, button)
return player:mousepressed(x, y, button)
end
function base:keypressed(key)
return player:keypressed(key)
end
function Level.load(map, key)
if type(map) == "string" then
map = love.image.newImageData(map)
end
local w, h = map:getDimensions()
local level = Level(w, h)
local pallete = {}
for i, obj in pairs(key) do
local pixel = {map:getPixel(i - 1, 0)}
pallete[pixel] = obj
end
local yoffset = math.floor(#key / w) + 1
h = h - yoffset
for x = 0, w - 1 do
for y = 0, h - 1 do
local pixel = {map:getPixel(x, y + yoffset)}
local obj = nil
for c, o in pairs(pallete) do
if color.equals(pixel, c, true) then
obj = o
break
end
end
if type(obj) == "function" then
obj = obj(x, y, level)
end
if type(obj) == "table" then
if instanceof(obj, Tile) then
level.domain:set(x, y, obj)
end
if instanceof(obj, physics.Entity) then
level.domain:addentity(obj)
end
else
level.domain:set(x, y, obj)
end
end
end
return level
end
end