- [NEW] Módul propi per al MAPA

- [WIP] Batman ja camina, bota, cau, puja i baixa escales
This commit is contained in:
2025-11-07 14:16:05 +01:00
parent cc2f1803a4
commit 14dc8a0948
5 changed files with 309 additions and 141 deletions

View File

@@ -1,15 +1,203 @@
batman = {
states = {IDLE=1, WALK=2, JUMP=3, FALL=4, FALLTHROUGH=5, STAIRS_IDLE=6, STAIRS_MOVING=7},
x = 7*8,
y = 14*8,
surface = nil,
flip = false,
frames = {0},
frame = 1,
state = 1,
jump_counter = 0,
ignore_first_tile = 0,
init = function()
batman.surface = surf.load("batman.gif")
batman.frames = {0,1,2,3}
end,
draw = function()
surf.source(batman.surface)
draw.surf(0,0,16,16,batman.x,batman.y,16,16,batman.flip)
local f = batman.frames[batman.frame]
draw.surf((f%8)*16,(f//8)*16,16,16,batman.x,batman.y,16,16,batman.flip)
local tx1, tx2, ty = ((batman.x+4)//8), ((batman.x+10)//8), ((batman.y+16)//8)
draw.rect((tx1)*8,ty*8,8,8, 4)
draw.rect((tx2)*8,ty*8,8,8, 4)
end,
update = function()
batman.frame = batman.frame + 1
if batman.frame > #batman.frames then batman.frame = 1 end
-- Si està IDLE
if batman.state == batman.states.IDLE then
-- Mirem si comença a caminar
if key.down(key.RIGHT) then
batman.state = batman.states.WALK
batman.frames = {0,1,2,3}
batman.frame = 1
elseif key.down(key.LEFT) then
batman.state = batman.states.WALK
batman.frames = {0,1,2,3}
batman.frame = 1
elseif key.down(key.DOWN) then
local tx, ty = ((batman.x+4)//8), ((batman.y+8)//8)
if (mapa.isStairs(tx,ty)) then
batman.state = batman.states.STAIRS_IDLE
batman.frames = {10}
batman.frame = 1
batman.x = ((batman.x+4)//8)*8
if (mapa.isStairs(tx-1,ty)) then
batman.x = batman.x - 8
end
elseif key.down(key.X) then
local tx, ty = ((batman.x+4)//8), ((batman.y+24)//8)
if (not mapa.isSolid(tx,ty)) then
batman.state = batman.states.FALL
batman.ignore_first_tile = 4
end
end
elseif key.down(key.UP) then
local tx, ty = ((batman.x+4)//8), ((batman.y)//8)
if (mapa.isStairs(tx,ty)) then
batman.state = batman.states.STAIRS_IDLE
batman.frames = {10}
batman.frame = 1
--batman.x = (batman.x//8)*8
batman.x = ((batman.x+4)//8)*8
if (mapa.isStairs(tx-1,ty)) then
batman.x = batman.x - 8
end
end
end
-- Mirem si ha de caure
local tx1, tx2, ty = ((batman.x+4)//8), ((batman.x+10)//8), ((batman.y+16)//8)
if (not mapa.isSolid(tx1,ty)) and (not mapa.isSolid(tx2,ty)) then
batman.state = batman.states.FALL
end
-- Mirem si comença a botar
if (batman.state ~= batman.states.FALL) and (key.down(key.X)) then
batman.state = batman.states.JUMP
batman.jump_counter = 0
end
end
if batman.state == batman.states.WALK then
-- Mirem si comença a caminar
if key.down(key.RIGHT) then
batman.state = batman.states.WALK
batman.flip=false
local tx, ty = ((batman.x+12)//8), ((batman.y+8)//8)
if not mapa.isSolid(tx,ty) then batman.x = batman.x + 2 end
elseif key.down(key.LEFT) then
batman.state = batman.states.WALK
batman.flip=true
local tx, ty = ((batman.x+3)//8), ((batman.y+8)//8)
if not mapa.isSolid(tx,ty) then batman.x = batman.x - 2 end
else
-- Ha parat de caminar
batman.state = batman.states.IDLE
batman.frames = {0}
batman.frame = 1
end
-- Mirem si ha de caure
local tx1, tx2, ty = ((batman.x+4)//8), ((batman.x+10)//8), ((batman.y+16)//8)
if (not mapa.isSolid(tx1,ty)) and (not mapa.isSolid(tx2,ty)) then
batman.state = batman.states.FALL
end
-- Mirem si comença a botar
if key.down(key.X) then
batman.state = batman.states.JUMP
batman.jump_counter = 0
end
end
if batman.state == batman.states.JUMP then
batman.y = batman.y - 4
batman.jump_counter = batman.jump_counter + 1
if batman.jump_counter == 5 then
batman.jump_counter = 0
batman.state = batman.states.FALL
batman.frames = {4}
batman.frame = 1
end
if key.down(key.RIGHT) then
batman.flip=false
local tx, ty = ((batman.x+12)//8), ((batman.y+8)//8)
if not mapa.isBlock(tx,ty) then batman.x = batman.x + 2 end
elseif key.down(key.LEFT) then
batman.flip=true
local tx, ty = ((batman.x+3)//8), ((batman.y+8)//8)
if not mapa.isBlock(tx,ty) then batman.x = batman.x - 2 end
end
end
if batman.state == batman.states.FALL then
if key.down(key.RIGHT) then
batman.flip=false
local tx, ty = ((batman.x+12)//8), ((batman.y+8)//8)
if not mapa.isSolid(tx,ty) then batman.x = batman.x + 2 end
elseif key.down(key.LEFT) then
batman.flip=true
local tx, ty = ((batman.x+3)//8), ((batman.y+8)//8)
if not mapa.isSolid(tx,ty) then batman.x = batman.x - 2 end
end
local tx1, tx2, ty = ((batman.x+4)//8), ((batman.x+10)//8), ((batman.y+16)//8)
if (batman.ignore_first_tile>0) or ((not mapa.isSolid(tx1,ty)) and (not mapa.isSolid(tx2,ty))) then
if batman.ignore_first_tile>0 then batman.ignore_first_tile = batman.ignore_first_tile - 1 end
batman.state = batman.states.FALL
batman.frames = {4}
batman.frame = 1
batman.y = batman.y + 4
else
batman.y = (batman.y//8)*8
batman.state = batman.states.IDLE
batman.frames = {0}
batman.frame = 1
end
end
if batman.state == batman.states.STAIRS_IDLE then
local moving = false
if key.down(key.DOWN) then
moving = true
local tx, ty = ((batman.x+4)//8), ((batman.y+16)//8)
if (mapa.isStairs(tx,ty)) then
if #batman.frames==1 then
batman.frames = {8,10,9,10}
batman.frame = 1
end
batman.y = batman.y + 2;
else
batman.state = batman.states.IDLE
batman.frames = {0}
batman.frame = 1
end
elseif key.down(key.UP) then
moving = true
local tx, ty = ((batman.x+4)//8), ((batman.y+7)//8)
if (mapa.isStairs(tx,ty)) then
if #batman.frames==1 then
batman.frames = {8,10,9,10}
batman.frame = 1
end
batman.y = batman.y - 2;
else
batman.state = batman.states.IDLE
batman.frames = {0}
batman.frame = 1
end
elseif key.down(key.X) then
batman.state = batman.states.IDLE
batman.frames = {0}
batman.frame = 1
end
if not moving then
batman.frames = {10}
batman.frame = 1
end
end
end,
}

View File

@@ -1,11 +1,6 @@
require "mapa"
editor = {
num = 1,
map_surf = {nil,nil},
current_layer = 1,
current_tile = 1,
ox = 0,
oy = 0,
mx = 0,
my = 0,
@@ -18,65 +13,19 @@ editor = {
end,
new = function()
editor.num = 1
editor.map_surf[1] = surf.new(256,256)
editor.map_surf[2] = surf.new(256,256)
mapa.new()
editor.current_layer = 1
editor.current_tile = 1
editor.ox = 0
editor.oy = 0
end,
load = function(level)
local base_name = "level" .. tostring(level)
local file = io.open("data/" .. base_name .. ".lev", "r")
if file then
for line in file:lines() do
local key,value = line:match("^(.+)=(.+)$")
if key and value then
if key == "start-x" then
editor.ox = tonumber(value)
elseif key == "start-y" then
editor.oy = tonumber(value)
end
end
end
editor.num = level
surf.free(editor.map_surf[1])
surf.free(editor.map_surf[2])
editor.map_surf[1] = surf.load(base_name .. "_1.gif")
editor.map_surf[2] = surf.load(base_name .. "_2.gif")
else
print("No s'ha trobat l'arxiu!")
end
end,
save = function()
local base_name = "level" .. tostring(editor.num)
local file = io.open("data/" .. base_name .. ".lev", "w")
if file then
file:write("start-x=" .. tostring(editor.ox) .. "\n")
file:write("start-y=" .. tostring(editor.oy) .. "\n")
end
surf.save(editor.map_surf[1], "data/" .. base_name .. "_1.gif")
surf.save(editor.map_surf[2], "data/" .. base_name .. "_2.gif")
end,
update = function()
view.origin(editor.ox,editor.oy)
view.origin(mapa.x,mapa.y)
surf.cls(66)
pal.trans(0)
surf.source(tiles)
map.surf(editor.map_surf[1])
map.draw()
mapa.draw(1)
--batman.draw()
mapa.draw(2)
surf.source(tiles)
map.surf(editor.map_surf[2])
map.draw()
map.surf(editor.map_surf[editor.current_layer])
map.surf(mapa.surf[editor.current_layer])
local mx,my = mouse.pos()
@@ -87,25 +36,23 @@ editor = {
if key.press(key.TAB) then
game_update = editor.tile_picker_update
elseif key.press(key.RIGHT) then
editor.ox = editor.ox - 8
mapa.x = mapa.x - 8
elseif key.press(key.LEFT) then
editor.ox = editor.ox + 8
mapa.x = mapa.x + 8
elseif key.press(key.DOWN) then
editor.oy = editor.oy - 8
mapa.y = mapa.y - 8
elseif key.press(key.UP) then
editor.oy = editor.oy + 8
mapa.y = mapa.y + 8
elseif key.press(key.N1) then
editor.current_layer=1
elseif key.press(key.N2) then
editor.current_layer=2
elseif key.press(key.N2) then
editor.current_layer=2
elseif key.press(key.F) then
editor.fill(tx//8, ty//8, map.tile(mx/8, my/8))
mapa.fill(tx//8, ty//8, map.tile(mx/8, my/8), editor.current_tile)
elseif key.down(key.LCTRL) and key.press(key.S) then
editor.save()
mapa.save()
elseif key.down(key.LCTRL) and key.press(key.L) then
editor.load(1)
mapa.load(1)
end
if mouse.down(mouse.LEFT) then
@@ -122,8 +69,8 @@ editor = {
if editor.mx == -1 then
editor.mx,editor.my = mx,my
end
editor.ox = editor.ox + 8*(mx-editor.mx)
editor.oy = editor.oy + 8*(my-editor.my)
mapa.x = mapa.x + 8*(mx-editor.mx)
mapa.y = mapa.y + 8*(my-editor.my)
editor.mx,editor.my = mx,my
else
editor.mx = -1
@@ -132,25 +79,6 @@ editor = {
text(tostring(editor.current_layer),0,0,22,42)
end,
fill = function(x, y, tile)
local stack = {}
table.insert(stack, {x = x, y = y})
while #stack > 0 do
local mw, mh = surf.size(editor.map_surf[1])
local pos = table.remove(stack)
local px, py = pos.x, pos.y
map.tile(px, py, editor.current_tile)
if px<mw-1 and map.tile(px+1, py) == tile then table.insert(stack, {x = px + 1, y = py}) end
if px>0 and map.tile(px-1, py) == tile then table.insert(stack, {x = px - 1, y = py}) end
if py<mh-1 and map.tile(px, py+1) == tile then table.insert(stack, {x = px, y = py + 1}) end
if py>0 and map.tile(px, py-1) == tile then table.insert(stack, {x = px, y = py - 1}) end
end
end,
tile_picker_update = function()
view.origin(0,0)
surf.cls(66)

View File

@@ -1,72 +1,30 @@
require "mapa"
require "batman"
game = {
num = 1,
map_surf = {nil,nil},
current_layer = 1,
current_tile = 1,
init = function(level_name)
init = function(level)
game_update = game.update
game.load(1)
mapa.load(level)
batman.init()
end,
load = function(level)
local base_name = "level" .. tostring(level)
local file = io.open("data/" .. base_name .. ".lev", "r")
if file then
for line in file:lines() do
local key,value = line:match("^(.+)=(.+)$")
if key and value then
--if key == "start-x" then
-- editor.ox = tonumber(value)
--elseif key == "start-y" then
-- editor.oy = tonumber(value)
--end
end
end
game.num = level
if game.map_surf[1] then surf.free(game.map_surf[1]) end
if game.map_surf[2] then surf.free(game.map_surf[2]) end
game.map_surf[1] = surf.load(base_name .. "_1.gif")
game.map_surf[2] = surf.load(base_name .. "_2.gif")
else
print("No s'ha trobat l'arxiu!")
end
sys.beat(5)
end,
update = function()
--view.origin(editor.ox,editor.oy)
local cam_x, cam_y = -(batman.x-7*8),-(batman.y-6*8)
if (cam_x>0) then cam_x=0 end
if (cam_y>0) then cam_y=0 end
view.origin(cam_x,cam_y)
surf.cls(66)
pal.trans(0)
surf.source(tiles)
map.surf(game.map_surf[1])
map.draw()
mapa.draw(1)
batman.draw()
mapa.draw(2)
surf.source(tiles)
map.surf(game.map_surf[2])
map.draw()
map.surf(game.map_surf[game.current_layer])
if key.press(key.TAB) then
--game_update = editor.tile_picker_update
elseif key.down(key.RIGHT) then
batman.x = batman.x + 1
elseif key.down(key.LEFT) then
batman.x = batman.x - 1
elseif key.down(key.DOWN) then
batman.y = batman.y + 1
elseif key.down(key.UP) then
batman.y = batman.y - 1
if sys.beat() then
batman.update()
end
--view.origin(0,0)

View File

@@ -13,7 +13,8 @@ end
function mini.init()
loadSurfaces()
editor.init(1)
--editor.init(1)
game.init(1)
end
function mini.update()

93
data/mapa.lua Normal file
View File

@@ -0,0 +1,93 @@
mapa = {
num = 1,
surf = {nil,nil},
x = 0,
y = 0,
new = function()
mapa.num = 1
mapa.surf[1] = surf.new(256,256)
mapa.surf[2] = surf.new(256,256)
mapa.x = 0
mapa.y = 0
end,
load = function(level)
local base_name = "level" .. tostring(level)
local file = io.open("data/" .. base_name .. ".lev", "r")
if file then
for line in file:lines() do
local key,value = line:match("^(.+)=(.+)$")
if key and value then
if key == "start-x" then
mapa.x = tonumber(value)
elseif key == "start-y" then
mapa.y = tonumber(value)
end
end
end
mapa.num = level
if mapa.surf[1] then surf.free(mapa.surf[1]) end
if mapa.surf[2] then surf.free(mapa.surf[2]) end
mapa.surf[1] = surf.load(base_name .. "_1.gif")
mapa.surf[2] = surf.load(base_name .. "_2.gif")
else
print("No s'ha trobat l'arxiu!")
end
end,
save = function()
local base_name = "level" .. tostring(mapa.num)
local file = io.open("data/" .. base_name .. ".lev", "w")
if file then
file:write("start-x=" .. tostring(mapa.x) .. "\n")
file:write("start-y=" .. tostring(mapa.y) .. "\n")
end
surf.save(mapa.surf[1], "data/" .. base_name .. "_1.gif")
surf.save(mapa.surf[2], "data/" .. base_name .. "_2.gif")
end,
draw = function(layer)
pal.trans(0)
surf.source(tiles)
map.surf(mapa.surf[layer])
map.draw()
map.surf(mapa.surf[1])
end,
fill = function(x, y, tile, fill_tile)
local stack = {}
table.insert(stack, {x = x, y = y})
while #stack > 0 do
local mw, mh = surf.size(map.surf[1])
local pos = table.remove(stack)
local px, py = pos.x, pos.y
map.tile(px, py, fill_tile)
if px<mw-1 and map.tile(px+1, py) == tile then table.insert(stack, {x = px + 1, y = py}) end
if px>0 and map.tile(px-1, py) == tile then table.insert(stack, {x = px - 1, y = py}) end
if py<mh-1 and map.tile(px, py+1) == tile then table.insert(stack, {x = px, y = py + 1}) end
if py>0 and map.tile(px, py-1) == tile then table.insert(stack, {x = px, y = py - 1}) end
end
end,
isSolid = function(x, y)
local tile = map.tile(x, y)
return (tile == 32) or (tile == 33) or (tile==36) or (tile==37)
end,
isBlock = function(x, y)
local tile = map.tile(x, y)
return (tile == 32)
end,
isStairs = function(x, y)
local tile = map.tile(x, y)
return (tile>33) and (tile<40)
end,
}