-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.lua
157 lines (144 loc) · 4.09 KB
/
map.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
local mapdata = require "mapdata"
local item = require "item"
local checkpoint = require "checkpoint"
local map = {}
-- newPolygonShape expects 3~8 vertices
function polylineToShape(polylines)
local vertices = {}
for _, polyline in ipairs(polylines) do
table.insert(vertices, polyline.x)
table.insert(vertices, -polyline.y)
end
return love.physics.newChainShape(false, vertices)
end
function rotated(x, y, angle)
local nx = x * math.cos(angle) - y * math.sin(angle)
local ny = x * math.sin(angle) + y * math.cos(angle)
return nx, ny
end
local wall = {}
function wall:getType()
return self.type
end
function map.new(world)
local platform = nil
local startPoint = nil
local items = {}
local origItems = {}
local checkpoints = {}
local texts = {}
for i, layer in ipairs(mapdata.layers) do
if layer.name == "platform" then
local bodies = {}
kills = {}
for i2, obj in ipairs(layer.objects) do
if obj.shape == "rectangle" then
local angle = -math.rad(obj.rotation)
local offx, offy = rotated(obj.width / 2, -obj.height / 2, angle)
local body = love.physics.newBody(world, obj.x + offx, -obj.y + offy, "static")
local shape = love.physics.newRectangleShape(0, 0, obj.width, obj.height, angle)
local fixture = love.physics.newFixture(body, shape, 1)
if obj.type == "kill" then
ud = setmetatable({type = "K"}, {__index = wall})
fixture:setUserData(ud)
table.insert(kills, body)
elseif obj.type == "start" then
ud = setmetatable({type = "S"}, {__index = wall})
fixture:setUserData(ud)
fixture:setSensor(true)
elseif obj.type == "goal" then
ud = setmetatable({type = "G"}, {__index = wall})
fixture:setUserData(ud)
fixture:setSensor(true)
else
table.insert(bodies, body)
end
elseif obj.shape == "point" and obj.type == "entry" then
startPoint = {x = obj.x, y = -obj.y}
elseif obj.shape == "point" and obj.type == "itemJump" then
table.insert(origItems, obj)
elseif obj.shape == "point" and obj.type == "itemDash" then
table.insert(origItems, obj)
elseif obj.shape == "point" and obj.type == "checkpoint" then
table.insert(checkpoints, checkpoint.new(world, obj.x, -obj.y))
elseif obj.shape == "text" then
table.insert(texts, obj)
end
end
platform = {
bodies = bodies,
kills = kills,
items = items,
origItems = origItems,
checkpoints = checkpoints,
texts = texts
}
end
end
return setmetatable(
{
world = world,
platform = platform,
startPoint = startPoint,
resetRequired = true
},
{__index = map}
)
end
function map:getStartPoint()
return self.startPoint.x, self.startPoint.y
end
function map:update()
if self.resetRequired then
self.resetRequired = false
for _, item in ipairs(self.platform.items) do
item:consume()
end
for _, obj in ipairs(self.platform.origItems) do
local type = nil
if obj.type == "itemJump" then
type = "J"
elseif obj.type == "itemDash" then
type = "D"
end
table.insert(self.platform.items, item.new(self.world, obj.x, -obj.y, type))
end
end
local i = 1
while i <= #self.platform.items do
local item = self.platform.items[i]
if item:isConsumed() then
item:destroy()
table.remove(self.platform.items, i)
else
i = i + 1
end
end
end
function map:render()
for _, body in ipairs(self.platform.bodies) do
for _, fixture in ipairs(body:getFixtures()) do
love.graphics.polygon("fill", body:getWorldPoints(fixture:getShape():getPoints()))
end
end
love.graphics.setColor(0xff, 0x00, 0x00, 0xff)
for _, body in ipairs(self.platform.kills) do
for _, fixture in ipairs(body:getFixtures()) do
love.graphics.polygon("fill", body:getWorldPoints(fixture:getShape():getPoints()))
end
end
love.graphics.setColor(0xff, 0xff, 0xff, 0xff)
for _, item in ipairs(self.platform.items) do
item:render()
end
for _, c in ipairs(self.platform.checkpoints) do
c:render()
end
for _, text in ipairs(self.platform.texts) do
love.graphics.print(text.text, text.x, -text.y, 0, 4, -4)
end
end
function map:resetItems()
self.resetRequired = true
end
return map