Grid occupied tiles?

BlitzMax Forums/BlitzMax Beginners Area/Grid occupied tiles?

Bee(Posted 2012) [#1]
I've flipped through a lot of these forums and Googled up plenty on tile mapping. I just don't see any clear information on how to be able to move things within a small grid, without putting them on top of existing objects or certain tiles.

If I move a playing piece to another and there is a piece there, I want to say that piece can't move to this square. If I spawn a monster and there is a monster on the square, I want it to find another find another square within CERTAIN limits of the entire grid. The grid is 4x4, but I only want monsters to spawn in the top 4x4.

I'm trying to sort out lists and arrays, but they don't seem to be helping me functionally because while I can add things to them, I don't seem to be able to retrieve anything from them usefully other than creating new types from the list or Foreach, then do this type stuff. Am I thinking wrong in that I could put each piece's root cords in a list or array and be able to have pieces scan this list or array?


Jesse(Posted 2012) [#2]
can you post a minimalist example of what you are trying to do. I am having a bit of a problem trying to understand how you are setting your grid system. In theory what you are trying to do seems very straight forward so I don't see the problem. Post it in code or in pseudocode form whichever is best for you as long as it's easy to understand.


Bee(Posted 2012) [#3]
Psuedocode:

My tilemap creates a plain 4x10 Grid with 64x64 tiles.

Self explanatory I would think.

It then places my Players on the bottom 4x4 of this grid based on 64x64. that move similar to chess pieces.

Type Player1 = New Player

Method Draw()
positionx = 64
positiony = 448
EndMethod


Type Player 2 = New Player

Method Draw()
positionx = 128
positiony = 448
EndMethod

Type Player 3 = New Player

Method Draw()
positionx = 64
positiony = 512
EndMethod

Type Player 4 = New Player

Method Draw()
positionx = 128
positiony = 512
EndMethod

During the Player Turn phase you have the ability to move your Player pieces around their bottom 4x4 of the 4x10 grid. This was easy to limit with Mouse/64*64 tricks.

What I can't figure out is how to let Player 1 know that Player 2 is already at 128,448 so it cannot move there.

Now I understand a procedure I've seen people use where it flags a tile "occupied" with a simple 1 or 0 check. This is going to be problematic for my system for a couple reasons. Mainly because my Players have attacks based on columns and rows within the 4x10 grid. Would look something like:


Function PlayerAttack()

GetEnemyPosition (lets say this finds Enemy at 64,64)
EnemyHealth - 100
If EnemyExistsPositionX = 128, 192,256
EnemyHealth - 100
EndIf
EndFunction

This horribly demonstrated attack would attack an entire Row of Enemies on the grid.

I can't figure how to find an enemy exists at 64,64, much less tell it to also attack 64,128 + 64,192+64,256 if an enemy exists there as well.

Essentially: I need a way to find out the X,Y of a player OR enemy before actions can take place between my players or enemies.

Right now the only way I've gotten around this is hard coding some weird and incredibly long x,y checks on multiples of 64. I know that is the incredibly long and broken way to do this.

Last edited 2012


Jesse(Posted 2012) [#4]
just create your grid of type "Tentity":

grid = new tentity[4,10]



this will create a grid of 4x10 of null Tentity objects.

when you move an entity into the tile position, just assign it like this:

         grid[x,y] = enemy1


to check if the tile is occupied you just check it like this
        if grid[x,y+1] = null
              grid[x,y+1] = grid[x,y]
              grid[x,y] = null
        endif


calculate the pixel precision for the entity only for display purpose.

if you are using the mouse to chose a tile in the maze.
you need to figure out in what tile the mouse is on by doing something like this.

first subtract the position of the maze from the mouse position :

px = MouseX() - gridPositionx
py = mouseY() - gridPositiony

then divide it by the tile size:

x = int (px / tileWidth)
y = int (py / tileHeight)

x and y now contain the tile at which the mouse is located.

if the maze is shifting positions of the screen just add the number of tiles shifted across and down to the x and y respectively.

I hope this helps.

Last edited 2012


Bee(Posted 2012) [#5]
Thank you yet again, Jesse. I understand this concept and will try to get it into my code.

I was looking through all the tutorial for Arrays and Lists, but they only explain how to use "Bob", "Tom", etc as strings and its a bit deceptive for someone like that's trying to use it for a greater good. This helps me with that as well.