From c167edccc656feb15861f9c2c2f48c2307d8869a Mon Sep 17 00:00:00 2001 From: Tim Anema Date: Thu, 2 Oct 2014 23:04:22 -0400 Subject: [PATCH] small refactors because I am stuck --- lib/body.lua | 40 ++++++++++++++++++++++++++++++++++++---- lib/light.lua | 14 +++++++------- lib/light_world.lua | 4 +++- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/lib/body.lua b/lib/body.lua index bcaf40b..c3b29d6 100644 --- a/lib/body.lua +++ b/lib/body.lua @@ -538,10 +538,42 @@ function body:drawShadow(light, l,t,w,h) local shadowRotation = math.atan2((self.x) - light.x, (self.y + self.oy) - light.y) self.shadowVert = { - {math.sin(shadowRotation) * self.imgHeight * length, (length * math.cos(shadowRotation) + 1.0) * self.imgHeight + (math.cos(shadowRotation) + 1.0) * self.shadowY, 0, 0, self.red, self.green, self.blue, self.alpha * self.fadeStrength * 255}, - {self.imgWidth + math.sin(shadowRotation) * self.imgHeight * length, (length * math.cos(shadowRotation) + 1.0) * self.imgHeight + (math.cos(shadowRotation) + 1.0) * self.shadowY, 1, 0, self.red, self.green, self.blue, self.alpha * self.fadeStrength * 255}, - {self.imgWidth, self.imgHeight + (math.cos(shadowRotation) + 1.0) * self.shadowY, 1, 1, self.red, self.green, self.blue, self.alpha * 255}, - {0, self.imgHeight + (math.cos(shadowRotation) + 1.0) * self.shadowY, 0, 1, self.red, self.green, self.blue, self.alpha * 255} + { + math.sin(shadowRotation) * self.imgHeight * length, + (length * math.cos(shadowRotation) + 1.0) * self.imgHeight + (math.cos(shadowRotation) + 1.0) * self.shadowY, + 0, 0, + self.red, + self.green, + self.blue, + self.alpha * self.fadeStrength * 255 + }, + { + self.imgWidth + math.sin(shadowRotation) * self.imgHeight * length, + (length * math.cos(shadowRotation) + 1.0) * self.imgHeight + (math.cos(shadowRotation) + 1.0) * self.shadowY, + 1, 0, + self.red, + self.green, + self.blue, + self.alpha * self.fadeStrength * 255 + }, + { + self.imgWidth, + self.imgHeight + (math.cos(shadowRotation) + 1.0) * self.shadowY, + 1, 1, + self.red, + self.green, + self.blue, + self.alpha * 255 + }, + { + 0, + self.imgHeight + (math.cos(shadowRotation) + 1.0) * self.shadowY, + 0, 1, + self.red, + self.green, + self.blue, + self.alpha * 255 + } } self.shadowMesh:setVertices(self.shadowVert) diff --git a/lib/light.lua b/lib/light.lua index 50be737..b169974 100644 --- a/lib/light.lua +++ b/lib/light.lua @@ -122,10 +122,14 @@ function light:setGlowStrength(strength) self.glowStrength = strength end +function light:inRange(l,t,w,h) + return self.x + self.range > l and self.x - self.range < (l+w) and self.y + self.range > t and self.y - self.range < (t+h) +end + function light:updateShadow(l,t,w,h, bodies) love.graphics.setShader(self.shader) - if self.x + self.range > l and self.x - self.range < (l+w) and self.y + self.range > t and self.y - self.range < (t+h) then - local lightposrange = {self.x, h - self.y, self.range} + if self:inRange(l,t,w,h) then + self.shader:send("lightPosition", {self.x - l, h - (self.y - t), self.z}) self.shader:send("lightRange", self.range) self.shader:send("lightColor", {self.red / 255.0, self.green / 255.0, self.blue / 255.0}) @@ -133,9 +137,8 @@ function light:updateShadow(l,t,w,h, bodies) self.shader:send("lightGlow", {1.0 - self.glowSize, self.glowStrength}) self.shader:send("lightAngle", math.pi - self.angle / 2.0) self.shader:send("lightDirection", self.direction) - + self.shadow:clear() love.graphics.setCanvas(self.shadow) - love.graphics.clear() -- calculate shadows local shadow_geometry = self:calculateShadows(bodies) @@ -172,9 +175,6 @@ function light:updateShadow(l,t,w,h, bodies) love.graphics.setStencil(stencils.poly(bodies)) love.graphics.rectangle("fill", l,t,w,h) love.graphics.setStencil() - self.visible = true - else - self.visible = false end love.graphics.setShader() end diff --git a/lib/light_world.lua b/lib/light_world.lua index 08b0d30..f36f68e 100644 --- a/lib/light_world.lua +++ b/lib/light_world.lua @@ -237,6 +237,7 @@ function light_world:draw(l,t,w,h,s) local last_buffer = love.graphics.getCanvas() love.graphics.setCanvas(self.render_buffer) + self.drawBackground(l,t,w,h,s) self:drawShadow(l,t,w,h,scale) self.drawForground(l,t,w,h,s) @@ -245,12 +246,13 @@ function light_world:draw(l,t,w,h,s) self:drawGlow(l,t,w,h,scale) self:drawRefraction(l,t,w,h,scale) self:drawReflection(l,t,w,h,scale) + love.graphics.setBackgroundColor(0, 0, 0) love.graphics.setBlendMode("alpha") love.graphics.setCanvas(last_buffer) love.graphics.setShader() love.graphics.setColor(255, 255, 255) - love.graphics.draw(self.render_buffer) + love.graphics.draw(self.render_buffer, l, t) end -- draw shadow