Waypoint help
in
Programming Questions
•
3 years ago
Hello all,
I have been messing around with the idea of way-points to be implemented into a game at a later date. I have almost all of the code working but am running to some crashes that I am not sure how to fix.The board is held in a 2D array of tiles, each of which can be clicked on. Clicking on one will save a waypoint in a linkedlist. The seeker (little ball) will move from it's current position to the next waypoint, then keep finding waypoints until it is at the end of the waypoint list.
My problem is that after the first cycle of waypoints, the program crashes with an index out of bounds error. It works for any number of waypoints, but once it reaches the final one, I get an error. I am not sure how to go about fixing this. If anyone could help me find it or suggest a better way to go about this (or both) I'd be very thankful.
I have been messing around with the idea of way-points to be implemented into a game at a later date. I have almost all of the code working but am running to some crashes that I am not sure how to fix.The board is held in a 2D array of tiles, each of which can be clicked on. Clicking on one will save a waypoint in a linkedlist. The seeker (little ball) will move from it's current position to the next waypoint, then keep finding waypoints until it is at the end of the waypoint list.
My problem is that after the first cycle of waypoints, the program crashes with an index out of bounds error. It works for any number of waypoints, but once it reaches the final one, I get an error. I am not sure how to go about fixing this. If anyone could help me find it or suggest a better way to go about this (or both) I'd be very thankful.
- import java.util.*;
- TileMap tilemap;
- Seeker seeker;
- void setup() {
- size(400,400);
- tilemap = new TileMap();
- seeker = new Seeker(0,0,20);
- }
- void draw() {
- ellipseMode(CORNER);
- tilemap.display();
- tilemap.mouseOver();
- tilemap.findNextWaypoint(seeker);
- tilemap.clearWaypointList(seeker);
- seeker.display();
- seeker.moveToWaypoint(tilemap.nextWaypoint);
- }
- class Seeker {
- int x;
- int y;
- int r;
- boolean reachedWaypoint;
- Seeker(int x_, int y_, int r_) {
- x = x_;
- y = y_;
- r = r_;
- reachedWaypoint = false;
- }
- void display() {
- fill(0,255,0);
- ellipse(x,y,r,r);
- }
- void moveToWaypoint(Waypoint waypoint) {
- if(waypoint != null) {
- if(dist(x,y,waypoint.getX(), waypoint.getY()) < 1) {
- reachedWaypoint = true;
- }
- if(x < waypoint.getX()) {
- x = x + 1;
- }
- if(x > waypoint.getX()) {
- x = x - 1;
- }
- if(y > waypoint.getY()) {
- y = y - 1;
- }
- if(y < waypoint.getY()) {
- y = y + 1;
- }
- }
- }
- void setReachedWaypoint() {
- reachedWaypoint = false;
- }
- boolean reachedWaypoint() {
- if(reachedWaypoint) {
- return true;
- }
- else {
- return false;
- }
- }
- int getX() {
- return x;
- }
- int getY() {
- return y;
- }
- int getR() {
- return r;
- }
- }
- class Tile {
- int x;
- int y;
- int w;
- int h;
- int type;
- boolean clicked;
- Tile(int x_, int y_,int w_, int h_) {
- x = x_;
- y = y_;
- w = w_;
- h = h_;
- type = 0;
- clicked = false;
- }
- void display() {
- fill(0,0,255);
- rect(x,y,w,h);
- }
- int getX() {
- return x;
- }
- int getY() {
- return y;
- }
- int getW(){
- return w;
- }
- int getH(){
- return h;
- }
- }
- class TileMap {
- public Waypoint currentWaypoint = null;
- public Waypoint nextWaypoint = null;
- public LinkedList waypointList = new LinkedList();
- public final int WIDTH = 20;
- public final int HEIGHT = 20;
- boolean clicked;
- int waypointState;
- public Tile[][] tiles;
- public int[][] terrain = new int[WIDTH][HEIGHT];
- public boolean[][] visited = new boolean[WIDTH][HEIGHT];
- TileMap() {
- clicked = false;
- waypointState = 0;
- tiles = new Tile[WIDTH][HEIGHT];
- for(int i = 0; i < WIDTH; i++) {
- for(int k = 0; k < HEIGHT; k++) {
- tiles[i][k] = new Tile(i*20, k*20, 20, 20);
- }
- }
- currentWaypoint = new Waypoint(0,0,tiles[0][0]);
- }
- void display() {
- for(int i = 0; i < WIDTH; i++) {
- for(int k = 0; k < HEIGHT; k++) {
- tiles[i][k].display();
- }
- }
- }
- void mouseOver() {
- if(!mousePressed) {
- clicked = false;
- }
- for(int i = 0; i < WIDTH; i++) {
- for(int k = 0; k < HEIGHT; k++) {
- if(mousePressed && clicked == false) {
- if(mouseX > tiles[i][k].getX() && mouseX < tiles[i][k].getX() + tiles[i][k].getW() &&
- mouseY > tiles[i][k].getY() && mouseY < tiles[i][k].getY() + tiles[i][k].getH()) {
- clicked = true;
- addWaypoint(tiles[i][k]);
- }
- }
- }
- }
- }
- void addWaypoint(Tile tile) {
- Waypoint way_pnt = new Waypoint(tile.getX(), tile.getY(), tile);
- waypointList.addLast(way_pnt);
- }
- void findNextWaypoint(Seeker seeker) {
- if(waypointList.size() == 1) {
- nextWaypoint = (Waypoint)waypointList.getFirst();
- }
- if(seeker.reachedWaypoint()) {
- currentWaypoint = nextWaypoint;
- waypointState = waypointState + 1;
- println("waypoint State: " + waypointState);
- if(waypointState <= waypointList.size()){
- nextWaypoint = (Waypoint)waypointList.get(waypointState);
- seeker.setReachedWaypoint();
- }
- }
- }
- void clearWaypointList(Seeker seeker) {
- if(waypointList.isEmpty() == false) {
- if(seeker.reachedWaypoint() && nextWaypoint.equals((Waypoint)waypointList.getLast())) {
- waypointList.clear();
- waypointState = 0;
- }
- }
- }
- int getWaypointListSize() {
- return waypointList.size();
- }
- }
- class Waypoint{
- int x;
- int y;
- Waypoint(int tempX, int tempY,Tile tile){
- x = tempX;
- y = tempY;
- }
- int getX(){
- return x;
- }
- int getY(){
- return y;
- }
- }
1