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 class = require(_PACKAGE.."/class")
|
||||
local normal_map = require(_PACKAGE..'/normal_map')
|
||||
local util = require(_PACKAGE..'/util')
|
||||
local anim8 = require(_PACKAGE..'/anim8')
|
||||
local vector = require(_PACKAGE..'/vector')
|
||||
local body = class()
|
||||
|
||||
local body = {}
|
||||
body.__index = body
|
||||
|
||||
body.glowShader = love.graphics.newShader(_PACKAGE.."/shaders/glow.glsl")
|
||||
body.materialShader = love.graphics.newShader(_PACKAGE.."/shaders/material.glsl")
|
||||
|
||||
function body:init(id, type, ...)
|
||||
local function new(id, type, ...)
|
||||
local args = {...}
|
||||
self.id = id
|
||||
self.type = type
|
||||
self.shine = true
|
||||
self.red = 1.0
|
||||
self.green = 1.0
|
||||
self.blue = 1.0
|
||||
self.alpha = 1.0
|
||||
self.glowRed = 255
|
||||
self.glowGreen = 255
|
||||
self.glowBlue = 255
|
||||
self.glowStrength = 0.0
|
||||
self.tileX = 0
|
||||
self.tileY = 0
|
||||
self.zheight = 1
|
||||
local obj = setmetatable({}, body)
|
||||
obj.id = id
|
||||
obj.type = type
|
||||
obj.shine = true
|
||||
obj.red = 1.0
|
||||
obj.green = 1.0
|
||||
obj.blue = 1.0
|
||||
obj.alpha = 1.0
|
||||
obj.glowRed = 255
|
||||
obj.glowGreen = 255
|
||||
obj.glowBlue = 255
|
||||
obj.glowStrength = 0.0
|
||||
obj.tileX = 0
|
||||
obj.tileY = 0
|
||||
obj.zheight = 1
|
||||
|
||||
self.castsNoShadow = false
|
||||
self.visible = true
|
||||
self.is_on_screen = true
|
||||
obj.castsNoShadow = false
|
||||
obj.visible = true
|
||||
obj.is_on_screen = true
|
||||
|
||||
if self.type == "circle" then
|
||||
self.x = args[1] or 0
|
||||
self.y = args[2] or 0
|
||||
if obj.type == "circle" then
|
||||
obj.x = args[1] or 0
|
||||
obj.y = args[2] or 0
|
||||
|
||||
circle_canvas = love.graphics.newCanvas(args[3]*2, args[3]*2)
|
||||
util.drawto(circle_canvas, 0, 0, 1, function()
|
||||
love.graphics.circle('fill', args[3], args[3], args[3])
|
||||
end)
|
||||
self.img = love.graphics.newImage(circle_canvas:getImageData())
|
||||
self.imgWidth = self.img:getWidth()
|
||||
self.imgHeight = self.img:getHeight()
|
||||
self.ix = self.imgWidth * 0.5
|
||||
self.iy = self.imgHeight * 0.5
|
||||
self:generateNormalMapFlat("top")
|
||||
obj.img = love.graphics.newImage(circle_canvas:getImageData())
|
||||
obj.imgWidth = obj.img:getWidth()
|
||||
obj.imgHeight = obj.img:getHeight()
|
||||
obj.ix = obj.imgWidth * 0.5
|
||||
obj.iy = obj.imgHeight * 0.5
|
||||
obj:generateNormalMapFlat("top")
|
||||
|
||||
self:setShadowType('circle', args[3], args[4], args[5])
|
||||
elseif self.type == "rectangle" then
|
||||
self.x = args[1] or 0
|
||||
self.y = args[2] or 0
|
||||
obj:setShadowType('circle', args[3], args[4], args[5])
|
||||
elseif obj.type == "rectangle" then
|
||||
obj.x = args[1] or 0
|
||||
obj.y = args[2] or 0
|
||||
|
||||
local rectangle_canvas = love.graphics.newCanvas(args[3], args[4])
|
||||
util.drawto(rectangle_canvas, 0, 0, 1, function()
|
||||
love.graphics.rectangle('fill', 0, 0, args[3], args[4])
|
||||
end)
|
||||
self.img = love.graphics.newImage(rectangle_canvas:getImageData())
|
||||
self.imgWidth = self.img:getWidth()
|
||||
self.imgHeight = self.img:getHeight()
|
||||
self.ix = self.imgWidth * 0.5
|
||||
self.iy = self.imgHeight * 0.5
|
||||
self:generateNormalMapFlat("top")
|
||||
obj.img = love.graphics.newImage(rectangle_canvas:getImageData())
|
||||
obj.imgWidth = obj.img:getWidth()
|
||||
obj.imgHeight = obj.img:getHeight()
|
||||
obj.ix = obj.imgWidth * 0.5
|
||||
obj.iy = obj.imgHeight * 0.5
|
||||
obj:generateNormalMapFlat("top")
|
||||
|
||||
self:setShadowType('rectangle', args[3], args[4])
|
||||
elseif self.type == "polygon" then
|
||||
self:setPoints(...)
|
||||
elseif self.type == "image" then
|
||||
self.img = args[1]
|
||||
self.x = args[2] or 0
|
||||
self.y = args[3] or 0
|
||||
if self.img then
|
||||
self.imgWidth = self.img:getWidth()
|
||||
self.imgHeight = self.img:getHeight()
|
||||
self.ix = self.imgWidth * 0.5
|
||||
self.iy = self.imgHeight * 0.5
|
||||
obj:setShadowType('rectangle', args[3], args[4])
|
||||
elseif obj.type == "polygon" then
|
||||
obj:setPoints(...)
|
||||
elseif obj.type == "image" then
|
||||
obj.img = args[1]
|
||||
obj.x = args[2] or 0
|
||||
obj.y = args[3] or 0
|
||||
if obj.img then
|
||||
obj.imgWidth = obj.img:getWidth()
|
||||
obj.imgHeight = obj.img:getHeight()
|
||||
obj.ix = obj.imgWidth * 0.5
|
||||
obj.iy = obj.imgHeight * 0.5
|
||||
end
|
||||
self:generateNormalMapFlat("top")
|
||||
self:setShadowType('rectangle', args[4] or self.imgWidth, args[5] or self.imgHeight, args[6], args[7])
|
||||
self.reflective = true
|
||||
elseif self.type == "animation" then
|
||||
self.img = args[1]
|
||||
self.x = args[2] or 0
|
||||
self.y = args[3] or 0
|
||||
self.animations = {}
|
||||
self.castsNoShadow = true
|
||||
self:generateNormalMapFlat("top")
|
||||
self.reflective = true
|
||||
elseif self.type == "refraction" then
|
||||
self.x = args[2] or 0
|
||||
self.y = args[3] or 0
|
||||
self:setNormalMap(args[1], args[4], args[5])
|
||||
self.width = args[4] or self.normalWidth
|
||||
self.height = args[5] or self.normalHeight
|
||||
self.ox = self.width * 0.5
|
||||
self.oy = self.height * 0.5
|
||||
self.refraction = true
|
||||
elseif self.type == "reflection" then
|
||||
self.x = args[2] or 0
|
||||
self.y = args[3] or 0
|
||||
self:setNormalMap(args[1], args[4], args[5])
|
||||
self.width = args[4] or self.normalWidth
|
||||
self.height = args[5] or self.normalHeight
|
||||
self.ox = self.width * 0.5
|
||||
self.oy = self.height * 0.5
|
||||
self.reflection = true
|
||||
obj:generateNormalMapFlat("top")
|
||||
obj:setShadowType('rectangle', args[4] or obj.imgWidth, args[5] or obj.imgHeight, args[6], args[7])
|
||||
obj.reflective = true
|
||||
elseif obj.type == "animation" then
|
||||
obj.img = args[1]
|
||||
obj.x = args[2] or 0
|
||||
obj.y = args[3] or 0
|
||||
obj.animations = {}
|
||||
obj.castsNoShadow = true
|
||||
obj:generateNormalMapFlat("top")
|
||||
obj.reflective = true
|
||||
elseif obj.type == "refraction" then
|
||||
obj.x = args[2] or 0
|
||||
obj.y = args[3] or 0
|
||||
obj:setNormalMap(args[1], args[4], args[5])
|
||||
obj.width = args[4] or obj.normalWidth
|
||||
obj.height = args[5] or obj.normalHeight
|
||||
obj.ox = obj.width * 0.5
|
||||
obj.oy = obj.height * 0.5
|
||||
obj.refraction = true
|
||||
elseif obj.type == "reflection" then
|
||||
obj.x = args[2] or 0
|
||||
obj.y = args[3] or 0
|
||||
obj:setNormalMap(args[1], args[4], args[5])
|
||||
obj.width = args[4] or obj.normalWidth
|
||||
obj.height = args[5] or obj.normalHeight
|
||||
obj.ox = obj.width * 0.5
|
||||
obj.oy = obj.height * 0.5
|
||||
obj.reflection = true
|
||||
end
|
||||
self.old_x, self.old_y = self.x, self.y
|
||||
obj.old_x, obj.old_y = obj.x, obj.y
|
||||
|
||||
return obj
|
||||
end
|
||||
|
||||
-- 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)
|
||||
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
|
50
lib/init.lua
50
lib/init.lua
@ -23,42 +23,46 @@ SOFTWARE.
|
||||
]]
|
||||
local _PACKAGE = string.gsub(...,"%.","/") or ""
|
||||
if string.len(_PACKAGE) > 0 then _PACKAGE = _PACKAGE .. "/" end
|
||||
local class = require(_PACKAGE..'class')
|
||||
local Light = require(_PACKAGE..'light')
|
||||
local Body = require(_PACKAGE..'body')
|
||||
local util = require(_PACKAGE..'util')
|
||||
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.refractionShader = love.graphics.newShader(_PACKAGE.."shaders/refraction.glsl")
|
||||
light_world.reflectionShader = love.graphics.newShader(_PACKAGE.."shaders/reflection.glsl")
|
||||
|
||||
function light_world:init(options)
|
||||
self.lights = {}
|
||||
self.bodies = {}
|
||||
self.post_shader = PostShader()
|
||||
local function new(options)
|
||||
local obj = {}
|
||||
obj.lights = {}
|
||||
obj.bodies = {}
|
||||
obj.post_shader = PostShader()
|
||||
|
||||
obj.l, obj.t, obj.s = 0, 0, 1
|
||||
obj.ambient = {0, 0, 0}
|
||||
obj.refractionStrength = 8.0
|
||||
obj.reflectionStrength = 16.0
|
||||
obj.reflectionVisibility = 1.0
|
||||
obj.shadowBlur = 2.0
|
||||
obj.glowBlur = 1.0
|
||||
obj.glowTimer = 0.0
|
||||
obj.glowDown = false
|
||||
|
||||
self.l, self.t, self.s = 0, 0, 1
|
||||
self.ambient = {0, 0, 0}
|
||||
self.refractionStrength = 8.0
|
||||
self.reflectionStrength = 16.0
|
||||
self.reflectionVisibility = 1.0
|
||||
self.shadowBlur = 2.0
|
||||
self.glowBlur = 1.0
|
||||
self.glowTimer = 0.0
|
||||
self.glowDown = false
|
||||
|
||||
self.disableGlow = false
|
||||
self.disableMaterial = false
|
||||
self.disableReflection = true
|
||||
self.disableRefraction = true
|
||||
obj.disableGlow = false
|
||||
obj.disableMaterial = false
|
||||
obj.disableReflection = true
|
||||
obj.disableRefraction = true
|
||||
|
||||
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
|
||||
|
||||
function light_world:refreshScreenSize(w, h)
|
||||
@ -304,4 +308,4 @@ function light_world:remove(to_kill)
|
||||
return false
|
||||
end
|
||||
|
||||
return light_world
|
||||
return setmetatable({new = new}, {__call = function(_, ...) return new(...) end})
|
||||
|
@ -1,24 +1,28 @@
|
||||
local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
|
||||
local class = require(_PACKAGE.."/class")
|
||||
local util = require(_PACKAGE..'/util')
|
||||
|
||||
local light = class()
|
||||
local light = {}
|
||||
light.__index = light
|
||||
|
||||
function light:init(x, y, r, g, b, range)
|
||||
self.direction = 0
|
||||
self.angle = math.pi * 2.0
|
||||
self.x = x or 0
|
||||
self.y = y or 0
|
||||
self.z = 1
|
||||
self.red = r or 255
|
||||
self.green = g or 255
|
||||
self.blue = b or 255
|
||||
self.range = range or 300
|
||||
self.smooth = 1.0
|
||||
self.glowSize = 0.1
|
||||
self.glowStrength = 0.0
|
||||
self.visible = true
|
||||
self.is_on_screen = true
|
||||
local function new(x, y, r, g, b, range)
|
||||
local obj = {
|
||||
direction = 0,
|
||||
angle = math.pi * 2.0,
|
||||
x = x or 0,
|
||||
y = y or 0,
|
||||
z = 1,
|
||||
red = r or 255,
|
||||
green = g or 255,
|
||||
blue = b or 255,
|
||||
range = range or 300,
|
||||
smooth = 1.0,
|
||||
glowSize = 0.1,
|
||||
glowStrength = 0.0,
|
||||
visible = true,
|
||||
is_on_screen = true,
|
||||
}
|
||||
|
||||
return setmetatable(obj, light)
|
||||
end
|
||||
|
||||
-- set position
|
||||
@ -118,4 +122,4 @@ function light:setVisible(visible)
|
||||
self.visible = visible
|
||||
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)
|
||||
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.
|
||||
]]
|
||||
local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
|
||||
local class = require(_PACKAGE..'/class')
|
||||
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 shaders = {}
|
||||
@ -44,18 +44,16 @@ for i,v in ipairs(files) do
|
||||
end
|
||||
end
|
||||
|
||||
function post_shader:init()
|
||||
self:refreshScreenSize()
|
||||
self.effects = {}
|
||||
local function new()
|
||||
local obj = {effects = {}}
|
||||
local class = setmetatable(obj, post_shader)
|
||||
class:refreshScreenSize()
|
||||
return class
|
||||
end
|
||||
|
||||
function post_shader:refreshScreenSize(w, h)
|
||||
w, h = w or love.window.getWidth(), h or love.window.getHeight()
|
||||
|
||||
self.back_buffer = love.graphics.newCanvas(w, h)
|
||||
|
||||
self.w = w
|
||||
self.h = h
|
||||
end
|
||||
|
||||
function post_shader:addEffect(shaderName, ...)
|
||||
@ -116,7 +114,6 @@ function post_shader:drawTiltShift(canvas, args)
|
||||
end
|
||||
|
||||
function post_shader:drawShader(shaderName, canvas, args)
|
||||
local w, h = love.graphics.getWidth(), love.graphics.getHeight()
|
||||
local current_arg = 1
|
||||
|
||||
local effect = shaders[shaderName]
|
||||
@ -164,4 +161,4 @@ function process_palette(palette)
|
||||
return palette
|
||||
end
|
||||
|
||||
return post_shader
|
||||
return setmetatable({new = new}, {__call = function(_, ...) return new(...) end})
|
||||
|
Loading…
Reference in New Issue
Block a user