2014-09-26 16:48:46 +00:00
|
|
|
--[[
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
2014-11-28 21:56:05 +00:00
|
|
|
Copyright (c) 2014 Marcus Ihde, Tim Anema
|
2014-09-26 16:48:46 +00:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
]]
|
2014-10-27 19:16:21 +00:00
|
|
|
local _PACKAGE = string.gsub(...,"%.","/") or ""
|
2014-12-02 01:41:09 +00:00
|
|
|
if string.len(_PACKAGE) > 0 then _PACKAGE = _PACKAGE .. "/" end
|
2014-10-27 19:16:21 +00:00
|
|
|
local Light = require(_PACKAGE..'light')
|
|
|
|
local Body = require(_PACKAGE..'body')
|
|
|
|
local util = require(_PACKAGE..'util')
|
|
|
|
local PostShader = require(_PACKAGE..'postshader')
|
2014-09-26 16:48:46 +00:00
|
|
|
|
2014-12-23 21:51:07 +00:00
|
|
|
local light_world = {}
|
|
|
|
light_world.__index = light_world
|
2014-09-26 20:52:16 +00:00
|
|
|
|
2017-07-17 21:01:13 +00:00
|
|
|
light_world.image_mask = util.loadShader(_PACKAGE.."/shaders/image_mask.glsl")
|
|
|
|
light_world.shadowShader = util.loadShader(_PACKAGE.."/shaders/shadow.glsl")
|
|
|
|
light_world.refractionShader = util.loadShader(_PACKAGE.."/shaders/refraction.glsl")
|
|
|
|
light_world.reflectionShader = util.loadShader(_PACKAGE.."/shaders/reflection.glsl")
|
2014-09-29 14:03:15 +00:00
|
|
|
|
2014-12-23 21:51:07 +00:00
|
|
|
local function new(options)
|
|
|
|
local obj = {}
|
|
|
|
obj.lights = {}
|
|
|
|
obj.bodies = {}
|
|
|
|
obj.post_shader = PostShader()
|
2017-07-17 21:01:13 +00:00
|
|
|
|
|
|
|
obj.l, obj.t, obj.s = 0, 0, 1
|
2014-12-23 21:51:07 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
obj.disableGlow = false
|
|
|
|
obj.disableMaterial = false
|
|
|
|
obj.disableReflection = true
|
|
|
|
obj.disableRefraction = true
|
2014-12-11 02:42:04 +00:00
|
|
|
|
2014-10-03 00:32:31 +00:00
|
|
|
options = options or {}
|
2014-12-23 21:51:07 +00:00
|
|
|
for k, v in pairs(options) do obj[k] = v end
|
2019-09-07 11:36:40 +00:00
|
|
|
for i, v in ipairs(obj.ambient) do if v > 1 then obj.ambient[i] = v / 255 end end
|
2014-09-29 14:03:15 +00:00
|
|
|
|
2014-12-23 21:51:07 +00:00
|
|
|
local world = setmetatable(obj, light_world)
|
|
|
|
world:refreshScreenSize()
|
|
|
|
|
|
|
|
return world
|
2014-09-26 20:52:16 +00:00
|
|
|
end
|
2014-09-26 17:38:55 +00:00
|
|
|
|
2014-10-08 12:55:05 +00:00
|
|
|
function light_world:refreshScreenSize(w, h)
|
2016-02-03 14:45:22 +00:00
|
|
|
w, h = w or love.graphics.getWidth(), h or love.graphics.getHeight()
|
2014-10-08 12:55:05 +00:00
|
|
|
|
2014-11-30 20:24:58 +00:00
|
|
|
self.w, self.h = w, h
|
2014-10-06 13:31:14 +00:00
|
|
|
self.render_buffer = love.graphics.newCanvas(w, h)
|
2014-12-20 03:50:42 +00:00
|
|
|
self.shadow_buffer = love.graphics.newCanvas(w, h)
|
2014-10-06 13:31:14 +00:00
|
|
|
self.normalMap = love.graphics.newCanvas(w, h)
|
2014-11-30 00:10:15 +00:00
|
|
|
self.shadowMap = love.graphics.newCanvas(w, h)
|
2014-10-06 13:31:14 +00:00
|
|
|
self.glowMap = love.graphics.newCanvas(w, h)
|
|
|
|
self.refractionMap = love.graphics.newCanvas(w, h)
|
|
|
|
self.reflectionMap = love.graphics.newCanvas(w, h)
|
2014-10-08 12:55:05 +00:00
|
|
|
|
|
|
|
self.post_shader:refreshScreenSize(w, h)
|
2014-09-26 17:38:55 +00:00
|
|
|
end
|
2014-09-27 16:54:48 +00:00
|
|
|
|
2014-12-04 16:42:28 +00:00
|
|
|
function light_world:update(dt)
|
2014-12-21 18:14:46 +00:00
|
|
|
for i = 1, #self.bodies do
|
|
|
|
self.bodies[i].is_on_screen = self.bodies[i]:inRange(-self.l,-self.t,self.w,self.h,self.s)
|
|
|
|
if self.bodies[i]:isVisible() then
|
|
|
|
self.bodies[i]:update(dt)
|
2014-12-04 16:42:28 +00:00
|
|
|
end
|
|
|
|
end
|
2014-12-21 18:14:46 +00:00
|
|
|
for i = 1, #self.lights do
|
|
|
|
self.lights[i].is_on_screen = self.lights[i]:inRange(self.l,self.t,self.w,self.h,self.s)
|
|
|
|
end
|
2014-12-04 16:42:28 +00:00
|
|
|
end
|
|
|
|
|
2014-11-30 20:24:58 +00:00
|
|
|
function light_world:draw(cb)
|
2019-09-07 11:36:40 +00:00
|
|
|
util.drawto(self.render_buffer, self.l, self.t, self.s, false, function()
|
|
|
|
cb(self.l,self.t,self.w,self.h,self.s)
|
2014-12-11 02:42:04 +00:00
|
|
|
_ = self.disableMaterial or self:drawMaterial( self.l,self.t,self.w,self.h,self.s)
|
2014-12-21 18:14:46 +00:00
|
|
|
self:drawShadows( self.l,self.t,self.w,self.h,self.s)
|
2014-12-11 02:42:04 +00:00
|
|
|
_ = self.disableGlow or self:drawGlow( self.l,self.t,self.w,self.h,self.s)
|
|
|
|
_ = self.disableRefraction or self:drawRefraction( self.l,self.t,self.w,self.h,self.s)
|
|
|
|
_ = self.disableReflection or self:drawReflection( self.l,self.t,self.w,self.h,self.s)
|
2014-10-22 02:48:19 +00:00
|
|
|
end)
|
2014-11-30 20:24:58 +00:00
|
|
|
self.post_shader:drawWith(self.render_buffer, self.l, self.t, self.s)
|
2014-10-22 12:37:19 +00:00
|
|
|
end
|
2014-10-22 02:48:19 +00:00
|
|
|
|
2014-11-28 15:55:43 +00:00
|
|
|
-- draw normal shading
|
2014-12-21 18:14:46 +00:00
|
|
|
function light_world:drawShadows(l,t,w,h,s)
|
2016-02-04 16:33:22 +00:00
|
|
|
love.graphics.setCanvas( self.normalMap )
|
|
|
|
love.graphics.clear()
|
|
|
|
love.graphics.setCanvas()
|
2019-09-07 11:36:40 +00:00
|
|
|
util.drawto(self.normalMap, l, t, s, false, function()
|
2014-12-21 18:14:46 +00:00
|
|
|
for i = 1, #self.bodies do
|
|
|
|
if self.bodies[i]:isVisible() then
|
|
|
|
self.bodies[i]:drawNormal()
|
2014-12-03 19:08:44 +00:00
|
|
|
end
|
2014-10-22 12:37:19 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2014-12-20 05:21:00 +00:00
|
|
|
self.shadowShader:send('normalMap', self.normalMap)
|
|
|
|
self.shadowShader:send("invert_normal", self.normalInvert == true)
|
|
|
|
|
2016-02-03 14:45:22 +00:00
|
|
|
love.graphics.setCanvas( self.shadow_buffer )
|
|
|
|
love.graphics.clear()
|
|
|
|
love.graphics.setCanvas()
|
2014-10-22 12:37:19 +00:00
|
|
|
for i = 1, #self.lights do
|
2014-12-20 03:50:42 +00:00
|
|
|
local light = self.lights[i]
|
2015-01-04 03:18:48 +00:00
|
|
|
if light:isVisible() then
|
2014-12-03 19:08:44 +00:00
|
|
|
-- create shadow map for this light
|
2016-02-03 14:45:22 +00:00
|
|
|
love.graphics.setCanvas( self.shadowMap )
|
|
|
|
love.graphics.clear()
|
|
|
|
love.graphics.setCanvas()
|
2018-05-30 03:45:58 +00:00
|
|
|
|
2019-09-07 11:36:40 +00:00
|
|
|
util.drawto(self.shadowMap, l, t, s, true, function()
|
2015-01-04 03:18:48 +00:00
|
|
|
--I dont know if it uses both or just calls both
|
2016-02-03 14:45:22 +00:00
|
|
|
love.graphics.stencil(function()
|
2014-12-21 21:00:06 +00:00
|
|
|
local angle = light.direction - (light.angle / 2.0)
|
2014-12-21 18:14:46 +00:00
|
|
|
love.graphics.arc("fill", light.x, light.y, light.range, angle, angle + light.angle)
|
2014-12-20 03:50:42 +00:00
|
|
|
end)
|
2016-02-04 16:33:22 +00:00
|
|
|
love.graphics.setStencilTest("greater",0)
|
2016-02-03 14:45:22 +00:00
|
|
|
love.graphics.stencil(function()
|
2015-01-04 03:18:48 +00:00
|
|
|
love.graphics.setShader(self.image_mask)
|
|
|
|
for k = 1, #self.bodies do
|
|
|
|
if self.bodies[k]:inLightRange(light) and self.bodies[k]:isVisible() then
|
|
|
|
self.bodies[k]:drawStencil()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
love.graphics.setShader()
|
|
|
|
end)
|
2016-02-04 16:33:22 +00:00
|
|
|
love.graphics.setStencilTest("equal", 0)
|
2014-12-21 18:14:46 +00:00
|
|
|
for k = 1, #self.bodies do
|
2015-01-04 03:18:48 +00:00
|
|
|
if self.bodies[k]:inLightRange(light) and self.bodies[k]:isVisible() then
|
|
|
|
self.bodies[k]:drawShadow(light)
|
2014-12-03 19:08:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
2018-05-30 03:45:58 +00:00
|
|
|
|
2014-12-03 19:08:44 +00:00
|
|
|
-- draw scene for this light using normals and shadowmap
|
2019-09-07 11:36:40 +00:00
|
|
|
self.shadowShader:send('lightColor', {light.red, light.green, light.blue})
|
|
|
|
self.shadowShader:send("lightPosition", {(light.x + l/s) * s, (light.y + t/s) * s, (light.z * 10) / 255})
|
2016-11-23 02:01:11 +00:00
|
|
|
self.shadowShader:send('lightRange',light.range * s)
|
2014-12-20 03:50:42 +00:00
|
|
|
self.shadowShader:send("lightSmooth", light.smooth)
|
|
|
|
self.shadowShader:send("lightGlow", {1.0 - light.glowSize, light.glowStrength})
|
2014-12-20 05:21:00 +00:00
|
|
|
util.drawCanvasToCanvas(self.shadowMap, self.shadow_buffer, {
|
2016-02-03 14:45:22 +00:00
|
|
|
blendmode = 'add',
|
2014-12-20 03:50:42 +00:00
|
|
|
shader = self.shadowShader,
|
|
|
|
stencil = function()
|
2014-12-21 21:00:06 +00:00
|
|
|
local angle = light.direction - (light.angle / 2.0)
|
2019-09-08 04:53:47 +00:00
|
|
|
love.graphics.arc(
|
|
|
|
"fill", (light.x + l/s) * s, (light.y + t/s) * s, light.range, angle, angle + light.angle
|
|
|
|
)
|
2014-12-20 03:50:42 +00:00
|
|
|
end
|
|
|
|
})
|
2014-12-03 19:08:44 +00:00
|
|
|
end
|
2014-10-22 12:37:19 +00:00
|
|
|
end
|
|
|
|
|
2014-12-02 01:41:09 +00:00
|
|
|
-- add in ambient color
|
2019-09-07 11:36:40 +00:00
|
|
|
util.drawto(self.shadow_buffer, l, t, s, false, function()
|
2016-02-03 14:45:22 +00:00
|
|
|
love.graphics.setBlendMode("add")
|
2014-10-22 12:37:19 +00:00
|
|
|
love.graphics.setColor({self.ambient[1], self.ambient[2], self.ambient[3]})
|
2014-11-07 01:03:00 +00:00
|
|
|
love.graphics.rectangle("fill", -l/s, -t/s, w/s,h/s)
|
2014-10-22 12:37:19 +00:00
|
|
|
end)
|
|
|
|
|
2014-12-20 03:50:42 +00:00
|
|
|
self.post_shader:drawBlur(self.shadow_buffer, {self.shadowBlur})
|
2016-02-03 14:45:22 +00:00
|
|
|
util.drawCanvasToCanvas(self.shadow_buffer, self.render_buffer, {blendmode = "multiply"})
|
2016-02-04 16:33:22 +00:00
|
|
|
love.graphics.setStencilTest()
|
2014-09-27 16:59:51 +00:00
|
|
|
end
|
|
|
|
|
2014-09-26 17:38:55 +00:00
|
|
|
-- draw material
|
2014-10-03 00:32:31 +00:00
|
|
|
function light_world:drawMaterial(l,t,w,h,s)
|
2014-12-21 18:14:46 +00:00
|
|
|
for i = 1, #self.bodies do
|
|
|
|
if self.bodies[i]:isVisible() then
|
|
|
|
self.bodies[i]:drawMaterial()
|
2014-12-03 19:08:44 +00:00
|
|
|
end
|
2014-09-26 17:38:55 +00:00
|
|
|
end
|
|
|
|
end
|
2014-09-27 16:59:51 +00:00
|
|
|
|
2014-09-26 17:38:55 +00:00
|
|
|
-- draw glow
|
2014-10-03 00:32:31 +00:00
|
|
|
function light_world:drawGlow(l,t,w,h,s)
|
2014-12-02 01:41:09 +00:00
|
|
|
if self.glowDown then
|
|
|
|
self.glowTimer = math.max(0.0, self.glowTimer - love.timer.getDelta())
|
|
|
|
else
|
|
|
|
self.glowTimer = math.min(self.glowTimer + love.timer.getDelta(), 1.0)
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.glowTimer == 1.0 or self.glowTimer == 0.0 then
|
|
|
|
self.glowDown = not self.glowDown
|
2014-09-27 16:59:51 +00:00
|
|
|
end
|
2014-10-22 12:37:19 +00:00
|
|
|
|
2014-12-11 02:42:04 +00:00
|
|
|
local has_glow = false
|
2014-10-22 12:37:19 +00:00
|
|
|
-- create glow map
|
2016-02-03 14:45:22 +00:00
|
|
|
love.graphics.setCanvas( self.glowMap )
|
|
|
|
love.graphics.clear()
|
|
|
|
love.graphics.setCanvas()
|
2019-09-07 11:36:40 +00:00
|
|
|
util.drawto(self.glowMap, l, t, s, false, function()
|
2014-12-21 18:14:46 +00:00
|
|
|
for i = 1, #self.bodies do
|
|
|
|
if self.bodies[i]:isVisible() and self.bodies[i].glowStrength > 0.0 then
|
2014-12-11 02:42:04 +00:00
|
|
|
has_glow = true
|
2014-12-21 18:14:46 +00:00
|
|
|
self.bodies[i]:drawGlow()
|
2014-12-03 19:08:44 +00:00
|
|
|
end
|
2014-10-22 12:37:19 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2014-12-11 02:42:04 +00:00
|
|
|
if has_glow then
|
2014-12-20 03:50:42 +00:00
|
|
|
self.post_shader:drawBlur(self.glowMap, {self.glowBlur})
|
2016-02-03 14:45:22 +00:00
|
|
|
util.drawCanvasToCanvas(self.glowMap, self.render_buffer, {blendmode = "add"})
|
2014-12-11 02:42:04 +00:00
|
|
|
end
|
2014-09-26 17:38:55 +00:00
|
|
|
end
|
|
|
|
-- draw refraction
|
2014-10-03 00:32:31 +00:00
|
|
|
function light_world:drawRefraction(l,t,w,h,s)
|
2014-10-22 12:37:19 +00:00
|
|
|
-- create refraction map
|
2016-02-03 14:45:22 +00:00
|
|
|
love.graphics.setCanvas( self.refractionMap )
|
|
|
|
love.graphics.clear()
|
|
|
|
love.graphics.setCanvas()
|
2019-09-07 11:36:40 +00:00
|
|
|
util.drawto(self.refractionMap, l, t, s, false, function()
|
2014-12-21 18:14:46 +00:00
|
|
|
for i = 1, #self.bodies do
|
|
|
|
if self.bodies[i]:isVisible() then
|
|
|
|
self.bodies[i]:drawRefraction()
|
2014-12-03 19:09:42 +00:00
|
|
|
end
|
2014-10-22 12:37:19 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2014-12-20 03:50:42 +00:00
|
|
|
self.refractionShader:send("backBuffer", self.render_buffer)
|
2014-10-01 13:03:59 +00:00
|
|
|
self.refractionShader:send("refractionStrength", self.refractionStrength)
|
2014-10-22 12:37:19 +00:00
|
|
|
util.drawCanvasToCanvas(self.refractionMap, self.render_buffer, {shader = self.refractionShader})
|
2014-09-26 17:38:55 +00:00
|
|
|
end
|
2014-09-27 16:59:51 +00:00
|
|
|
|
2014-09-26 17:38:55 +00:00
|
|
|
-- draw reflection
|
2014-10-03 00:32:31 +00:00
|
|
|
function light_world:drawReflection(l,t,w,h,s)
|
2014-10-22 12:37:19 +00:00
|
|
|
-- create reflection map
|
2016-02-03 14:45:22 +00:00
|
|
|
love.graphics.setCanvas( self.reflectionMap )
|
|
|
|
love.graphics.clear()
|
|
|
|
love.graphics.setCanvas()
|
2019-09-07 11:36:40 +00:00
|
|
|
util.drawto(self.reflectionMap, l, t, s, false, function()
|
2014-12-21 18:14:46 +00:00
|
|
|
for i = 1, #self.bodies do
|
|
|
|
if self.bodies[i]:isVisible() then
|
|
|
|
self.bodies[i]:drawReflection()
|
2014-12-03 19:08:44 +00:00
|
|
|
end
|
2014-10-22 12:37:19 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2014-12-20 03:50:42 +00:00
|
|
|
self.reflectionShader:send("backBuffer", self.render_buffer)
|
2014-10-01 13:03:59 +00:00
|
|
|
self.reflectionShader:send("reflectionStrength", self.reflectionStrength)
|
|
|
|
self.reflectionShader:send("reflectionVisibility", self.reflectionVisibility)
|
2014-10-22 12:37:19 +00:00
|
|
|
util.drawCanvasToCanvas(self.reflectionMap, self.render_buffer, {shader = self.reflectionShader})
|
2014-09-26 17:38:55 +00:00
|
|
|
end
|
2014-09-27 16:59:51 +00:00
|
|
|
|
2014-09-26 17:38:55 +00:00
|
|
|
-- new light
|
|
|
|
function light_world:newLight(x, y, red, green, blue, range)
|
2014-10-03 00:32:31 +00:00
|
|
|
self.lights[#self.lights + 1] = Light(x, y, red, green, blue, range)
|
2014-09-26 17:38:55 +00:00
|
|
|
return self.lights[#self.lights]
|
|
|
|
end
|
2014-09-27 16:59:51 +00:00
|
|
|
|
2014-10-27 13:20:01 +00:00
|
|
|
function light_world:clear()
|
|
|
|
light_world:clearLights()
|
2014-10-28 01:32:51 +00:00
|
|
|
light_world:clearBodies()
|
2014-10-27 13:20:01 +00:00
|
|
|
end
|
|
|
|
|
2017-07-17 21:01:13 +00:00
|
|
|
function light_world:setTranslation(l, t, s)
|
2014-12-04 00:22:45 +00:00
|
|
|
self.l, self.t, self.s = l or self.l, t or self.t, s or self.s
|
|
|
|
end
|
|
|
|
|
2014-12-02 01:41:09 +00:00
|
|
|
function light_world:setScale(s) self.s = s end
|
|
|
|
function light_world:clearLights() self.lights = {} end
|
2014-12-21 18:14:46 +00:00
|
|
|
function light_world:clearBodies() self.bodies = {} end
|
2019-09-07 11:36:40 +00:00
|
|
|
function light_world:setAmbientColor(red, green, blue)
|
|
|
|
self.ambient = {red, green, blue}
|
|
|
|
for i, v in ipairs(self.ambient) do if v > 1 then self.ambient[i] = v / 255 end end
|
|
|
|
end
|
2014-12-06 17:33:00 +00:00
|
|
|
function light_world:setShadowBlur(blur) self.shadowBlur = blur end
|
2014-12-02 01:41:09 +00:00
|
|
|
function light_world:setGlowStrength(strength) self.glowBlur = strength end
|
|
|
|
function light_world:setRefractionStrength(strength) self.refractionStrength = strength end
|
|
|
|
function light_world:setReflectionStrength(strength) self.reflectionStrength = strength end
|
|
|
|
function light_world:setReflectionVisibility(visibility) self.reflectionVisibility = visibility end
|
2014-12-21 18:14:46 +00:00
|
|
|
function light_world:getBodyCount() return #self.bodies end
|
|
|
|
function light_world:getBody(n) return self.bodies[n] end
|
2014-12-02 01:41:09 +00:00
|
|
|
function light_world:getLightCount() return #self.lights end
|
|
|
|
function light_world:getLight(n) return self.lights[n] end
|
|
|
|
function light_world:newRectangle(...) return self:newBody("rectangle", ...) end
|
2014-12-04 16:42:28 +00:00
|
|
|
function light_world:newAnimationGrid(...) return self:newBody("animation", ...) end
|
2014-12-02 01:41:09 +00:00
|
|
|
function light_world:newCircle(...) return self:newBody("circle", ...) end
|
|
|
|
function light_world:newPolygon(...) return self:newBody("polygon", ...) end
|
|
|
|
function light_world:newImage(...) return self:newBody("image", ...) end
|
2014-12-11 02:42:04 +00:00
|
|
|
|
2017-07-17 21:01:13 +00:00
|
|
|
function light_world:newRefraction(...)
|
2014-12-11 02:42:04 +00:00
|
|
|
self.disableRefraction = false
|
2017-07-17 21:01:13 +00:00
|
|
|
return self:newBody("refraction", ...)
|
2014-12-11 02:42:04 +00:00
|
|
|
end
|
2017-07-17 21:01:13 +00:00
|
|
|
function light_world:newReflection(normal, ...)
|
2014-12-11 02:42:04 +00:00
|
|
|
self.disableReflection = false
|
2017-07-17 21:01:13 +00:00
|
|
|
return self:newBody("reflection", ...)
|
2014-12-11 02:42:04 +00:00
|
|
|
end
|
2014-09-27 17:46:46 +00:00
|
|
|
|
2014-09-26 17:38:55 +00:00
|
|
|
-- new body
|
|
|
|
function light_world:newBody(type, ...)
|
2014-12-21 18:14:46 +00:00
|
|
|
local id = #self.bodies + 1
|
|
|
|
self.bodies[id] = Body(id, type, ...)
|
|
|
|
return self.bodies[#self.bodies]
|
2014-09-26 17:38:55 +00:00
|
|
|
end
|
2014-09-27 17:46:46 +00:00
|
|
|
|
2015-01-12 01:01:34 +00:00
|
|
|
function light_world:is_body(target)
|
|
|
|
return target.type ~= nil
|
|
|
|
end
|
|
|
|
|
|
|
|
function light_world:is_light(target)
|
|
|
|
return target.angle ~= nil
|
|
|
|
end
|
|
|
|
|
2014-11-03 22:54:15 +00:00
|
|
|
function light_world:remove(to_kill)
|
2015-01-12 01:01:34 +00:00
|
|
|
if self:is_body(to_kill) then
|
2014-12-21 18:14:46 +00:00
|
|
|
for i = 1, #self.bodies do
|
|
|
|
if self.bodies[i] == to_kill then
|
|
|
|
table.remove(self.bodies, i)
|
2014-11-03 22:54:15 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
2015-01-12 01:01:34 +00:00
|
|
|
elseif self:is_light(to_kill) then
|
2014-11-03 22:54:15 +00:00
|
|
|
for i = 1, #self.lights do
|
|
|
|
if self.lights[i] == to_kill then
|
|
|
|
table.remove(self.lights, i)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- failed to find it
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2014-12-23 21:51:07 +00:00
|
|
|
return setmetatable({new = new}, {__call = function(_, ...) return new(...) end})
|