Factoritzades funcions per a conversió de coordenades.

[NEW] Tiles animats
This commit is contained in:
2026-03-28 21:08:45 +01:00
parent 639d2e98ad
commit ac9fcebca9
15 changed files with 393 additions and 201 deletions

View File

@@ -87,7 +87,7 @@ end
function abad:move( x, y ) function abad:move( x, y )
self.x = x self.x = x
self.y = y self.y = y
local hab,xx, yy = arc_mapa_get_map_coords(x, y) local hab,xx, yy = coords.world_to_tile(x, y)
self.hab = hab self.hab = hab
end end
@@ -113,7 +113,7 @@ end
function abad_make_safe( force ) function abad_make_safe( force )
force = force or false force = force or false
local hab, xx, yy = arc_mapa_get_map_coords(abad.x, abad.y) local hab, xx, yy = coords.world_to_tile(abad.x, abad.y)
if abad.safe.hab~=hab or force then if abad.safe.hab~=hab or force then
abad.safe.hab=hab abad.safe.hab=hab
abad.safe.x=xx abad.safe.x=xx
@@ -139,7 +139,7 @@ function abad_hurt(howmuch)
-- abad.hab=abad.safe.hab -- abad.hab=abad.safe.hab
-- abad.x=abad.safe.x -- abad.x=abad.safe.x
-- abad.y=abad.safe.y -- abad.y=abad.safe.y
local abad_x, abad_y = arc_mapa_get_coords ( abad.safe.hab, abad.safe.x, abad.safe.y ) local abad_x, abad_y = coords.room_to_world ( abad.safe.hab, abad.safe.x, abad.safe.y )
abad:move(abad_x, abad_y) abad:move(abad_x, abad_y)
abad.hurting=60 abad.hurting=60
abad.update=abad_state_normal abad.update=abad_state_normal
@@ -291,7 +291,7 @@ function abad_advance()
abad.update=abad_state_normal abad.update=abad_state_normal
abad.frame=0 abad.frame=0
end end
local hab,xx, yy = arc_mapa_get_map_coords(abad.x, abad.y) local hab,xx, yy = coords.world_to_tile(abad.x, abad.y)
abad.hab = hab abad.hab = hab
end end

View File

@@ -9,7 +9,7 @@ bol_gif_row = 3
bol={hab=39,x=4,y=3,w=32,h=16,bb={x=0,y=0,w=16,h=8}} bol={hab=39,x=4,y=3,w=32,h=16,bb={x=0,y=0,w=16,h=8}}
function bol.init() function bol.init()
local world_x, world_y = arc_mapa_get_coords(bol.hab,bol.x,bol.y) local world_x, world_y = coords.room_to_world(bol.hab,bol.x,bol.y)
bol.x=world_x-8 bol.x=world_x-8
bol.y=world_y+2 bol.y=world_y+2
bol.update=bol.update bol.update=bol.update

View File

