[WIP] Ja se pot scrollar per tot el mapa sense problemes. Ara falta que els tiles de les vores carreguen des del primer pixel i no només quan hi ha espai suficient

This commit is contained in:
2026-03-19 00:45:54 +01:00
parent d35ad7d5b5
commit 0d239e7ab0
4 changed files with 112 additions and 39 deletions

View File

@@ -16,38 +16,87 @@ function game_init(menu)
game_update=update_game game_update=update_game
end end
function render_view ( hab, x ,y ) -- function mapa_draw(hab)
-- for ty=0,5 do
-- for tx=0,11 do
-- local tile=mapa[1+hab][1+tx+ty*12]
-- if tile~=256 and (tile<126 or mapa.step>4) then
-- -- draw.surf((tile&15)*8,64+(tile>>4)*8,8,8,tx*8,ty*8)
-- local txr = arcade_config.tiles_per_row-1
-- local txr2 = arcade_config.tiles_per_row_base2
-- local toff= arcade_config.tiles_offset
-- local tw = arcade_config.tiles_width
-- local th = arcade_config.tiles_height
-- draw.surf((tile&txr)*tw,toff+(tile>>txr2)*th,tw,th,tx*tw,ty*th)
-- end
-- end
-- end
-- end
function render_view ( x, y )
local curr_ssurf = surf.source() local curr_ssurf = surf.source()
local curr_tsurf = surf.target() local curr_tsurf = surf.target()
local room_x = hab % mapa_rooms_per_piso local nroom, room_x, room_y, tile_offset_x, tile_offset_y = viewp:tile()
local room_y = math.floor(hab / mapa_rooms_per_piso) print("COORDS= "..x..", "..y)
local first_x = -1 print("ROOM= "..nroom)
local first_y = -1 print("RCOORDS= "..room_x..", "..room_y)
local first_x = 0 print("TILE_OFF= "..tile_offset_x..", "..tile_offset_y)
local first_y = 0
if room_x == 0 then first_x = 0 end max_tiles_col = (arcade_config.resolucion.width/arcade_config.tiles_width)+1; -- Los que caben en una pantalla y 1 parcial
if room_y == 0 then first_y = 0 end max_tiles_row = (arcade_config.resolucion.height/arcade_config.tiles_height)+1; -- Los que caben en una pantalla y 1 parcial
curr_room = nroom
curr_room_x = room_x
curr_room_y = room_y
surf.target(map_buffer) surf.target(map_buffer)
surf.cls(16) surf.cls(16)
surf.source(tiles) surf.source(tiles)
for ry=0,2 do tile_y = room_y
for rx=0,2 do for ty=0,max_tiles_row-1 do
local buffer_x = rx*mapa_room_cols*tw tile_x = room_x
local buffer_y = ry*mapa_room_rows*th
local room = (room_y+ry+first_y)*mapa_rooms_per_piso+(room_x+rx+first_x) if tile_y>mapa_room_rows-1 then
draw_hab(room, buffer_x, buffer_y) nroom = nroom + 10
tile_y = 0
end end
curr_room = nroom
for tx=0,max_tiles_col-1 do
if tile_x>mapa_room_cols-1 then
curr_room = curr_room+1
tile_x = 0
end
if curr_room >= 80 then
break
end
local tile=mapa[1+curr_room][1+tile_x+tile_y*mapa_room_cols]
-- print("R"..curr_room.." ("..tile_x..", "..tile_y..") > "..tile)
-- print(" TILE= "..tile)
if tile~=256 and (tile<126 or mapa.step>4) then
-- draw.surf((tile&15)*8,64+(tile>>4)*8,8,8,tx*8,ty*8)
local txr = arcade_config.tiles_per_row-1
local txr2 = arcade_config.tiles_per_row_base2
local toff= arcade_config.tiles_offset
local tw = arcade_config.tiles_width
local th = arcade_config.tiles_height
draw.surf((tile&txr)*tw,toff+(tile>>txr2)*th,tw,th,tx*tw,ty*th)
end
tile_x = tile_x+1
end
tile_y = tile_y+1
end end
surf.source(map_buffer) surf.source(map_buffer)
surf.target(0) surf.target(0)
local vrx, vry = viewp:roomXY(hab) draw.surf(tile_offset_x,tile_offset_y,viewp.width,viewp.height,0,0,viewp.width,viewp.height)
print(hab.." "..vrx.." "..vry)
draw.surf(viewp.x-vrx,viewp.y-vry,viewp.width,viewp.height,0,0,viewp.width,viewp.height)
surf.source(curr_ssurf) surf.source(curr_ssurf)
surf.source(curr_tsurf) surf.source(curr_tsurf)
@@ -91,17 +140,16 @@ function update_game()
--- mapa_update(abad.hab,cameras[current_camera].hab) --- mapa_update(abad.hab,cameras[current_camera].hab)
local vp_x, vp_y = viewp:position() local vp_x, vp_y = viewp:position()
if key.down(keyRight) then if key.down(keyRight) then
vp_x = vp_x+1 if vp_x+1<=(arcade_config.tiles_width*mapa_room_cols*(mapa_rooms_per_piso))-(arcade_config.resolucion.width) then vp_x = vp_x+1 end
elseif key.down(keyLeft) then elseif key.down(keyLeft) then
if vp_x>0 then vp_x = vp_x-1 end if vp_x>0 then vp_x = vp_x-1 end
elseif key.down(keyUp) then elseif key.down(keyUp) then
if vp_y>0 then vp_y = vp_y -1 end if vp_y>0 then vp_y = vp_y -1 end
elseif key.down(keyDown) then elseif key.down(keyDown) then
vp_y = vp_y+1 if vp_y+1<=(arcade_config.tiles_height*mapa_room_rows*mapa_pisos)-(arcade_config.resolucion.height) then vp_y = vp_y+1 end
end end
viewp:position(vp_x, vp_y) viewp:position(vp_x, vp_y)
local room = viewp:room() render_view(vp_x, vp_y)
render_view(room)
fps_print() fps_print()
viewp:print() viewp:print()
end end

