Rehandled Darkness and Lights. Refactored lots of stuff. Rewritten shaders.

This commit is contained in:
lustlion 2022-02-21 18:24:20 +01:00
parent 192b1e6ca0
commit a944192f67
19 changed files with 166 additions and 202 deletions

View File

@ -1,4 +1,10 @@
Collision = {}
LoadedObjects.Collisions = {}
LoadedObjects.Platforms = {}
LoadedObjects.Ladders = {}
LoadedObjects.Hazards = {}
--[[
Collision

44
code/darkness.lua Normal file
View File

@ -0,0 +1,44 @@
Darkness = {}
Canvas = {}
function Darkness.Create()
Darkness.width = game.width/game.scale
Darkness.height = game.height/game.scale
Canvas.Darkness = love.graphics.newCanvas(
Darkness.width,
Darkness.height
)
end
function Darkness.Recreate()
Canvas.Darkness:release()
Darkness.Create()
end
function Darkness.Reset()
love.graphics.setCanvas(Canvas.Darkness)
love.graphics.setBlendMode("replace")
love.graphics.setColor(0.1,0.1,0.1,1)
love.graphics.rectangle(
"fill",
0,
0,
Darkness.width,
Darkness.height
)
love.graphics.setCanvas()
end
function Darkness.DrawStart()
Darkness.Reset()
love.graphics.setCanvas(Canvas.Darkness)
end
function Darkness.DrawEnd()
love.graphics.setCanvas()
love.graphics.setBlendMode("alpha")
love.graphics.setColor(1,1,1,1)
love.graphics.draw(Canvas.Darkness)
end
Darkness.Create()

View File

@ -1,6 +1,6 @@
function DebugUI()
local mouse_x, mouse_y = love.mouse.getPosition()
for _, light in pairs(Lights) do
for _, light in pairs(LoadedObjects.Lights) do
love.graphics.print(light.pos.x,light.pos.x,light.pos.y)
love.graphics.print(light.pos.y,light.pos.x,light.pos.y+20)
love.graphics.print(light.pos.x,light.pos.x,light.pos.y+40)

View File

@ -27,7 +27,7 @@ CursedBook = Entity:New(x,y)
-- light
o.light_range = 500
o.light = CreateLight(o.pos.x,o.pos.y,o.light_range,2,HEX2RGB("#fe00d1"))
o.light = Light:New(o.pos.x,o.pos.y,o.light_range,2,HEX2RGB("#fe00d1"))
table.insert(LoadedObjects.Entities,o)
o.id = #LoadedObjects.Entities

View File

@ -14,7 +14,7 @@ function Decoration:New(x,y,animation,lightRange)
if lightRange ~= nil then
o.lightRange = lightRange
o.light = CreateLight(o.pos.x,o.pos.y,o.lightRange)
o.light = Light:New(o.pos.x,o.pos.y,o.lightRange)
end
table.insert(LoadedObjects.Entities,o)

View File

@ -20,7 +20,7 @@ Fairy = Entity:New(x,y)
-- light
o.light_range = 80
o.light = CreateLight(o.pos.x,o.pos.y,o.light_range,nil,HEX2RGB("#fed100"))
o.light = Light:New(o.pos.x,o.pos.y,o.light_range,nil,HEX2RGB("#fed100"))
-- timer
o.particle_timer = 0

View File

@ -27,6 +27,8 @@ function HookAnchor:HandleAnimation()
end
function HookAnchor:DrawBackground()
Entity.DrawBackground(self)
love.graphics.setColor(1,1,1,1)
love.graphics.circle(
"line",
-Camera.pos.x + self.pos.x,

View File

@ -28,7 +28,7 @@ Kupo = Entity:New(x,y)
o.hostile = true
o.lightRange = o.range/2
o.light = CreateLight(o.pos.x,o.pos.y,o.lightRange)
o.light = Light:New(o.pos.x,o.pos.y,o.lightRange)
table.insert(LoadedObjects.Entities,o)
o.id = #LoadedObjects.Entities

View File

@ -1,4 +1,5 @@
Particle = Entity:New(x,y)
LoadedObjects.Particles = {}
function Particle:New(x,y,particle_data)
local o = Entity:New(x,y)
@ -31,7 +32,7 @@ Particle = Entity:New(x,y)
o.lightRange = particle_data.light
local flicker = particle_data.light_flicker or nil
local color = particle_data.light_color or nil
o.light = CreateLight(o.pos.x,o.pos.y,o.lightRange,flicker,color)
o.light = Light:New(o.pos.x,o.pos.y,o.lightRange,flicker,color)
end
-- animations
@ -44,8 +45,8 @@ Particle = Entity:New(x,y)
end
end
table.insert(LoadedParticles,o)
o.id = #LoadedParticles
table.insert(LoadedObjects.Particles,o)
o.id = #LoadedObjects.Particles
setmetatable(o, self)
self.__index = self

View File

@ -40,7 +40,7 @@
to = {x = 8, y = 0} -- gameworld pixels
}
o.lightRange = 10 -- screen pixels
o.lightRange = 40 -- screen pixels
-- status
o.isDashing = false
@ -66,7 +66,7 @@
o:getBoundingBox(o.body,0,3,-1,-3)
-- lights
o.light = CreateLight(o.pos.x,o.pos.y,o.lightRange)
o.light = Light:New(o.pos.x,o.pos.y,o.lightRange)
table.insert(LoadedObjects.Entities,o)
o.id = #LoadedObjects.Entities

View File

@ -1,4 +1,5 @@
Entity = {class = "Entity"}
LoadedObjects.Entities = {}
function Entity:New(x,y)
o = {}
@ -250,7 +251,9 @@ function Entity:Debug()
end
function Entity:DrawBackground()
end
require "code/entities/kupo"
require "code/entities/arrow"
require "code/entities/decoration"

View File

@ -9,7 +9,7 @@ function GameStep()
end
end
for _, particle in pairs(LoadedParticles) do
for _, particle in pairs(LoadedObjects.Particles) do
particle:DoPhysics()
end
for _, enty in pairs(LoadedObjects.Entities) do
@ -68,19 +68,21 @@ function GameDraw()
-- prepare
GameworldDrawPrepare()
GameWorldUpdateLights()
-- background
GameworldDrawBackground()
GameworldDrawLights()
GameworldDrawEntitiesBackground()
-- foreground
GameworldDrawForeground()
if LevelData.properties.darkness then
GameworldDrawLighting()
end
GameworldDrawParticles()
GameworldDrawEntities()
if LevelData.properties.darkness then
GameworldDrawDarkness()
end
-- end
GameworldDrawEnd()

View File

@ -2,6 +2,7 @@ function GameworldDrawPrepare()
if game_resize then
Camera.height = game.height
Camera.width = game.width
Darkness.Recreate()
end
pcr, pcg, pcb, pca = love.graphics.getColor()
love.graphics.scale(game.scale,game.scale)
@ -72,24 +73,53 @@ function GameworldDrawForeground()
end
end
function GameworldDrawLighting()
if game_resize then
Canvas.Darkness:release()
Canvas.Darkness = CreateDarkness()
love.graphics.setCanvas(Canvas.Darkness)
SetDarkness()
love.graphics.setCanvas()
function GameworldDrawDarkness()
Darkness.Reset()
Darkness.DrawStart()
love.graphics.setBlendMode("replace")
love.graphics.setColor(0,0,0,0)
for _, light in pairs(LoadedObjects.Lights) do
if light.range ~= 0 then
local position = {
x = (light.pos.x - Camera.pos.x) / game.scale,
y = (light.pos.y - Camera.pos.y) / game.scale
}
local range = (light.range + light.flicker) / game.scale
love.graphics.circle(
"fill",
position.x,
position.y,
range
)
end
end
Darkness.DrawEnd()
end
function GameworldDrawLights()
for _, light in pairs(LoadedObjects.Lights) do
if light.range ~= 0 then
love.graphics.setColor(light.color[1],light.color[2],light.color[3],1)
Shader.RadiusGradient:send("pos_x",- Camera.pos.x + light.pos.x)
Shader.RadiusGradient:send("pos_y",- Camera.pos.y + light.pos.y)
Shader.RadiusGradient:send("range",light.range)
Shader.RadiusGradient:send("scale",game.scale)
love.graphics.setShader(Shader.RadiusGradient)
love.graphics.circle(
"fill",
- Camera.pos.x + light.pos.x,
- Camera.pos.y + light.pos.y,
light.range
)
love.graphics.setShader()
end
end
end
function GameWorldUpdateLights()
for _, light in pairs(LoadedObjects.Lights) do
light:Flicker()
end
-- work on lighting canvas
love.graphics.setCanvas(Canvas.Darkness)
SetDarkness()
DoLights()
DoBorder()
-- apply to game canvas
love.graphics.setColor(1,1,1,1)
love.graphics.scale(game.scale,game.scale)
love.graphics.setCanvas()
DrawDarkness()
love.graphics.scale(1/game.scale,1/game.scale)
end

View File

@ -1,11 +1,7 @@
Lights = {}
LightTimer = 0
Light = {}
LoadedObjects.Lights = {}
function CreateDarkness()
return love.graphics.newCanvas(game.width/game.scale, game.height/game.scale)
end
function CreateLight(x,y,range,flicker,color,lum)
function Light:New(x,y,range,flicker,color,lum)
local o = {}
o.pos = {
x = x,
@ -17,117 +13,35 @@ function CreateLight(x,y,range,flicker,color,lum)
o.flicker_value = flicker or 2
o.flicker = 0
o.dim = 0
o.flicker_speed = flicker_speed or 60/12
o.flicker_time = 0
o.flicker_time = 60/12
o.flicker_timer = 0
table.insert(Lights,o)
o.id = #Lights
table.insert(LoadedObjects.Lights,o)
o.id = #LoadedObjects.Lights
setmetatable(o, self)
self.__index = self
return o
end
function KillLight(light)
if light.id ~= nil then
for _, e in pairs(Lights) do
if e.id > light.id then
function Light:Kill()
if self.id ~= nil then
for _, e in pairs(LoadedObjects.Lights) do
if e.id > self.id then
e.id = e.id - 1
end
end
table.remove(Lights,light.id)
table.remove(LoadedObjects.Lights,self.id)
end
light = nil
self = nil
end
function SetDarkness()
love.graphics.setCanvas(Canvas.Darkness)
function Light:Flicker()
self.flicker_timer = self.flicker_timer + 1
love.graphics.setColor(0,0,0,1)
love.graphics.rectangle("fill",0,0,game.width ,game.height)
love.graphics.setCanvas()
end
function DoLights()
love.graphics.setCanvas(Canvas.Darkness)
for _, light in pairs(Lights) do
light.flicker_time = light.flicker_time + 1
if light.flicker_time >= light.flicker_speed then
light.flicker_time = light.flicker_time - light.flicker_speed
light.flicker = 0 + math.random(0,1)
light.flicker = math.min(math.max(light.flicker, -light.flicker_value),light.flicker_value)
end
if self.flicker_timer >= self.flicker_time then
self.flicker_timer = self.flicker_timer - self.flicker_time
self.flicker = math.random(0,1)
self.flicker = math.min(math.max(self.flicker, -self.flicker_value), self.flicker_value)
end
love.graphics.setBlendMode("replace")
for _, light in pairs(Lights) do
if light.range ~= 0 then
love.graphics.setColor(light.color[1],light.color[2],light.color[3],1)
local position = {
x = (light.pos.x - Camera.pos.x) / game.scale,
y = (light.pos.y - Camera.pos.y) / game.scale
}
local range = (1 + light.range + light.flicker) / game.scale
love.graphics.circle(
"fill",
position.x,
position.y,
range
)
end
end
love.graphics.setColor(0,0,0,0)
for _, light in pairs(Lights) do
if light.range ~= 0 then
local position = {
x = (light.pos.x - Camera.pos.x) / game.scale,
y = (light.pos.y - Camera.pos.y) / game.scale
}
local range = (light.range + light.flicker) / game.scale
love.graphics.circle(
"fill",
position.x,
position.y,
range
)
end
end
love.graphics.setBlendMode("alpha")
love.graphics.setColor(0,0,0,1)
Shaders.InsideLight:send("game_scale", game.scale)
for _, light in pairs(Lights) do
if light.range ~= 0 then
local position = {
x = (light.pos.x - Camera.pos.x) / game.scale,
y = (light.pos.y - Camera.pos.y) / game.scale
}
local range = (light.range + light.flicker) / game.scale
Shaders.InsideLight:send("light_color", light.color)
Shaders.InsideLight:send("light_pos", {position.x*game.scale, position.y*game.scale})
Shaders.InsideLight:send("range", range)
love.graphics.setShader(Shaders.InsideLight)
love.graphics.circle(
"fill",
position.x,
position.y,
range
)
love.graphics.setShader()
end
end
love.graphics.setCanvas()
end
function DoBorder()
love.graphics.setCanvas(Canvas.Darkness)
love.graphics.setCanvas()
end
function DrawDarkness()
love.graphics.draw(Canvas.Darkness, 0, 0, 0, 1/game.scale)
end

View File

@ -1,11 +1,4 @@
LoadedObjects = {
Entities = {},
Collisions = {},
Platforms = {},
Ladders = {},
Hazards = {}
}
LoadedObjects = {}
-- level functions
function LoadedObjects.DrawCollisions()

View File

@ -12,16 +12,19 @@ require "code/hex"
require "code/in_out"
-- classes
require "code/audio"
require "code/entity"
require "code/animation"
require "code/collision"
require "code/objects"
require "code/level"
require "code/camera"
require "code/animation"
require "code/audio"
-- objects
require "code/entity"
require "code/collision"
require "code/lights"
require "code/objects"
-- functions
require "code/darkness"
require "code/debug"
require "code/demo"
require "code/keybind"

View File

@ -2,7 +2,7 @@ return {
name = "test",
tileset = tileset.library,
properties = {
darkness = false
darkness = true
},
tiles = {
{ 1, 4, 0, 0, 0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},

View File

@ -1,52 +1,23 @@
Shaders = {}
Shaders.InsideLight = love.graphics.newShader[[
uniform vec2 light_pos;
uniform vec3 light_color;
Shader = {}
Shader.RadiusGradient = love.graphics.newShader[[
uniform float pos_x;
uniform float pos_y;
uniform float range;
uniform float game_scale;
uniform float scale;
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords ){
vec4 pixel = Texel(texture, texture_coords );//This is the current pixel color
float distance_x = light_pos.x - screen_coords.x;
float distance_y = light_pos.y - screen_coords.y;
float distance = sqrt( pow(distance_x,2) + pow(distance_y,2) ) / game_scale;
float distance_x = pos_x - screen_coords.x / scale;
float distance_y = pos_y - screen_coords.y / scale;
float distance = sqrt( pow(distance_x,2) + pow(distance_y,2) ) ;
if (distance < range){
float alpha = 1-(distance/10);
pixel.a = alpha;
if (color.r<light_color.r){
color.r = light_color.r;
}
if (color.g<light_color.g){
color.g = light_color.g;
}
if (color.b<light_color.b){
color.b = light_color.b;
}
}
return pixel * color;
}
]]
Shaders.OutsideDark = love.graphics.newShader[[
uniform vec2 light_pos;
uniform float range;
uniform float game_scale;
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords ){
vec4 pixel = Texel(texture, texture_coords );//This is the current pixel color
float distance_x = light_pos.x - screen_coords.x;
float distance_y = light_pos.y - screen_coords.y;
float distance = sqrt( pow(distance_x,2) + pow(distance_y,2) ) / game_scale;
if (distance < range){
float alpha = (distance/range);
float alpha = 1-(2*distance/range);
if (pixel.a > alpha){
pixel.a = alpha;
}
}
return pixel * color;
return pixel * color * color;
}
]]

View File

@ -22,20 +22,15 @@ function love.load()
game = {
scale = 2,
width = love.graphics.getWidth(),
height = love.graphics.getHeight(),
paused = false
height = love.graphics.getHeight()
}
require "code/require"
logPrint(loveInitLog)
loveInitLog = nil
Canvas = {
Darkness = CreateDarkness()
}
love.graphics.setCanvas(Canvas.Darkness)
SetDarkness()
love.graphics.setCanvas()
Canvas = {}
Darkness.Create()
Camera.width = game.width
Camera.height = game.height