From 4a969a0aecedd2811cbac793b25294a607a54027 Mon Sep 17 00:00:00 2001 From: Tim Anema Date: Wed, 10 Dec 2014 21:06:12 -0500 Subject: [PATCH] fixed creation of bodies so that they all use x and y as the center point and not center and topleft --- examples/animation.lua | 2 +- lib/body.lua | 32 +++++++++++++++++++++----------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/examples/animation.lua b/examples/animation.lua index 1ced09a..e44d7b7 100644 --- a/examples/animation.lua +++ b/examples/animation.lua @@ -81,7 +81,7 @@ function love.draw() love.graphics.rectangle("fill", -x/scale, -y/scale, love.graphics.getWidth()/scale, love.graphics.getHeight()/scale) animation:drawAnimation() - animation2:draw(image, 200, 100) + animation2:draw(image, 200, 30) end) love.graphics.pop() end diff --git a/lib/body.lua b/lib/body.lua index fb3f7ac..180ef2c 100644 --- a/lib/body.lua +++ b/lib/body.lua @@ -80,9 +80,6 @@ function body:init(id, type, ...) self.img = args[1] self.x = args[2] or 0 self.y = args[3] or 0 - self.ix = 0 - self.iy = 0 - self.width, self.height = 16, 16 self.animations = {} self.castsNoShadow = true self:generateNormalMapFlat("top") @@ -153,14 +150,19 @@ function body:newGrid(frameWidth, frameHeight, imageWidth, imageHeight, left, to end -- frameWidth, frameHeight, imageWidth, imageHeight, left, top, border function body:addAnimation(name, frames, durations, onLoop) - self.current_animation_name = self.current_animation_name or name self.animations[name] = anim8.newAnimation(frames, durations, onLoop) - self.animation = self.animations[self.current_animation_name] + + if not self.current_animation_name then + self:setAnimation(name) + end end function body:setAnimation(name) self.current_animation_name = name self.animation = self.animations[self.current_animation_name] + + local frame = self.animation.frames[self.animation.position] + _,_,self.width, self.height = frame:getViewport() end function body:gotoFrame(frame) self.animation:gotoFrame(frame) end @@ -172,9 +174,15 @@ function body:pauseAtEnd() self.animation:pauseAtEnd() end function body:pauseAtStart() self.animation:pauseAtStart() end function body:update(dt) - local frame = self.animation.frames[self.animation.position] - _,_,self.width, self.height = frame:getViewport() - self.animation:update(dt) + if self.type == "animation" and self.animation then + local frame = self.animation.frames[self.animation.position] + _,_,self.width, self.height = frame:getViewport() + self.imgWidth, self.imgHeight = self.width, self.height + self.normalWidth, self.normalHeight = self.width, self.height + self.ix, self.iy = self.imgWidth * 0.5,self.imgHeight * 0.5 + self.nx, self.ny = self.ix, self.iy + self.animation:update(dt) + end end -- set position @@ -293,11 +301,14 @@ function body:setPoints(...) if px > self.width then self.width = px end if py > self.height then self.height = py end end + -- normalize width and height self.width = self.width - self.x self.height = self.height - self.y for i = 1, #points, 2 do points[i], points[i+1] = points[i] - self.x, points[i+1] - self.y end + self.x = self.x + (self.width * 0.5) + self.y = self.y + (self.height * 0.5) poly_canvas = love.graphics.newCanvas(self.width, self.height) util.drawto(poly_canvas, 0, 0, 1, function() @@ -312,7 +323,6 @@ function body:setPoints(...) --wrapping with polygon normals causes edges to show --also we do not need wrapping for this default normal map self.normal:setWrap("clamp", "clamp") - self.nx, self.ny = 0, 0 self:setShadowType('polygon', ...) end @@ -507,14 +517,14 @@ function body:isInRange(l, t, w, h, s) end function body:drawAnimation() - self.animation:draw(self.img, self.x, self.y) + self.animation:draw(self.img, self.x - self.ix, self.y - self.iy) end function body:drawNormal() if not self.refraction and not self.reflection and self.normalMesh then love.graphics.setColor(255, 255, 255) if self.type == 'animation' then - self.animation:draw(self.normal, self.x, self.y) + self.animation:draw(self.normal, self.x - self.nx, self.y - self.ny) else love.graphics.draw(self.normalMesh, self.x - self.nx, self.y - self.ny) end