Path Finding Tiles Grid


#1

I’m making a new path finding enemy, that checks the tiles around him to see if he can walk on them. Sadly he doesn’t move or moves onto the wrong square. Can anyone tell me why my code not working?
here the main page, sry but i got a table and image so it won’t run for anyone

PImage map;
Table PathTable;
TableRow PT;
int s = 25;
ArrayList<Tiles> tiles = new ArrayList<Tiles>();
ArrayList<Enemy> enemy = new ArrayList<Enemy>();
int ty;
int j,ii;
int turn = 1;
void setup(){
  size(600,600);
  PathTable = loadTable("path.csv");
  PT = PathTable.getRow(0);
  for (int i = 0; i < 576; i++){
    ii = i;
    while (ii > 23){
      j += 1;
      ii -= 24;
    }
    ty = PT.getInt(i);
    tiles.add(new Tiles(ty,ii*s,j*s));
    j = 0;
  }
  enemy.add(new Enemy(0));
  /*PT.setInt(282,1);
  saveTable(PathTable,"path.csv");*/
  map = loadImage("m1.png");
}// setup

void draw(){
  //background(0);
  stroke(255,0,0);
  image(map,0,0);
  /*for (int i = 0; i < s; i++){
    line(i * s, 0, i * s, height);
    line(0, i * s, width, i * s);
  }*/
  for(int i = 0; i < tiles.size(); i++){
    Tiles t = tiles.get(i);
    t.show();
  }
  for (Enemy e: enemy){
    e.show();
    e.move();
  }
}

here the code for the enemy

class Enemy{
  boolean moved = false;
  boolean safe = false;
  int x, y, type;
  Enemy(int type){
    this.type = type;
    if (type == 0){
      x = 0;
      y = 3 * 25;
    }
  }
  void show(){
    fill(255,0,255);
    rect(x,y,s,s);
  }
  
  void move(){// here is the problem
    if (turn == 1 && moved == false){
      int way = 3;
      for (int i = 0; i < tiles.size(); i++){
        Tiles t = tiles.get(i);
        if (t.type == 1){
          if (way == 1 && t.y - 25 == y && t.x == x){safe = true;}
          if (way == 2 && t.y + 25 == y && t.x == x){safe = true;}
          if (way == 3 && t.y - 25 == x && t.y == y){safe = true;}
          if (way == 4 && t.y + 25 == x && t.x == y){safe = true;}
        }
      }
      if (safe == true){
        if (way == 1){
          y += 25;
          moved = true;
        }
        if (way == 2){
          y -= 25;
          moved = true;
        }
        if (way == 3){
          x += 25;
          moved = true;
        }
        if (way == 4){
          x -= 25;
          moved = true;
        }
      }
    }
  }// move
}

here the code for the tiles

class Tiles{
  int type;
  int x,y;
  Tiles(int type, int x, int y){
    this.type = type;
    this.x = x;
    this.y = y;
  }
  
  void show(){
    if (type == 1){fill(210,180,140);}
    if (type == 2){fill(0,205,0);}
    if (type == 3){fill(0,0,255);}
    rect(x,y,25,25);
    stroke(0);
    text(type,10,550);
  }
}

ok, so the enemy won’t move or moves incorrect. I made a path with titles to try it out. Can anyone tell me why he won’t move to the dirt tile in front of him. I sent it to 3 because that is the one he needs to move.


#2

The Problem probably has to do with the t.y-25==x in if (way == 3 && and so on). And according to your move function, it will only move once and then halt. You Need to reset the moved and safe variables. And i don‘t know what Type the tiles are you want it on, But it will only ever Walk of the Type is 1, and only once.