My problem: i have an tiled map and a "independent" char (i want to try to do a kind of another layer) that i want to put over the map. However i don't understand how to update the char directly in the map and not quite in the window area. I mean what's the code way of merge my "tileHero" over the "tile" one? There is a way or should i rearrange code in order to change the Hero position all times map is scrolled? Thx in advance for help/sugestions.
Full package in attachment
Code: Select all
-- Roguelike Engine Test v0.1
-- Roadmap
-- 0.1 Split tile image file loading + Mouse Scroll Map
-- 0.2 Add Hero + Key Movement
-- 0.3 Parse unique tile image file + text array for data tiles
-- 0.4 Map loading file
-- 0.5 Map algorithm
function love.load()
setupMap()
setupHero()
initWindow()
end
function initWindow ()
window_size_x = tile_w * map_display_w + ( 2 * tile_w )
window_size_y = tile_h * map_display_h + ( 2 * tile_h )
windowfeatures = love.graphics.setMode( window_size_x, window_size_y, no, no, no )
love.graphics.setCaption( "Roguelike Engine Test v0.1" )
love.mouse.setPosition( window_size_x/2, window_size_x/2 )
love.graphics.setFont(12)
end
function setupMap()
tile = {}
for i=0,3 do -- Grass = 0, Tree = 1, Lava = 2, Water = 3
tile[i] = love.graphics.newImage( "tile"..i..".png" )
end
map_w = 20 -- Map Size X
map_h = 20 -- Map Size Y
map_x = 0 -- Reset X Map Position
map_y = 0 -- Reset Y Map Position
map_offset_x = 0 -- Window/Camera Offset X
map_offset_y = 0 -- Window/Camera Offset Y
map_display_w = 9 -- Window/Camera Size X
map_display_h = 9 -- Window/Camera Size Y
tile_w = 32 -- Tile Size X
tile_h = 32 -- Tile Size Y
map={
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 1, 0, 0, 2, 2, 2, 0, 3, 0, 3, 0, 1, 1, 1, 0, 0, 0, 0, 0},
{ 0, 1, 0, 0, 2, 0, 2, 0, 3, 0, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0},
{ 0, 1, 1, 0, 2, 2, 2, 0, 0, 3, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0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
hero_x = 5
hero_y = 5
end
function draw_map()
for y=1, map_display_h do
for x=1, map_display_w do
love.graphics.draw(
tile[map[y+map_y][x+map_x]],
(x*tile_w)+map_offset_x,
(y*tile_h)+map_offset_y )
end
end
end
function draw_hero()
love.graphics.draw( tileHero, hero_x * tile_w + map_offset_x , hero_y * tile_h + map_offset_y)
end
function love.update()
mx = love.mouse.getX( )
my = love.mouse.getY( )
if my < tile_h then
map_y = map_y-1
if map_y < 0 then map_y = 0; end
end
bound_y = tile_h + map_display_h * tile_h
if my > bound_y then
map_y = map_y+1
if map_y > map_h-map_display_h then map_y = map_h-map_display_h; end
end
if mx < tile_w then
map_x = math.max(map_x-1, 0)
end
bound_x = tile_w + map_display_w * tile_w
if mx > bound_x then
map_x = math.min(map_x+1, map_w-map_display_w)
end
function love.keypressed(key, unicode)
if key == 'up' then
hero_y = hero_y-1
if map_y < 0 then map_y = 0; end
end
if key == 'down' then
hero_y = hero_y+1
if map_y > map_h-map_display_h then map_y = map_h-map_display_h; end
end
if key == 'left' then
hero_x = math.max(hero_x-1, 0)
end
if key == 'right' then
hero_x = math.min(hero_x+1, map_w-map_display_w)
end
end
end
function love.draw()
draw_map()
draw_hero()
love.graphics.print("FPS: "..love.timer.getFPS(), 10, 20)
end