- [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)
This commit is contained in:
2025-06-26 14:06:34 +02:00
parent 47c7edde46
commit 8ba0ca9163
7 changed files with 218 additions and 50 deletions

View File

@@ -33,6 +33,20 @@ animations = {
{ frame={x=48,y=0,w=16,h=17}, wait=4, reversed=true } { 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"] = { ["mummy_walk"] = {
cycle = {1,2,1,3}, cycle = {1,2,1,3},
frames = { frames = {
@@ -41,4 +55,22 @@ animations = {
{ frame={x=32,y=24,w=16,h=17}, wait=4 } { 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 },
}
},
} }

View File

@@ -91,7 +91,7 @@ editor = {
sprites.hero.pos.y = ty*8-9 sprites.hero.pos.y = ty*8-9
elseif key.press(key.F9) then elseif key.press(key.F9) then
editor.play() editor.play()
elseif key.press(key.F11 ) then elseif key.press(key.F11) then
reload_textures() reload_textures()
end end
end, end,

View File

@@ -57,6 +57,10 @@ rooms = {
editor.modified = false editor.modified = false
end, 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() draw = function()
-- Retallem la pantalla a la zona de joc -- Retallem la pantalla a la zona de joc
view.clip(0,8,160,96) view.clip(0,8,160,96)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

@@ -1,15 +1,27 @@
require "animations" require "animations"
require "templates"
sprites = { sprites = {
hero = nil, hero = nil,
list = {}, list = {},
pause_ia = false, 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() init = function()
sprites.hero = { sprites.hero = {
type = "hero",
pos = { x=28, y=4*12*8+71 }, pos = { x=28, y=4*12*8+71 },
size= { w=16, h=17 }, 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_frame = 1,
current_wait = 1, current_wait = 1,
flipped = false, flipped = false,
@@ -18,24 +30,27 @@ sprites = {
jumping = 0, jumping = 0,
stairs = false stairs = false
} }
local mummy = { table.insert(sprites.list, templates.create("mummy", {pos={x=100, y=4*12*8+71},flipped=true}))
pos = { x=100, y=4*12*8+71 },
size = { w=16,h=17 }, --local mummy = {
bbo = { left=0, top=0, right=0, bottom=0 }, -- pos = { x=100, y=4*12*8+71 },
current_frame = 1, -- size = { w=16,h=17 },
current_wait = 1, -- bbo = { left=3, top=2, right=3, bottom=0 },
flipped = true, -- current_frame = 1,
animation = "mummy_walk", -- current_wait = 1,
ia = sprites.update_mummy -- flipped = true,
} -- animation = "mummy_walk",
table.insert(sprites.list, mummy) -- ia = sprites.update_mummy
--}
--table.insert(sprites.list, mummy)
end, end,
set_animation=function(sprite, animation) set_animation=function(sprite, animation)
if sprite.animation ~= animation then if sprite.animation ~= animation then
sprite.animation = animation sprite.animation = animation
sprite.current_frame = 1 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
end, end,
@@ -75,6 +90,7 @@ sprites = {
end, end,
update_mummy = function(spr) update_mummy = function(spr)
if spr.state == templates.ALIVE then
if spr.flipped then if spr.flipped then
local tx, ty = (spr.pos.x+3)>>3, (spr.pos.y+16)>>3 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 if map.tile(tx,ty) < 16 and map.tile(tx,ty-1) < 16 and map.tile(tx,ty+1) > 0 then
@@ -90,6 +106,47 @@ sprites = {
spr.flipped = not spr.flipped spr.flipped = not spr.flipped
end end
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
sprites.remove(spr)
end
end, end,
update_hero = function() update_hero = function()
@@ -97,6 +154,15 @@ sprites = {
local anim = "hero_stand" local anim = "hero_stand"
local move_anim = "hero_walk" 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... -- SI ESTÀ BOTANT...
if sprites.hero.jumping > 0 then if sprites.hero.jumping > 0 then
anim = "hero_jump" anim = "hero_jump"
@@ -169,14 +235,24 @@ sprites = {
--end --end
end end
end end
-- SI POLSA SPACE... -- SI POLSA BOTAR...
if key.down(key.SPACE) or pad.down(pad.A) then if key.down(key.Z) or pad.down(pad.A) then
sprites.hero.jumping = 17 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
end end
end end
-- ESTIGA COM ESTIGA, SI POLSA ESQUERRA O DRETA... -- ESTIGA COM ESTIGA, SI POLSA ESQUERRA O DRETA...
if not sprites.hero.shooting then
if key.down(key.LEFT) or pad.down(pad.LEFT) then if key.down(key.LEFT) or pad.down(pad.LEFT) then
sprites.hero.flipped = true sprites.hero.flipped = true
anim = move_anim anim = move_anim
@@ -200,7 +276,7 @@ sprites = {
end end
end end
end end
end
sprites.set_animation(sprites.hero, anim) sprites.set_animation(sprites.hero, anim)
end, end,
@@ -216,6 +292,8 @@ sprites = {
local cycle = animations[sprite.animation].cycle[sprite.current_frame] local cycle = animations[sprite.animation].cycle[sprite.current_frame]
local frame = animations[sprite.animation].frames[cycle] local frame = animations[sprite.animation].frames[cycle]
local reversed = frame.reversed or false 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 end
} }

41
data/templates.lua Normal file
View File

@@ -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,
}

View File

@@ -9,5 +9,18 @@ util={
else else
return false return false
end 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 end
} }