diff --git a/README.md b/README.md index b445dcb..c57b0ee 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ :todoing --isinlightrange still not working right -add body animations # light_world.lua @@ -18,8 +17,6 @@ local LightWorld = require "lib" --the path to where light_world is (in this rep -- create light world 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 }) diff --git a/examples/animation.lua b/examples/animation.lua new file mode 100644 index 0000000..d2f77a3 --- /dev/null +++ b/examples/animation.lua @@ -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 + diff --git a/examples/complex.lua b/examples/complex.lua index f0a9e26..077a911 100644 --- a/examples/complex.lua +++ b/examples/complex.lua @@ -93,8 +93,6 @@ function love.load() ambient = {15,15,15}, refractionStrength = 16.0, reflectionVisibility = 0.75, - drawBackground = drawBackground, - drawForeground = drawForeground }) mouseLight = lightWorld:newLight(0, 0, 255, 191, 127, lightRange) @@ -195,7 +193,7 @@ function love.update(dt) for i = 1, phyCnt do if phyBody[i] and (phyBody[i]:isAwake() or offsetChanged) 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 phyLight[i]:setPosition(phyBody[i]:getPosition()) elseif phyLight[i]:getType() == "image" then diff --git a/examples/gamera.lua b/examples/gamera.lua index 3fa2587..794848d 100644 --- a/examples/gamera.lua +++ b/examples/gamera.lua @@ -15,8 +15,6 @@ function love.load() -- create light world lightWorld = LightWorld({ - drawBackground = drawBackground, - drawForeground = drawForeground, ambient = {55,55,55}, refractionStrength = 32.0, reflectionVisibility = 0.75, diff --git a/examples/gfx/braid.png b/examples/gfx/braid.png deleted file mode 100644 index 7193775..0000000 Binary files a/examples/gfx/braid.png and /dev/null differ diff --git a/examples/gfx/braid_NRM.png b/examples/gfx/braid_NRM.png deleted file mode 100644 index 21aa04b..0000000 Binary files a/examples/gfx/braid_NRM.png and /dev/null differ diff --git a/examples/gfx/scott_pilgrim.png b/examples/gfx/scott_pilgrim.png new file mode 100644 index 0000000..a5f091b Binary files /dev/null and b/examples/gfx/scott_pilgrim.png differ diff --git a/examples/gfx/scott_pilgrim_NRM.png b/examples/gfx/scott_pilgrim_NRM.png new file mode 100644 index 0000000..f749c66 Binary files /dev/null and b/examples/gfx/scott_pilgrim_NRM.png differ diff --git a/examples/hump.lua b/examples/hump.lua index 87964ed..d5a198c 100644 --- a/examples/hump.lua +++ b/examples/hump.lua @@ -15,8 +15,6 @@ function love.load() -- create light world lightWorld = LightWorld({ - drawBackground = drawBackground, - drawForeground = drawForeground, ambient = {55,55,55}, refractionStrength = 32.0, reflectionVisibility = 0.75, diff --git a/examples/normalMap.lua b/examples/normalMap.lua index 9cc1e02..8230c55 100644 --- a/examples/normalMap.lua +++ b/examples/normalMap.lua @@ -11,8 +11,6 @@ function love.load() -- create light world lightWorld = LightWorld({ - drawBackground = drawBackground, - drawForeground = drawForeground, ambient = {55,55,55}, refractionStrength = 32.0, reflectionVisibility = 0.75, diff --git a/examples/short.lua b/examples/short.lua index a6f50c8..a798d38 100644 --- a/examples/short.lua +++ b/examples/short.lua @@ -16,11 +16,7 @@ function love.load() -- create light world lightWorld = LightWorld({ - drawBackground = drawBackground, - drawForeground = drawForeground, ambient = {55,55,55}, - refractionStrength = 32.0, - reflectionVisibility = 0.75, }) -- create light diff --git a/lib/body.lua b/lib/body.lua index 1580737..e685686 100644 --- a/lib/body.lua +++ b/lib/body.lua @@ -1,9 +1,10 @@ -local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or "" -local class = require(_PACKAGE.."/class") -local normal_map = require(_PACKAGE..'/normal_map') -local util = require(_PACKAGE..'/util') -local vec2 = require(_PACKAGE..'/vec2') -local body = class() +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 vec2 = require(_PACKAGE..'/vec2') +local body = class() body.glowShader = love.graphics.newShader(_PACKAGE.."/shaders/glow.glsl") body.materialShader = love.graphics.newShader(_PACKAGE.."/shaders/material.glsl") @@ -75,6 +76,17 @@ function body:init(id, type, ...) 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.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 self:initNormal(...) self.refraction = true @@ -132,6 +144,26 @@ function body:refresh() 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 function body:setPosition(x, y) if x ~= self.x or y ~= self.y then @@ -464,7 +496,11 @@ end function body:drawNormal() if not self.refraction and not self.reflection and self.normalMesh then love.graphics.setColor(255, 255, 255) - love.graphics.draw(self.normalMesh, self.x - self.nx, self.y - self.ny) + 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) + end end end @@ -492,6 +528,11 @@ function body:drawGlow() love.graphics.setColor(0, 0, 0) end 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 love.graphics.setShader() @@ -519,6 +560,8 @@ function body:drawRefraction() love.graphics.polygon("fill", unpack(self.data)) elseif self.type == "image" and self.img then 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 @@ -531,10 +574,18 @@ function body:drawReflection() end if self.reflective and self.img then love.graphics.setColor(0, 255, 0) - love.graphics.draw(self.img, self.x - self.ix, self.y - self.iy) + 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) + end elseif not self.reflection and self.img then love.graphics.setColor(0, 0, 0) - love.graphics.draw(self.img, self.x - self.ix, self.y - self.iy) + 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) + end end end @@ -543,7 +594,11 @@ function body:drawMaterial() love.graphics.setShader(self.materialShader) love.graphics.setColor(255, 255, 255) self.materialShader:send("material", self.material) - love.graphics.draw(self.normal, self.x - self.nx, self.y - self.ny) + 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) + end love.graphics.setShader() end end diff --git a/lib/init.lua b/lib/init.lua index fe14315..33c3171 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -85,6 +85,15 @@ function light_world:refreshScreenSize(w, h) self.post_shader:refreshScreenSize(w, h) 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) util.drawto(self.render_buffer, self.l, self.t, self.s, function() 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:getLight(n) return self.lights[n] 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:newPolygon(...) return self:newBody("polygon", ...) end function light_world:newImage(...) return self:newBody("image", ...) end diff --git a/main.lua b/main.lua index 31b52fc..2026d20 100644 --- a/main.lua +++ b/main.lua @@ -179,10 +179,8 @@ function exf.resume() -- create light world lightWorld = LightWorld({ - drawBackground = exf.drawBackground, - drawForeground = exf.drawForeground + ambient = {127, 127, 127} }) - lightWorld:setAmbientColor(127, 127, 127) -- create light lightMouse = lightWorld:newLight(0, 0, 255, 127, 63, 500)