From 340a3a4b92de5495b47e8e1e102178edfd97514f Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 19 Mar 2017 00:21:02 +0100 Subject: Night commit, added PhysicalBody, newImage to sprite --- not/Hero.lua | 19 +++++++-------- not/PhysicalBody.lua | 16 +++++++++++++ not/Sprite.lua | 66 ++++++++++++++++++++++++++++++++++------------------ 3 files changed, 68 insertions(+), 33 deletions(-) create mode 100644 not/PhysicalBody.lua (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 7ddc724..8d226ac 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -1,12 +1,6 @@ --- `Hero` --- Entity controlled by a player. It has a physical body and a sprite. Can play animations and interact with other instances of the same class. +--- `Hero` +-- Hero (naut) entity that exists in a game world. -- Collision category: [2] - --- WHOLE CODE HAS FLAG OF "need a cleanup" -require "not.Sprite" - --- Metatable of `Hero` --- nils initialized in constructor Hero = { -- General and physics name = "empty", @@ -44,10 +38,13 @@ Hero = { -- Animations table animations = require "animations" } + +-- `Hero` is a child of `PhysicalBody`. +require "not.PhysicalBody" Hero.__index = Hero -setmetatable(Hero, Sprite) +setmetatable(Hero, PhysicalBody) --- Constructor of `Hero` +-- Constructor of `Hero`. function Hero:new (game, world, x, y, name) -- Meta local o = {} @@ -64,7 +61,7 @@ function Hero:new (game, world, x, y, name) o.body:setFixedRotation(true) -- Misc o.name = name or "empty" - o:setImage(newImage("assets/nauts/"..o.name..".png")) + o:setImage(Sprite.newImage("assets/nauts/"..o.name..".png")) o.world = game o.punchcd = 0 -- Animation diff --git a/not/PhysicalBody.lua b/not/PhysicalBody.lua new file mode 100644 index 0000000..6e6a8a6 --- /dev/null +++ b/not/PhysicalBody.lua @@ -0,0 +1,16 @@ +--- `PhysicalBody` +-- Abstract class for drawable body existing in Box2D's physical 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 = Sprite:new(imagePath) + return o +end \ No newline at end of file diff --git a/not/Sprite.lua b/not/Sprite.lua index 1cc46f7..905816b 100644 --- a/not/Sprite.lua +++ b/not/Sprite.lua @@ -1,32 +1,54 @@ -- `Sprite` -- Abstract class for drawable animated entities. - --- Metatable Sprite = { - animations--[[table with animations]], - current--[[animations.default]], - image--[[love.graphics.newImage()]], + animations =--[[table with animations]]nil, + current =--[[animations.default]]nil, + image =--[[love.graphics.newImage]]nil, frame = 1, delay = .1, } Sprite.__index = Sprite +-- Constructor of `Sprite`. +function Sprite:new (imagePath) + local o = setmetatable({}, self) + if type(imagePath) == "string" then + o:setImage(self.newImage(imagePath)) + end + return o +end + -- Cleans up reference to image on deletion. -function Sprite:delete() +function Sprite:delete () self.image = nil end --- Sets an Image as a image. -function Sprite:setImage(image) +-- Creates new Image object from path. Key-colours two shades of green. Static. +function Sprite.newImage (path) + local imagedata = love.image.newImageData(path) + local transparency = function(x, y, r, g, b, a) + if (r == 0 and g == 128 and b == 64) or + (r == 0 and g == 240 and b == 6) then + a = 0 + end + return r, g, b, a + end + imagedata:mapPixel(transparency) + local image = love.graphics.newImage(imagedata) + return image +end + +-- Sets an Image as an image. +function Sprite:setImage (image) self.image = image end -- Returns current image Image. -function Sprite:getImage() +function Sprite:getImage () return self.image end -- Sets new animations list. -function Sprite:setAnimationsList(t) +function Sprite:setAnimationsList (t) if t then self.animations = t self:setAnimation("default") @@ -34,45 +56,45 @@ function Sprite:setAnimationsList(t) end -- Sets current animation by table key. -function Sprite:setAnimation(animation) +function Sprite:setAnimation (animation) self.frame = 1 self.delay = Sprite.delay -- INITIAL from metatable self.current = self.animations[animation] end -- Returns current animation table. -function Sprite:getAnimation() +function Sprite:getAnimation () return self.current end -- Get frame quad for drawing. -function Sprite:getQuad() +function Sprite:getQuad () if self.animations and self.current then return self.current[self.frame] end end -- Drawing self to LOVE2D buffer. --- If there is no Quad, it will draw entire image. -function Sprite:draw(...) - local s, q = self:getImage(), self:getQuad() - if s then +-- If there is no Quad, it will draw entire image. It won't draw anything if there is no image. +function Sprite:draw (...) + local i, q = self:getImage(), self:getQuad() + if i then love.graphics.setColor(255,255,255,255) - if q then love.graphics.draw(s, q, ...) - else love.graphics.draw(s, ...) end + if q then love.graphics.draw(i, q, ...) + else love.graphics.draw(i, ...) end end end -- Animation updating. -function Sprite:update(dt) +function Sprite:update (dt) if self.animations and self.current then self.delay = self.delay - dt if self.delay < 0 then self.delay = self.delay + Sprite.delay -- INITIAL from metatable - self:nextFrame() + self:goToNextFrame() end end end -- Moving to the next frame. -function Sprite:nextFrame() +function Sprite:goToNextFrame () if self.current.repeated or not (self.frame == self.current.frames) then self.frame = (self.frame % self.current.frames) + 1 else -- cgit v1.1