-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.lua
68 lines (56 loc) · 1.51 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
local Map = {}
local STI = require("sti")
local Coin = require("coin")
local Spike = require("spike")
local Box = require("box")
local Enemy = require("enemy")
local Player = require("player")
function Map:load()
self.currentLevel = 1
World = love.physics.newWorld(0,2000)
World:setCallbacks(beginContact, endContact)
self:init()
end
function Map:init()
self.level = STI("map/level-"..self.currentLevel..".lua", {"box2d"})
self.level:box2d_init(World)
self.solidLayer = self.level.layers.solid
self.groundLayer = self.level.layers.ground
self.entityLayer = self.level.layers.entity
self.solidLayer.visible = false
self.entityLayer.visible = false
MapWidth = self.groundLayer.width * 16
self:spawnEntities()
end
function Map:next()
self:clean()
self.currentLevel = self.currentLevel + 1
self:init()
Player:resetPosition()
end
function Map:clean()
self.level:box2d_removeLayer("solid")
Coin.removeAll()
Enemy.removeAll()
Box.removeAll()
Spike.removeAll()
end
function Map:update()
if Player.x > MapWidth - 16 then
self:next()
end
end
function Map:spawnEntities()
for i,v in ipairs(self.entityLayer.objects) do
if v.type == "spikes" then
Spike.new(v.x + v.width / 2, v.y)
elseif v.type == "box" then
Box.new(v.x + v.width / 2, v.y + v.height / 2)
elseif v.type == "enemy" then
Enemy.new(v.x + v.width / 2, v.y + v.height / 2)
elseif v.type == "coin" then
Coin.new(v.x, v.y)
end
end
end
return Map