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
|
--- `PhysicalBody`
-- Abstract class for drawable entity existing in `not.World`.
PhysicalBody = {
body =--[[love.physics.newBody]]nil,
}
-- `PhysicalBody` is a child of `Sprite`.
require "not.Sprite"
PhysicalBody.__index = PhysicalBody
setmetatable(PhysicalBody, Sprite)
--[[ Constructor of `PhysicalBody`.
function PhysicalBody:new (world, x, y, imagePath)
local o = setmetatable({}, self)
o:init(world, x, y, imagePath)
return o
end
]]
-- Initializer of `PhysicalBody`.
function PhysicalBody:init (world, x, y, imagePath)
Sprite.init(self, imagePath)
self.body = love.physics.newBody(world.world, x, y)
end
-- Add new fixture to body.
function PhysicalBody:addFixture (shape, density)
local shape = love.physics.newPolygonShape(shape)
local fixture = love.physics.newFixture(self.body, shape, density)
return fixture
end
-- Position-related methods.
function PhysicalBody:getPosition ()
return self.body:getPosition()
end
function PhysicalBody:setPosition (x, y)
self.body:setPosition(x, y)
end
-- Various setters from Body.
-- type: BodyType ("static", "dynamic", "kinematic")
function PhysicalBody:setBodyType (type)
self.body:setType(type)
end
function PhysicalBody:setBodyFixedRotation (bool)
self.body:setFixedRotation(bool)
end
-- Update of `PhysicalBody`.
function PhysicalBody:update (dt)
Sprite.update(self, dt)
if self.body:isDestroyed() then return end
end
-- Draw of `PhysicalBody`.
function PhysicalBody:draw (offset_x, offset_y, scale, debug)
Sprite.draw(self, offset_x, offset_y, scale)
if debug then
for _,fixture in pairs(self.body:getFixtureList()) do
local category = fixture:getCategory()
if category == 1 then
love.graphics.setColor(255, 69, 0, 140)
end
if category == 2 then
love.graphics.setColor(137, 255, 0, 120)
end
if category == 3 then
love.graphics.setColor(137, 0, 255, 40)
end
-- TODO: `world` is not a member of `PhysicalBody` or its instance normally.
love.graphics.polygon("fill", self.world.camera:translatePoints(self.body:getWorldPoints(fixture:getShape():getPoints())))
end
end
end
|