- [NEW] Comence a implementar enemics

- [NEW] Sistema de IA bàsica
This commit is contained in:
2025-06-23 19:20:41 +02:00
parent a5634a5869
commit 4fc968dcc4
4 changed files with 70 additions and 21 deletions

View File

@@ -3,19 +3,32 @@ require "animations"
sprites = {
hero = nil,
list = {},
pause_ia = false,
init = function()
sprites.hero = {
pos = { x=28, y=4*12*8+71 },
size= { w=16, h=17 },
bbo = { left=0, top=0, right=0, bottom=0 },
flipped = false,
jumping = 0,
animation = "hero_stand",
stairs = false,
current_frame = 1,
current_wait = 1
current_wait = 1,
flipped = false,
animation = "hero_stand",
ia = sprites.update_hero,
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)
end,
set_animation=function(sprite, animation)
@@ -35,27 +48,51 @@ sprites = {
end,
update = function()
if not sys.beat() then return end
sprites.update_sprite(sprites.hero)
for i,v in ipairs(sprites.list) do
sprites.update_sprite(v)
end
if not sprites.pause_ia then
sprites.hero.ia()
for i,v in ipairs(sprites.list) do
v.ia(v)
end
end
end,
update_sprite = function(sprite)
if sys.beat() then
sprite.current_wait = sprite.current_wait - 1
if sprite.current_wait == 0 then
if sprite.current_frame < #animations[sprite.animation].cycle then
sprite.current_frame = sprite.current_frame + 1
else
sprite.current_frame = 1
end
local cycle = animations[sprite.animation].cycle[sprite.current_frame]
sprite.current_wait = animations[sprite.animation].frames[cycle].wait
sprite.current_wait = sprite.current_wait - 1
if sprite.current_wait == 0 then
if sprite.current_frame < #animations[sprite.animation].cycle then
sprite.current_frame = sprite.current_frame + 1
else
sprite.current_frame = 1
end
local cycle = animations[sprite.animation].cycle[sprite.current_frame]
sprite.current_wait = animations[sprite.animation].frames[cycle].wait
end
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
else
spr.flipped = not spr.flipped
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
end
end,
update_hero = function()
if not sys.beat() then return end
-- Update hero
local anim = "hero_stand"
local move_anim = "hero_walk"
@@ -68,7 +105,7 @@ sprites = {
--draw.rect(tx1<<3,ty<<3,8,8,8)
--draw.rect(tx2<<3,ty<<3,8,8,28)
if map.tile(tx1,ty) == 0 and map.tile(tx2,ty) == 0 then
if ty<rooms.pos.y then
if ty+1<rooms.pos.y then
game.change_room(0,-1)
else
if sprites.hero.jumping > 1 then sprites.hero.pos.y = sprites.hero.pos.y - 1 end
@@ -87,7 +124,7 @@ sprites = {
anim = move_anim
local tx1, tx2, ty = (sprites.hero.pos.x+4)>>3, (sprites.hero.pos.x+11)>>3, (sprites.hero.pos.y)>>3
if map.tile(tx1,ty) < 16 or map.tile(tx2,ty) < 16 then
if ty<rooms.pos.y then
if ty+1<rooms.pos.y then
game.change_room(0,-1)
else
sprites.hero.pos.y = sprites.hero.pos.y - 1
@@ -169,7 +206,9 @@ sprites = {
draw = function()
surf.source(surf_sprites)
for i,v in ipairs(sprites.list) do
sprites.draw_sprite(v)
end
sprites.draw_sprite(sprites.hero)
end,