diff options
-rw-r--r-- | not/Trap.lua | 76 | ||||
-rw-r--r-- | not/World.lua | 87 |
2 files changed, 96 insertions, 67 deletions
diff --git a/not/Trap.lua b/not/Trap.lua new file mode 100644 index 0000000..84de5b0 --- /dev/null +++ b/not/Trap.lua @@ -0,0 +1,76 @@ +Trap = require "not.PhysicalBody":extends() + +-- TODO: Move flames' animations to config file. +local animations = { + default = { + [1] = love.graphics.newQuad(0, 0, 42, 19, 168, 19), + [2] = love.graphics.newQuad(42, 0, 42, 19, 168, 19), + frames = 2, + repeated = true + }, + fadein = { + [1] = love.graphics.newQuad(84, 0, 42, 19, 168, 19), + [2] = love.graphics.newQuad(126, 0, 42, 19, 168, 19), + frames = 2, + repeated = false + }, + fadeout = { + [1] = love.graphics.newQuad(126, 0, 42, 19, 168, 19), + [2] = love.graphics.newQuad(84, 0, 42, 19, 168, 19), + frames = 2, + repeated = false + } +} + +function Trap:new (direction, x, y, world, imagePath) + Trap.__super.new(self, x, y, world, imagePath) + self:setAnimationsList(animations) + self:setBodyType("static") + + local mirror = 1 + if direction == "left" then mirror = -1 end + local fixture = self:addFixture({0, 0, 41 * mirror, 0, 41 * mirror, 18, 0, 18}) + fixture:setCategory(3) + fixture:setMask(1) + fixture:setUserData({0, direction}) + fixture:setSensor(true) + + self.mirror = mirror +end + +function Trap:fadeIn () + self.hidden = false + self:setBodyActive(true) + if self.animations.fadein then + self:setAnimation("fadein") + end +end + +function Trap:fadeOut () + self:setBodyActive(false) + if self.animations.fadeout then + self:setAnimation("fadeout") + else + self.hidden = true + end +end + +function Trap:getHorizontalMirror () + return self.mirror +end + +function Trap:goToNextFrame () + if self.current.repeated or not (self.frame == self.current.frames) then + self.frame = (self.frame % self.current.frames) + 1 + elseif self.current == self.animations.fadeout then + self:setAnimation("default") + self.hidden = true + else + self:setAnimation("default") + end +end + +-- TODO: Trap@damage is hotfix for clashing. +function Trap:damage () end + +return Trap diff --git a/not/World.lua b/not/World.lua index e32c11d..9a37965 100644 --- a/not/World.lua +++ b/not/World.lua @@ -10,6 +10,8 @@ require "not.Ray" require "not.Cloud" require "not.CloudGenerator" require "not.Layer" +require "not.Timer" +require "not.Trap" --- ZA WARUDO! -- TODO: Missing documentation on most of World's methods. @@ -83,64 +85,15 @@ function World:initLayers () end end --- TODO: Move flames to separate class `Trap`. -- TODO: Make collisions for category 3 more customizable or create new category for traps/area effects. local -function createFlame (self, x, y, mirror, timerIn, timerOut) - local trap = require("not.PhysicalBody")(x, y, self, "assets/decorations/205-flames.png") - trap:setAnimationsList({ - default = { - [1] = love.graphics.newQuad(0, 0, 42, 19, 168, 19), - [2] = love.graphics.newQuad(42, 0, 42, 19, 168, 19), - frames = 2, - repeated = true - }, - fadein = { - [1] = love.graphics.newQuad(84, 0, 42, 19, 168, 19), - [2] = love.graphics.newQuad(126, 0, 42, 19, 168, 19), - frames = 2, - repeated = false - }, - fadeout = { - [1] = love.graphics.newQuad(126, 0, 42, 19, 168, 19), - [2] = love.graphics.newQuad(84, 0, 42, 19, 168, 19), - frames = 2, - repeated = false - } - }) - - -- hotfix for clash - trap.body:setUserData(trap) - trap.damage = function () end - - local fixture = trap:addFixture({0,0, 41*mirror,0, 41*mirror,18, 0,18}) - fixture:setCategory(3) - fixture:setMask(1) - local direction = "right" - if mirror < 0 then direction = "left" end - fixture:setUserData({0, direction}) - fixture:setSensor(true) - trap:setBodyType("static") - trap.layer = self.layers.decorations - - trap.getHorizontalMirror = function (self) return mirror end - - trap.goToNextFrame = function (self) - if self.current.repeated or not (self.frame == self.current.frames) then - self.frame = (self.frame % self.current.frames) + 1 - elseif self.current == self.animations.fadeout then - self:setAnimation("default") - self.hidden = true - else - self:setAnimation("default") - end - end +function createFlame (self, x, y, direction, timerIn, timerOut) + local trap = Trap(direction, x, y, self, "assets/decorations/205-flames.png") + + trap.layer = self.layers.platforms - timerIn:register(trap.setBodyActive, trap, true) - timerIn:register(trap.setAnimation, trap, "fadein") - timerIn:register(function (self) trap.hidden = false end, trap) - timerOut:register(trap.setBodyActive, trap, false) - timerOut:register(trap.setAnimation, trap, "fadeout") + timerIn:register(trap.fadeIn, trap) + timerOut:register(trap.fadeOut, trap) self:insertEntity(trap) end @@ -188,18 +141,18 @@ function World:buildMap () end -- TODO: Make flames and other traps more configurable through map config file. if op.flames then - local timerIn = require("not.Timer")(10) - local timerOut = require("not.Timer")(5) - - timerIn:register(timerOut.start, timerOut) - timerOut:register(timerIn.start, timerIn) - - createFlame(self, -62, 16, 1, timerIn, timerOut) - createFlame(self, 63, 16, -1, timerIn, timerOut) - - self:insertEntity(timerIn) - self:insertEntity(timerOut) - timerOut:start() + local timerIn = Timer(10) + local timerOut = Timer(5) + + timerIn:register(timerOut.start, timerOut) + timerOut:register(timerIn.start, timerIn) + + createFlame(self, -62, 16, "right", timerIn, timerOut) + createFlame(self, 63, 16, "left", timerIn, timerOut) + + self:insertEntity(timerIn) + self:insertEntity(timerOut) + timerOut:start() end end end |