mirror of
https://github.com/tanema/light_world.lua.git
synced 2024-12-24 20:24:19 +00:00
learned how to create class like things in lua finally so I took out other peoples code
This commit is contained in:
parent
456a29ce95
commit
1026092cbd
168
lib/body.lua
168
lib/body.lua
@ -1,110 +1,114 @@
|
|||||||
local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
|
local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
|
||||||
local class = require(_PACKAGE.."/class")
|
|
||||||
local normal_map = require(_PACKAGE..'/normal_map')
|
local normal_map = require(_PACKAGE..'/normal_map')
|
||||||
local util = require(_PACKAGE..'/util')
|
local util = require(_PACKAGE..'/util')
|
||||||
local anim8 = require(_PACKAGE..'/anim8')
|
local anim8 = require(_PACKAGE..'/anim8')
|
||||||
local vector = require(_PACKAGE..'/vector')
|
local vector = require(_PACKAGE..'/vector')
|
||||||
local body = class()
|
|
||||||
|
local body = {}
|
||||||
|
body.__index = body
|
||||||
|
|
||||||
body.glowShader = love.graphics.newShader(_PACKAGE.."/shaders/glow.glsl")
|
body.glowShader = love.graphics.newShader(_PACKAGE.."/shaders/glow.glsl")
|
||||||
body.materialShader = love.graphics.newShader(_PACKAGE.."/shaders/material.glsl")
|
body.materialShader = love.graphics.newShader(_PACKAGE.."/shaders/material.glsl")
|
||||||
|
|
||||||
function body:init(id, type, ...)
|
local function new(id, type, ...)
|
||||||
local args = {...}
|
local args = {...}
|
||||||
self.id = id
|
local obj = setmetatable({}, body)
|
||||||
self.type = type
|
obj.id = id
|
||||||
self.shine = true
|
obj.type = type
|
||||||
self.red = 1.0
|
obj.shine = true
|
||||||
self.green = 1.0
|
obj.red = 1.0
|
||||||
self.blue = 1.0
|
obj.green = 1.0
|
||||||
self.alpha = 1.0
|
obj.blue = 1.0
|
||||||
self.glowRed = 255
|
obj.alpha = 1.0
|
||||||
self.glowGreen = 255
|
obj.glowRed = 255
|
||||||
self.glowBlue = 255
|
obj.glowGreen = 255
|
||||||
self.glowStrength = 0.0
|
obj.glowBlue = 255
|
||||||
self.tileX = 0
|
obj.glowStrength = 0.0
|
||||||
self.tileY = 0
|
obj.tileX = 0
|
||||||
self.zheight = 1
|
obj.tileY = 0
|
||||||
|
obj.zheight = 1
|
||||||
|
|
||||||
self.castsNoShadow = false
|
obj.castsNoShadow = false
|
||||||
self.visible = true
|
obj.visible = true
|
||||||
self.is_on_screen = true
|
obj.is_on_screen = true
|
||||||
|
|
||||||
if self.type == "circle" then
|
if obj.type == "circle" then
|
||||||
self.x = args[1] or 0
|
obj.x = args[1] or 0
|
||||||
self.y = args[2] or 0
|
obj.y = args[2] or 0
|
||||||
|
|
||||||
circle_canvas = love.graphics.newCanvas(args[3]*2, args[3]*2)
|
circle_canvas = love.graphics.newCanvas(args[3]*2, args[3]*2)
|
||||||
util.drawto(circle_canvas, 0, 0, 1, function()
|
util.drawto(circle_canvas, 0, 0, 1, function()
|
||||||
love.graphics.circle('fill', args[3], args[3], args[3])
|
love.graphics.circle('fill', args[3], args[3], args[3])
|
||||||
end)
|
end)
|
||||||
self.img = love.graphics.newImage(circle_canvas:getImageData())
|
obj.img = love.graphics.newImage(circle_canvas:getImageData())
|
||||||
self.imgWidth = self.img:getWidth()
|
obj.imgWidth = obj.img:getWidth()
|
||||||
self.imgHeight = self.img:getHeight()
|
obj.imgHeight = obj.img:getHeight()
|
||||||
self.ix = self.imgWidth * 0.5
|
obj.ix = obj.imgWidth * 0.5
|
||||||
self.iy = self.imgHeight * 0.5
|
obj.iy = obj.imgHeight * 0.5
|
||||||
self:generateNormalMapFlat("top")
|
obj:generateNormalMapFlat("top")
|
||||||
|
|
||||||
self:setShadowType('circle', args[3], args[4], args[5])
|
obj:setShadowType('circle', args[3], args[4], args[5])
|
||||||
elseif self.type == "rectangle" then
|
elseif obj.type == "rectangle" then
|
||||||
self.x = args[1] or 0
|
obj.x = args[1] or 0
|
||||||
self.y = args[2] or 0
|
obj.y = args[2] or 0
|
||||||
|
|
||||||
local rectangle_canvas = love.graphics.newCanvas(args[3], args[4])
|
local rectangle_canvas = love.graphics.newCanvas(args[3], args[4])
|
||||||
util.drawto(rectangle_canvas, 0, 0, 1, function()
|
util.drawto(rectangle_canvas, 0, 0, 1, function()
|
||||||
love.graphics.rectangle('fill', 0, 0, args[3], args[4])
|
love.graphics.rectangle('fill', 0, 0, args[3], args[4])
|
||||||
end)
|
end)
|
||||||
self.img = love.graphics.newImage(rectangle_canvas:getImageData())
|
obj.img = love.graphics.newImage(rectangle_canvas:getImageData())
|
||||||
self.imgWidth = self.img:getWidth()
|
obj.imgWidth = obj.img:getWidth()
|
||||||
self.imgHeight = self.img:getHeight()
|
obj.imgHeight = obj.img:getHeight()
|
||||||
self.ix = self.imgWidth * 0.5
|
obj.ix = obj.imgWidth * 0.5
|
||||||
self.iy = self.imgHeight * 0.5
|
obj.iy = obj.imgHeight * 0.5
|
||||||
self:generateNormalMapFlat("top")
|
obj:generateNormalMapFlat("top")
|
||||||
|
|
||||||
self:setShadowType('rectangle', args[3], args[4])
|
obj:setShadowType('rectangle', args[3], args[4])
|
||||||
elseif self.type == "polygon" then
|
elseif obj.type == "polygon" then
|
||||||
self:setPoints(...)
|
obj:setPoints(...)
|
||||||
elseif self.type == "image" then
|
elseif obj.type == "image" then
|
||||||
self.img = args[1]
|
obj.img = args[1]
|
||||||
self.x = args[2] or 0
|
obj.x = args[2] or 0
|
||||||
self.y = args[3] or 0
|
obj.y = args[3] or 0
|
||||||
if self.img then
|
if obj.img then
|
||||||
self.imgWidth = self.img:getWidth()
|
obj.imgWidth = obj.img:getWidth()
|
||||||
self.imgHeight = self.img:getHeight()
|
obj.imgHeight = obj.img:getHeight()
|
||||||
self.ix = self.imgWidth * 0.5
|
obj.ix = obj.imgWidth * 0.5
|
||||||
self.iy = self.imgHeight * 0.5
|
obj.iy = obj.imgHeight * 0.5
|
||||||
end
|
end
|
||||||
self:generateNormalMapFlat("top")
|
obj:generateNormalMapFlat("top")
|
||||||
self:setShadowType('rectangle', args[4] or self.imgWidth, args[5] or self.imgHeight, args[6], args[7])
|
obj:setShadowType('rectangle', args[4] or obj.imgWidth, args[5] or obj.imgHeight, args[6], args[7])
|
||||||
self.reflective = true
|
obj.reflective = true
|
||||||
elseif self.type == "animation" then
|
elseif obj.type == "animation" then
|
||||||
self.img = args[1]
|
obj.img = args[1]
|
||||||
self.x = args[2] or 0
|
obj.x = args[2] or 0
|
||||||
self.y = args[3] or 0
|
obj.y = args[3] or 0
|
||||||
self.animations = {}
|
obj.animations = {}
|
||||||
self.castsNoShadow = true
|
obj.castsNoShadow = true
|
||||||
self:generateNormalMapFlat("top")
|
obj:generateNormalMapFlat("top")
|
||||||
self.reflective = true
|
obj.reflective = true
|
||||||
elseif self.type == "refraction" then
|
elseif obj.type == "refraction" then
|
||||||
self.x = args[2] or 0
|
obj.x = args[2] or 0
|
||||||
self.y = args[3] or 0
|
obj.y = args[3] or 0
|
||||||
self:setNormalMap(args[1], args[4], args[5])
|
obj:setNormalMap(args[1], args[4], args[5])
|
||||||
self.width = args[4] or self.normalWidth
|
obj.width = args[4] or obj.normalWidth
|
||||||
self.height = args[5] or self.normalHeight
|
obj.height = args[5] or obj.normalHeight
|
||||||
self.ox = self.width * 0.5
|
obj.ox = obj.width * 0.5
|
||||||
self.oy = self.height * 0.5
|
obj.oy = obj.height * 0.5
|
||||||
self.refraction = true
|
obj.refraction = true
|
||||||
elseif self.type == "reflection" then
|
elseif obj.type == "reflection" then
|
||||||
self.x = args[2] or 0
|
obj.x = args[2] or 0
|
||||||
self.y = args[3] or 0
|
obj.y = args[3] or 0
|
||||||
self:setNormalMap(args[1], args[4], args[5])
|
obj:setNormalMap(args[1], args[4], args[5])
|
||||||
self.width = args[4] or self.normalWidth
|
obj.width = args[4] or obj.normalWidth
|
||||||
self.height = args[5] or self.normalHeight
|
obj.height = args[5] or obj.normalHeight
|
||||||
self.ox = self.width * 0.5
|
obj.ox = obj.width * 0.5
|
||||||
self.oy = self.height * 0.5
|
obj.oy = obj.height * 0.5
|
||||||
self.reflection = true
|
obj.reflection = true
|
||||||
end
|
end
|
||||||
self.old_x, self.old_y = self.x, self.y
|
obj.old_x, obj.old_y = obj.x, obj.y
|
||||||
|
|
||||||
|
return obj
|
||||||
end
|
end
|
||||||
|
|
||||||
-- refresh
|
-- refresh
|
||||||
@ -714,4 +718,4 @@ function body:drawImageShadow(light)
|
|||||||
love.graphics.draw(self.shadowMesh, self.x - self.ox, self.y - self.oy, 0, s, s)
|
love.graphics.draw(self.shadowMesh, self.x - self.ox, self.y - self.oy, 0, s, s)
|
||||||
end
|
end
|
||||||
|
|
||||||
return body
|
return setmetatable({new = new}, {__call = function(_, ...) return new(...) end})
|
||||||
|
@ -1,47 +0,0 @@
|
|||||||
-- class.lua
|
|
||||||
-- Compatible with Lua 5.1 (not 5.0).
|
|
||||||
local class = function(base, init)
|
|
||||||
local c = {} -- a new class instance
|
|
||||||
if not init and type(base) == 'function' then
|
|
||||||
init = base
|
|
||||||
base = nil
|
|
||||||
elseif type(base) == 'table' then
|
|
||||||
-- our new class is a shallow copy of the base class!
|
|
||||||
for i,v in pairs(base) do
|
|
||||||
c[i] = v
|
|
||||||
end
|
|
||||||
c._base = base
|
|
||||||
end
|
|
||||||
-- the class will be the metatable for all its objects,
|
|
||||||
-- and they will look up their methods in it.
|
|
||||||
c.__index = c
|
|
||||||
|
|
||||||
-- expose a constructor which can be called by <classname>(<args>)
|
|
||||||
local mt = {}
|
|
||||||
mt.__call = function(class_tbl, ...)
|
|
||||||
local obj = {}
|
|
||||||
setmetatable(obj,c)
|
|
||||||
if class_tbl.init then
|
|
||||||
class_tbl.init(obj,...)
|
|
||||||
else
|
|
||||||
-- make sure that any stuff from the base class is initialized!
|
|
||||||
if base and base.init then
|
|
||||||
base.init(obj, ...)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return obj
|
|
||||||
end
|
|
||||||
c.init = init
|
|
||||||
c.is_a = function(self, klass)
|
|
||||||
local m = getmetatable(self)
|
|
||||||
while m do
|
|
||||||
if m == klass then return true end
|
|
||||||
m = m._base
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
setmetatable(c, mt)
|
|
||||||
return c
|
|
||||||
end
|
|
||||||
|
|
||||||
return class
|
|
48
lib/init.lua
48
lib/init.lua
@ -23,42 +23,46 @@ SOFTWARE.
|
|||||||
]]
|
]]
|
||||||
local _PACKAGE = string.gsub(...,"%.","/") or ""
|
local _PACKAGE = string.gsub(...,"%.","/") or ""
|
||||||
if string.len(_PACKAGE) > 0 then _PACKAGE = _PACKAGE .. "/" end
|
if string.len(_PACKAGE) > 0 then _PACKAGE = _PACKAGE .. "/" end
|
||||||
local class = require(_PACKAGE..'class')
|
|
||||||
local Light = require(_PACKAGE..'light')
|
local Light = require(_PACKAGE..'light')
|
||||||
local Body = require(_PACKAGE..'body')
|
local Body = require(_PACKAGE..'body')
|
||||||
local util = require(_PACKAGE..'util')
|
local util = require(_PACKAGE..'util')
|
||||||
local PostShader = require(_PACKAGE..'postshader')
|
local PostShader = require(_PACKAGE..'postshader')
|
||||||
|
|
||||||
local light_world = class()
|
local light_world = {}
|
||||||
|
light_world.__index = light_world
|
||||||
|
|
||||||
light_world.shadowShader = love.graphics.newShader(_PACKAGE.."/shaders/shadow.glsl")
|
light_world.shadowShader = love.graphics.newShader(_PACKAGE.."/shaders/shadow.glsl")
|
||||||
light_world.refractionShader = love.graphics.newShader(_PACKAGE.."shaders/refraction.glsl")
|
light_world.refractionShader = love.graphics.newShader(_PACKAGE.."shaders/refraction.glsl")
|
||||||
light_world.reflectionShader = love.graphics.newShader(_PACKAGE.."shaders/reflection.glsl")
|
light_world.reflectionShader = love.graphics.newShader(_PACKAGE.."shaders/reflection.glsl")
|
||||||
|
|
||||||
function light_world:init(options)
|
local function new(options)
|
||||||
self.lights = {}
|
local obj = {}
|
||||||
self.bodies = {}
|
obj.lights = {}
|
||||||
self.post_shader = PostShader()
|
obj.bodies = {}
|
||||||
|
obj.post_shader = PostShader()
|
||||||
|
|
||||||
self.l, self.t, self.s = 0, 0, 1
|
obj.l, obj.t, obj.s = 0, 0, 1
|
||||||
self.ambient = {0, 0, 0}
|
obj.ambient = {0, 0, 0}
|
||||||
self.refractionStrength = 8.0
|
obj.refractionStrength = 8.0
|
||||||
self.reflectionStrength = 16.0
|
obj.reflectionStrength = 16.0
|
||||||
self.reflectionVisibility = 1.0
|
obj.reflectionVisibility = 1.0
|
||||||
self.shadowBlur = 2.0
|
obj.shadowBlur = 2.0
|
||||||
self.glowBlur = 1.0
|
obj.glowBlur = 1.0
|
||||||
self.glowTimer = 0.0
|
obj.glowTimer = 0.0
|
||||||
self.glowDown = false
|
obj.glowDown = false
|
||||||
|
|
||||||
self.disableGlow = false
|
obj.disableGlow = false
|
||||||
self.disableMaterial = false
|
obj.disableMaterial = false
|
||||||
self.disableReflection = true
|
obj.disableReflection = true
|
||||||
self.disableRefraction = true
|
obj.disableRefraction = true
|
||||||
|
|
||||||
options = options or {}
|
options = options or {}
|
||||||
for k, v in pairs(options) do self[k] = v end
|
for k, v in pairs(options) do obj[k] = v end
|
||||||
|
|
||||||
self:refreshScreenSize()
|
local world = setmetatable(obj, light_world)
|
||||||
|
world:refreshScreenSize()
|
||||||
|
|
||||||
|
return world
|
||||||
end
|
end
|
||||||
|
|
||||||
function light_world:refreshScreenSize(w, h)
|
function light_world:refreshScreenSize(w, h)
|
||||||
@ -304,4 +308,4 @@ function light_world:remove(to_kill)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
return light_world
|
return setmetatable({new = new}, {__call = function(_, ...) return new(...) end})
|
||||||
|
@ -1,24 +1,28 @@
|
|||||||
local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
|
local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
|
||||||
local class = require(_PACKAGE.."/class")
|
|
||||||
local util = require(_PACKAGE..'/util')
|
local util = require(_PACKAGE..'/util')
|
||||||
|
|
||||||
local light = class()
|
local light = {}
|
||||||
|
light.__index = light
|
||||||
|
|
||||||
function light:init(x, y, r, g, b, range)
|
local function new(x, y, r, g, b, range)
|
||||||
self.direction = 0
|
local obj = {
|
||||||
self.angle = math.pi * 2.0
|
direction = 0,
|
||||||
self.x = x or 0
|
angle = math.pi * 2.0,
|
||||||
self.y = y or 0
|
x = x or 0,
|
||||||
self.z = 1
|
y = y or 0,
|
||||||
self.red = r or 255
|
z = 1,
|
||||||
self.green = g or 255
|
red = r or 255,
|
||||||
self.blue = b or 255
|
green = g or 255,
|
||||||
self.range = range or 300
|
blue = b or 255,
|
||||||
self.smooth = 1.0
|
range = range or 300,
|
||||||
self.glowSize = 0.1
|
smooth = 1.0,
|
||||||
self.glowStrength = 0.0
|
glowSize = 0.1,
|
||||||
self.visible = true
|
glowStrength = 0.0,
|
||||||
self.is_on_screen = true
|
visible = true,
|
||||||
|
is_on_screen = true,
|
||||||
|
}
|
||||||
|
|
||||||
|
return setmetatable(obj, light)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- set position
|
-- set position
|
||||||
@ -118,4 +122,4 @@ function light:setVisible(visible)
|
|||||||
self.visible = visible
|
self.visible = visible
|
||||||
end
|
end
|
||||||
|
|
||||||
return light
|
return setmetatable({new = new}, {__call = function(_, ...) return new(...) end})
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
normal_map = {}
|
local normal_map = {}
|
||||||
|
|
||||||
function normal_map.fromHeightMap(heightMap, strength)
|
function normal_map.fromHeightMap(heightMap, strength)
|
||||||
local imgData = heightMap:getData()
|
local imgData = heightMap:getData()
|
||||||
|
@ -22,10 +22,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
]]
|
]]
|
||||||
local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
|
local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
|
||||||
local class = require(_PACKAGE..'/class')
|
|
||||||
local util = require(_PACKAGE..'/util')
|
local util = require(_PACKAGE..'/util')
|
||||||
|
|
||||||
local post_shader = class()
|
local post_shader = {}
|
||||||
|
post_shader.__index = post_shader
|
||||||
|
|
||||||
local files = love.filesystem.getDirectoryItems(_PACKAGE.."/shaders/postshaders")
|
local files = love.filesystem.getDirectoryItems(_PACKAGE.."/shaders/postshaders")
|
||||||
local shaders = {}
|
local shaders = {}
|
||||||
@ -44,18 +44,16 @@ for i,v in ipairs(files) do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function post_shader:init()
|
local function new()
|
||||||
self:refreshScreenSize()
|
local obj = {effects = {}}
|
||||||
self.effects = {}
|
local class = setmetatable(obj, post_shader)
|
||||||
|
class:refreshScreenSize()
|
||||||
|
return class
|
||||||
end
|
end
|
||||||
|
|
||||||
function post_shader:refreshScreenSize(w, h)
|
function post_shader:refreshScreenSize(w, h)
|
||||||
w, h = w or love.window.getWidth(), h or love.window.getHeight()
|
w, h = w or love.window.getWidth(), h or love.window.getHeight()
|
||||||
|
|
||||||
self.back_buffer = love.graphics.newCanvas(w, h)
|
self.back_buffer = love.graphics.newCanvas(w, h)
|
||||||
|
|
||||||
self.w = w
|
|
||||||
self.h = h
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function post_shader:addEffect(shaderName, ...)
|
function post_shader:addEffect(shaderName, ...)
|
||||||
@ -116,7 +114,6 @@ function post_shader:drawTiltShift(canvas, args)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function post_shader:drawShader(shaderName, canvas, args)
|
function post_shader:drawShader(shaderName, canvas, args)
|
||||||
local w, h = love.graphics.getWidth(), love.graphics.getHeight()
|
|
||||||
local current_arg = 1
|
local current_arg = 1
|
||||||
|
|
||||||
local effect = shaders[shaderName]
|
local effect = shaders[shaderName]
|
||||||
@ -164,4 +161,4 @@ function process_palette(palette)
|
|||||||
return palette
|
return palette
|
||||||
end
|
end
|
||||||
|
|
||||||
return post_shader
|
return setmetatable({new = new}, {__call = function(_, ...) return new(...) end})
|
||||||
|
Loading…
Reference in New Issue
Block a user