Commit 913c42bd authored by Anya Helene Bagge's avatar Anya Helene Bagge 🦆

Ting fra forelesning om arv

parent 3b08ad4e
......@@ -21,7 +21,7 @@ import inf101.games.simgame.objects.Destination;
import inf101.games.simgame.objects.IWalker;
import inf101.games.simgame.objects.SimObjectFactory;
import inf101.games.simgame.objects.Source;
import inf101.games.simgame.objects.Walker;
import inf101.games.simgame.objects.Pathfinder;
import inf101.games.simgame.objects.Wall;
import inf101.grid.IArea;
import inf101.grid.IGrid2D;
......
package inf101.games.simgame.objects;
import java.awt.Color;
import java.util.Arrays;
import inf101.games.gui.IImage;
import inf101.games.gui.TextImage;
import inf101.games.simgame.INavigator;
import inf101.games.simgame.ISimActor;
import inf101.games.simgame.ISimGame;
import inf101.grid.Direction;
import inf101.grid.IPosition;
public class Pathfinder extends Walker implements ISimActor, IWalker {
private final IImage walkerImage = new TextImage(getIdString() + String.valueOf((char) ('a' + N % 26)), Color.WHITE,
null);
private static int N = 0;
private Direction dir = Direction.WEST;
public Pathfinder(ISimGame game) {
super(game);
N++;
}
@Override
public IImage getImage() {
return walkerImage;
}
@Override
public void step(int time) {
if (game.hasDestination(getPosition())) {
game.removeObject(this);
return;
}
INavigator navigator = game.getNavigator();
IPosition myPosition = getPosition();
Direction dir = navigator.getDirectionFrom(myPosition);
if (dir == Direction.NONE)
return;
for (Direction toTry : Arrays.asList(Direction.FORWARD, Direction.LEFT, Direction.RIGHT)) {
IPosition nextPos = myPosition.go(dir.resolve(toTry));
if (move(nextPos))
return;
}
}
@Override
public String getIdString() {
return "w";
}
}
package inf101.games.simgame.objects;
import inf101.games.gui.IImage;
import inf101.games.simgame.ISimGame;
import inf101.grid.IPosition;
public class Pensjonist extends Walker {
public Pensjonist(ISimGame game) {
super(game);
}
@Override
public void step(int count) {
if (count % 2 == 0)
return;
IPosition newPos = getPosition().west();
move(newPos);
}
@Override
public String getIdString() {
return "p";
}
@Override
public IImage getImage() {
return null;
}
}
......@@ -39,7 +39,7 @@ public class SimObjectFactory {
case "|":
return new Wall(game);
case "w":
return new Walker(game);
return new Pathfinder(game);
case "z":
return new Zombie(game);
// TODO: konstruer nye typer simobjekter her
......
package inf101.games.simgame.objects;
import java.awt.Color;
import java.util.Arrays;
import inf101.games.gui.IImage;
import inf101.games.gui.TextImage;
import inf101.games.simgame.INavigator;
import inf101.games.simgame.ISimActor;
import inf101.games.simgame.ISimGame;
import inf101.grid.Direction;
import inf101.grid.IPosition;
public class Walker implements ISimActor, IWalker {
private ISimGame game;
private final IImage walkerImage = new TextImage(getIdString() + String.valueOf((char) ('a' + N % 26)), Color.WHITE, null);
private static int N = 0;
private Direction dir = Direction.WEST;
public class Walker implements IWalker {
protected ISimGame game;
public Walker(ISimGame game) {
this.game = game;
N++;
}
@Override
public IImage getImage() {
return walkerImage;
public void step(int time) {
}
@Override
public void step(int time) {
if (game.hasDestination(getPosition())) {
game.removeObject(this);
return;
}
INavigator navigator = game.getNavigator();
public IPosition getPosition() {
return game.getPosition(this);
}
IPosition myPosition = getPosition();
Direction dir = navigator.getDirectionFrom(myPosition);
if (dir == Direction.NONE)
return;
for (Direction toTry : Arrays.asList(Direction.FORWARD, Direction.LEFT, Direction.RIGHT)) {
IPosition nextPos = myPosition.go(dir.resolve(toTry));
if (game.getArea().contains(nextPos))
if (!game.isOccupied(nextPos)) {
game.moveObject(this, nextPos);
return;
}
}
@Override
public IImage getImage() {
throw new UnsupportedOperationException("Don't know what I look like");
}
@Override
public IPosition getPosition() {
return game.getPosition(this);
public String getIdString() {
throw new UnsupportedOperationException("Don't know my ID string");
}
@Override
......@@ -65,9 +41,13 @@ public class Walker implements ISimActor, IWalker {
return true;
}
@Override
public String getIdString() {
return "w";
boolean move(IPosition newPos) {
if(RulesChecker.check(this, newPos, game)) {
game.moveObject(this, newPos);
return true;
}
else
return false;
}
}
......@@ -11,15 +11,13 @@ import inf101.games.simgame.ISimGame;
import inf101.grid.Direction;
import inf101.grid.IPosition;
public class Zombie implements ISimActor, IWalker {
private ISimGame game;
public class Zombie extends Walker implements ISimActor, IWalker {
private final IImage walkerImage = new TextImage(getIdString() + String.valueOf((char) ('a' + N % 26)), Color.WHITE,
null);
private static int N = 0;
private Direction dir = Direction.WEST;
public Zombie(ISimGame game) {
this.game = game;
super(game);
N++;
}
......@@ -30,26 +28,10 @@ public class Zombie implements ISimActor, IWalker {
@Override
public void step(int time) {
// TODO: fyll inn, så zombien beveger seg ett steg Direction.WEST hver
// rundt.
//
// Må sjekke at den ikke kolliderer med noe (game.isOccupied(pos)), og
// at den fjerner seg selv når den når et mål (game.hasDestination(pos)
}
IPosition newPos = getPosition().west();
@Override
public IPosition getPosition() {
return game.getPosition(this);
}
move(newPos);
@Override
public boolean isMovable() {
return true;
}
@Override
public boolean isExclusive() {
return true;
}
@Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment