changed the class handling

This commit is contained in:
Tim Anema 2014-09-26 16:52:16 -04:00
parent d56b4ca660
commit fd58dd6a86
5 changed files with 230 additions and 228 deletions

View File

@ -22,13 +22,14 @@ function love.load()
-- create shadow bodys -- create shadow bodys
circleTest = lightWorld:newCircle(256, 256, 16) circleTest = lightWorld:newCircle(256, 256, 16)
rectangleTest = lightWorld:newRectangle(512, 512, 64, 64) rectangleTest = lightWorld:newRectangle(512, 512, 64, 64)
imageTest = lightWorld:newImage(image, 64, 64, 24, 6) imageTest = lightWorld:newImage(image, 64, 64, 24, 6)
imageTest:setNormalMap(image_normal) imageTest:setNormalMap(image_normal)
imageTest:setGlowMap(glow) imageTest:setGlowMap(glow)
imageTest:setOffset(12, -10) imageTest:setOffset(12, -10)
-- create body object -- create body object
objectTest = lightWorld:newBody("refraction", normal, 64, 64, 128, 128) objectTest = lightWorld:newRefraction(normal, 64, 64, 128, 128)
--objectTest:setShine(false) --objectTest:setShine(false)
--objectTest:setShadowType("rectangle") --objectTest:setShadowType("rectangle")
--objectTest:setShadowDimension(64, 64) --objectTest:setShadowDimension(64, 64)

View File

