From a953273e8069251d43e1c9464792fb70bebe8f57 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Fri, 4 Nov 2022 22:34:34 +0000 Subject: feat: add waypoints with movement animation and particle effect --- raccoon.lua | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/raccoon.lua b/raccoon.lua index 5a3c648..c777c1d 100644 --- a/raccoon.lua +++ b/raccoon.lua @@ -228,6 +228,12 @@ function Movement:is_moving() return self.cur.x ~= 0 or self.cur.y ~= 0 end +local Pixel = Component() +function Pixel:init(c) + self.color = c + return self +end + local Sprite = Component() function Sprite:init(i) self.i = i @@ -284,6 +290,32 @@ function CircleEffect:draw(x,y) circb(x,y,self.r1,self.color) end +local PosAnim = Component() +function PosAnim:init(f,d) + self.t = 0 + self.d = d + self.f = f + return self +end + +local Particles = Component() +function Particles:init(d) + self.t = 0 + self.d = d + self.parts = {} + return self +end +function Particles:spawn(pos, entities) + if math.random(10) == 1 then + local offset = math.random(7) -- TODO change to pos.width once implemented + local speed = 0.2 + math.random() + table.insert(entities, + Entity():with_component(Pixel(12)) + :with_component(Pos(pos.x + offset,pos.y)) + :with_component(PosAnim(function(t,p) p.y = p.y - speed end, 10))) + end +end + local InputSystem = System(Input) function InputSystem:exec(entity) local input = entity.get_component[Input] @@ -403,7 +435,10 @@ end local DrawingSystem = System(Pos) function DrawingSystem:exec(entity,level) local x, y = entity.get_component[Pos]:to_screen(level) - if entity.has_component[Sprite] then + if entity.has_component[Pixel] then + local c = entity.get_component[Pixel].color + pix(x, y, c) + elseif entity.has_component[Sprite] then local sprite = entity.get_component[Sprite] spr(sprite.i, x, y, 0) end @@ -471,6 +506,28 @@ function EffectSystem:exec(entity) end end +local PosAnimSystem = System(Pos, PosAnim) +function PosAnimSystem:exec(entity) + local pos = entity.get_component[Pos] + local anim = entity.get_component[PosAnim] + anim.f(anim.t, pos) + anim.t = anim.t + 1 + if anim.d and anim.t > anim.d then + return true--, {anim} + end +end + +local ParticlesSystem = System(Pos, Particles) +function ParticlesSystem:exec(entity, game) + local pos = entity.get_component[Pos] + local parts = entity.get_component[Particles] + parts:spawn(pos,game.entities) + parts.t = parts.t + 1 + if parts.d and parts.t > parts.d then + return true, {parts} + end +end + local player = Entity() :with_component(Pos(100,10)) :with_component(Movement()) @@ -491,6 +548,12 @@ local level1_entities = { Entity():with_component(Pos(16*8,11*8)):with_component(Sprite(272)), Entity():with_component(Pos(47*8, 8*8)):with_component(Sprite(272)), Entity():with_component(Pos(85*8, 7*8)):with_component(Sprite(272)), + -- Waypoints + Entity():with_component(Pos(15*8,11*8)):with_component(Sprite(288)) + :with_component(PosAnim(function(t,p) p.y = p.y - math.sin(t/6)/3 end)) + :with_component(Particles()), + Entity():with_component(Pos(48*8, 8*8)):with_component(Sprite(288)) + :with_component(PosAnim(function(t,p) p.y = p.y - math.sin(t/6)/3 end)), } -- TODO turn this into something more structured, containing everything related @@ -514,6 +577,8 @@ function TIC() MovementSystem:run(game.entities) ActionSystem:run(game.entities, game) EffectSystem:run(game.entities) + PosAnimSystem:run(game.entities) + ParticlesSystem:run(game.entities, game) LevelSystem:run(game.levels, game) DrawingSystem:draw(game) @@ -534,6 +599,7 @@ end -- -- 000:0000000000777700077777000740400000444400022222200488884000800800 -- 016:0003330000044300000444000007770000077600004774000008840000080800 +-- 032:0222220022ccc2202ccccc202ccccc2022ccc220022222000022200000020000 -- -- -- cgit v1.2.3