Tilemap

Hey all!

I’m more or less a beginner. That is, Ive done some scripting, some simple programming etc before, but nothing major. Now I’ve starting cludging together a simple game, and I’ve decided to use a tilemap for my levels. I’ve long understood the basic idea of how tilemaps work, but I’ve not programmed them myself.

This is what I came up with as the very most basic prototype:

–# Main
– tileTest02

– Use this function to perform your initial setup
function setup()
displayMode(STANDARD)
parameter.watch(“1/DeltaTime”)

myTile = mesh()
myTile.texture = readImage("Cargo Bot:Crate Blue 2")

for j = 1, 100 do
    for i = 1, 100 do
        myTile:addRect(0,0,42,42)
    end
end

tileNr=1
for j = 1, 100 do
    y=j*42
    for i = 1, 100 do
        x=i*42
        myTile:setRect(tileNr, x, y, 42,42)
        tileNr = tileNr + 1
    end
end

end

– This function gets called once every frame
function draw()
– This sets a dark background color
background(40, 40, 50)

pushMatrix()
    translate((math.sin(ElapsedTime)*200)-2100,((math.cos(ElapsedTime)*200))-2100)
    myTile.draw(myTile)
popMatrix()

end

Now, is that a sensible way to go about things? (Of course, in the future I’ll have a comma separated list for which texture to use etc, probably using a texturesheet and setRectTex)

I’m wondering: Is this a sensible way to create a tilemap, do I have to hand code culling etc?

That code didn’t paste very well, sorry about that. Here it is:

function setup()
displayMode(STANDARD)
parameter.watch(“1/DeltaTime”)

myTile = mesh()
myTile.texture = readImage("Cargo Bot:Crate Blue 2")

for j = 1, 100 do
    for i = 1, 100 do
        myTile:addRect(0,0,42,42)
    end
end

tileNr=1
for j = 1, 100 do
    y=j*42
    for i = 1, 100 do
        x=i*42
        myTile:setRect(tileNr, x, y, 42,42)
        tileNr = tileNr + 1
    end
end

end

function draw()
– This sets a dark background color
background(40, 40, 50)

pushMatrix()
    translate((math.sin(ElapsedTime)*200)-2100,((math.cos(ElapsedTime)*200))-2100)
    myTile.draw(myTile)
popMatrix()

end

if you use ~~~ before your code and ~~~ after your code, it will display it correctly.
3 of them each time (not sure what they are called)

Like this?

--# Main
-- tileTest02

function setup()
    displayMode(STANDARD)
    parameter.watch("1/DeltaTime")
    myTile = mesh()
    myTile.texture = readImage("Cargo Bot:Crate Blue 2")
    
    -- set up 10000 rectangles in the mesh (100 and 100 just used for similarity to the layout stage
    for j = 1, 100 do
        for i = 1, 100 do
            myTile:addRect(0,0,42,42)
        end
    end
    
    -- set up the x/y layout of the tiles
    tileNr=1
    for j = 1, 100 do
        y=j*42
        for i = 1, 100 do
            x=i*42
            myTile:setRect(tileNr, x, y, 42,42)
            tileNr = tileNr + 1
        end
    end
end

-- This function gets called once every frame
function draw()
    background(40, 40, 50)  
    pushMatrix()
        translate((math.sin(ElapsedTime)*200)-2100,((math.cos(ElapsedTime)*200))-2100)
        myTile.draw(myTile)
    popMatrix()
end

Sweet, thx :slight_smile:

x

You could condense those for loops into one for loop. You can then remove the three global variables tileNr, x, and y.

    for j = 1, 100 do
        for i = 1, 100 do
            myTile:addRect(i*42, j*42, 42, 42)
        end
    end

That will save you about 30k iterations and even more when you make larger tilemaps.

Edit: You should take a look at my RPG utility. I’m not sure of you’re trying to make an RPG or not, but I have a good setup for creating tile maps and rendering them. You can make floor tiles, then put objects on them (such as trees and walls and houses), also add animated tiles (such as water), and then add decoration objects. Basically it is comprised of multiple tile maps stacked on top of each other.

http://twolivesleft.com/Codea/Talk/discussion/3058/

There is a lot of RPG elements coded in with the project, but just pick out the map generation stuff and you can use it for any type of game.

Hey, thanks for the feedback :slight_smile:
Yeah, my code there is very much “do this step by step and keep it straight in your head” :slight_smile: Thanks for the link. I think your RPG-Generator is a bit beyond my aspirations for the moment, but I’ll have a look at it. (Taken that I understand some of it I’ll probably learn a lot :smiley: )