I want to generate a lot of Factories and randomly spread them to my Tilemap.
So, I’m getting the tilemap size with:
local x, y, w, h = tilemap.get_bounds("/levelgo#tilemap")
size_x = (w - 2) * 32 -- 32 is the tile size
size_y = (h - 2) * 32 -- 2 it's a safe margin
bottom = 4 -- the bottom of screen
After that, the function spawn_itens will show my Factories:
function spawn_items()
local pos_x = math.random(bottom, size_x) -- The X random position for the Factory from 4 to Tilemap width
local pos_y = math.random(bottom, size_y) -- The Y random position for the Factory from 4 to Tilemap height
end
Inside this function, later, I will do:
f = "#items_factory"
local p = factory.create(f, vmath.vector3(pos_x, pos_y, 0), nil, {}, 0.6)
And also include a counter to limit the number of creating Factories.
I think with that code the random Factories will spawn only inside the map…
Now, how do I know if there is something in the place where the Factory will spawn? Like the player, static objects or a factory?
My entire logic it’s wrong? What’s the best way to do this?
If the entity responsible for spawning things need to know a lot about the level, you should store that data in that entity or somewhere easily accessible (like a shared Lua module with state).
If your game state can be expressed in a simple grid, you can store everything in a 2D array (with a Lua table).
The example “Ocean Commotion” on our examples github repo contains code for managing the game state in a table. It’s a Candy Crush clone but will hopefully get you some ideas.
Hello Sicher!
Thanks for your help! I’ve downloaded the example and I must confess that I spent a lot of time playing!
These ‘Candy Crush’ type games are addictive! haha
Looking into the code some things become more clear to me, but in the other hand I saw that I’m far away from being a game programer… But there is no end without a beginning, and I’m learning many things with the help of defold community, thank you!
I don’t know if my game can be expressed in a simple grid but I think so…
At this point the random factories are created inside the tilemap, but I think that’s the wrong way, because I’ll need much more than 128 items and I’m getting this message:
Yes, it will consume a bit more memory, but you have good control over memory usage. Set it to a number that works. If you later on needs to optimize for memory you can return to this. Generally, graphics will take much more space than the memory for sprite components etc.