-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.lua
153 lines (117 loc) · 3.19 KB
/
game.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
Loader = require "AdvTiledLoader/Loader"
Loader.path = "maps/"
Camera = require "hump.camera"
require "globals"
require "colortile"
require "player"
local player
local allSolidTiles
game = {}
function game:enter()
love.graphics.setBackgroundColor( 0, 0, 0, 255 )
map = Loader.load("level5.tmx")
world = love.physics.newWorld(0, 200, true)
world:setCallbacks(beginContact, endContact, preSolve, postSolve)
translateToCenterX = love.graphics:getWidth() / 2 - map.width*Globals.getInstance().getTileSize() / 2
translateToCenterY = love.graphics:getHeight() / 2 - map.height*Globals.getInstance().getTileSize() / 2
allSolidTiles, startX, startY = findTiles(map)
player = Player.create(startX, startY)
cam = Camera(startX, startY)
cam:zoomTo(4)
end
function gameWon()
Gamestate.switch(win)
end
function beginContact(a, b, coll)
local playerObject = nil
local tileObject = nil
if a:getUserData() == "player" then
playerObject = a
tileObject = b
elseif b:getUserData() == "player" then
playerObject = b
tileObject = a
else
return
end
x, y = playerObject:getBody():getLinearVelocity()
if playerObject:getBody():getY() < tileObject:getBody():getY() and y >= 0 then
player.onAir = false
end
if tileObject:getCategory() == 2 then
gameWon()
end
if tileObject:getCategory() == 7 then
player.deleting = true
return
end
end
function endContact(a, b, coll)
end
function preSolve(a, b, coll)
end
function postSolve(a, b, coll, normalimpulse1, tangentimpulse1, normalimpulse2, tangentimpulse2)
end
function adoption(f)
if player.adopt then
if f:getUserData() ~= "player" then
local category = f:getCategory()
if category > 2 and category <= 5 then
player.adopt = false
player:setMask(category)
return 0
end
end
end
return 1
end
function game:update(dt)
handleInput(dt)
local alive, x, y = player:update()
if not alive then
player = Player.create(x, y)
end
if player.adopt then
local coord1, coord2, coord3, coord4 = player:getAABB()
world:rayCast(coord1, coord2, coord3, coord4, adoption)
end
local posX, posY = player:getPosition()
cam:lockPosition(posX, posY)
world:update(dt)
end
function game:draw()
cam:attach()
map:draw()
player:draw()
cam:detach()
end
function handleInput(dt)
player:handleInput(dt)
if love.keyboard.isDown("escape") then
Gamestate.switch(menu)
end
end
function findTiles(map)
local colorTiles = {}
local ground_types = {"start", "finish", "red", "green", "blue", "gray", "dark_gray"}
for i, ground_type in ipairs(ground_types) do
local layer = map.tl["map"]
for tileX=1,map.width do
for tileY=1,map.height do
local tile
if layer.tileData[tileY] then
tile = map.tiles[layer.tileData[tileY][tileX]]
end
if tile and tile.properties[ground_type] then
if ground_type == "start" then
startX = (tileX-1) * Globals.getInstance().getTileSize()
startY = (tileY-1) * Globals.getInstance().getTileSize()
end
local tile = ColorTile.create((tileX-1)*Globals.getInstance().getTileSize(), (tileY-1)*Globals.getInstance().getTileSize(), ground_type, i)
table.insert(colorTiles, tile)
end
end
end
end
return colorTiles, startX, startY
end