@ -1,170 +1,172 @@
local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or "" local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
local class = require(_PACKAGE.."/class") local class = require(_PACKAGE.."/class")
local body = class(function(o, world, id, type, ...) local body = class()
function body:init(world, id, type, ...)
local args = {...} local args = {...}
o.id = id self.id = id
o.type = type self.type = type
o.normal = nil self.normal = nil
o.material = nil self.material = nil
o.glow = nil self.glow = nil
o.world = world self.world = world
if o.type == "circle" then if self.type == "circle" then
o.x = args[1] or 0 self.x = args[1] or 0
o.y = args[2] or 0 self.y = args[2] or 0
o.radius = args[3] or 16 self.radius = args[3] or 16
o.ox = args[4] or 0 self.ox = args[4] or 0
o.oy = args[5] or 0 self.oy = args[5] or 0
o.shadowType = "circle" self.shadowType = "circle"
o.reflection = false self.reflection = false
o.reflective = false self.reflective = false
o.refraction = false self.refraction = false
o.refractive = false self.refractive = false
world.isShadows = true world.isShadows = true
elseif o.type == "rectangle" then elseif self.type == "rectangle" then
o.x = args[1] or 0 self.x = args[1] or 0
o.y = args[2] or 0 self.y = args[2] or 0
o.width = args[3] or 64 self.width = args[3] or 64
o.height = args[4] or 64 self.height = args[4] or 64
o.ox = o.width * 0.5 self.ox = self.width * 0.5
o.oy = o.height * 0.5 self.oy = self.height * 0.5
o.shadowType = "rectangle" self.shadowType = "rectangle"
o.data = { self.data = {
o.x - o.ox, self.x - self.ox,
o.y - o.oy, self.y - self.oy,
o.x - o.ox + o.width, self.x - self.ox + self.width,
o.y - o.oy, self.y - self.oy,
o.x - o.ox + o.width, self.x - self.ox + self.width,
o.y - o.oy + o.height, self.y - self.oy + self.height,
o.x - o.ox, self.x - self.ox,
o.y - o.oy + o.height self.y - self.oy + self.height
} }
o.reflection = false self.reflection = false
o.reflective = false self.reflective = false
o.refraction = false self.refraction = false
o.refractive = false self.refractive = false
world.isShadows = true world.isShadows = true
elseif o.type == "polygon" then elseif self.type == "polygon" then
o.shadowType = "polygon" self.shadowType = "polygon"
o.data = args or {0, 0, 0, 0, 0, 0} self.data = args or {0, 0, 0, 0, 0, 0}
o.reflection = false self.reflection = false
o.reflective = false self.reflective = false
o.refraction = false self.refraction = false
o.refractive = false self.refractive = false
world.isShadows = true world.isShadows = true
elseif o.type == "image" then elseif self.type == "image" then
o.img = args[1] self.img = args[1]
o.x = args[2] or 0 self.x = args[2] or 0
o.y = args[3] or 0 self.y = args[3] or 0
if o.img then if self.img then
o.imgWidth = o.img:getWidth() self.imgWidth = self.img:getWidth()
o.imgHeight = o.img:getHeight() self.imgHeight = self.img:getHeight()
o.width = args[4] or o.imgWidth self.width = args[4] or self.imgWidth
o.height = args[5] or o.imgHeight self.height = args[5] or self.imgHeight
o.ix = o.imgWidth * 0.5 self.ix = self.imgWidth * 0.5
o.iy = o.imgHeight * 0.5 self.iy = self.imgHeight * 0.5
o.vert = { self.vert = {
{ 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0 },
{ o.width, 0.0, 1.0, 0.0 }, { self.width, 0.0, 1.0, 0.0 },
{ o.width, o.height, 1.0, 1.0 }, { self.width, self.height, 1.0, 1.0 },
{ 0.0, o.height, 0.0, 1.0 }, { 0.0, self.height, 0.0, 1.0 },
} }
o.msh = love.graphics.newMesh(o.vert, o.img, "fan") self.msh = love.graphics.newMesh(self.vert, self.img, "fan")
else else
o.width = args[4] or 64 self.width = args[4] or 64
o.height = args[5] or 64 self.height = args[5] or 64
end end
o.ox = args[6] or o.width * 0.5 self.ox = args[6] or self.width * 0.5
o.oy = args[7] or o.height * 0.5 self.oy = args[7] or self.height * 0.5
o.shadowType = "rectangle" self.shadowType = "rectangle"
o.data = { self.data = {
o.x - o.ox, self.x - self.ox,
o.y - o.oy, self.y - self.oy,
o.x - o.ox + o.width, self.x - self.ox + self.width,
o.y - o.oy, self.y - self.oy,
o.x - o.ox + o.width, self.x - self.ox + self.width,
o.y - o.oy + o.height, self.y - self.oy + self.height,
o.x - o.ox, self.x - self.ox,
o.y - o.oy + o.height self.y - self.oy + self.height
} }
o.reflection = false self.reflection = false
o.reflective = true self.reflective = true
o.refraction = false self.refraction = false
o.refractive = false self.refractive = false
world.isShadows = true world.isShadows = true
elseif o.type == "refraction" then elseif self.type == "refraction" then
o.normal = args[1] self.normal = args[1]
o.x = args[2] or 0 self.x = args[2] or 0
o.y = args[3] or 0 self.y = args[3] or 0
if o.normal then if self.normal then
o.normalWidth = o.normal:getWidth() self.normalWidth = self.normal:getWidth()
o.normalHeight = o.normal:getHeight() self.normalHeight = self.normal:getHeight()
o.width = args[4] or o.normalWidth self.width = args[4] or self.normalWidth
o.height = args[5] or o.normalHeight self.height = args[5] or self.normalHeight
o.nx = o.normalWidth * 0.5 self.nx = self.normalWidth * 0.5
o.ny = o.normalHeight * 0.5 self.ny = self.normalHeight * 0.5
o.normal:setWrap("repeat", "repeat") self.normal:setWrap("repeat", "repeat")
o.normalVert = { self.normalVert = {
{0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0},
{o.width, 0.0, 1.0, 0.0}, {self.width, 0.0, 1.0, 0.0},
{o.width, o.height, 1.0, 1.0}, {self.width, self.height, 1.0, 1.0},
{0.0, o.height, 0.0, 1.0} {0.0, self.height, 0.0, 1.0}
} }
o.normalMesh = love.graphics.newMesh(o.normalVert, o.normal, "fan") self.normalMesh = love.graphics.newMesh(self.normalVert, self.normal, "fan")
else else
o.width = args[4] or 64 self.width = args[4] or 64
o.height = args[5] or 64 self.height = args[5] or 64
end end
o.ox = o.width * 0.5 self.ox = self.width * 0.5
o.oy = o.height * 0.5 self.oy = self.height * 0.5
o.reflection = false self.reflection = false
o.reflective = false self.reflective = false
o.refraction = true self.refraction = true
o.refractive = false self.refractive = false
world.isRefraction = true world.isRefraction = true
elseif o.type == "reflection" then elseif self.type == "reflection" then
o.normal = args[1] self.normal = args[1]
o.x = args[2] or 0 self.x = args[2] or 0
o.y = args[3] or 0 self.y = args[3] or 0
if o.normal then if self.normal then
o.normalWidth = o.normal:getWidth() self.normalWidth = self.normal:getWidth()
o.normalHeight = o.normal:getHeight() self.normalHeight = self.normal:getHeight()
o.width = args[4] or o.normalWidth self.width = args[4] or self.normalWidth
o.height = args[5] or o.normalHeight self.height = args[5] or self.normalHeight
o.nx = o.normalWidth * 0.5 self.nx = self.normalWidth * 0.5
o.ny = o.normalHeight * 0.5 self.ny = self.normalHeight * 0.5
o.normal:setWrap("repeat", "repeat") self.normal:setWrap("repeat", "repeat")
o.normalVert = { self.normalVert = {
{0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0},
{o.width, 0.0, 1.0, 0.0}, {self.width, 0.0, 1.0, 0.0},
{o.width, o.height, 1.0, 1.0}, {self.width, self.height, 1.0, 1.0},
{0.0, o.height, 0.0, 1.0} {0.0, self.height, 0.0, 1.0}
} }
o.normalMesh = love.graphics.newMesh(o.normalVert, o.normal, "fan") self.normalMesh = love.graphics.newMesh(self.normalVert, self.normal, "fan")
else else
o.width = args[4] or 64 self.width = args[4] or 64
o.height = args[5] or 64 self.height = args[5] or 64
end end
o.ox = o.width * 0.5 self.ox = self.width * 0.5
o.oy = o.height * 0.5 self.oy = self.height * 0.5
o.reflection = true self.reflection = true
o.reflective = false self.reflective = false
o.refraction = false self.refraction = false
o.refractive = false self.refractive = false
world.isReflection = true world.isReflection = true
end end
o.shine = true self.shine = true
o.red = 0 self.red = 0
o.green = 0 self.green = 0
o.blue = 0 self.blue = 0
o.alpha = 1.0 self.alpha = 1.0
o.glowRed = 255 self.glowRed = 255
o.glowGreen = 255 self.glowGreen = 255
o.glowBlue = 255 self.glowBlue = 255
o.glowStrength = 0.0 self.glowStrength = 0.0
o.tileX = 0 self.tileX = 0
o.tileY = 0 self.tileY = 0
end) end
-- refresh -- refresh
function body:refresh() function body:refresh()
@ -297,11 +299,6 @@ function body:getPoints()
return unpack(self.data) return unpack(self.data)
end end
-- set shadow on/off -- set shadow on/off
function body:setShadowType(type)
self.shadowType = type
self.world.changed = true
end
-- set shadow on/off
function body:setShadow(b) function body:setShadow(b)
self.castsNoShadow = not b self.castsNoShadow = not b
self.world.changed = true self.world.changed = true

