Example of 3D tile based engine (to use for zelda like games)

Thanks Xavier for the info, I’ll start digging through it tomorrow. Looking forward to your updates.

Okay Xavier

@lruizlopez137 Please look at the date of when things were posted, and stop bumping old discussions.

If ya have nothing to add to the discussion please do not bump it!

My daughter and I made a maze out of this:

https://gist.github.com/GlueBalloon/2f75849193aeedf0b508

It’s not easy! See if you can do it without peeking at the code.

Thanks Xavier for leaving this awesome thing here for all of us.

How do you make enemies spawn on a certain place when the world is build up like this? Like if you want a enemy to spawn on the zero in the middle?

self.data =
    {
        {1, 1, 1, 1, 1, 1, 1, 1},
        {1, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 0, 0, 0, 0, 0, 1},
        {1, 1, 1, 1, 1, 1, 1, 1}
    }
end

Divide the world up into “tiles”, eg 10 pixels by 10 pixels.

Then if you want an enemy to spawn in the middle of tile [5][3], you do it at the screen position (5.5, 3.5) x 10 = (55, 35)

You don’t have to use just 1 and 0. You can use any character you like, eg “c” means a chest, “g” means gold, “e” means enemy spawn point, etc, to tell your program what to put in each tile on the map. I wrote a bunch of posts on how to make a side scrolling game, using a tile map, here.

@Ignatz Is it possible to make one more of those an the other one draw on top of the other one? So you just need to put an E in the middle of the other one, do you understand what I am asking for?

@Ignatz if we use the code abow as an exemple how would you do to make it work with it? :slight_smile:

@llEmill, you really need Xavier for the best answer, but I’ve been messing with this code very recently so perhaps I can help.

The code works by detecting the tile you are on, and then looking at the map to find out what all the tiles are around you. This order is important. First, where are you. Second, what’s around you. This is how it manages to only draw the tiles that are onscreen, and not draw any of the tiles that are offscreen.

The drawing code is in the World class, and if you look at it you can see the section where it decides what tile to draw based on whether it has encountered a 0, 1, or 2. This is purely about what gets drawn–there is no game logic here to start with, but you can add some if you want.

The game logic is in the draw method of the Main tab, and as originally coded there are really only two logistics that get evaluated:

  • are you moving? If so, tell the World class to redraw the world based on your movement.
  • are you moving into a wall? If so, don’t allow the player to go any further.

There’s a lot more that can be done, and that’s why Xavier left it for us.

So to do what you want:

  • in setup, define a new instance of Floor and name it spawner–because it should look just like the normal floor.
  • in World, pick a number to represent a spawning tile that’s not an already-defined tile type–let’s say 8. On the map, in the place where you want the monster to spawn, replace the 0 with an 8.
  • in World’s draw method, find the place where it goes through the numbers to decide what to draw, and at the end of that list add code that draws a spawner whenever it detects an 8.

…so, that’s the basic mechanics of adding a spawner tile. To actually make it spawn something, that’s a different story, and I’m not as qualified to help there. I would suppose you would have to have World detect whenever it’s drawing a spawner tile, and call some method that places a monster there. Then the monster would have to have its own logic for moving, etc, which would be in another class–or in the draw method, if you prefer.

It may help you to look through the mod I made myself, to see how I did tile replacement. I ended up defining a couple new tiles myself.

Thank you very much @UberGoober it helped a lot!