@@ -7,7 +7,7 @@ ch = arcade_config.character_height
caco={} caco={}
function caco.new(_hab,_x,_y,_flip) function caco.new(_hab,_x,_y,_flip)
local world_x, world_y = arc_mapa_get_coords(_hab,_x,_y) local world_x, world_y = coords.room_to_world(_hab,_x,_y)
return {hab=_hab, return {hab=_hab,
x=world_x, x=world_x,
y=world_y, y=world_y,

127
data/coords.lua Normal file
View File

@@ -0,0 +1,127 @@
-- Transformacio de coordenades entre el que es gastava en
-- el Cacaus original i l'arcade
-- coords.lua
local coords = {}
-- Configuración (se inyecta desde fuera)
function coords.set_config(cfg)
coords.TILE_W = cfg.tiles_width
coords.TILE_H = cfg.tiles_height
coords.ROOM_COLS = cfg.room_cols -- tiles por habitación en X
coords.ROOM_ROWS = cfg.room_rows -- tiles por habitación en Y
coords.ROOMS_PER_FLOOR = cfg.rooms_per_floor
coords.ROOM_W = coords.TILE_W * coords.ROOM_COLS
coords.ROOM_H = coords.TILE_H * coords.ROOM_ROWS
end
-- -------------------------------------------------
-- World (pixel) → tile en la surface de mini que guarda el mapa
function coords.world_to_mini_tile(x, y)
-- posició local dins de l'habitació (pixels)
local map_x = math.floor(x / coords.TILE_W)
local map_y = math.floor(y / coords.TILE_H)
return map_x, map_y
end
-- -------------------------------------------------
-- World (pixel) → tile dins de l'habitació
function coords.world_to_tile(x, y)
local room = coords.world_to_room(x, y)
-- posició local dins de l'habitació (pixels)
local local_x = x % coords.ROOM_W
local local_y = y % coords.ROOM_H
-- coordenades del tile
local tile_col = math.floor(local_x / coords.TILE_W)
local tile_row = math.floor(local_y / coords.TILE_H)
-- offsets dins del tile
local offset_x = local_x % coords.TILE_W
local offset_y = local_y % coords.TILE_H
return room, tile_col, tile_row, offset_x, offset_y
end
-- -------------------------------------------------
-- World (pixel) → room index
function coords.world_to_room(x, y)
local room_col = math.floor(x / coords.ROOM_W)
local room_row = math.floor(y / coords.ROOM_H)
return room_row * coords.ROOMS_PER_FLOOR + room_col
end
function coords.room_to_world(hab, tile_x, tile_y)
-- La primera habitació es la 0
-- El primer tile es 0
local world_x = coords.TILE_W * ( (hab % coords.ROOMS_PER_FLOOR)*coords.ROOM_COLS + tile_x)
local world_y = coords.TILE_H * (math.floor(hab / coords.ROOMS_PER_FLOOR)*coords.ROOM_ROWS + tile_y)
return world_x, world_y
end
function coords.room_index_to_xy ( room, tile_idx )
local x = (tile_idx-1) % coords.ROOM_COLS
local y = math.floor((tile_idx-1) / coords.ROOM_COLS)
return x, y
end
function coords.room_to_coord ( room, center )
center = center or false
local col = room % coords.ROOMS_PER_FLOOR -- columna de l'habitació
local row = math.floor(room / coords.ROOMS_PER_FLOOR) -- fila de l'habitació
local x = col * coords.ROOM_W
local y = row * coords.ROOM_H
if center then
-- moure x,y al centre de l'habitació
x = x + (coords.ROOM_W // 2)
y = y + (coords.ROOM_H // 2)
end
return x, y
end
-- local TILE_W = arcade_config.tiles_width
-- local TILE_H = arcade_config.tiles_height
-- local ROOM_COLS = mapa_room_cols
-- local ROOM_ROWS = mapa_room_rows
-- local ROOMS_PER_FLOOR = mapa_rooms_per_piso
-- local ROOM_W = TILE_W*ROOM_COLS
-- local ROOM_H = TILE_H*ROOM_ROWS
-- function coords_world_to_tile (x, y)
-- local calc_col = math.floor(x / TILE_W) % ROOM_COLS
-- local calc_row = math.floor(y / TILE_H) % ROOM_ROWS
-- local calc_room = math.floor(y / (TILE_H * ROOM_ROWS))*ROOMS_PER_FLOOR + math.floor(x / ROOM_W)
--
-- local tile_offset_x = x - (calc_col * TILE_W) - ((calc_room % ROOMS_PER_FLOOR)*ROOM_W)
-- local tile_offset_y = y - (calc_row * TILE_H) - ( math.floor(calc_room / ROOMS_PER_FLOOR)*ROOM_H)
--
-- -- room, x, y, offset x, offset y
-- return calc_room, calc_col, calc_row, tile_offset_x, tile_offset_y
-- end
-- function coords_world_to_room (x, y)
-- local calc_col = math.floor(x / ROOM_W)
-- local calc_row = math.floor(y / ROOM_H)
--
-- local calc_room = calc_row*ROOMS_PER_FLOOR+calc_col
-- return calc_room
-- end
return coords
-- local cfg = {
-- tiles_width = arcade_config.tiles_width,
-- tiles_height = arcade_config.tiles_height,
-- room_cols = mapa_room_cols,
-- room_rows = mapa_room_rows,
-- rooms_per_floor = mapa_rooms_per_piso,
-- }
-- local coords = require "coords"
-- coords.set_config(cfg)

View File

@@ -21,7 +21,7 @@ local res_h = arcade_config.resolucion.height
local view_tile_id = false local view_tile_id = false
local view_checking_tile = false local view_checking_tile = false
viewp = viewport.new() viewp = viewport.new(arcade_config.resolucion.width, arcade_config.resolucion.height)
viewp:position(0,0) viewp:position(0,0)
actors={} actors={}
@@ -74,7 +74,7 @@ function game_init(menu)
table.insert( actors, zombie.new(68, 3, 3,false) ) table.insert( actors, zombie.new(68, 3, 3,false) )
table.insert( actors, zombie.new(73, 3, 3,false) ) table.insert( actors, zombie.new(73, 3, 3,false) )
local abad_x, abad_y = arc_mapa_get_coords ( 10, 4, 3 ) local abad_x, abad_y = coords.room_to_world ( 10, 4, 3 )
-- local abad_x, abad_y = arc_mapa_get_coords ( 77, 3, 2 ) -- local abad_x, abad_y = arc_mapa_get_coords ( 77, 3, 2 )
abad:move(abad_x, abad_y) abad:move(abad_x, abad_y)
abad_make_safe( true ) abad_make_safe( true )
@@ -140,14 +140,12 @@ function update_game()
-- if vp_y+1<=(tile_h*mapa_room_rows*mapa_pisos)-(res_h) then vp_y = vp_y+1 end -- if vp_y+1<=(tile_h*mapa_room_rows*mapa_pisos)-(res_h) then vp_y = vp_y+1 end
--end --end
load_tilemap( sf_mapa )
if key.press(key.N1) then if key.press(key.N1) then
local hab = abad.hab-1 local hab = abad.hab-1
if hab<0 then hab=0 end if hab<0 then hab=0 end
local hab_x = 4 local hab_x = 4
local hab_y = 3 local hab_y = 3
local abad_x, abad_y = arc_mapa_get_coords ( hab, hab_x, hab_y) local abad_x, abad_y = coords.room_to_world ( hab, hab_x, hab_y)
abad:move(abad_x, abad_y) abad:move(abad_x, abad_y)
local scr_ax, scr_ay = viewp:screen_coords(abad_x, abad_y) local scr_ax, scr_ay = viewp:screen_coords(abad_x, abad_y)
end end
@@ -156,16 +154,19 @@ function update_game()
if hab<0 then hab=0 end if hab<0 then hab=0 end
local hab_x = 4 local hab_x = 4
local hab_y = 3 local hab_y = 3
local abad_x, abad_y = arc_mapa_get_coords ( hab, hab_x, hab_y) local abad_x, abad_y = coords.room_to_world ( hab, hab_x, hab_y)
abad:move(abad_x, abad_y) abad:move(abad_x, abad_y)
local scr_ax, scr_ay = viewp:screen_coords(abad_x, abad_y) local scr_ax, scr_ay = viewp:screen_coords(abad_x, abad_y)
end end
if key.press(key.N0) then if key.press(key.N0) then
local abad_x, abad_y = arc_mapa_get_coords ( 24, 1, 3 ) local abad_x, abad_y = coords.room_to_world ( 24, 1, 3 )
print(abad_x..", "..abad_y) print(abad_x..", "..abad_y)
abad:move(abad_x, abad_y) abad:move(abad_x, abad_y)
local scr_ax, scr_ay = viewp:screen_coords(abad_x, abad_y) local scr_ax, scr_ay = viewp:screen_coords(abad_x, abad_y)
end end
arc_mapa_update()
for key,actor in pairs(actors) do for key,actor in pairs(actors) do
actor:update() actor:update()
--if actor.hab==cacau.hab and actor~=abad then --if actor.hab==cacau.hab and actor~=abad then
@@ -208,7 +209,7 @@ function update_game()
viewp:print() viewp:print()
msg_print(0,14,"ABAD= "..abad.x..", "..abad.y, true) msg_print(0,14,"ABAD= "..abad.x..", "..abad.y, true)
msg_print(0,21,"VIEW= "..vp_x..", "..vp_y, true) msg_print(0,21,"VIEW= "..vp_x..", "..vp_y, true)
local hab, xx, yy = arc_mapa_get_map_coords(abad.x, abad.y) local hab, xx, yy = coords.world_to_tile(abad.x, abad.y)
msg_print(0,28,hab.." ( "..xx..", "..yy.." )", true) msg_print(0,28,hab.." ( "..xx..", "..yy.." )", true)
-- msg_print(0,35,hab.." ( "..xx..", "..yy.." )", true) -- msg_print(0,35,hab.." ( "..xx..", "..yy.." )", true)
msg_print(0,42," JH= "..abad.jump_height,true) msg_print(0,42," JH= "..abad.jump_height,true)
@@ -271,7 +272,7 @@ end
function write_tile(x, y, yplus, print_type, align ) function write_tile(x, y, yplus, print_type, align )
local scr_x, scr_y = viewp:screen_coords(x, y) local scr_x, scr_y = viewp:screen_coords(x, y)
local hab, xx, yy = arc_mapa_get_map_coords(x, y) local hab, xx, yy = coords.world_to_tile(x, y)
yplus = yplus or 0 yplus = yplus or 0
print_type = print_type or false print_type = print_type or false
@@ -283,7 +284,8 @@ function write_tile(x, y, yplus, print_type, align )
end end
draw.rectf(scr_x+txt_offset,scr_y+yplus,14,7,16) draw.rectf(scr_x+txt_offset,scr_y+yplus,14,7,16)
local msg = mapa_get_tile(hab,xx,yy) -- local msg = mapa_get_tile(hab,xx,yy)
local msg = arc_get_tile(x,y)
if print_type then if print_type then
msg = msg.." "..arc_check_tile(x, y) msg = msg.." "..arc_check_tile(x, y)
end end

View File

@@ -16,7 +16,7 @@ function gorro.init()
-- gorro.x=habs[r][2]*8 -- gorro.x=habs[r][2]*8
gorro.x=habs[r][2] gorro.x=habs[r][2]
local world_x, world_y = arc_mapa_get_coords(gorro.hab,gorro.x,gorro.y) local world_x, world_y = coords.room_to_world(gorro.hab,gorro.x,gorro.y)
gorro.x=world_x gorro.x=world_x
gorro.y=world_y gorro.y=world_y
gorro.update=gorro.update gorro.update=gorro.update

View File

@@ -10,7 +10,7 @@ gota_gif_row = 0
gota={} gota={}
function gota.new(_hab,_x,_y,_freq,_x_offset, _y_offset) function gota.new(_hab,_x,_y,_freq,_x_offset, _y_offset)
local world_x, world_y = arc_mapa_get_coords(_hab,_x,_y) local world_x, world_y = coords.room_to_world(_hab,_x,_y)
_x_offset = _x_offset or 0 _x_offset = _x_offset or 0
_y_offset = _y_offset or 0 _y_offset = _y_offset or 0
world_x = world_x+_x_offset world_x = world_x+_x_offset

View File

@@ -16,7 +16,7 @@ function gps.init()
-- gps.x=habs[r][2]*8 -- gps.x=habs[r][2]*8
gps.x=habs[r][2] gps.x=habs[r][2]
local world_x, world_y = arc_mapa_get_coords(gps.hab,gps.x,gps.y) local world_x, world_y = coords.room_to_world(gps.hab,gps.x,gps.y)
gps.x=world_x gps.x=world_x
gps.y=world_y gps.y=world_y

View File

@@ -1,4 +1,5 @@
arcade_config = require("arcade_config") arcade_config = require("arcade_config")
coords = require "coords"
require "fps" require "fps"
require "fade" require "fade"
@@ -11,6 +12,26 @@ require "game"
require "switches" require "switches"
-- require "scenes" -- require "scenes"
coords.set_config({
tiles_width = arcade_config.tiles_width,
tiles_height = arcade_config.tiles_height,
room_cols = mapa_room_cols,
room_rows = mapa_room_rows,
rooms_per_floor = mapa_rooms_per_piso,
})
function editor_to_map_tile(editor_tile)
local result = 0
if editor_tile<256 then result = editor_tile + arcade_config.tiles_offset end
return result
end
function map_to_editor_tile(map_tile)
local result = map_tile - arcade_config.tiles_offset
if map_tile==0 then result = 256 end
return result
end
function load_tilemap( sf_mapa ) function load_tilemap( sf_mapa )
local mapa_tw, mapa_th = surf.size(sf_mapa) local mapa_tw, mapa_th = surf.size(sf_mapa)
local nrooms = mapa_rooms_per_piso*mapa_pisos local nrooms = mapa_rooms_per_piso*mapa_pisos
@@ -27,8 +48,7 @@ function load_tilemap( sf_mapa )
end end
xroom = yroom xroom = yroom
for tx=0,mapa_tw-1 do for tx=0,mapa_tw-1 do
local tile=mapa[1+xroom][1+x+y*mapa_room_cols] local tile=editor_to_map_tile(mapa[1+xroom][1+x+y*mapa_room_cols])
if tile<128 then tile= tile + 128 end
map.tile(tx, ty, tile) map.tile(tx, ty, tile)
x = x + 1 x = x + 1
if x == mapa_room_cols then if x == mapa_room_cols then
@@ -91,9 +111,9 @@ function mini.init()
btnCycle2 = tonumber(config.key("btncycle2")) or pad.LEFTSHOULDER btnCycle2 = tonumber(config.key("btncycle2")) or pad.LEFTSHOULDER
btnPause = tonumber(config.key("btnpause")) or pad.START btnPause = tonumber(config.key("btnpause")) or pad.START
-- game_init() -- logo_init()
-- intro_init() -- intro_init()
logo_init() game_init()
-- final_init() -- final_init()
end end
@@ -158,35 +178,3 @@ function arc_textB(str, x, y, col, colB)
draw.surf(0,0,sw,sh,x,y,dw,dh) draw.surf(0,0,sw,sh,x,y,dw,dh)
surf.source(curr_surf_src) surf.source(curr_surf_src)
end end
function arc_textB2(str, x, y, col, colB)
local ox, oy = view.origin()
local curr_surf_tgt = surf.target()
local curr_surf_src = surf.source()
local sw = arcade_config.org_resolucion.width
local sh = arcade_config.org_resolucion.height
local dw = arcade_config.resolucion.width
local dh = arcade_config.resolucion.height
colB = colB or 16
surf.target(textsf)
view.origin(0,0)
surf.cls(0)
draw.text(str,0,0,colB)
draw.text(str,1,0,colB)
draw.text(str,2,0,colB)
draw.text(str,0,1,colB)
draw.text(str,2,1,colB)
draw.text(str,0,2,colB)
draw.text(str,1,2,colB)
draw.text(str,2,2,colB)
draw.text(str,1,1,col)
-- print("arc_B "..str)
surf.source(textsf)
surf.target(curr_surf_tgt)
view.origin(ox,oy)
-- draw.surf(0,0,sw,sh,x,y,dw,dh)
draw.surf(0,0)
surf.source(curr_surf_src)
end

View File

@@ -2,6 +2,7 @@ mapa_room_cols = 12; -- en quantitat de tiles
mapa_room_rows = 6; -- en quantitat de tiles mapa_room_rows = 6; -- en quantitat de tiles
mapa_rooms_per_piso = 10 mapa_rooms_per_piso = 10
mapa_pisos = 8 mapa_pisos = 8
mapa_empty_tile = 256
-- 0 1 2 3 4 5 6 7 8 9 -- 0 1 2 3 4 5 6 7 8 9
-- 10 11 12 13 14 15 16 17 18 19 -- 10 11 12 13 14 15 16 17 18 19

View File

@@ -78,27 +78,23 @@ function mapa_save()
end end
anim_tiles={113,114,112,116,117,115,119,120,118,122,121} anim_tiles={113,114,112,116,117,115,119,120,118,122,121}
function mapa_update(hab1,hab2) function arc_mapa_update()
mapa.wait=mapa.wait+1 mapa.wait=mapa.wait+1
if mapa.wait==6 then if mapa.wait==6 then
mapa.wait = 0 mapa.wait = 0
mapa.step=(mapa.step+1)&31 mapa.step=(mapa.step+1)&31
local hab=hab1 local x0, y0, x1, y1 = viewp:get()
local tile = 0
repeat for y=y0,y1,arcade_config.tiles_height do
for ty=0,5 do for x=x0,x1,arcade_config.tiles_width do
for tx=0,11 do tile=map_to_editor_tile(arc_get_tile(x,y))
local tile=mapa[1+hab][1+tx+ty*12]
if tile>=112 and tile<126 then if tile>=112 and tile<126 then
mapa[1+hab][1+tx+ty*12]=anim_tiles[tile-111] arc_set_tile(x,y,editor_to_map_tile(anim_tiles[tile-111]))
end end
end end
end end
if hab==hab2 then break end
hab=hab2
until false
end end
end end
@@ -119,19 +115,6 @@ function mapa_draw(hab)
end end
end end
function mapa_set_tile(hab,x,y,tile)
mapa[1+hab][1+x+y*12]=tile
end
function mapa_set_tile_by_index(hab,index,tile)
print("MSTBI= R"..hab..", I"..index..", T="..mapa[1+hab][index].." -> "..tile)
mapa[1+hab][index]=tile
end
function mapa_get_tile(hab,x,y)
return mapa[1+hab][1+x+y*12]
end
function mapa_cycle_colors(hab) function mapa_cycle_colors(hab)
for i=1,72 do for i=1,72 do
local tile=mapa[1+hab][i] local tile=mapa[1+hab][i]
@@ -145,74 +128,28 @@ function mapa_cycle_colors(hab)
end end
end end
-- function check_tile(hab,x,y)
-- msg_print(0,35,"c_t( "..hab..", "..x..", "..y.." )")
-- print("c_t( "..hab..", "..x..", "..y.." )")
-- -- local xx=math.min(11,math.max(0,math.floor(x/16)))
-- -- local yy=math.min(5,math.max(0,math.floor(y/16)))
-- --rect(xx*8,yy*8,xx*8+8,yy*8+8,3)
--
-- -- tiletype => void=0 / nonpc=1 / stair=2 /
-- -- switch=3 / half=4 / block=5
--
-- local tile=mapa_get_tile(hab,x,y)
-- if tile<8 then
-- return tiletype.half
-- elseif tile<15 then
-- return tiletype.stair
-- elseif tile==15 then
-- return tiletype.switch
-- elseif tile<64 then
-- return tiletype.block
-- elseif tile==111 then
-- return tiletype.nonpc
-- else
-- return tiletype.void
-- end
-- end
function arc_mapa_get_coords ( hab, tile_x , tile_y ) function arc_set_tile(world_x,world_y,tile)
-- La primera habitació es la 0 local tx, ty = coords.world_to_mini_tile(world_x, world_y)
-- El primer tile es 0 map.tile(tx, ty, tile)
x = ((hab % 10)*mapa_room_cols + tile_x)*arcade_config.tiles_width
y = (math.floor(hab/10)*mapa_room_rows + tile_y)*arcade_config.tiles_height
return x, y
end end
function arc_mapa_get_map_coords ( x, y ) function arc_set_tile_by_index(hab,index,tile)
local tw = arcade_config.tiles_width local tile_x, tile_y = coords.room_index_to_xy ( hab, index )
local th = arcade_config.tiles_height local world_x, world_y = coords.room_to_world(hab, tile_x, tile_y)
local cols = mapa_room_cols arc_set_tile(world_x, world_y, tile)
local rows = mapa_room_rows
local rooms_per_floor = mapa_rooms_per_piso
local calc_col = math.floor(x / tw) % cols
local calc_row = math.floor(y / th) % rows
local calc_room = math.floor(y / (th * rows))*rooms_per_floor+math.floor(x / (tw * cols))
return calc_room, calc_col, calc_row
end end
function arc_get_tile(world_x,world_y) function arc_get_tile(world_x,world_y)
-- local xx=math.min(11,math.max(0,math.floor(x/8))) local map_x, map_y = coords.world_to_mini_tile(world_x,world_y)
-- local yy=math.min(5,math.max(0,math.floor(y/8))) return map.tile(map_x,map_y)
--rect(xx*8,yy*8,xx*8+8,yy*8+8,3)
-- tiletype => void=0 / nonpc=1 / stair=2 /
-- switch=3 / half=4 / block=5
local hab, xx, yy = arc_mapa_get_map_coords(world_x, world_y)
-- print("ARC_GT= "..hab.."> "..x..", "..y.." => "..tile)
local tile=mapa_get_tile(hab,xx,yy)
return tile
end end
function arc_check_tile(world_x,world_y) function arc_check_tile(world_x,world_y)
-- tiletype => void=0 / nonpc=1 / stair=2 / -- tiletype => void=0 / nonpc=1 / stair=2 /
-- switch=3 / half=4 / block=5 -- switch=3 / half=4 / block=5
local tile=arc_get_tile(world_x, world_y) local tile=map_to_editor_tile(arc_get_tile(world_x, world_y))
if tile<8 then if tile<8 then
return tiletype.half return tiletype.half
elseif tile<15 then elseif tile<15 then
@@ -256,3 +193,115 @@ function mapa_draw_minimap()
surf.cls(16) surf.cls(16)
draw.surf(0,0,128*o2aX,96*o2Ax,0,0) draw.surf(0,0,128*o2aX,96*o2Ax,0,0)
end end
-- el el fitxer del mapa els tiles comencen per 0 pero estàn en el offset 128
-- Sustituida per arc_set_tile()
-- function mapa_set_tile(hab,x,y,tile)
-- print("MST= R"..hab..", "..x..", "..y.." -> "..tile)
--
-- -- NO es mante el mapa i la surface
-- -- mapa[1+hab][1+x+y*mapa_room_cols]=tile
--
-- local tx = (hab % mapa_rooms_per_piso)*mapa_room_cols+x
-- local ty = math.floor(hab / mapa_rooms_per_piso)*mapa_room_rows+y
-- -- if tile<arcade_config.tiles_offset then tile= tile + arcade_config.tiles_offset end
-- -- map.tile(tx, ty, tile)
-- map.tile(tx, ty, tile)
-- end
-- Sustituida per arc_set_tile_by_index()
-- function mapa_set_tile_by_index(hab,index,tile)
-- -- print("MSTBI= R"..hab..", I"..index..", T="..mapa[1+hab][index].." -> "..tile)
-- -- mapa[1+hab][index]=tile
-- local xx = ((index-1) % mapa_room_cols)
-- local yy = math.floor((index-1) / mapa_room_cols)
--
-- local tx = (hab % mapa_rooms_per_piso)*mapa_room_cols+xx
-- local ty = math.floor(hab / mapa_rooms_per_piso)*mapa_room_rows+yy
--
-- map.tile(tx, ty, tile+arcade_config.tiles_offset)
-- -- mapa_set_tile(hab, xx, yy, tile+arcade_config.tiles_offset)
-- end
-- sustituida per arc_get_tile
-- function mapa_get_tile(hab,x,y)
-- -- com se va mantenint tant mapa com la surface de map simultaniament
-- -- no deuria haver problemes
-- return mapa[1+hab][1+x+y*mapa_room_cols]
-- end
-- Sustituida per arc_mapa_update()
-- function mapa_update(hab1,hab2)
-- mapa.wait=mapa.wait+1
--
-- if mapa.wait==6 then
-- mapa.wait=0
-- mapa.step=(mapa.step+1)&31
--
-- local hab=hab1
--
-- repeat
-- for ty=0,5 do
-- for tx=0,11 do
-- local tile=mapa[1+hab][1+tx+ty*12]
-- if tile>=112 and tile<126 then
-- mapa[1+hab][1+tx+ty*12]=anim_tiles[tile-111]
-- end
-- end
-- end
-- if hab==hab2 then break end
-- hab=hab2
-- until false
-- end
-- end
-- function check_tile(hab,x,y)
-- msg_print(0,35,"c_t( "..hab..", "..x..", "..y.." )")
-- print("c_t( "..hab..", "..x..", "..y.." )")
-- -- local xx=math.min(11,math.max(0,math.floor(x/16)))
-- -- local yy=math.min(5,math.max(0,math.floor(y/16)))
-- --rect(xx*8,yy*8,xx*8+8,yy*8+8,3)
--
-- -- tiletype => void=0 / nonpc=1 / stair=2 /
-- -- switch=3 / half=4 / block=5
--
-- local tile=mapa_get_tile(hab,x,y)
-- if tile<8 then
-- return tiletype.half
-- elseif tile<15 then
-- return tiletype.stair
-- elseif tile==15 then
-- return tiletype.switch
-- elseif tile<64 then
-- return tiletype.block
-- elseif tile==111 then
-- return tiletype.nonpc
-- else
-- return tiletype.void
-- end
-- end
-- Sustituida per coords.room_to_world()
--function arc_mapa_get_coords ( hab, tile_x , tile_y )
-- -- La primera habitació es la 0
-- -- El primer tile es 0
-- x = ((hab % 10)*mapa_room_cols + tile_x)*arcade_config.tiles_width
-- y = (math.floor(hab/10)*mapa_room_rows + tile_y)*arcade_config.tiles_height
--
-- return x, y
--end
-- Sustituida per coords.world_to_tile()
-- function arc_mapa_get_map_coords ( x, y )
-- local tw = arcade_config.tiles_width
-- local th = arcade_config.tiles_height
-- local cols = mapa_room_cols
-- local rows = mapa_room_rows
-- local rooms_per_floor = mapa_rooms_per_piso
--
-- local calc_col = math.floor(x / tw) % cols
-- local calc_row = math.floor(y / th) % rows
-- local calc_room = math.floor(y / (th * rows))*rooms_per_floor+math.floor(x / (tw * cols))
--
-- return calc_room, calc_col, calc_row
-- end

View File

@@ -16,7 +16,7 @@ function peu.init()
-- peu.x=habs[r][2]*8 -- peu.x=habs[r][2]*8
peu.x=habs[r][2] peu.x=habs[r][2]
local world_x, world_y = arc_mapa_get_coords(peu.hab,peu.x,peu.y) local world_x, world_y = coords.room_to_world(peu.hab,peu.x,peu.y)
peu.x=world_x peu.x=world_x
peu.y=world_y peu.y=world_y

View File

@@ -18,10 +18,10 @@ switches={
function switches.start(x, y) function switches.start(x, y)
if switches.cooldown>0 then return end if switches.cooldown>0 then return end
local hab, hx, hy = arc_mapa_get_map_coords(x, y) local hab, hx, hy = coords.world_to_tile(x, y)
local tile_idx = (hx+hy*mapa_room_cols)+1 local tile_idx = (hx+hy*mapa_room_cols)+1
print("Switch > "..x..", "..y.." / "..hab..", "..hx..", "..hy.." T="..tile_idx) -- mapa_set_tile_by_index(hab,tile_idx,57)
mapa_set_tile_by_index(hab,tile_idx,57) arc_set_tile(x, y, editor_to_map_tile(57))
switches.current_list=switches[hab+1][tile_idx] switches.current_list=switches[hab+1][tile_idx]
switches.current_index=2 switches.current_index=2
switches.wait=0 switches.wait=0
@@ -38,7 +38,17 @@ function switches.update()
if switches.wait>=6 then if switches.wait>=6 then
switches.wait=0 switches.wait=0
mapa_set_tile_by_index(switches.current_list[1]-1,switches.current_list[switches.current_index]+1,256) local hab= switches.current_list[1]-1
local tile_idx= switches.current_list[switches.current_index]+1
-- print(hab.." ("..tile_x..", "..tile_y..")")
-- local tile_x, tile_y = coords.room_index_to_xy ( hab, tile_idx )
-- local world_x, world_y = coords.room_to_world(hab, tile_x, tile_y)
-- arc_set_tile(world_x, world_y, 256)
arc_set_tile_by_index(hab,tile_idx,mapa_empty_tile)
-- mapa_set_tile_by_index(switches.current_list[1]-1,switches.current_list[switches.current_index]+1,256)
switches.current_index=switches.current_index+1 switches.current_index=switches.current_index+1
if switches.current_index>#switches.current_list then if switches.current_index>#switches.current_list then
switches.current_list=nil switches.current_list=nil

View File

@@ -1,22 +1,26 @@
require "map" require "map"
local arcade_config = require("arcade_config")
viewport={} viewport={}
function viewport.new() function viewport.new(_width, _height)
return { x=0, y=0, return { x=0, y=0,
width=arcade_config.resolucion.width, width=_width,
height=arcade_config.resolucion.height, height=_height,
get=viewport.get,
position=viewport.position, position=viewport.position,
print=viewport.print, print=viewport.print,
room=viewport.coord2room, room=viewport.coord2room,
roomXY= viewport.room2coord, --roomXY= viewport.room2coord,
tile= viewport.coord2tile, tile= viewport.tile,
last_tile= viewport.last_tile,
screen_coords = viewport.screen_coords, screen_coords = viewport.screen_coords,
inside = viewport.inside } inside = viewport.inside }
end end
function viewport:get()
return self.x, self.y, self.x+self.width, self.y+self.height
end
function viewport:screen_coords ( x, y ) function viewport:screen_coords ( x, y )
local scr_x = x-self.x local scr_x = x-self.x
local scr_y = y-self.y local scr_y = y-self.y
@@ -29,58 +33,69 @@ function viewport:inside( x, y, w, h )
return result return result
end end
function viewport:coord2tile () -- function viewport:coord2tile (x, y)
local tw = arcade_config.tiles_width -- local tw = arcade_config.tiles_width
local th = arcade_config.tiles_height -- local th = arcade_config.tiles_height
local cols = mapa_room_cols -- local cols = mapa_room_cols
local rows = mapa_room_rows -- local rows = mapa_room_rows
local rooms_per_floor = mapa_rooms_per_piso -- local rooms_per_floor = mapa_rooms_per_piso
-- local xx = x or self.x
-- local yy = y or self.y
--
-- local calc_col = math.floor(xx / tw) % cols
-- local calc_row = math.floor(yy / th) % rows
-- local calc_room = math.floor(yy / (th * rows))*rooms_per_floor+math.floor(xx / (tw * cols))
-- -- print("X= "..self.x.." / W="..tw.." / C= "..cols.." > "..calc_col)
-- -- print("Y= "..self.y.." / H="..th.." / R= "..rows.." > "..calc_row)
-- -- print("CR= "..calc_row.." / RF= "..rooms_per_floor.." / CC= "..calc_col)
-- -- local tile_offset_x = self.x - (calc_col * tw * cols)
-- -- local tile_offset_y = self.y - (calc_row * th * rows)
-- local tile_offset_x = xx - (calc_col * tw) - ((calc_room%rooms_per_floor)*cols*tw)
-- local tile_offset_y = yy - (calc_row * th) - ( math.floor(calc_room/10)*rows*th)
-- -- print(self.x.." C"..calc_col.." W"..tw.." R"..calc_room.." c"..cols.." > "..tile_offset_x)
-- -- room, x, y, offset x, offset y
-- return calc_room, calc_col, calc_row, tile_offset_x, tile_offset_y
-- end
local calc_col = math.floor(self.x / tw) % cols function viewport:tile ()
local calc_row = math.floor(self.y / th) % rows return coords.world_to_tile(self.x, self.y)
local calc_room = math.floor(self.y / (th * rows))*rooms_per_floor+math.floor(self.x / (tw * cols)) end
-- print("X= "..self.x.." / W="..tw.." / C= "..cols.." > "..calc_col)
-- print("Y= "..self.y.." / H="..th.." / R= "..rows.." > "..calc_row) function viewport:last_tile ()
-- print("CR= "..calc_row.." / RF= "..rooms_per_floor.." / CC= "..calc_col) return coords.world_to_tile(self.x+self.width, self.y+self.height)
-- local tile_offset_x = self.x - (calc_col * tw * cols)
-- local tile_offset_y = self.y - (calc_row * th * rows)
local tile_offset_x = self.x - (calc_col * tw) - ((calc_room%rooms_per_floor)*cols*tw)
local tile_offset_y = self.y - (calc_row * th) - ( math.floor(calc_room/10)*rows*th)
-- print(self.x.." C"..calc_col.." W"..tw.." R"..calc_room.." c"..cols.." > "..tile_offset_x)
-- room, x, y, offset x, offset y
return calc_room, calc_col, calc_row, tile_offset_x, tile_offset_y
end end
function viewport:coord2room () function viewport:coord2room ()
local tw = arcade_config.tiles_width return coords.world_to_room(self.x, self.y)
local th = arcade_config.tiles_height -- local tw = arcade_config.tiles_width
local cols = mapa_room_cols -- local th = arcade_config.tiles_height
local rows = mapa_room_rows -- local cols = mapa_room_cols
local rooms_per_floor = mapa_rooms_per_piso -- local rows = mapa_room_rows
-- local rooms_per_floor = mapa_rooms_per_piso
local calc_col = math.floor(self.x / (tw * cols)) --
local calc_row = math.floor(self.y / (th * rows)) -- local calc_col = math.floor(self.x / (tw * cols))
-- local calc_row = math.floor(self.y / (th * rows))
local calc_room = calc_row*rooms_per_floor+calc_col --
return calc_room -- local calc_room = calc_row*rooms_per_floor+calc_col
-- return calc_room
end end
function viewport:room2coord ( room ) -- function viewport:room2coord ( room )
local tw = arcade_config.tiles_width -- local tw = arcade_config.tiles_width
local th = arcade_config.tiles_height -- local th = arcade_config.tiles_height
local cols = mapa_room_cols -- local cols = mapa_room_cols
local rows = mapa_room_rows -- local rows = mapa_room_rows
local rooms_per_floor = mapa_rooms_per_piso -- local rooms_per_floor = mapa_rooms_per_piso
--
local x = (room % rooms_per_floor) * cols * tw -- local x = (room % rooms_per_floor) * cols * tw
local y = math.floor(room/rooms_per_floor) * rows * th -- local y = math.floor(room/rooms_per_floor) * rows * th
--
local room_center_x_offset = (cols * tw) >> 1 -- local room_center_x_offset = (cols * tw) >> 1
local room_center_y_offset = (rows * th) >> 1 -- local room_center_y_offset = (rows * th) >> 1
--
-- return x+room_center_x_offset, y+room_center_y_offset -- -- return x+room_center_x_offset, y+room_center_y_offset
return x, y -- return x, y
end -- end
function viewport:position(x, y) function viewport:position(x, y)
local nx = (x ~= nil) and x or self.x local nx = (x ~= nil) and x or self.x

View File

@@ -5,7 +5,7 @@ ch = arcade_config.character_height
zombie={} zombie={}
function zombie.new(_hab,_x,_y,_flip) function zombie.new(_hab,_x,_y,_flip)
local world_x, world_y = arc_mapa_get_coords(_hab,_x,_y) local world_x, world_y = coords.room_to_world(_hab,_x,_y)
return {hab=_hab, return {hab=_hab,
x=world_x, x=world_x,
y=world_y, y=world_y,