View File

@ -21,8 +21,8 @@ local class = function(base, init)
mt.__call = function(class_tbl, ...) mt.__call = function(class_tbl, ...)
local obj = {} local obj = {}
setmetatable(obj,c) setmetatable(obj,c)
if init then if class_tbl.init then
init(obj,...) class_tbl.init(obj,...)
else else
-- make sure that any stuff from the base class is initialized! -- make sure that any stuff from the base class is initialized!
if base and base.init then if base and base.init then

View File

@ -1,26 +1,28 @@
local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or "" local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
local class = require(_PACKAGE.."/class") local class = require(_PACKAGE.."/class")
local light = class(function(l, world, x, y, r, g, b, range) local light = class()
l.world = world
l.direction = 0 function light:init(world, x, y, r, g, b, range)
l.angle = math.pi * 2.0 self.world = world
l.range = 0 self.direction = 0
l.shadow = love.graphics.newCanvas() self.angle = math.pi * 2.0
l.shine = love.graphics.newCanvas() self.range = 0
l.x = x or 0 self.shadow = love.graphics.newCanvas()
l.y = y or 0 self.shine = love.graphics.newCanvas()
l.z = 15 self.x = x or 0
l.red = r or 255 self.y = y or 0
l.green = g or 255 self.z = 15
l.blue = b or 255 self.red = r or 255
l.range = range or 300 self.green = g or 255
l.smooth = 1.0 self.blue = b or 255
l.glowSize = 0.1 self.range = range or 300
l.glowStrength = 0.0 self.smooth = 1.0
l.changed = true self.glowSize = 0.1
l.visible = true self.glowStrength = 0.0
end) self.changed = true
self.visible = true
end
-- set position -- set position
function light:setPosition(x, y, z) function light:setPosition(x, y, z)

View File

