From 538a1f77f8f4d044848cc4812fac5dceb3eb93fc Mon Sep 17 00:00:00 2001 From: lustlion Date: Wed, 27 Oct 2021 11:06:08 +0200 Subject: [PATCH] New lighting! --- assets/terrain/tileset.png | Bin 2432 -> 2429 bytes data/levels/33 (2).json | 1 + data/levels/level1.json | 2 +- data/scripts/camera.lua | 8 +-- data/scripts/entities/arrow.lua | 4 +- data/scripts/entities/kupo.lua | 100 ++++++++++++++++---------------- data/scripts/entity.lua | 3 +- data/scripts/level.lua | 19 +++++- data/scripts/lights.lua | 53 ++++++++++++----- data/tiles.lua | 18 +++++- main.lua | 41 +++++++------ 11 files changed, 158 insertions(+), 91 deletions(-) create mode 100644 data/levels/33 (2).json diff --git a/assets/terrain/tileset.png b/assets/terrain/tileset.png index 9c2b2a974a7085918850ef8da405350a14b1efd1..107445feae4c35cf35f17fbf018b8624a37acafb 100644 GIT binary patch literal 2429 zcmeHI{ZrBh7{|y&P4iOOolQ-bCzfK_S`$JNP(%{RbQEaj3yC%i=DF_4xkxzsN7@?#C@3{w_T;fA<@ye;NlM%TN}*&GyWuhqVQi$g?Yg<*B7RQd|XFZMnV34uVSF|o_7D5K7o_~O8S z&VjOLrMHbsn`DHMPJxckzC8~D?KnWd`G@CJsD&y)J<}V*e7V5aQF2R^$IW`MLK6?^=dGw1S|iTn z(MDW;)?+AUZJo!Zq9omCHH{S%R1HIuTvA2aY|90M$@4|e?}JGgio+!B|T?w79b@qzxC zf?m6>Ml^Yq$CT$HzE5Z;0MdvG+q#T_P&*YhNpYW77V8r3+0&QUzIF8|udcaznpMm1 zyD}DCeNd}Jtr9X)@LlD?0wsoG6~eV=5f~LDCaZ?kiNUE%I-Xu zsW4P$u32_g?}FDPTLOh1;Tt=<_sgSCuxk&$+FBN<7bf4@KtC?YKwA#*a zj=&8{ZM=p61BGG#I3?kf%zKLWM3qcr>p-vwwOlo~s#e#S`e4#kvcYX5PQo+1DyVYz zrxilX1xJK_rA`Fxg=98h0en;0Nt`vj0lV}~&W!yQMx%393n7($`-&w)TZgi->*ROZ ze7b0OQhnIZIAcy10Bm14C<1oT@1xx|?Pu0YtZ>jgR zT?s0_i%Dc5reXlLVe^Gkn)^(?byq6O?{N*-35w8`q5>9r*aK=2 zHbw2Haq6c*c1H&HSu4&)U3c*cRj;5XPrZ9Ce$=W~1} z@Sh`arK9A!+4MSk6d@Y))AWmE5NMNkXy86l7EfI<&u#M95}dnnrWZbL3Qa-&4Rs+m zs1_-(`I`vTcMV>|`u@8{PNtI07&E2K8Z?XueCN0}AEDNH2Pm!7!Mz<}0^_iykl7wr zyq@yj@G7jsU%8FG)2n%k|Vjw9V=$4xJDd&VlaN3c&?`6&{lZNj{@j28J}$=12i zHz;PK8V~kKLR(hX;EJhaMGJ3g^i9G(a#F0?f<>8+xo5O-mr+ud8|nV1oGYpeh43HF ze(WMnyDq~_nDuTP?k+%8NMknlzJ2j7c3JTIP{O0Gd`em20*RuMw)tM48lju|;Ue!N z|7^aNZij^4wYPeoS5GgIyt`9l}WL{>8l77&*n@h~_vzfJB5hL99O6g&NpvNh$S+O_n)SCH z1V)fj4`=M25SaY6s7>@>{P^VfnU$INW&~+LzLVb%^kis6%s zpPZN*ZW!qtzUmUiw`2wHj{Vu=03$p#c~LekwZV!INzYI#hdTL1>>*Qk!IyDoD4F%T zbCU36&*w0{k_De3qQsFQ_A7613VoFhEJoQ< zxG||B0~ z$er~tQ}tn}DUagvjqDDT^;LgZur1<`+t8kli88VdBGLpX{A`hE*J9y_p0@bg0`N#< z9Kf^+lqoqFuTuK~IBw}Q57bD^%9riju=r_8344Ik9xLFPTfTAC=9@wK5b>IIKu!=?J;pArtrVgs34KN{@%j!1n;03f%^lto(p2wWW7BU R= cx then self.pos.x = cx end if self.pos.y > cy then self.pos.y = cy end if self.pos.x < 0 then self.pos.x = 0 end diff --git a/data/scripts/entities/arrow.lua b/data/scripts/entities/arrow.lua index 5d13d77..9e148ee 100644 --- a/data/scripts/entities/arrow.lua +++ b/data/scripts/entities/arrow.lua @@ -12,7 +12,8 @@ Arrow = Entity:New(x,y) } o.sprite_offset = {x = 13, y = 1} o.stuck = false - + o.illuminated = true + setmetatable(o, self) self.__index = self table.insert(LoadedEntities,o) @@ -53,5 +54,6 @@ function Arrow:DoPhysics() self.pos.y = self.pos.y + self.vel.y / 5 self.vel.x = 0 self.vel.y = 0 + self.illuminated = false end end diff --git a/data/scripts/entities/kupo.lua b/data/scripts/entities/kupo.lua index 7bcb9bf..5291d15 100644 --- a/data/scripts/entities/kupo.lua +++ b/data/scripts/entities/kupo.lua @@ -19,10 +19,11 @@ Kupo = Entity:New(x,y) o.bow_frames = 6 o.bow_extraframes = 18 o.bow_aim_frames = 8 - - o.lightRange = o.range + o.hostile = false + + o.lightRange = o.range/10 o.light = CreateLight(o.pos.x,o.pos.y,o.lightRange) - + setmetatable(o, self) self.__index = self @@ -37,9 +38,8 @@ function Kupo:Smart() local distance_y = self.target.y - self.pos.y local distance = math.sqrt(distance_x ^ 2 + distance_y ^ 2) local angle = math.atan(distance_y/distance_x) - + self.draw_bow = false if distance <= self.range then - self.draw_bow = true if distance_x > 0 then self.sprite_flip.x = 1 @@ -48,55 +48,57 @@ function Kupo:Smart() self.sprite_flip.x = -1 end - -- fix so it can rotate from 0 to 360 - if math.deg(self.bow_rotation - angle) < 0 then - self.bow_rotation = self.bow_rotation + math.rad(360) - end - - -- fix so it can rotate from 360 to 0 - if math.deg(self.bow_rotation - angle) > 180 then - self.bow_rotation = self.bow_rotation - math.rad(360) - end - - -- actual rotation - if self.bow_rotation < angle then - self.bow_rotation = self.bow_rotation + math.rad(2) - else - self.bow_rotation = self.bow_rotation - math.rad(2) - end - --set in place - if math.abs(math.deg(self.bow_rotation) - math.deg(angle)) < 2 then - self.bow_rotation = angle - end - - -- holding tight dispersion -- also affets arrows - if self.bow_rotation == angle then - self.bow_rotation = self.bow_rotation + math.rad(math.random(math.abs(self.bow_frame-self.bow_aim_frames-self.bow_frames)/2)) - end - - -- AIMING AI - - self.bow_subframe = self.bow_subframe + current_dt - - if self.bow_subframe > self.bow_speed then - self.bow_subframe = self.bow_subframe - self.bow_speed - if self.bow_frame == 3 then - self.bow_aim_frame = self.bow_aim_frame + 1 - if self.bow_aim_frame > self.bow_aim_frames then - self.bow_aim_frame = self.bow_aim_frame - self.bow_aim_frames - self.bow_frame = self.bow_frame + 1 - Arrow:New(self.pos.x,self.pos.y,self.bow_rotation,15) - end - else - self.bow_frame = self.bow_frame + 1 + if self.hostile == true then + self.draw_bow = true + -- fix so it can rotate from 0 to 360 + if math.deg(self.bow_rotation - angle) < 0 then + self.bow_rotation = self.bow_rotation + math.rad(360) end - if self.bow_frame > self.bow_frames + self.bow_extraframes then - self.bow_frame = self.bow_frame - self.bow_frames - self.bow_extraframes + + -- fix so it can rotate from 360 to 0 + if math.deg(self.bow_rotation - angle) > 180 then + self.bow_rotation = self.bow_rotation - math.rad(360) + end + + -- actual rotation + if self.bow_rotation < angle then + self.bow_rotation = self.bow_rotation + math.rad(2) + else + self.bow_rotation = self.bow_rotation - math.rad(2) + end + --set in place + if math.abs(math.deg(self.bow_rotation) - math.deg(angle)) < 2 then + self.bow_rotation = angle + end + + -- holding tight dispersion -- also affets arrows + if self.bow_rotation == angle then + self.bow_rotation = self.bow_rotation + math.rad(math.random(math.abs(self.bow_frame-self.bow_aim_frames-self.bow_frames)/2)) + end + + -- AIMING AI + + self.bow_subframe = self.bow_subframe + current_dt + + if self.bow_subframe > self.bow_speed then + self.bow_subframe = self.bow_subframe - self.bow_speed + if self.bow_frame == 3 then + self.bow_aim_frame = self.bow_aim_frame + 1 + if self.bow_aim_frame > self.bow_aim_frames then + self.bow_aim_frame = self.bow_aim_frame - self.bow_aim_frames + self.bow_frame = self.bow_frame + 1 + Arrow:New(self.pos.x,self.pos.y,self.bow_rotation,15) + end + else + self.bow_frame = self.bow_frame + 1 + end + if self.bow_frame > self.bow_frames + self.bow_extraframes then + self.bow_frame = self.bow_frame - self.bow_frames - self.bow_extraframes + end end end else self.bow_frame = 6 - self.draw_bow = true -- rest bow animation if distance_x > 0 then if self.bow_rotation > math.rad(45) then diff --git a/data/scripts/entity.lua b/data/scripts/entity.lua index 10db048..ad55084 100644 --- a/data/scripts/entity.lua +++ b/data/scripts/entity.lua @@ -15,6 +15,7 @@ function Entity:New(x,y) o.sprite_scale = {x = 1, y = 1} o.sprite_rotation = math.rad(0) o.sprite_flip = { x = 1, y = 1} + o.illuminated = false setmetatable(o, self) self.__index = self return o @@ -113,7 +114,7 @@ function DrawAnimation(animation, x, y, rotate, sx, sy) end function Entity:Animate() - if game_paused ~= true then + if game_paused ~= true and self.anim.path ~= nil then -- try to animate self.anim.subframe = self.anim.subframe + current_dt diff --git a/data/scripts/level.lua b/data/scripts/level.lua index 423df81..7904ada 100644 --- a/data/scripts/level.lua +++ b/data/scripts/level.lua @@ -136,6 +136,14 @@ function TileGetDepth(tile_id) end end +function TileGetLight(tile_id) + for _, properties in ipairs(Tiles) do + if properties.id == tile_id then + return properties.light + end + end +end + function GridDisplay() for i = 1, #LevelTiles do for j = 1, #LevelTiles[i] do @@ -160,10 +168,19 @@ function TileCreateObjects() if LevelTiles[i][j] ~= 0 then local type = TileGetType(LevelTiles[i][j]) - + local light = TileGetLight(LevelTiles[i][j]) local base_x = tileProperties.scale * j * tileProperties.width + tileProperties.scale * (levelProperties.offset.x - tileProperties.height) local base_y = tileProperties.scale * i * tileProperties.height + tileProperties.scale * (levelProperties.offset.y - tileProperties.height) + + if light ~= 0 and light ~= nil then + CreateLight( + base_x + tileProperties.width/2 * tileProperties.scale, + base_y + tileProperties.height/2 * tileProperties.scale, + light + ) + end + if type == "whole" then local col = Collision:New( base_x, diff --git a/data/scripts/lights.lua b/data/scripts/lights.lua index 9f4183e..709eaf9 100644 --- a/data/scripts/lights.lua +++ b/data/scripts/lights.lua @@ -13,46 +13,67 @@ function CreateLight(x,y,range) } o.range = range o.flicker = 0 + o.dim = 0 table.insert(Lights,o) return o end +function SetDarkness() + love.graphics.setColor(0,0,0,1) + love.graphics.rectangle("fill",0,0,game.width,game.height) +end + function DoDarkness() - love.graphics.setColor(0,0,0) + love.graphics.setColor(0,0,0,0.3) love.graphics.rectangle("fill",0,0,game.width,game.height) end function DoLights() LightTimer = LightTimer + 1 - + if LightTimer >= 3 then LightTimer = LightTimer - 3 for _, light in pairs(Lights) do - light.flicker = math.random(-1,1) - end + light.flicker = math.random(-2,2) + light.dim = (light.range+light.flicker)/5 + end end - + love.graphics.setBlendMode("replace") -- first, border love.graphics.setColor(1,1,1) for _, light in pairs(Lights) do - - love.graphics.circle( - "fill", + + --[[love.graphics.circle( + "fill", light.pos.x - Camera.pos.x, light.pos.y - Camera.pos.y, light.range + light.flicker + 1 - ) - end - love.graphics.setColor(0,0,0,0) - -- then, light - for _, light in pairs(Lights) do + )]] + end + for _, enty in pairs(LoadedEntities) do + if enty.illuminated == true then + enty:Draw() + end + end + love.graphics.setColor(0,0,0,0.5) + for _, light in pairs(Lights) do love.graphics.circle( - "fill", + "fill", light.pos.x - Camera.pos.x, light.pos.y - Camera.pos.y, light.range + light.flicker - ) + ) + end + -- then, light + love.graphics.setColor(0,0,0,0) + for _, light in pairs(Lights) do + love.graphics.circle( + "fill", + light.pos.x - Camera.pos.x, + light.pos.y - Camera.pos.y, + light.range + light.flicker - light.dim + ) end love.graphics.setBlendMode("alpha") end @@ -62,4 +83,4 @@ end function DrawDarkness() love.graphics.draw(Canvas.Darkness, 0, 0, 0, 0.5, 0.5) -end \ No newline at end of file +end diff --git a/data/tiles.lua b/data/tiles.lua index d7edb07..4d572ca 100644 --- a/data/tiles.lua +++ b/data/tiles.lua @@ -272,7 +272,8 @@ return { { id = 55, type = "empty", - depth = "foreground" + depth = "foreground", + light = 60 }, { id = 56, @@ -324,6 +325,21 @@ return { type = "whole", depth = "foreground" }, + { + id = 66, + type = "whole", + depth = "foreground" + }, + { + id = 67, + type = "whole", + depth = "foreground" + }, + { + id = 68, + type = "whole", + depth = "foreground" + }, { id = 119, type = "whole", diff --git a/main.lua b/main.lua index 898e379..4c73ff6 100644 --- a/main.lua +++ b/main.lua @@ -20,17 +20,20 @@ function love.load() Canvas = { Darkness = CreateDarkness() } + love.graphics.setCanvas(Canvas.Darkness) + SetDarkness() + love.graphics.setCanvas() Camera.width = game.width Camera.height = game.height levelList = {"level1","2","3","ewae","tileset"} levelNum = 1 currLevel = levelList[levelNum] + LoadedEntities = {} LevelLoadTiles() main_Player = Player:New(0,20) - LoadedEntities = {} table.insert(LoadedEntities,main_Player) - table.insert(LoadedEntities,Kupo:New(450,100)) - table.insert(LoadedEntities,Kupo:New(250,150)) + table.insert(LoadedEntities,Kupo:New(700,150)) + table.insert(LoadedEntities,Kupo:New(800,150)) main_Player.sprite = love.graphics.newImage("assets/characters/nancy/idle1.png") main_Player:LoadAnimation(animation.nancy.idle) end @@ -47,16 +50,6 @@ function love.update(dt) fps_count = fps_count + 1 current_dt = dt - -- saveproof to game resize - if game.width ~= love.graphics.getWidth() or game.height ~= love.graphics.getHeight() then - game.width = love.graphics.getWidth() - game.height = love.graphics.getHeight() - Camera.height = game.height - Camera.width = game.width - Canvas.Darkness:release() - Canvas.Darkness = CreateDarkness() - end - -- GAME STEP if not do_pause then SetCollisionFlags(main_Player) @@ -65,7 +58,7 @@ function love.update(dt) enty:DoPhysics() end AnimateTiles() - Camera:CenterAt(main_Player.pos.x, main_Player.pos.y,LevelInfo.Width,LevelInfo.Height) + Camera:CenterAt(main_Player.pos.x, main_Player.pos.y) --camera:ScreenAt(main_Player.pos.x, main_Player.pos.y,game.width,game.height) end end @@ -110,6 +103,20 @@ function love.keypressed(key) end function love.draw() + + -- saveproof to game resize + if game.width ~= love.graphics.getWidth() or game.height ~= love.graphics.getHeight() then + game.width = love.graphics.getWidth() + game.height = love.graphics.getHeight() + Camera.height = game.height + Camera.width = game.width + Canvas.Darkness:release() + Canvas.Darkness = CreateDarkness() + love.graphics.setCanvas(Canvas.Darkness) + SetDarkness() + love.graphics.setCanvas() + end + -- GAME WORLD love.graphics.scale(game.scale,game.scale) love.graphics.setColor(1,1,1,1) @@ -123,7 +130,7 @@ function love.draw() -- Save color local pcr, pcg, pcb, pca = love.graphics.getColor() - + love.graphics.setCanvas(Canvas.Darkness) DoDarkness() DoLights() @@ -133,11 +140,11 @@ function love.draw() love.graphics.setColor(1,1,1,1) love.graphics.setCanvas() love.graphics.scale(1,1) - DrawDarkness() + DrawDarkness() -- HUD -- Scale control textScale = 0.5 - + --debug if debug then DebugUI() end if debug_collision then DebugColisions() end