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
|
-- `Ground`
-- Static platform physical object with a sprite. `Players` can walk on it.
-- Collision category: [1]
-- WHOLE CODE HAS FLAG OF "need a cleanup"
-- Metatable of `Ground`
-- nils initialized in constructor
Ground = {
body = nil,
shape = nil,
fixture = nil,
world = nil,
sprite = nil
}
-- Constructor of `Ground`
function Ground:new (game, world, x, y, shape, sprite)
local o = {}
setmetatable(o, self)
self.__index = self
o.body = love.physics.newBody(world, x, y)
o.shape = love.physics.newPolygonShape(shape)
o.fixture = love.physics.newFixture(o.body, o.shape)
o.sprite = love.graphics.newImage(sprite)
o.fixture:setCategory(1)
o.fixture:setFriction(0.2)
o.world = game
return o
end
-- Destructor of `Ground`
function Ground:delete ()
-- body deletion is handled by world deletion
self.sprite = nil
end
-- Draw of `Ground`
function Ground:draw (offset_x, offset_y, scale, debug)
-- defaults
local offset_x = offset_x or 0
local offset_y = offset_y or 0
local scale = scale or 1
local debug = debug or false
-- sprite draw
love.graphics.setColor(255,255,255,255)
love.graphics.draw(self.sprite, (self.body:getX()+offset_x)*scale, (self.body:getY()+offset_y)*scale, 0, scale, scale)
-- debug draw
if debug then
love.graphics.setColor(180, 180, 180, 120)
love.graphics.polygon("fill", self.world.camera:translatePoints(self.body:getWorldPoints(self.shape:getPoints())))
end
end
|