@ -28,70 +28,72 @@ local vector = require(_PACKAGE..'/vector')
local Light = require(_PACKAGE..'/light') local Light = require(_PACKAGE..'/light')
local Body = require(_PACKAGE..'/body') local Body = require(_PACKAGE..'/body')
local light_world = class(function(o) local light_world = class()
o.translate_x = 0
o.translate_y = 0
o.translate_x_old = 0
o.translate_y_old = 0
o.direction = 0
o.last_buffer = nil function light_world:init()
self.translate_x = 0
self.translate_y = 0
self.translate_x_old = 0
self.translate_y_old = 0
self.direction = 0
o.lights = {} self.last_buffer = nil
o.ambient = {0, 0, 0}
o.body = {}
o.refraction = {}
o.shadow = love.graphics.newCanvas()
o.shadow2 = love.graphics.newCanvas()
o.shine = love.graphics.newCanvas()
o.shine2 = love.graphics.newCanvas()
o.normalMap = love.graphics.newCanvas()
o.glowMap = love.graphics.newCanvas()
o.glowMap2 = love.graphics.newCanvas()
o.refractionMap = love.graphics.newCanvas()
o.refractionMap2 = love.graphics.newCanvas()
o.reflectionMap = love.graphics.newCanvas()
o.reflectionMap2 = love.graphics.newCanvas()
o.normalInvert = false
o.glowBlur = 1.0
o.glowTimer = 0.0
o.glowDown = false
o.refractionStrength = 8.0
o.pixelShadow = love.graphics.newCanvas() self.lights = {}
o.pixelShadow2 = love.graphics.newCanvas() self.ambient = {0, 0, 0}
self.body = {}
self.refraction = {}
self.shadow = love.graphics.newCanvas()
self.shadow2 = love.graphics.newCanvas()
self.shine = love.graphics.newCanvas()
self.shine2 = love.graphics.newCanvas()
self.normalMap = love.graphics.newCanvas()
self.glowMap = love.graphics.newCanvas()
self.glowMap2 = love.graphics.newCanvas()
self.refractionMap = love.graphics.newCanvas()
self.refractionMap2 = love.graphics.newCanvas()
self.reflectionMap = love.graphics.newCanvas()
self.reflectionMap2 = love.graphics.newCanvas()
self.normalInvert = false
self.glowBlur = 1.0
self.glowTimer = 0.0
self.glowDown = false
self.refractionStrength = 8.0
o.blurv = love.graphics.newShader("shader/blurv.glsl") self.pixelShadow = love.graphics.newCanvas()
o.blurh = love.graphics.newShader("shader/blurh.glsl") self.pixelShadow2 = love.graphics.newCanvas()
o.blurv:send("screen", {love.window.getWidth(), love.window.getHeight()})
o.blurh:send("screen", {love.window.getWidth(), love.window.getHeight()})
o.shader = love.graphics.newShader("shader/poly_shadow.glsl") self.blurv = love.graphics.newShader("shader/blurv.glsl")
o.glowShader = love.graphics.newShader("shader/glow.glsl") self.blurh = love.graphics.newShader("shader/blurh.glsl")
o.normalShader = love.graphics.newShader("shader/normal.glsl") self.blurv:send("screen", {love.window.getWidth(), love.window.getHeight()})
o.normalInvertShader = love.graphics.newShader("shader/normal_invert.glsl") self.blurh:send("screen", {love.window.getWidth(), love.window.getHeight()})
o.materialShader = love.graphics.newShader("shader/material.glsl")
o.refractionShader = love.graphics.newShader("shader/refraction.glsl")
o.refractionShader:send("screen", {love.window.getWidth(), love.window.getHeight()})
o.reflectionShader = love.graphics.newShader("shader/reflection.glsl")
o.reflectionShader:send("screen", {love.window.getWidth(), love.window.getHeight()})
o.reflectionStrength = 16.0 self.shader = love.graphics.newShader("shader/poly_shadow.glsl")
o.reflectionVisibility = 1.0 self.glowShader = love.graphics.newShader("shader/glow.glsl")
o.changed = true self.normalShader = love.graphics.newShader("shader/normal.glsl")
o.blur = 2.0 self.normalInvertShader = love.graphics.newShader("shader/normal_invert.glsl")
o.optionShadows = true self.materialShader = love.graphics.newShader("shader/material.glsl")
o.optionPixelShadows = true self.refractionShader = love.graphics.newShader("shader/refraction.glsl")
o.optionGlow = true self.refractionShader:send("screen", {love.window.getWidth(), love.window.getHeight()})
o.optionRefraction = true self.reflectionShader = love.graphics.newShader("shader/reflection.glsl")
o.optionReflection = true self.reflectionShader:send("screen", {love.window.getWidth(), love.window.getHeight()})
o.isShadows = false
o.isLight = false self.reflectionStrength = 16.0
o.isPixelShadows = false self.reflectionVisibility = 1.0
o.isGlow = false self.changed = true
o.isRefraction = false self.blur = 2.0
o.isReflection = false self.optionShadows = true
end) self.optionPixelShadows = true
self.optionGlow = true
self.optionRefraction = true
self.optionReflection = true
self.isShadows = false
self.isLight = false
self.isPixelShadows = false
self.isGlow = false
self.isRefraction = false
self.isReflection = false
end
-- update -- update
function light_world:update() function light_world:update()