You will continue to add functionality to your existing project from the previous task. There is no new repository to clone.
Video link: https://youtu.be/2Kd3yTYmTrc
Last one!
As before, watching this video is optional, and all of the information you need for this task is contained within this page. However, you may find it helpful for getting a better understanding of the task, and what is expected of you
In this task, you will implement the following specs. Upon completion, you will have implemented a new, smarter enemy type that can chase the Player while avoiding obstacles.
Before you can begin this, however, you will need to modify the Vector2D
class in the
app.gameengine.model.physics
package to work well with HashMaps, and by extension Graphs. In order
to achieve this, you must override the equals
and hashCode
methods that the
Vector2D
class inherits from the Object
class. You do not need to, and are not
expected to, fully understand these methods, however if you wish to understand them an instructor or TA would be
happy to explain them to you.
equals
, which takes one parameter of type Object
and
returns a boolean. It should appear as follows:
@Override public boolean equals(Object obj) { if (this == obj) { return true; } else if (obj == null || this.getClass() != obj.getClass()) { return false; } Vector2D other = (Vector2D) obj; return Math.abs(this.x - other.x) < 1e-9 && Math.abs(this.y - other.y) < 1e-9; }
hashCode
which takes no parameters and returns an int. You will also
need to import java.util.Objects
. The method should appear as follows:
@Override public int hashCode() { return Objects.hash(Math.round(x / 1e-9), Math.round(y / 1e-9)); }
In the app.gameengine.model.ai
package, access the Pathfinding
class and
implement the following method:
findPathAvoidWalls
that takes in a Level,
a Vector2D representing starting location, and a Vector2D representing ending location that returns
a LinkedListNode of Vector2D.
Math.floor
method for finding the tile that
contains a given vector.
From here, you will now implement a new Decision and Enemy type that utilizes this pathfinding.
app.gameengine.model.ai
package, create a class named MoveTowardsPlayerAvoidWalls
that extends Decision
.
doAction
method. This method should generate a path for or manipulate the
DynamicGameObject
from the parameter.
doAction
method from the
MoveTowardsPlayer
class, except if it generates a path it should use the output
of findPathAvoidWalls
instead of findPath
.
app.games.topdownobjects
package, create a class named SmartEnemy
that extends
Enemy.
DecisionTree
that contains only a MoveTowardsPlayerAvoidWalls
decision with no child nodes.
You may now want to add a new SmartEnemy
to some topdown level to view the new behavior you've written.
There is no testing utility for this task. However, you may find your previously written testing
utility validatePath
helpful. You can access this testing utility by creating a new instance of
a TestTask2
object.
In the app.tests
package, create a class called TestTask6
and write tests for the
findPathAvoidWalls
method.
Implement the methods and classes from the specification. You may find it helpful to write tests for
findPathAvoidWalls
before beginning your implementation.
The feedback in Autolab will be given in 3 phases. If you don't complete a phase, then feedback for the following phase(s) will not be provided.
Once you complete all 3 phases, you will have completed this Task and Autolab will confirm this with a score of 1.0 for complete.