Help with battle system

I need to find out where to start for a battle system for my turn based jrpg, where the player and an enemy are in a 3 by 3 grid. In order for the enemy to attack the player, the enemy must be adjacent to the player.

My question is: how would I go about creating the grid and having my code know wether or not the player and enemy are adjacent.

Thanks for any help!

Hi @Beanmeister12 and welcome!

You should start by defining an enum with the types of cell states, like

public enum CellType
{
    EMPTY,
    PLAYER,
    ENEMY
}

After that, create an 2D Array of that type.

// GRID_COL_COUNT = 3
// GRID_ROW_COUNT = 3
CellType[,] grid = new CellType[GRID_COL_COUNT, GRID_ROW_COUNT]; // 3x3

Finally, initialize your grid with CellType.EMPTY and put your player and enemy on the grid.
Remember to store the coordinates of the player and the enemy to not iterate the grid looking for them.

grid[1,1] = CellType.PLAYER;
grid[0,1] = CellType.ENEMY;

If the player or the enemy is on the grid[x,y], you can do a generic search with a method like this:

public bool CanAttack(int x, int y)
{
    if( x < 0 || x > GRID_COL_COUNT || y < 0 || y > GRID_ROW_COUNT || grid[x,y]==CellType.EMPTY)
    {
        return false;
    }
    for(int xIndex = 0; xIndex < GRID_COL_COUNT; xIndex++)
    {
        for(int yIndex = 0; yIndex < GRID_ROW_COUNT; yIndex++)
       {
           if( xIndex != yIndex)
           {
               if( grid[xIndex, yIndex] != CellType.EMPTY && grid[xIndex,yIndex] != grid[x,y])
               {
                   return true;
               }
           }
       }
    }
    return false;
}

I’ll let you write the code for the attack method because you know better than me what you want to achieve.

Also, don’t copy/paste the code without reading and understanding it first, I didn’t tried it out in Unity :wink:

Good luck!

hello, I would like to create an undertale battle system can someone please tell me how