- [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:
172
data/sprites.lua
172
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<rooms.pos.x then
|
||||
game.change_room(-1,0)
|
||||
else
|
||||
sprites.hero.pos.x = sprites.hero.pos.x - 1
|
||||
if not sprites.hero.shooting then
|
||||
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<rooms.pos.x then
|
||||
game.change_room(-1,0)
|
||||
else
|
||||
sprites.hero.pos.x = sprites.hero.pos.x - 1
|
||||
end
|
||||
end
|
||||
end
|
||||
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
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user