I want to create a Gridworld with one "Car"-actor and a fix spawn location:
package gridworld.blatt3;
import gridworld.framework.actor.*;
import gridworld.framework.grid.Grid;
import gridworld.framework.grid.Location;
public class Traffic {
public static void main(String[] args) {
TrafficWorld world = new TrafficWorld();
System.out.println("Anwendung startet...");
Location loc1 = new Location(0,0);
Grid.put(loc1, new Car());
world.show();
}
}
package gridworld.framework.actor;
import gridworld.framework.actor.bugs.Flower;
import gridworld.framework.grid.Grid;
import gridworld.framework.grid.Location;
import java.awt.*;
public class Car extends AbstractActor {
//Schrittgröße//
int speed=5;
int sideLenght=50;
int step = 0;
//Richtung //
public Car () {
setDirection(Location.EAST);
setColor(Color.BLUE);
}
public void act () {
move();
}
public void move() {
int speedCount=0;
//Springt immer um 'speed'-Schritte //
do {
step++;
speedCount++;
if (step < sideLenght) {
System.out.println("Step: "+step);
Location loc = getLocation();
Location next = loc.getAdjacentLocation(getDirection());
moveTo(next);
}
else {
System.out.println("car drives off the grid");
removeSelfFromGrid();
break;
}
} while (speedCount!=speed);
}
}
package gridworld.framework.actor;
import gridworld.framework.grid.BoundedGrid;
public class TrafficWorld extends ActorWorld {
private static final int rowSize = 1;
private static final int colSize = 50;
public TrafficWorld() {
super(new BoundedGrid<Actor>(rowSize, colSize));
}
}
How can I define a specific spawn location for "Car"? E.g. I want the Actors to spawn in the top left corner of the Grid, I created a Location object "loc1" to use with Grid.put(), but I get an error: java: non-static method put(gridworld.framework.grid.Location,E) cannot be referenced from a static context
I'm using openjdk-15.0.1
You can use the
ActorWorld.add()
method with theLocation
argument to place the actor at a specific location:In your case it would be something like: