From b262cb3eec1a797832d168f9e6d144468fb48c1d Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 19 Mar 2017 01:35:43 +0100 Subject: Initializers in PhysicalBody, Hero and Sprite --- not/Hero.lua | 52 +++++++++++++++++++++++++++++----------------------- not/PhysicalBody.lua | 8 +++++++- not/Sprite.lua | 11 ++++++++--- 3 files changed, 44 insertions(+), 27 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 8d226ac..eee7a83 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -36,7 +36,7 @@ Hero = { -- Sounds sfx = require "sounds", -- Animations table - animations = require "animations" + animations = nil } -- `Hero` is a child of `PhysicalBody`. @@ -46,28 +46,9 @@ setmetatable(Hero, PhysicalBody) -- Constructor of `Hero`. function Hero:new (game, world, x, y, name) - -- Meta - local o = {} - setmetatable(o, self) - -- Physics - local group = -1-#game.Nauts - o.body = love.physics.newBody(world, x, y, "dynamic") - o.shape = love.physics.newRectangleShape(10, 16) - o.fixture = love.physics.newFixture(o.body, o.shape, 8) - o.fixture:setUserData(o) - o.fixture:setCategory(2) - o.fixture:setMask(2) - o.fixture:setGroupIndex(group) - o.body:setFixedRotation(true) - -- Misc - o.name = name or "empty" - o:setImage(Sprite.newImage("assets/nauts/"..o.name..".png")) - o.world = game - o.punchcd = 0 - -- Animation - o.current = o.animations.default - o:createEffect("respawn") - -- Portrait load for first object created + local o = setmetatable({}, self) + o:init(name, game, x, y) + -- Load portraits statically. if self.portrait_sprite == nil then self.portrait_sprite = love.graphics.newImage("assets/portraits.png") self.portrait_frame = love.graphics.newImage("assets/menu.png") @@ -75,6 +56,31 @@ function Hero:new (game, world, x, y, name) return o end +-- Initializator of `Hero`. +function Hero:init (name, world, x, y) + -- Find imagePath basing on hero name and call super initializator. + local fileName = name or Hero.name -- INITIAL from metatable + local imagePath = string.format("assets/nauts/%s.png", fileName) + PhysicalBody.init(self, world, x, y, imagePath) + -- To be removed or heavily changed. + self.world = world + self.punchcd = 0 + -- To be moved to PhysicalBody abstract. + local group = -1-#world.Nauts + self.body = love.physics.newBody(world.world, x, y, "dynamic") + self.shape = love.physics.newRectangleShape(10, 16) + self.fixture = love.physics.newFixture(self.body, self.shape, 8) + self.fixture:setUserData(self) + self.fixture:setCategory(2) + self.fixture:setMask(2) + self.fixture:setGroupIndex(group) + self.body:setFixedRotation(true) + -- Actual `Hero` initialization. + self.name = name + self:setAnimationsList(require("animations")) + self:createEffect("respawn") +end + -- Control set managment function Hero:assignControlSet(set) self.controlset = set diff --git a/not/PhysicalBody.lua b/not/PhysicalBody.lua index 6e6a8a6..1f91faf 100644 --- a/not/PhysicalBody.lua +++ b/not/PhysicalBody.lua @@ -11,6 +11,12 @@ setmetatable(PhysicalBody, Sprite) -- Constructor of `PhysicalBody`. function PhysicalBody:new (world, x, y, imagePath) - local o = Sprite:new(imagePath) + local o = setmetatable({}, self) + o:init(world, x, y, imagePath) return o +end + +-- Initializator of `PhysicalBody`. +function PhysicalBody:init (world, x, y, imagePath) + Sprite.init(self, imagePath) end \ No newline at end of file diff --git a/not/Sprite.lua b/not/Sprite.lua index 905816b..6342f60 100644 --- a/not/Sprite.lua +++ b/not/Sprite.lua @@ -12,9 +12,7 @@ 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 + o:init(imagePath) return o end @@ -23,6 +21,13 @@ function Sprite:delete () self.image = nil end +-- Initializes new Sprite instance. +function Sprite:init (imagePath) + if type(imagePath) == "string" then + self:setImage(Sprite.newImage(imagePath)) + end +end + -- Creates new Image object from path. Key-colours two shades of green. Static. function Sprite.newImage (path) local imagedata = love.image.newImageData(path) -- cgit v1.1