View File

@@ -14,8 +14,10 @@ function mini.init()
logo=surf.new(arcade_config.logo_sf.width,arcade_config.logo_sf.height) logo=surf.new(arcade_config.logo_sf.width,arcade_config.logo_sf.height)
back=surf.new(arcade_config.surface.width,arcade_config.surface.height) back=surf.new(arcade_config.surface.width,arcade_config.surface.height)
-- buffer per a 3x3 habitacions -- buffer per a 3x3 habitacions
local map_buffer_width = 3*mapa_room_cols*arcade_config.tiles_width -- local map_buffer_width = 3*mapa_room_cols*arcade_config.tiles_width
local map_buffer_height = 3*mapa_room_rows*arcade_config.tiles_height -- local map_buffer_height = 3*mapa_room_rows*arcade_config.tiles_height
local map_buffer_width = arcade_config.surface.width+2*arcade_config.tiles_width
local map_buffer_height = arcade_config.surface.height+2*arcade_config.tiles_height
map_buffer=surf.new(map_buffer_width,map_buffer_height) map_buffer=surf.new(map_buffer_width,map_buffer_height)
fade.init() fade.init()

View File

@@ -1,7 +1,7 @@
mapa_room_cols = 12; -- en quantitat de tiles 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 = 7 mapa_pisos = 8
-- 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

@@ -2,7 +2,7 @@ require "map"
local arcade_config = require("arcade_config") local arcade_config = require("arcade_config")
local viewport={} viewport={}
function viewport.new() function viewport.new()
return { x=0, y=0, return { x=0, y=0,
@@ -11,10 +11,33 @@ function viewport.new()
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 }
end 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
end
function viewport:coord2room () function viewport:coord2room ()
local tw = arcade_config.tiles_width local tw = arcade_config.tiles_width
local th = arcade_config.tiles_height local th = arcade_config.tiles_height
@@ -29,15 +52,15 @@ function viewport:coord2room ()
return calc_room 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