-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.lua
87 lines (81 loc) · 2.28 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
local base = require("base")
local game = setmetatable({}, {__index = base})
game.objects = require("objects")
game.terrain = require("terrain")
game.rain = require("rain")
game.world = love.physics.newWorld(0, 9.81 * 64, true)
require('lib/camera')
--[[ Callback ]]
function game:load()
local index, object
for index, object in pairs(self) do
if type(object) == "table" then
object:load()
end
end
self.world:setCallbacks(self.beginContact, self.endContact, self.preSolve, self.postSolve)--
self.objects:circle(self.world)
self.terrain.x = self.objects[1].fixture:getBody():getWorldPoint(self.objects[1].shape:getPoint())
self.terrain.world = self.world
self.rain.world = self.world
end
function game:update(dt)
local index, object
for index, object in pairs(self) do
if type(object) ~= "function" then
object:update(dt)
end
end
self.terrain.x = self.objects[1].fixture:getBody():getWorldPoint(self.objects[1].shape:getPoint())
self.rain.x = self.objects[1].fixture:getBody():getWorldPoint(self.objects[1].shape:getPoint())
camera:follow(self.objects[1].fixture:getBody(), dt)
end
function game:draw()
camera:set()
local index, object
for index, object in pairs(self) do
if type(object) == "table" then
object:draw()
end
end
camera:unset()
end
function game:mousepressed(x, y, button, isTouch, presses)
local index, object
x, y = camera:screenToLocal(x, y)
print("Click", x, y)
for index, object in pairs(self) do
if type(object) == "table" then
object:mousepressed(x, y, button, isTouch, presses)
end
end
if (button == 1) then
self.objects:waterCircle(self.world, x, y)
end
end
--[[ Collision ]]
function game.beginContact(a, b, col)
a = a:getUserData()
b = b:getUserData()
a:beginContact(b, col)
b:beginContact(a, col)
end
function game.endContact(a, b, col)
a = a:getUserData()
b = b:getUserData()
a:endContact(b, col)
b:endContact(a, col)
end
function game.preSolve(a, b, col)
a = a:getUserData()
b = b:getUserData()
a:preSolve(b, col)
b:preSolve(a, col)
end
function game.postSolve(a, b, col, normalImpulse, tangentImpulse)
a = a:getUserData()
b = b:getUserData()
a:postSolve(b, col, normalImpulse, tangentImpulse)
b:postSolve(a, col, normalImpulse, tangentImpulse)
end
return game