mirror of
https://github.com/tanema/light_world.lua.git
synced 2024-12-24 20:24:19 +00:00
added in initial animations
This commit is contained in:
parent
7d5ae45a9c
commit
ada9857394
@ -1,5 +1,4 @@
|
|||||||
:todoing
|
:todoing
|
||||||
-isinlightrange still not working right
|
|
||||||
-add body animations
|
-add body animations
|
||||||
|
|
||||||
# light_world.lua
|
# light_world.lua
|
||||||
@ -18,8 +17,6 @@ local LightWorld = require "lib" --the path to where light_world is (in this rep
|
|||||||
|
|
||||||
-- create light world
|
-- create light world
|
||||||
lightWorld = LightWorld({
|
lightWorld = LightWorld({
|
||||||
drawBackground = drawBackground, --the callback to use for drawing the background
|
|
||||||
drawForeground = drawForeground, --the callback to use for drawing the foreground
|
|
||||||
ambient = {55,55,55}, --the general ambient light in the environment
|
ambient = {55,55,55}, --the general ambient light in the environment
|
||||||
})
|
})
|
||||||
|
|
||||||
|
65
examples/animation.lua
Normal file
65
examples/animation.lua
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
-- Example: Animation Example
|
||||||
|
local LightWorld = require "lib"
|
||||||
|
|
||||||
|
function love.load()
|
||||||
|
x, y, scale = 0, 0, 1
|
||||||
|
-- load images
|
||||||
|
image = love.graphics.newImage("examples/gfx/scott_pilgrim.png")
|
||||||
|
image_normal = love.graphics.newImage("examples/gfx/scott_pilgrim_NRM.png")
|
||||||
|
|
||||||
|
-- create light world
|
||||||
|
lightWorld = LightWorld({
|
||||||
|
ambient = {55,55,55},
|
||||||
|
refractionStrength = 32.0,
|
||||||
|
reflectionVisibility = 0.75,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- create light
|
||||||
|
lightMouse = lightWorld:newLight(0, 0, 255, 127, 63, 300)
|
||||||
|
lightMouse:setGlowStrength(0.3)
|
||||||
|
|
||||||
|
-- create shadow bodys
|
||||||
|
animation = lightWorld:newAnimationGrid(image, 100, 100)
|
||||||
|
animation:setNormalMap(image_normal)
|
||||||
|
grid = animation:newGrid(108, 140)
|
||||||
|
animation:addAnimation('run right', grid('1-8', 1), 0.1)
|
||||||
|
end
|
||||||
|
|
||||||
|
function love.update(dt)
|
||||||
|
love.window.setTitle("Light vs. Shadow Engine (FPS:" .. love.timer.getFPS() .. ")")
|
||||||
|
|
||||||
|
if love.keyboard.isDown("down") then
|
||||||
|
y = y - dt * 200
|
||||||
|
elseif love.keyboard.isDown("up") then
|
||||||
|
y = y + dt * 200
|
||||||
|
end
|
||||||
|
|
||||||
|
if love.keyboard.isDown("right") then
|
||||||
|
x = x - dt * 200
|
||||||
|
elseif love.keyboard.isDown("left") then
|
||||||
|
x = x + dt * 200
|
||||||
|
end
|
||||||
|
|
||||||
|
if love.keyboard.isDown("-") then
|
||||||
|
scale = scale - 0.01
|
||||||
|
elseif love.keyboard.isDown("=") then
|
||||||
|
scale = scale + 0.01
|
||||||
|
end
|
||||||
|
|
||||||
|
lightWorld:update(dt) --only needed for animation
|
||||||
|
lightMouse:setPosition((love.mouse.getX() - x)/scale, (love.mouse.getY() - y)/scale)
|
||||||
|
end
|
||||||
|
|
||||||
|
function love.draw()
|
||||||
|
lightWorld:setTranslation(x, y, scale)
|
||||||
|
love.graphics.push()
|
||||||
|
love.graphics.translate(x, y)
|
||||||
|
love.graphics.scale(scale)
|
||||||
|
lightWorld:draw(function()
|
||||||
|
love.graphics.setColor(255, 255, 255)
|
||||||
|
love.graphics.rectangle("fill", -x/scale, -y/scale, love.graphics.getWidth()/scale, love.graphics.getHeight()/scale)
|
||||||
|
animation.animation:draw(image, 100, 100)
|
||||||
|
end)
|
||||||
|
love.graphics.pop()
|
||||||
|
end
|
||||||
|
|
@ -93,8 +93,6 @@ function love.load()
|
|||||||
ambient = {15,15,15},
|
ambient = {15,15,15},
|
||||||
refractionStrength = 16.0,
|
refractionStrength = 16.0,
|
||||||
reflectionVisibility = 0.75,
|
reflectionVisibility = 0.75,
|
||||||
drawBackground = drawBackground,
|
|
||||||
drawForeground = drawForeground
|
|
||||||
})
|
})
|
||||||
|
|
||||||
mouseLight = lightWorld:newLight(0, 0, 255, 191, 127, lightRange)
|
mouseLight = lightWorld:newLight(0, 0, 255, 191, 127, lightRange)
|
||||||
@ -195,7 +193,7 @@ function love.update(dt)
|
|||||||
for i = 1, phyCnt do
|
for i = 1, phyCnt do
|
||||||
if phyBody[i] and (phyBody[i]:isAwake() or offsetChanged) then
|
if phyBody[i] and (phyBody[i]:isAwake() or offsetChanged) then
|
||||||
if phyLight[i]:getType() == "polygon" then
|
if phyLight[i]:getType() == "polygon" then
|
||||||
--phyLight[i]:setPoints(phyBody[i]:getWorldPoints(phyShape[i]:getPoints()))
|
phyLight[i]:setPoints(phyBody[i]:getWorldPoints(phyShape[i]:getPoints()))
|
||||||
elseif phyLight[i]:getType() == "circle" then
|
elseif phyLight[i]:getType() == "circle" then
|
||||||
phyLight[i]:setPosition(phyBody[i]:getPosition())
|
phyLight[i]:setPosition(phyBody[i]:getPosition())
|
||||||
elseif phyLight[i]:getType() == "image" then
|
elseif phyLight[i]:getType() == "image" then
|
||||||
|
@ -15,8 +15,6 @@ function love.load()
|
|||||||
|
|
||||||
-- create light world
|
-- create light world
|
||||||
lightWorld = LightWorld({
|
lightWorld = LightWorld({
|
||||||
drawBackground = drawBackground,
|
|
||||||
drawForeground = drawForeground,
|
|
||||||
ambient = {55,55,55},
|
ambient = {55,55,55},
|
||||||
refractionStrength = 32.0,
|
refractionStrength = 32.0,
|
||||||
reflectionVisibility = 0.75,
|
reflectionVisibility = 0.75,
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 2.7 MiB |
Binary file not shown.
Before Width: | Height: | Size: 3.5 MiB |
BIN
examples/gfx/scott_pilgrim.png
Normal file
BIN
examples/gfx/scott_pilgrim.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
BIN
examples/gfx/scott_pilgrim_NRM.png
Normal file
BIN
examples/gfx/scott_pilgrim_NRM.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 41 KiB |
@ -15,8 +15,6 @@ function love.load()
|
|||||||
|
|
||||||
-- create light world
|
-- create light world
|
||||||
lightWorld = LightWorld({
|
lightWorld = LightWorld({
|
||||||
drawBackground = drawBackground,
|
|
||||||
drawForeground = drawForeground,
|
|
||||||
ambient = {55,55,55},
|
ambient = {55,55,55},
|
||||||
refractionStrength = 32.0,
|
refractionStrength = 32.0,
|
||||||
reflectionVisibility = 0.75,
|
reflectionVisibility = 0.75,
|
||||||
|
@ -11,8 +11,6 @@ function love.load()
|
|||||||
|
|
||||||
-- create light world
|
-- create light world
|
||||||
lightWorld = LightWorld({
|
lightWorld = LightWorld({
|
||||||
drawBackground = drawBackground,
|
|
||||||
drawForeground = drawForeground,
|
|
||||||
ambient = {55,55,55},
|
ambient = {55,55,55},
|
||||||
refractionStrength = 32.0,
|
refractionStrength = 32.0,
|
||||||
reflectionVisibility = 0.75,
|
reflectionVisibility = 0.75,
|
||||||
|
@ -16,11 +16,7 @@ function love.load()
|
|||||||
|
|
||||||
-- create light world
|
-- create light world
|
||||||
lightWorld = LightWorld({
|
lightWorld = LightWorld({
|
||||||
drawBackground = drawBackground,
|
|
||||||
drawForeground = drawForeground,
|
|
||||||
ambient = {55,55,55},
|
ambient = {55,55,55},
|
||||||
refractionStrength = 32.0,
|
|
||||||
reflectionVisibility = 0.75,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
-- create light
|
-- create light
|
||||||
|
55
lib/body.lua
55
lib/body.lua
@ -2,6 +2,7 @@ local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
|
|||||||
local class = require(_PACKAGE.."/class")
|
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 vec2 = require(_PACKAGE..'/vec2')
|
local vec2 = require(_PACKAGE..'/vec2')
|
||||||
local body = class()
|
local body = class()
|
||||||
|
|
||||||
@ -75,6 +76,17 @@ function body:init(id, type, ...)
|
|||||||
self:generateNormalMapFlat("top")
|
self:generateNormalMapFlat("top")
|
||||||
self:setShadowType('rectangle', args[4] or self.imgWidth, args[5] or self.imgHeight, args[6], args[7])
|
self:setShadowType('rectangle', args[4] or self.imgWidth, args[5] or self.imgHeight, args[6], args[7])
|
||||||
self.reflective = true
|
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.ix = 0
|
||||||
|
self.iy = 0
|
||||||
|
self.width, self.height = 16, 16
|
||||||
|
self.animations = {}
|
||||||
|
self.castsNoShadow = true
|
||||||
|
self:generateNormalMapFlat("top")
|
||||||
|
self.reflective = true
|
||||||
elseif self.type == "refraction" then
|
elseif self.type == "refraction" then
|
||||||
self:initNormal(...)
|
self:initNormal(...)
|
||||||
self.refraction = true
|
self.refraction = true
|
||||||
@ -132,6 +144,26 @@ function body:refresh()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function body:newGrid(frameWidth, frameHeight, imageWidth, imageHeight, left, top, border)
|
||||||
|
return anim8.newGrid(
|
||||||
|
frameWidth, frameHeight,
|
||||||
|
imageWidth or self.img:getWidth(), imageHeight or self.img:getHeight(),
|
||||||
|
left, top, border
|
||||||
|
)
|
||||||
|
end
|
||||||
|
-- frameWidth, frameHeight, imageWidth, imageHeight, left, top, border
|
||||||
|
function body:addAnimation(name, frames, durations, onLoop)
|
||||||
|
self.current_animation_name = self.current_animation or name
|
||||||
|
self.animations[name] = anim8.newAnimation(frames, durations, onLoop)
|
||||||
|
self.animation = self.animations[self.current_animation_name]
|
||||||
|
end
|
||||||
|
|
||||||
|
function body:update(dt)
|
||||||
|
local frame = self.animation.frames[self.animation.position]
|
||||||
|
_,_,self.width, self.height = frame:getViewport()
|
||||||
|
self.animation:update(dt)
|
||||||
|
end
|
||||||
|
|
||||||
-- set position
|
-- set position
|
||||||
function body:setPosition(x, y)
|
function body:setPosition(x, y)
|
||||||
if x ~= self.x or y ~= self.y then
|
if x ~= self.x or y ~= self.y then
|
||||||
@ -464,8 +496,12 @@ end
|
|||||||
function body:drawNormal()
|
function body:drawNormal()
|
||||||
if not self.refraction and not self.reflection and self.normalMesh then
|
if not self.refraction and not self.reflection and self.normalMesh then
|
||||||
love.graphics.setColor(255, 255, 255)
|
love.graphics.setColor(255, 255, 255)
|
||||||
|
if self.type == 'animation' then
|
||||||
|
self.animation:draw(self.normal, self.x, self.y)
|
||||||
|
else
|
||||||
love.graphics.draw(self.normalMesh, self.x - self.nx, self.y - self.ny)
|
love.graphics.draw(self.normalMesh, self.x - self.nx, self.y - self.ny)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function body:drawGlow()
|
function body:drawGlow()
|
||||||
@ -492,6 +528,11 @@ function body:drawGlow()
|
|||||||
love.graphics.setColor(0, 0, 0)
|
love.graphics.setColor(0, 0, 0)
|
||||||
end
|
end
|
||||||
love.graphics.draw(self.img, self.x - self.ix, self.y - self.iy)
|
love.graphics.draw(self.img, self.x - self.ix, self.y - self.iy)
|
||||||
|
elseif self.type == "animation" then
|
||||||
|
if self.glow then
|
||||||
|
print('glowmaps not yet supported for animations')
|
||||||
|
end
|
||||||
|
self.animation:draw(self.img, self.x - self.ix, self.y - self.iy)
|
||||||
end
|
end
|
||||||
|
|
||||||
love.graphics.setShader()
|
love.graphics.setShader()
|
||||||
@ -519,6 +560,8 @@ function body:drawRefraction()
|
|||||||
love.graphics.polygon("fill", unpack(self.data))
|
love.graphics.polygon("fill", unpack(self.data))
|
||||||
elseif self.type == "image" and self.img then
|
elseif self.type == "image" and self.img then
|
||||||
love.graphics.draw(self.img, self.x - self.ix, self.y - self.iy)
|
love.graphics.draw(self.img, self.x - self.ix, self.y - self.iy)
|
||||||
|
elseif self.type == 'animation' then
|
||||||
|
self.animation:draw(self.img, self.x - self.ix, self.y - self.iy)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -531,11 +574,19 @@ function body:drawReflection()
|
|||||||
end
|
end
|
||||||
if self.reflective and self.img then
|
if self.reflective and self.img then
|
||||||
love.graphics.setColor(0, 255, 0)
|
love.graphics.setColor(0, 255, 0)
|
||||||
|
if self.type == 'animation' then
|
||||||
|
self.animation:draw(self.img, self.x - self.ix, self.y - self.iy)
|
||||||
|
else
|
||||||
love.graphics.draw(self.img, self.x - self.ix, self.y - self.iy)
|
love.graphics.draw(self.img, self.x - self.ix, self.y - self.iy)
|
||||||
|
end
|
||||||
elseif not self.reflection and self.img then
|
elseif not self.reflection and self.img then
|
||||||
love.graphics.setColor(0, 0, 0)
|
love.graphics.setColor(0, 0, 0)
|
||||||
|
if self.type == 'animation' then
|
||||||
|
self.animation:draw(self.img, self.x - self.ix, self.y - self.iy)
|
||||||
|
else
|
||||||
love.graphics.draw(self.img, self.x - self.ix, self.y - self.iy)
|
love.graphics.draw(self.img, self.x - self.ix, self.y - self.iy)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function body:drawMaterial()
|
function body:drawMaterial()
|
||||||
@ -543,7 +594,11 @@ function body:drawMaterial()
|
|||||||
love.graphics.setShader(self.materialShader)
|
love.graphics.setShader(self.materialShader)
|
||||||
love.graphics.setColor(255, 255, 255)
|
love.graphics.setColor(255, 255, 255)
|
||||||
self.materialShader:send("material", self.material)
|
self.materialShader:send("material", self.material)
|
||||||
|
if self.type == 'animation' then
|
||||||
|
self.animation:draw(self.normal, self.x - self.nx, self.y - self.ny)
|
||||||
|
else
|
||||||
love.graphics.draw(self.normal, self.x - self.nx, self.y - self.ny)
|
love.graphics.draw(self.normal, self.x - self.nx, self.y - self.ny)
|
||||||
|
end
|
||||||
love.graphics.setShader()
|
love.graphics.setShader()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
10
lib/init.lua
10
lib/init.lua
@ -85,6 +85,15 @@ function light_world:refreshScreenSize(w, h)
|
|||||||
self.post_shader:refreshScreenSize(w, h)
|
self.post_shader:refreshScreenSize(w, h)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function light_world:update(dt)
|
||||||
|
for i = 1, #self.body do
|
||||||
|
if self.body[i]:isInRange(self.l,self.t,self.w,self.h,self.s) and
|
||||||
|
self.body[i].type == 'animation' then
|
||||||
|
self.body[i]:update(dt)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function light_world:draw(cb)
|
function light_world:draw(cb)
|
||||||
util.drawto(self.render_buffer, self.l, self.t, self.s, function()
|
util.drawto(self.render_buffer, self.l, self.t, self.s, function()
|
||||||
cb( self.l,self.t,self.w,self.h,self.s)
|
cb( self.l,self.t,self.w,self.h,self.s)
|
||||||
@ -242,6 +251,7 @@ function light_world:getBody(n) return self.body[n] end
|
|||||||
function light_world:getLightCount() return #self.lights end
|
function light_world:getLightCount() return #self.lights end
|
||||||
function light_world:getLight(n) return self.lights[n] end
|
function light_world:getLight(n) return self.lights[n] end
|
||||||
function light_world:newRectangle(...) return self:newBody("rectangle", ...) end
|
function light_world:newRectangle(...) return self:newBody("rectangle", ...) end
|
||||||
|
function light_world:newAnimationGrid(...) return self:newBody("animation", ...) end
|
||||||
function light_world:newCircle(...) return self:newBody("circle", ...) end
|
function light_world:newCircle(...) return self:newBody("circle", ...) end
|
||||||
function light_world:newPolygon(...) return self:newBody("polygon", ...) end
|
function light_world:newPolygon(...) return self:newBody("polygon", ...) end
|
||||||
function light_world:newImage(...) return self:newBody("image", ...) end
|
function light_world:newImage(...) return self:newBody("image", ...) end
|
||||||
|
4
main.lua
4
main.lua
@ -179,10 +179,8 @@ function exf.resume()
|
|||||||
|
|
||||||
-- create light world
|
-- create light world
|
||||||
lightWorld = LightWorld({
|
lightWorld = LightWorld({
|
||||||
drawBackground = exf.drawBackground,
|
ambient = {127, 127, 127}
|
||||||
drawForeground = exf.drawForeground
|
|
||||||
})
|
})
|
||||||
lightWorld:setAmbientColor(127, 127, 127)
|
|
||||||
|
|
||||||
-- create light
|
-- create light
|
||||||
lightMouse = lightWorld:newLight(0, 0, 255, 127, 63, 500)
|
lightMouse = lightWorld:newLight(0, 0, 255, 127, 63, 500)
|
||||||
|
Loading…
Reference in New Issue
Block a user