Search code examples
javaattributestilegame-development

Tiles with Special attributes


So I am creating a tile based game, if the character the main player control steps into certain areas on the map he/she gets a energy boost or poisoned depending on which tile the players character step/lands in. I have already created a two dimensional array with player objects already. So I wanted to ask how should I implement boosting or poisoning characters who steps on certain positions on the map. So I am wondering do I have to create an extra dimension to add these special "Zones" within my map, or so I create an additional Linked lists to store the special "zones" and implement them ?

 public final class Engine
{
    private int maximumAttackerWarrior;
    private int maximumDefenderWarrior;
    private int attackerWarrior;
    private int defenderWarrior;
    private final Random rand;
    private ArrayList<Integer> defenderA;
    private ArrayList<Integer> attackerA;

    /**
     * Constructor of class Engine
     */
    public Engine()    {
        // initialise instance variables
        rand = new Random();
        maximumAttackerWarrior = 0;
        maximumDefenderarrior = 0;
        attackerWarrior = 0;
        defenderWarrior = 0;
        defenderDices = new ArrayList<Integer>();
        attackerDices = new ArrayList<Integer>();
    }
public map{
// purely for visualization purposes 
Zones [][] map =
{
    {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
 };}}

Solution

  • If the only tile types you will use are boost/poison, then I would use the map array to store the boost/poison values of each position (positive values meaning boost and negative meaning poison, 0 is regular tile). And store the player x and y positions separately.

    On the other hand, if you think you might have even more fancy tiles later, I would go with an object oriented approach where the map contains tile objects (you can use GoF Flyweight pattern to eliminate repeated object creation, if your map is very large).

    Hope this helps