I’m currently working on a Sokoban game. My game currently has a menu with buttons for each level. when i click the button it loads a proxy collection to load my “Level” gameobject , and when it returns “proxy_loaded” i send back a message what level to initialize.
my initialize function runs through the tilemap and creates the player and boxes gameobjects in tables and stores the walls and targets in tables according to the tiles.
this is the setup for the menu gui script:
local levels = { "Level01", "Level02"} -- id of level button
local levelSelected = nil
function on_message(self, message_id, message, sender)
if message_id == hash("proxy_loaded") then
-- New world is loaded. Init and enable it.
msg.post(sender, "init")
msg.post(sender, "enable")
msg.post("main:/Level", "initialize", {level = levelSelected})
end
end
function on_input(self, action_id, action)
if action_id == hash("touch") and action.pressed then
local button = nil
for i, level in ipairs(levels) do
button = gui.get_node(level)
if gui.pick_node(button, action.x, action.y) then
levelSelected = level
msg.post("#menu", "disable")
msg.post("proxy#gameproxy", "load")
return
end
end
end
end
heres the Level script:
function on_message(self, message_id, message, sender)
--load the level passed by proxy loaded message
if message_id == hash("initialize") then
initLevel(message.level)
end
end
function initLevel(level)
local tileMap = "Level#" .. level
for i = 1, 10 do
tiles[i] = {}
for j = 1, 10 do
-- create box objects
if tilemap.get_tile(tileMap,"boxes",i,j) == ind_box_place then
tilemap.set_tile(tileMap,"boxes",i,j,0) -- clear tile
tilemap.set_tile(tileMap,"floor",i,j,ind_floor_spr) -- set tile to floor
local p = vmath.vector3( (i - 1) * TILE_SIZE, (j - 1) * TILE_SIZE, 1)
local component = "factory#boxfactory"
local box = factory.create(component, p)
table.insert(boxes, box)
end
-- create player object
if tilemap.get_tile(tileMap,"player",i,j) == ind_player_place then
tilemap.set_tile(tileMap,"player",i,j,0) -- clear tile
tilemap.set_tile(tileMap,"floor",i,j,ind_floor_spr) -- set tile to floot
local p = vmath.vector3( (i - 1) * TILE_SIZE, (j - 1) * TILE_SIZE, 1)
local component = "factory#playerfactory"
local player = factory.create(component, p)
-- table.insert(players, player)
players = player
end
-- get walls and targets of game
if tilemap.get_tile(tileMap,"walls",i,j) == ind_wall then
tiles[i][j] = "wall"
elseif tilemap.get_tile(tileMap,"floor",i,j) == ind_target then
tiles[i][j] = "target"
else
tiles[i][j] = "nope"
end
end
end
boxesLeft = table.getn(boxes)
label.set_text("Level#BoxesLeft", "Boxes Left: " .. boxesLeft)
end
the problem i am having is how how to add the tilmaps dynamicly?
i built everything this way so that i dont have to create a proxy for each level and separate game object and will only have to choose which tilemap to init the level? when i add another tilemap to the gameobject it just covers the first tilemap?
thanks!