diff options
author | Federico Igne <git@federicoigne.com> | 2022-11-04 22:34:34 +0000 |
---|---|---|
committer | Federico Igne <git@federicoigne.com> | 2022-11-04 22:34:34 +0000 |
commit | a953273e8069251d43e1c9464792fb70bebe8f57 (patch) | |
tree | 97455ed940d10c4cb7ea775715a148c5ac75ff68 | |
parent | 0ab284eac8781511fcd744ec2ac3cfc36f4dd7bb (diff) | |
download | raccoon-a953273e8069251d43e1c9464792fb70bebe8f57.tar.gz raccoon-a953273e8069251d43e1c9464792fb70bebe8f57.zip |
feat: add waypoints with movement animation and particle effect
-rw-r--r-- | raccoon.lua | 68 |
1 files changed, 67 insertions, 1 deletions
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() | |||
228 | return self.cur.x ~= 0 or self.cur.y ~= 0 | 228 | return self.cur.x ~= 0 or self.cur.y ~= 0 |
229 | end | 229 | end |
230 | 230 | ||
231 | local Pixel = Component() | ||
232 | function Pixel:init(c) | ||
233 | self.color = c | ||
234 | return self | ||
235 | end | ||
236 | |||
231 | local Sprite = Component() | 237 | local Sprite = Component() |
232 | function Sprite:init(i) | 238 | function Sprite:init(i) |
233 | self.i = i | 239 | self.i = i |
@@ -284,6 +290,32 @@ function CircleEffect:draw(x,y) | |||
284 | circb(x,y,self.r1,self.color) | 290 | circb(x,y,self.r1,self.color) |
285 | end | 291 | end |
286 | 292 | ||
293 | local PosAnim = Component() | ||
294 | function PosAnim:init(f,d) | ||
295 | self.t = 0 | ||
296 | self.d = d | ||
297 | self.f = f | ||
298 | return self | ||
299 | end | ||
300 | |||
301 | local Particles = Component() | ||
302 | function Particles:init(d) | ||
303 | self.t = 0 | ||
304 | self.d = d | ||
305 | self.parts = {} | ||
306 | return self | ||
307 | end | ||
308 | function Particles:spawn(pos, entities) | ||
309 | if math.random(10) == 1 then | ||
310 | local offset = math.random(7) -- TODO change to pos.width once implemented | ||
311 | local speed = 0.2 + math.random() | ||
312 | table.insert(entities, | ||
313 | Entity():with_component(Pixel(12)) | ||
314 | :with_component(Pos(pos.x + offset,pos.y)) | ||
315 | :with_component(PosAnim(function(t,p) p.y = p.y - speed end, 10))) | ||
316 | end | ||
317 | end | ||
318 | |||
287 | local InputSystem = System(Input) | 319 | local InputSystem = System(Input) |
288 | function InputSystem:exec(entity) | 320 | function InputSystem:exec(entity) |
289 | local input = entity.get_component[Input] | 321 | local input = entity.get_component[Input] |
@@ -403,7 +435,10 @@ end | |||
403 | local DrawingSystem = System(Pos) | 435 | local DrawingSystem = System(Pos) |
404 | function DrawingSystem:exec(entity,level) | 436 | function DrawingSystem:exec(entity,level) |
405 | local x, y = entity.get_component[Pos]:to_screen(level) | 437 | local x, y = entity.get_component[Pos]:to_screen(level) |
406 | if entity.has_component[Sprite] then | 438 | if entity.has_component[Pixel] then |
439 | local c = entity.get_component[Pixel].color | ||
440 | pix(x, y, c) | ||
441 | elseif entity.has_component[Sprite] then | ||
407 | local sprite = entity.get_component[Sprite] | 442 | local sprite = entity.get_component[Sprite] |
408 | spr(sprite.i, x, y, 0) | 443 | spr(sprite.i, x, y, 0) |
409 | end | 444 | end |
@@ -471,6 +506,28 @@ function EffectSystem:exec(entity) | |||
471 | end | 506 | end |
472 | end | 507 | end |
473 | 508 | ||
509 | local PosAnimSystem = System(Pos, PosAnim) | ||
510 | function PosAnimSystem:exec(entity) | ||
511 | local pos = entity.get_component[Pos] | ||
512 | local anim = entity.get_component[PosAnim] | ||
513 | anim.f(anim.t, pos) | ||
514 | anim.t = anim.t + 1 | ||
515 | if anim.d and anim.t > anim.d then | ||
516 | return true--, {anim} | ||
517 | end | ||
518 | end | ||
519 | |||
520 | local ParticlesSystem = System(Pos, Particles) | ||
521 | function ParticlesSystem:exec(entity, game) | ||
522 | local pos = entity.get_component[Pos] | ||
523 | local parts = entity.get_component[Particles] | ||
524 | parts:spawn(pos,game.entities) | ||
525 | parts.t = parts.t + 1 | ||
526 | if parts.d and parts.t > parts.d then | ||
527 | return true, {parts} | ||
528 | end | ||
529 | end | ||
530 | |||
474 | local player = Entity() | 531 | local player = Entity() |
475 | :with_component(Pos(100,10)) | 532 | :with_component(Pos(100,10)) |
476 | :with_component(Movement()) | 533 | :with_component(Movement()) |
@@ -491,6 +548,12 @@ local level1_entities = { | |||
491 | Entity():with_component(Pos(16*8,11*8)):with_component(Sprite(272)), | 548 | Entity():with_component(Pos(16*8,11*8)):with_component(Sprite(272)), |
492 | Entity():with_component(Pos(47*8, 8*8)):with_component(Sprite(272)), | 549 | Entity():with_component(Pos(47*8, 8*8)):with_component(Sprite(272)), |
493 | Entity():with_component(Pos(85*8, 7*8)):with_component(Sprite(272)), | 550 | Entity():with_component(Pos(85*8, 7*8)):with_component(Sprite(272)), |
551 | -- Waypoints | ||
552 | Entity():with_component(Pos(15*8,11*8)):with_component(Sprite(288)) | ||
553 | :with_component(PosAnim(function(t,p) p.y = p.y - math.sin(t/6)/3 end)) | ||
554 | :with_component(Particles()), | ||
555 | Entity():with_component(Pos(48*8, 8*8)):with_component(Sprite(288)) | ||
556 | :with_component(PosAnim(function(t,p) p.y = p.y - math.sin(t/6)/3 end)), | ||
494 | } | 557 | } |
495 | 558 | ||
496 | -- TODO turn this into something more structured, containing everything related | 559 | -- TODO turn this into something more structured, containing everything related |
@@ -514,6 +577,8 @@ function TIC() | |||
514 | MovementSystem:run(game.entities) | 577 | MovementSystem:run(game.entities) |
515 | ActionSystem:run(game.entities, game) | 578 | ActionSystem:run(game.entities, game) |
516 | EffectSystem:run(game.entities) | 579 | EffectSystem:run(game.entities) |
580 | PosAnimSystem:run(game.entities) | ||
581 | ParticlesSystem:run(game.entities, game) | ||
517 | LevelSystem:run(game.levels, game) | 582 | LevelSystem:run(game.levels, game) |
518 | 583 | ||
519 | DrawingSystem:draw(game) | 584 | DrawingSystem:draw(game) |
@@ -534,6 +599,7 @@ end | |||
534 | -- <SPRITES> | 599 | -- <SPRITES> |
535 | -- 000:0000000000777700077777000740400000444400022222200488884000800800 | 600 | -- 000:0000000000777700077777000740400000444400022222200488884000800800 |
536 | -- 016:0003330000044300000444000007770000077600004774000008840000080800 | 601 | -- 016:0003330000044300000444000007770000077600004774000008840000080800 |
602 | -- 032:0222220022ccc2202ccccc202ccccc2022ccc220022222000022200000020000 | ||
537 | -- </SPRITES> | 603 | -- </SPRITES> |
538 | 604 | ||
539 | -- <MAP> | 605 | -- <MAP> |