small refactors because I am stuck

This commit is contained in:
Tim Anema 2014-10-02 23:04:22 -04:00
parent 73e203bd76
commit c167edccc6
3 changed files with 46 additions and 12 deletions

View File

@ -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) local shadowRotation = math.atan2((self.x) - light.x, (self.y + self.oy) - light.y)
self.shadowVert = { 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}, math.sin(shadowRotation) * self.imgHeight * length,
{self.imgWidth, self.imgHeight + (math.cos(shadowRotation) + 1.0) * self.shadowY, 1, 1, self.red, self.green, self.blue, self.alpha * 255}, (length * math.cos(shadowRotation) + 1.0) * self.imgHeight + (math.cos(shadowRotation) + 1.0) * self.shadowY,
{0, self.imgHeight + (math.cos(shadowRotation) + 1.0) * self.shadowY, 0, 1, self.red, self.green, self.blue, self.alpha * 255} 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) self.shadowMesh:setVertices(self.shadowVert)

View File

@ -122,10 +122,14 @@ function light:setGlowStrength(strength)
self.glowStrength = strength self.glowStrength = strength
end 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) function light:updateShadow(l,t,w,h, bodies)
love.graphics.setShader(self.shader) 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 if self:inRange(l,t,w,h) then
local lightposrange = {self.x, h - self.y, self.range}
self.shader:send("lightPosition", {self.x - l, h - (self.y - t), self.z}) self.shader:send("lightPosition", {self.x - l, h - (self.y - t), self.z})
self.shader:send("lightRange", self.range) self.shader:send("lightRange", self.range)
self.shader:send("lightColor", {self.red / 255.0, self.green / 255.0, self.blue / 255.0}) 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("lightGlow", {1.0 - self.glowSize, self.glowStrength})
self.shader:send("lightAngle", math.pi - self.angle / 2.0) self.shader:send("lightAngle", math.pi - self.angle / 2.0)
self.shader:send("lightDirection", self.direction) self.shader:send("lightDirection", self.direction)
self.shadow:clear()
love.graphics.setCanvas(self.shadow) love.graphics.setCanvas(self.shadow)
love.graphics.clear()
-- calculate shadows -- calculate shadows
local shadow_geometry = self:calculateShadows(bodies) 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.setStencil(stencils.poly(bodies))
love.graphics.rectangle("fill", l,t,w,h) love.graphics.rectangle("fill", l,t,w,h)
love.graphics.setStencil() love.graphics.setStencil()
self.visible = true
else
self.visible = false
end end
love.graphics.setShader() love.graphics.setShader()
end end

View File

@ -237,6 +237,7 @@ function light_world:draw(l,t,w,h,s)
local last_buffer = love.graphics.getCanvas() local last_buffer = love.graphics.getCanvas()
love.graphics.setCanvas(self.render_buffer) love.graphics.setCanvas(self.render_buffer)
self.drawBackground(l,t,w,h,s) self.drawBackground(l,t,w,h,s)
self:drawShadow(l,t,w,h,scale) self:drawShadow(l,t,w,h,scale)
self.drawForground(l,t,w,h,s) 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:drawGlow(l,t,w,h,scale)
self:drawRefraction(l,t,w,h,scale) self:drawRefraction(l,t,w,h,scale)
self:drawReflection(l,t,w,h,scale) self:drawReflection(l,t,w,h,scale)
love.graphics.setBackgroundColor(0, 0, 0) love.graphics.setBackgroundColor(0, 0, 0)
love.graphics.setBlendMode("alpha") love.graphics.setBlendMode("alpha")
love.graphics.setCanvas(last_buffer) love.graphics.setCanvas(last_buffer)
love.graphics.setShader() love.graphics.setShader()
love.graphics.setColor(255, 255, 255) love.graphics.setColor(255, 255, 255)
love.graphics.draw(self.render_buffer) love.graphics.draw(self.render_buffer, l, t)
end end
-- draw shadow -- draw shadow