Factoritzades funcions per a conversió de coordenades.
[NEW] Tiles animats
This commit is contained in:
127
data/coords.lua
Normal file
127
data/coords.lua
Normal 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)
|
||||
Reference in New Issue
Block a user