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

@@ -1,22 +1,26 @@
require "map"
local arcade_config = require("arcade_config")
viewport={}
function viewport.new()
function viewport.new(_width, _height)
return { x=0, y=0,
width=arcade_config.resolucion.width,
height=arcade_config.resolucion.height,
width=_width,
height=_height,
get=viewport.get,
position=viewport.position,
print=viewport.print,
room=viewport.coord2room,
roomXY= viewport.room2coord,
tile= viewport.coord2tile,
--roomXY= viewport.room2coord,
tile= viewport.tile,
last_tile= viewport.last_tile,
screen_coords = viewport.screen_coords,
inside = viewport.inside }
end
function viewport:get()
return self.x, self.y, self.x+self.width, self.y+self.height
end
function viewport:screen_coords ( x, y )
local scr_x = x-self.x
local scr_y = y-self.y
@@ -29,58 +33,69 @@ function viewport:inside( x, y, w, h )
return result
end
function viewport:coord2tile ()
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(self.x / tw) % cols
local calc_row = math.floor(self.y / th) % rows
local calc_room = math.floor(self.y / (th * rows))*rooms_per_floor+math.floor(self.x / (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 = 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
-- function viewport:coord2tile (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 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
function viewport:tile ()
return coords.world_to_tile(self.x, self.y)
end
function viewport:last_tile ()
return coords.world_to_tile(self.x+self.width, self.y+self.height)
end
function viewport:coord2room ()
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(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
return coords.world_to_room(self.x, self.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(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
end
function viewport:room2coord ( room )
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 x = (room % rooms_per_floor) * cols * tw
local y = math.floor(room/rooms_per_floor) * rows * th
local room_center_x_offset = (cols * tw) >> 1
local room_center_y_offset = (rows * th) >> 1
-- return x+room_center_x_offset, y+room_center_y_offset
return x, y
end
-- function viewport:room2coord ( room )
-- 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 x = (room % rooms_per_floor) * cols * tw
-- local y = math.floor(room/rooms_per_floor) * rows * th
--
-- local room_center_x_offset = (cols * tw) >> 1
-- local room_center_y_offset = (rows * th) >> 1
--
-- -- return x+room_center_x_offset, y+room_center_y_offset
-- return x, y
-- end
function viewport:position(x, y)
local nx = (x ~= nil) and x or self.x