diff options
-rw-r--r-- | not/PhysicalBody.lua | 9 | ||||
-rw-r--r-- | not/Platform.lua | 74 |
2 files changed, 29 insertions, 54 deletions
diff --git a/not/PhysicalBody.lua b/not/PhysicalBody.lua index 24e87c8..7c05262 100644 --- a/not/PhysicalBody.lua +++ b/not/PhysicalBody.lua @@ -57,9 +57,14 @@ 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 - if fixture:getCategory() == 2 then + 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) - else + 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. diff --git a/not/Platform.lua b/not/Platform.lua index 7dcea6c..0c03e73 100644 --- a/not/Platform.lua +++ b/not/Platform.lua @@ -1,67 +1,37 @@ --- `Platform` +--- `Platform` -- Static platform physical object with a sprite. `Players` can walk on it. -- Collision category: [1] -- TODO: reformat code to follow new code patterns -- TODO: comment uncovered code parts - --- WHOLE CODE HAS FLAG OF "need a cleanup" -require "not.Sprite" - --- Metatable of `Platform` --- nils initialized in constructor Platform = { - body = nil, - shape = nil, - fixture = nil, - world = nil, + world = --[[not.World]]nil, } + +-- `Platform` is a child of `PhysicalBody`. +require "not.PhysicalBody" Platform.__index = Platform -setmetatable(Platform, Sprite) +setmetatable(Platform, PhysicalBody) -- Constructor of `Platform` function Platform:new (game, world, x, y, shape, sprite, animations) - local o = {} - setmetatable(o, self) - o.body = love.physics.newBody(world, x, y) - -- MULTIPLE SHAPES NEED TO BE REWRITED! - o.shape = {} - if type(shape[1]) == "number" then - local poly = love.physics.newPolygonShape(shape) - table.insert(o.shape, poly) - o.fixture = love.physics.newFixture(o.body, poly) - o.fixture:setCategory(1) - o.fixture:setFriction(0.2) - else - for i,v in pairs(shape) do - local poly = love.physics.newPolygonShape(v) - table.insert(o.shape, poly) - local fixture = love.physics.newFixture(o.body, poly) - fixture:setCategory(1) - fixture:setFriction(0.2) - end - end - -- END HERE - o:setImage(love.graphics.newImage(sprite)) - o:setAnimationsList(animations) - o.world = game + local o = setmetatable({}, self) + o:init(animations, shape, game, x, y, sprite) return o end --- Position -function Platform:getPosition() - return self.body:getPosition() -end - --- Draw of `Platform` --- TODO: see todos in `not.Sprite.draw`. -function Platform:draw (offset_x, offset_y, scale, debug) - Sprite.draw(self, offset_x, offset_y, scale) - - -- debug draw - if debug then - love.graphics.setColor(255, 69, 0, 140) - for i,v in pairs(self.shape) do - love.graphics.polygon("fill", self.world.camera:translatePoints(self.body:getWorldPoints(v:getPoints()))) - end +-- Initializator of `Platform`. +function Platform:init (animations, shape, world, x, y, imagePath) + PhysicalBody.init(self, world, x, y, imagePath) + self:setAnimationsList(animations) + self.world = world + -- Create table of shapes if single shape is passed. + if type(shape[1]) == "number" then + shape = {shape} + end + -- Add all shapes from as fixtures to body. + for _,single in pairs(shape) do + local fixture = self:addFixture(single) + fixture:setCategory(1) + fixture:setFriction(0.2) end end
\ No newline at end of file |