From 1a669029ead7b89fd6f9e35514b62488a64f9017 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Wed, 1 May 2019 11:07:53 +0200 Subject: [PATCH] modules/world: add a basic sprite system for the Actor2D --- gamecore/modules/world/actors/actor2D.lua | 33 ++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/gamecore/modules/world/actors/actor2D.lua b/gamecore/modules/world/actors/actor2D.lua index d7c722d..e4dd584 100644 --- a/gamecore/modules/world/actors/actor2D.lua +++ b/gamecore/modules/world/actors/actor2D.lua @@ -43,6 +43,7 @@ function Actor2D:setManagers(world) self.world = world self.scene = world.scene self.obj = world.obj + self.assets = self.scene.assets end function Actor2D:setDebugColor(r,g,b) @@ -69,6 +70,7 @@ function Actor2D:initPhysics(x, y, w, h, isSolid) self.isSolid = isSolid or false self:setFilter() + self:setSprite() end function Actor2D:register() @@ -161,6 +163,34 @@ end -- DRAW FUNCTIONS -- Draw the actors. +function Actor2D:setSprite(spritename, ox, oy) + self.sprite = {} + self.sprite.name = spritename or nil + self.sprite.ox = ox or 0 + self.sprite.oy = oy or 0 + self.sprite.sx = 1 + self.sprite.sy = 1 + self.sprite.exist = (spritename ~= nil) +end + +function Actor2D:setSpriteScallingX(sx) + local sx = sx or 1 +end + +function Actor2D:setSpriteScallingY(sy) + local sy = sy or 1 +end + +function Actor2D:drawSprite(x, y, r, sx, sy, ox, oy, kx, ky) + if (self.sprite.name ~= nil) then + local ox = oy or self.sprite.ox + local oy = oy or self.sprite.oy + local sx = sx or self.sprite.sx + local sy = sy or self.sprite.sy + self.assets.sprites[self.sprite.name]:drawAnimation(x, y, r, sx, sy, ox, oy, kx, ky) + end +end + function Actor2D:getCenter() return (self.x + (self.w / 2)), (self.y + (self.h / 2)) end @@ -170,7 +200,8 @@ function Actor2D:getViewCenter() end function Actor2D:draw() - -- here will be update actions + local x, y = math.floor(self.x), math.floor(self.y) + self:drawSprite(self.x, self.y) end function Actor2D:drawHitbox()