// Return the location of the first object of the given food type within // the given distance of the location. If none is found, return (-1, -1) public Vector2 getCloseFood(BoardManager.Food type, int distance, Vector2 currentCell, BoardManager.Food[, ] foodArray) { for (int xOffset = -distance; xOffset < distance + 1; xOffset++) { for (int yOffset = -distance; yOffset < distance + 1; yOffset++) { int curX = (int)currentCell.x + xOffset; int curY = (int)currentCell.y + yOffset; if (curX >= 0 && curX < SIZE && curY >= 0 && curY < SIZE) { if (foodArray[curX, curY] == type) { return(new Vector2(curX, curY)); } } } } return(new Vector2(-1, -1)); }
// ACTION METHOD: Returns the main steering vector to accomplish this action, // allong with setting this.action to null and updating insistances when the // action is finished public Vector2 seekFood(BoardManager.Food foodType, SmellType smellType) { Vector2 goalSteering = new Vector2(-1, -1); // If we've seen the food and stored its location, just arrive at the location // (If we don't have a target to arrive at the vector is (-1, -1)) if (this.target.x >= 0 && this.target.y >= 0) { goalSteering = arriveAt(new Vector2(this.target.x * CELL_SIZE, this.target.y * CELL_SIZE)); // If we have arrived at the location, apply the current goal to the insistances if (this.target.x == this.currentCell.x && this.target.y == this.currentCell.y) { this.goal.apply(this.insistance); this.goal = null; this.target = new Vector2(-1, -1); } } else { // If we haven't seen any food yet, check if we see one // Check if within 3 blocks of food type (represents the agent seeing the food) Vector2 closeBush = getCloseFood(foodType, 3, currentCell, GameManager.instance.getFoodArray()); if (closeBush.x >= 0 && closeBush.y >= 0) { this.target = closeBush; goalSteering = arriveAt(new Vector2(closeBush.x * CELL_SIZE, closeBush.y * CELL_SIZE)); } else { // If we still can't see a bush, just follow the smell goalSteering = this.directionOfSmell(currentCell, GameManager.instance.getSmellArray(), smellType) * MAX_ACCEL; } } return(goalSteering); }