From 8ba0ca9163b9247c9f75d0747aaa707e8fe3210b Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Thu, 26 Jun 2025 14:06:34 +0200 Subject: [PATCH] - [NEW] modul 'templates' - [FIX] El primer frame de les animacions no es veia - [NEW] L'heroi ja dispara - [NEW] Les momies ja moren (petada, arreglar) --- data/animations.lua | 32 +++++++++ data/editor.lua | 4 +- data/rooms.lua | 6 +- data/sprites.gif | Bin 1264 -> 1687 bytes data/sprites.lua | 172 ++++++++++++++++++++++++++++++++------------ data/templates.lua | 41 +++++++++++ data/util.lua | 13 ++++ 7 files changed, 218 insertions(+), 50 deletions(-) create mode 100644 data/templates.lua diff --git a/data/animations.lua b/data/animations.lua index 2d2be1a..dc507ad 100644 --- a/data/animations.lua +++ b/data/animations.lua @@ -33,6 +33,20 @@ animations = { { frame={x=48,y=0,w=16,h=17}, wait=4, reversed=true } } }, + ["hero_shoot"] = { + cycle = {1,2,3}, + frames = { + { frame={x=64,y=0,w=24,h=17}, wait=4 }, + { frame={x=88,y=0,w=24,h=17}, wait=2 }, + { frame={x=88,y=0,w=24,h=17}, wait=2 } + } + }, + ["bullet"] = { + cycle = {1}, + frames = { + { frame={x=0,y=19,w=4,h=3}, wait=4 } + } + }, ["mummy_walk"] = { cycle = {1,2,1,3}, frames = { @@ -41,4 +55,22 @@ animations = { { frame={x=32,y=24,w=16,h=17}, wait=4 } } }, + ["mummy_dying"] = { + cycle = {1,2,3,4,5,6,7,6}, + frames = { + { frame={x=48,y=24,w=16,h=17}, wait=2 }, + { frame={x=64,y=24,w=16,h=17}, wait=2 }, + { frame={x=80,y=24,w=16,h=17}, wait=2 }, + { frame={x=96,y=24,w=16,h=17}, wait=2 }, + { frame={x=112,y=24,w=16,h=17}, wait=2 }, + { frame={x=128,y=24,w=16,h=17}, wait=4 }, + { frame={x=144,y=24,w=16,h=17}, wait=2 } + } + }, + ["mummy_dead"] = { + cycle = {1}, + frames = { + { frame={x=128,y=24,w=16,h=17}, wait=4 }, + } + }, } \ No newline at end of file diff --git a/data/editor.lua b/data/editor.lua index 3444d41..640cb21 100644 --- a/data/editor.lua +++ b/data/editor.lua @@ -87,11 +87,11 @@ editor = { elseif key.press(key.TAB) or key.press(key.ESCAPE) then editor.picker.show() elseif key.press(key.P) then - sprites.hero.pos.x = tx*8-4 + sprites.hero.pos.x = tx*8-4 sprites.hero.pos.y = ty*8-9 elseif key.press(key.F9) then editor.play() - elseif key.press(key.F11 ) then + elseif key.press(key.F11) then reload_textures() end end, diff --git a/data/rooms.lua b/data/rooms.lua index 844d3e2..507f186 100644 --- a/data/rooms.lua +++ b/data/rooms.lua @@ -56,7 +56,11 @@ rooms = { surf.save(rooms.surf_items, "data/rooms_items.bin") editor.modified = false end, - + + is_outside = function(x, y) + return x < rooms.pos.x or y < rooms.pos.y or x > rooms.pos.x + 20 or y > rooms.pos.y + 12 + end, + draw = function() -- Retallem la pantalla a la zona de joc view.clip(0,8,160,96) diff --git a/data/sprites.gif b/data/sprites.gif index c81b9e8dfd78bc1cc819213508d23be3b11b499a..5fd6df69720fdcb89c8d0100e4da9415a8038118 100644 GIT binary patch delta 1564 zcmV+%2IKkg36~9!j(-clv8>Xlt&6zZ?<5EWLxkjBG+%NR5E3!QR&!W9>ZZ>qP=>8a zonBx`NUd6VVCXqm){>@WwRN19+vss)ZheJ6?Qdj%dRK9OVKaVNH9U%DXhn5ukcWeO zl$B-=mtUESn3-;0WTa#@O`c(>sZn&5LZ_v&j7+knC7YZtwtu9tys)}F4#Nx+91IK` zbHBi7%#3)ZX92?w#mC9PGu79~$^j1#2U{fE#@^HB*vewo*2V)I1L?%)**EIw_%sgb z4Da&fX`}Yrs1K@i!5ix}rLlsFF}z=LxpR2%7Ff<|P;F#lde z@naiGl`A`v6Myy+=1Ge-DO$s+@@C4ODJuTVNJ1pZVJ3s_9J&%*xTi;i!Zb;1i`1Ou zY67KL)1p6~M6-fj8T21XsyDh8GcjPmfw!~*sHKW_=8T#x}KgKtff!0aRO#3wr-T9`n6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Y&xGsUM~p@rgl@MRfVN) z0VQct`-Pg%XmEDyC1Kaq>w6qm|B&=@de5i5rGRxiY-~$*gl%DKcSL=Tk3tTT3=;g3fjmT;oQ;ZrjgY0L0S*kQl>;0DU#OFnmzi9WtCpCs4u3m6o$5*M{;#L9E)solR?q~tw6WzP=-r(lP}jetu* zH7Fnzuc$$jACt;aK(_2ts2LdwAp3x9RWJe$91yVfLE8WVG~un=cI(0tbn~Je{6enX z19oB2J#6>^UB!(b&)6%Nuw}S`Gb_$pLat;kj{~2*OnC>oFt6t5zwd>cgW6PdR zyY}oMq-^WHa)8r;zeZl*-hFe2+&dUV_73!0$8tc;iDMk3B#TH0$EzpTf&BpT(bD?_ z&@NZ*b>u$IYfm3OAQtbPbAusYUL<|H`N%dV?^^M3V!r&n!~{uCBgn&o^Ks+Dty*Fr%vt+z@lmt#&0A$^Ws(p!)1X z4Syw7!T%z43B%l)LDbD%DXi zKzm8PA8BjvJ`5Bzl?0pSgfPSl3xw5L3iK86#cYukaJo$%W-Sw)A%^S76;GxaWf89| z8l?q8s%*-W;qfWSHcLvg3z%BEYo$MUvVSudp|%04r$Mv&v7e@{3azTEwmLL9vgA_B zrLfMT%dNRiO+qrk`iiwPtVF}=l{7~yPCJY0P>!_CE)A&K=TKWMHgr!b_uLS98vwZC z^4zyS{&>xWya~CRt-@d+-tGka8a|M}`a&K#&-gwJu;C0lOt6W596T{?;}tU$z-s6R zn(nz!oXg=EUt3iMZFzKMe3$H!FNSMmxci}dd3Jr)`d%iy-ZE!R3g7zj%s21+^Uy~x z{q)pVZ~gVyXRrPC+;{K&_uz*w{`lmVZ~pn{r?39{?6>d!`|!sv|NQjVZ~y)8ga80L DF@Q2N diff --git a/data/sprites.lua b/data/sprites.lua index 230525f..8235e09 100644 --- a/data/sprites.lua +++ b/data/sprites.lua @@ -1,15 +1,27 @@ require "animations" +require "templates" sprites = { hero = nil, list = {}, pause_ia = false, + remove = function(sprite) + for i,v in ipairs(sprites.list) do + if v == sprite then + table.remove(sprites.list, i) + print("Sprite removed: "..sprite.type) + return + end + end + end, + init = function() sprites.hero = { + type = "hero", pos = { x=28, y=4*12*8+71 }, size= { w=16, h=17 }, - bbo = { left=0, top=0, right=0, bottom=0 }, + bbo = { left=3, top=2, right=3, bottom=0 }, current_frame = 1, current_wait = 1, flipped = false, @@ -18,24 +30,27 @@ sprites = { jumping = 0, stairs = false } - local mummy = { - pos = { x=100, y=4*12*8+71 }, - size = { w=16,h=17 }, - bbo = { left=0, top=0, right=0, bottom=0 }, - current_frame = 1, - current_wait = 1, - flipped = true, - animation = "mummy_walk", - ia = sprites.update_mummy - } - table.insert(sprites.list, mummy) + table.insert(sprites.list, templates.create("mummy", {pos={x=100, y=4*12*8+71},flipped=true})) + + --local mummy = { + -- pos = { x=100, y=4*12*8+71 }, + -- size = { w=16,h=17 }, + -- bbo = { left=3, top=2, right=3, bottom=0 }, + -- current_frame = 1, + -- current_wait = 1, + -- flipped = true, + -- animation = "mummy_walk", + -- ia = sprites.update_mummy + --} + --table.insert(sprites.list, mummy) end, set_animation=function(sprite, animation) if sprite.animation ~= animation then sprite.animation = animation sprite.current_frame = 1 - sprite.current_wait = 1 + local cycle = animations[sprite.animation].cycle[1] + sprite.current_wait = animations[sprite.animation].frames[cycle].wait end end, @@ -75,20 +90,62 @@ sprites = { end, update_mummy = function(spr) - if spr.flipped then - local tx, ty = (spr.pos.x+3)>>3, (spr.pos.y+16)>>3 - if map.tile(tx,ty) < 16 and map.tile(tx,ty-1) < 16 and map.tile(tx,ty+1) > 0 then - spr.pos.x = spr.pos.x - 1 + if spr.state == templates.ALIVE then + if spr.flipped then + local tx, ty = (spr.pos.x+3)>>3, (spr.pos.y+16)>>3 + if map.tile(tx,ty) < 16 and map.tile(tx,ty-1) < 16 and map.tile(tx,ty+1) > 0 then + spr.pos.x = spr.pos.x - 1 + else + spr.flipped = not spr.flipped + end else - spr.flipped = not spr.flipped + local tx, ty = (spr.pos.x+12)>>3, (spr.pos.y+16)>>3 + if map.tile(tx,ty) < 16 and map.tile(tx,ty-1) < 16 and map.tile(tx,ty+1) > 0 then + spr.pos.x = spr.pos.x + 1 + else + spr.flipped = not spr.flipped + end + end + elseif spr.state == templates.DYING then + if spr.animation ~= "mummy_dying" then + spr.animation = "mummy_dying" + else + if spr.current_frame == 8 then + spr.animation = "mummy_dead" + spr.state = templates.DEAD + print("DEAD") -- [TOFIX] + end + end + elseif spr.state == templates.DEAD then + -- [TODO] + end + end, + + update_bullet = function(spr) + local tx, ty = (spr.pos.x+2)>>3, (spr.pos.y+1)>>3 + + if rooms.is_outside(tx,ty) then sprites.remove(spr) return end + + local x1,y1,w1,h1 = util.aabb(spr) + for i,v in ipairs(sprites.list) do + if v.enemy then + local x2,y2,w2,h2 = util.aabb(v) + if util.check_aabb_collision(x1,y1,w1,h1, x2,y2,w2,h2) then + if v.state == templates.ALIVE then v.state = templates.DYING end + sprites.remove(spr) + return + end + end + end + + if map.tile(tx,ty) < 16 then + if spr.flipped then + spr.pos.x = spr.pos.x - 8 + else + spr.pos.x = spr.pos.x + 8 end else - local tx, ty = (spr.pos.x+12)>>3, (spr.pos.y+16)>>3 - if map.tile(tx,ty) < 16 and map.tile(tx,ty-1) < 16 and map.tile(tx,ty+1) > 0 then - spr.pos.x = spr.pos.x + 1 - else - spr.flipped = not spr.flipped - end + sprites.remove(spr) end end, @@ -97,6 +154,15 @@ sprites = { local anim = "hero_stand" local move_anim = "hero_walk" + if sprites.hero.shooting then + if sprites.hero.current_frame==3 then + sprites.hero.shooting = false + if sprites.hero.flipped then sprites.hero.pos.x=sprites.hero.pos.x+8 end + sprites.set_animation(sprites.hero, "hero_stand") + else + return + end + end -- SI ESTÀ BOTANT... if sprites.hero.jumping > 0 then anim = "hero_jump" @@ -169,38 +235,48 @@ sprites = { --end end end - -- SI POLSA SPACE... - if key.down(key.SPACE) or pad.down(pad.A) then + -- SI POLSA BOTAR... + if key.down(key.Z) or pad.down(pad.A) then sprites.hero.jumping = 17 + + -- SI POLSA DISPAR... + elseif key.down(key.X) or pad.down(pad.B) then + sprites.hero.shooting = true + local x = sprites.hero.flipped and sprites.hero.pos.x+8 or sprites.hero.pos.x-8 + local bullet = templates.create("bullet", {pos={x=sprites.hero.pos.x, y=sprites.hero.pos.y+8}, flipped=sprites.hero.flipped}) + table.insert(sprites.list, bullet) + if sprites.hero.flipped then sprites.hero.pos.x=sprites.hero.pos.x-8 end + anim = "hero_shoot" end end end end -- ESTIGA COM ESTIGA, SI POLSA ESQUERRA O DRETA... - if key.down(key.LEFT) or pad.down(pad.LEFT) then - sprites.hero.flipped = true - anim = move_anim - local tx, ty = (sprites.hero.pos.x+3)>>3, (sprites.hero.pos.y+16)>>3 - if map.tile(tx,ty) < 16 and map.tile(tx,ty-1) < 16 then - if tx>3, (sprites.hero.pos.y+16)>>3 + if map.tile(tx,ty) < 16 and map.tile(tx,ty-1) < 16 then + if tx>3, (sprites.hero.pos.y+16)>>3 - if map.tile(tx,ty) < 16 and map.tile(tx,ty-1) < 16 then - if tx>rooms.pos.x+19 then - game.change_room(1,0) - else - sprites.hero.pos.x = sprites.hero.pos.x + 1 + elseif key.down(key.RIGHT) or pad.down(pad.RIGHT) then + sprites.hero.flipped = false + anim = move_anim + local tx, ty = (sprites.hero.pos.x+12)>>3, (sprites.hero.pos.y+16)>>3 + if map.tile(tx,ty) < 16 and map.tile(tx,ty-1) < 16 then + if tx>rooms.pos.x+19 then + game.change_room(1,0) + else + sprites.hero.pos.x = sprites.hero.pos.x + 1 + end end end end - sprites.set_animation(sprites.hero, anim) end, @@ -216,6 +292,8 @@ sprites = { local cycle = animations[sprite.animation].cycle[sprite.current_frame] local frame = animations[sprite.animation].frames[cycle] local reversed = frame.reversed or false - draw.surf(frame.frame.x, frame.frame.y, frame.frame.w, frame.frame.h, sprite.pos.x, sprite.pos.y, sprite.size.w, sprite.size.h, (not reversed) ~= (not sprite.flipped)) + draw.surf(frame.frame.x, frame.frame.y, frame.frame.w, frame.frame.h, sprite.pos.x, sprite.pos.y, frame.frame.w, frame.frame.h, (not reversed) ~= (not sprite.flipped)) + local x,y,w,h = util.aabb(sprite) + --draw.rect(x,y,w,h,8) end } \ No newline at end of file diff --git a/data/templates.lua b/data/templates.lua new file mode 100644 index 0000000..0d07c87 --- /dev/null +++ b/data/templates.lua @@ -0,0 +1,41 @@ +templates = { + ALIVE = 0, + DYING = 1, + DEAD = 2, + RESURRECTING = 3, + + create = function(type, options) + local sprite + if type == "mummy" then + sprite = { + type = type, + pos = options.pos,--{ x=100, y=4*12*8+71 }, + size = { w=16,h=17 }, + bbo = { left=3, top=2, right=3, bottom=0 }, + current_frame = 1, + current_wait = 1, + flipped = options.flipped, + animation = "mummy_walk", + state = templates.ALIVE, + enemy = true, + ia = sprites.update_mummy + } + elseif type == "bullet" then + sprite = { + type = type, + pos = options.pos,--{ x=100, y=4*12*8+71 }, + size = { w=4,h=3 }, + bbo = { left=0, top=0, right=0, bottom=0 }, + current_frame = 1, + current_wait = 1, + flipped = options.flipped, + animation = "bullet", + ia = sprites.update_bullet + } + else + error("Template not recognized") + end + print("creat sprite de tipus "..type) + return sprite + end, +} \ No newline at end of file diff --git a/data/util.lua b/data/util.lua index cefafde..6cf9231 100644 --- a/data/util.lua +++ b/data/util.lua @@ -9,5 +9,18 @@ util={ else return false end + end, + + aabb = function(sprite) + local x = sprite.pos.x + sprite.bbo.left + local y = sprite.pos.y + sprite.bbo.top + local w = sprite.size.w - sprite.bbo.right - sprite.bbo.left + local h = sprite.size.h - sprite.bbo.bottom - sprite.bbo.top + return x, y, w, h + end, + + check_aabb_collision = function(x1,y1,w1,h1, x2,y2,w2,h2) + return ( x1 < x2 + w2 and x1 + w1 > x2 and y1 < y2 + h2 and y1 + h1 > y2 ) end + } \ No newline at end of file