How to make class of classes MAP { LAYER{ TILE(X, Y, ID) }} ?

edited March 2018 in How To...

I want to for example make map and in map is layers and every layer is any tile... How to make it ?

Tagged:

Answers

  • edited March 2018
    class Map {
      Layer[] layers;
    }
    
    class Layer {
      Tile[] tiles;
    }
    
    class Tile {
      int x, y, id;
    }
    

    Note that ( depending on your grid size and tile density or how you want to retrieve the information ) you could rid of x and y, and instead use Tile[][] tiles

    Note also: there are many many different ways to do this! Your question does not have enough detail.

  • nice, so and how to create or get x y ?

  • good ?

    ArrayList<Map> maps = new ArrayList<Map>();
    
    void setup() {
      size(400, 200);
      // # ADD MAP # \\
      maps.add( new Map());
      // # ADD LAYER # \\
      maps.get(0).layers.add( new Layer());
      // # ADD SOME TILES # \\
      maps.get(0).layers.get(0).tiles.add( new Tile(0, 50));
      maps.get(0).layers.get(0).tiles.add( new Tile(0, 150));
      maps.get(0).layers.get(0).tiles.add( new Tile(100, 0));
    }
    void draw() {
      for (int a = 0; a < maps.size(); a++) {
        for (int b = 0; b < maps.get(a).layers.size(); b++) {
          for (int c = 0; c < maps.get(a).layers.get(b).tiles.size(); c++) {
            maps.get(a).layers.get(b).tiles.get(c).draw();
          }
        }
      }
    }
    
    class Map {
      ArrayList<Layer> layers = new ArrayList<Layer>();
    }
    
    class Layer {
      ArrayList<Tile> tiles = new ArrayList<Tile>();
    }
    
    class Tile {
      int x, y;
      Tile(int x, int y) {
        this.x = x;
        this.y = y;
      }
      void draw() {
        rect(x, y, 50, 50);
      }
    }
    
  • or this ???? can i make tile as extends object ?

    ArrayList<Map> maps = new ArrayList<Map>();
    
    void setup() {
      size(400, 200);
      // # ADD MAP # \\
      maps.add( new Map());
      // # ADD LAYER # \\
      maps.get(0).layers.add( new Layer());
      // # ADD SOME TILES # \\
      maps.get(0).layers.get(0).tiles.add( new Tile(0, 50));
      maps.get(0).layers.get(0).tiles.add( new Tile(0, 150));
      maps.get(0).layers.get(0).tiles.add( new Tile(100, 0));
    }
    
    void draw() {  
      for (int i = 0; i < maps.size(); i++) {
        maps.get(i).draw();
      }
    }
    
    class Map {
      ArrayList<Layer> layers = new ArrayList<Layer>();
      void draw() {
        for (int i = 0; i < layers.size(); i++) {
          layers.get(i).draw();
        }
      }
    }
    
    class Layer {
      ArrayList<Tile> tiles = new ArrayList<Tile>();
      void draw() {
        for (int i = 0; i < tiles.size(); i++) {
          tiles.get(i).draw();
        }
      }
    }
    
    class Tile {
      int x, y;
      Tile(int x, int y) {
        this.x = x;
        this.y = y;
      }
      void draw() {
        rect(x, y, 50, 50);
      }
    }
    
  • How to adding a tile in map and layer as GameObject ?

    ArrayList<Map> maps = new ArrayList<Map>();
    ArrayList<GameObject> objects = new ArrayList <GameObject>();
    
    void setup() {
      size(400, 200);
      // # ADD MAP # \\
      maps.add( new Map());
      // # ADD LAYER # \\
      maps.get(0).layers.add( new Layer());
      // # ADD SOME TILES # \\
      maps.get(0).layers.get(0).tiles.add( new Tile(0, 50));
      maps.get(0).layers.get(0).tiles.add( new Tile(0, 150));
      maps.get(0).layers.get(0).tiles.add( new Tile(100, 0));
    }
    
    void draw() {  
      for (GameObject entity : objects) {
        entity.draw(); // ### I WANT TO DRAW ALL OF  OBJECTS, BUT NOW I HAVE ONLY TILES... BUT NOT WORKING ###
      }
      /*for (int i = 0; i < maps.size(); i++) {
        maps.get(i).draw(); ### OLD CODE ###
      }*/
    }
    
    class Map {
      ArrayList<Layer> layers = new ArrayList<Layer>();
      void draw() {
        for (int i = 0; i < layers.size(); i++) {
          layers.get(i).draw();
        }
      }
    }
    
    class Layer {
      ArrayList<Tile> tiles = new ArrayList<Tile>();
      void draw() {
        for (int i = 0; i < tiles.size(); i++) {
          tiles.get(i).draw();
        }
      }
    }
    
    class Tile extends GameObject{
      Tile(int x, int y) {
        this.x = x;
        this.y = y;
      }
      void draw() {
        rect(x, y, 50, 50);
      }
    }
    
    abstract class GameObject {
      int x, y;
      abstract void draw();
    }
    
  • Can you help me ? :/

  • edited March 2018

    I wrote a Map for you sometime ago. Didn't you like it?

    Maybe you start by explaining in a long way, what you want to achieve with layers, what the relationship is between map, layer and field, if the map is bigger than screen or not and what you have problems with exactly. What are your goals.

    How to adding a tile in map and layer as GameObject ?

    When you ask questions like this, nobody knows what you want. It's not even clear what you mean, which is part of what? Do you see that I wrote more now then you in the entire discussion (or forum)?

    As Jeremy has said:

    Your question does not have enough detail.

  • I want to add tile and draw with object array....

  • And how to get not public variable ?

    ArrayList<GameObject> objects = new ArrayList <GameObject>();
    
    void setup() {
      size(400, 200);
      for (int x = 0; x<=width; x+=20) {
        for (int y = 0; y<=height; y+=20) {
          objects.add( new Effect(x, y));
        }
      }
    }
    
    void draw() {  
      background(10);
      for (GameObject entity : objects) {
        entity.draw();
        entity.handle();
        println(entity.size); // NOT WORK...
      }
    }
    
    class Tile extends GameObject {
      Tile(int x, int y) {
        this.x = x;
        this.y = y;
      }
      void draw() {
        noStroke();
        fill(255);
        rect(x, y, 50, 50);
      }
      void handle() {
      }
    }
    
    class Effect extends GameObject {
      int size = 20;
      float c = random(255);
      Effect(int x, int y) {
        this.x = x;
        this.y = y;
      }
      void draw() {
        noStroke();
        colorMode(HSB);
        fill(c, 255, 255);
        rect(x, y, size, size);
      }
      void handle() {
        c+=1;
        c%=255;
        size+=random(-1, 2);
        x+=random(-1, 2);
        y+=random(-1, 2);
      }
    }
    
    abstract class GameObject {
      int x, y;
      abstract void draw();
      abstract void handle();
    }
    
  • There are many many different ways to do this! Your question does not have enough detail.

    Different data structures and algorithms make sense for tiling:

    • a checkboard in which there are only two
    • or a satellite map in which each one is unique
    • or an rpg game map in which there are many types, but they may be reused in different ways
      • with rotation, and/or with customized tiles
      • and/or with layered effects, etc. etc. etc.

    "How do I do it?"

    DO WHAT??

    Your question does not have enough detail.

  • edited March 2018

    the size error above occurred because in objects there are tiles and effects mixed but only effects have size, tiles haven't (or whatever reason)

    corrected version:

    ArrayList<GameObject> objects = new ArrayList <GameObject>();
    
    void setup() {
      size(400, 200);
      for (int x = 0; x<=width; x+=20) {
        for (int y = 0; y<=height; y+=20) {
          objects.add( new Effect(x, y));
        }
      }
    }
    
    void draw() {  
      background(10);
      for (GameObject entity : objects) {
        entity.draw();
        entity.handle();
        println(entity.size); // NOT WORK...
      }
    }
    
    // ====================================================
    
    class Tile extends GameObject {
      Tile(int x, int y) {
        this.x = x;
        this.y = y;
      }
      void draw() {
        noStroke();
        fill(255);
        rect(x, y, 50, 50);
      }
      void handle() {
      }
    }
    // ====================================================
    
    class Effect extends GameObject {
    
      float c = random(255);
      Effect(int x, int y) {
        this.x = x;
        this.y = y;
      }
      void draw() {
        noStroke();
        colorMode(HSB);
        fill(c, 255, 255);
        rect(x, y, size, size);
      }
      void handle() {
        c+=1;
        c%=255;
        size+=random(-1, 2);
        x+=random(-1, 2);
        y+=random(-1, 2);
      }
    }
    // ====================================================
    
    abstract class GameObject {
    
      int x, y;
      int size = 20;
    
      abstract void draw();
      abstract void handle();
    }
    
Sign In or Register to comment.