diff --git a/project-1/src/APoint.java b/project-1/src/APoint.java new file mode 100644 index 0000000000000000000000000000000000000000..f2b41467614b9fa5df2839b182e9122317780a8a --- /dev/null +++ b/project-1/src/APoint.java @@ -0,0 +1,37 @@ +import java.lang.Math; + +public class APoint { + private double x,y; + + public APoint(double x, double y) { + this.x = x; + this.y = y; + } + + public double getX() { + return x; + } + + public void setX(double x) { + this.x = x; + } + + public double getY() { + return y; + } + + public void setY(double y) { + this.y = y; + } + + public double distanceTo(APoint other) { + return Math.sqrt(Math.pow(this.x - other.getX(),2) + Math.pow((this.y - other.getY()) ,2)); + } + + public APoint getDirection (APoint other) { + double xd = (other.x-this.x)/distanceTo(other); + double yd = (other.y-this.getY())/distanceTo(other); + + return new APoint(xd,yd); + } +} diff --git a/project-1/src/Entity.java b/project-1/src/Entity.java index fd1d0d16f794e2e697f8fadfd2b839af611902b7..a1d92a726d76ba5a1d150675d080c93f004c3df2 100644 --- a/project-1/src/Entity.java +++ b/project-1/src/Entity.java @@ -5,18 +5,18 @@ import java.lang.*; public class Entity { private Image img; - private Point pos; + private APoint pos; - public Entity(String filename, Point pos) { + public Entity(String filename, APoint pos) { this.img = new Image(filename); this.pos = pos; } - public Point getPos() { + public APoint getPos() { return pos; } - public void setPos(Point pos) { + public void setPos(APoint pos) { this.pos = pos; } } diff --git a/project-1/src/Player.java b/project-1/src/Player.java index 0e689b9b4762f6bb940a2db40ace521a7e365204..3df6e685cef719a256aa05a961c09d3b428a94ff 100644 --- a/project-1/src/Player.java +++ b/project-1/src/Player.java @@ -1,13 +1,12 @@ import bagel.*; -import bagel.util.*; public class Player extends Entity{ private Image image; - private Point pos; + private APoint pos; private int energy; //displayed at position (20,760) in black font size 20 and font DejaVuSans-Bol(ttf file provided in res zip file) private static int stepsize = 10; - public Player(String filename, Point pos, int energy) { + public Player(String filename, APoint pos, int energy) { super(filename, pos); this.energy = energy; } @@ -16,7 +15,7 @@ public class Player extends Entity{ return image; } - public Point getPos() { + public APoint getPos() { return pos; } diff --git a/project-1/src/Point.java b/project-1/src/Point.java index dd1729396a4909d472fc1d3f0bec9eccac50d5f1..47984c1be27f4d5b5a3faf0946e1b328003fc6bc 100644 --- a/project-1/src/Point.java +++ b/project-1/src/Point.java @@ -23,7 +23,7 @@ public class Point{ public void setY(double y) { this.y = y; } - + public double distanceTo(Point other) { return Math.sqrt(Math.pow(this.x - other.getX(),2) + Math.pow((this.y - other.getY()) ,2)); } diff --git a/project-1/src/Sandwich.java b/project-1/src/Sandwich.java index de7df0b34dd483c20afc3c713a6f447adf9e96dc..d58250fbed0420193df57b1dc340772e3b0dfbfd 100644 --- a/project-1/src/Sandwich.java +++ b/project-1/src/Sandwich.java @@ -1,11 +1,7 @@ import bagel.*; -import bagel.util.*; public class Sandwich extends Entity { - private Image image; - private Point pos; - - public Sandwich(String filename, Point pos) { + public Sandwich(String filename, APoint pos) { super(filename, pos); } } diff --git a/project-1/src/ShadowTreasure.java b/project-1/src/ShadowTreasure.java index 6889fcaf3df267d45fc6d82525f6d10d218b3148..0b4b9e3379714d971b93ba6ad929325788e66711 100644 --- a/project-1/src/ShadowTreasure.java +++ b/project-1/src/ShadowTreasure.java @@ -1,11 +1,11 @@ import bagel.*; -import java.io.BufferedReader; -import java.io.FileReader; +import java.io.FileNotFoundException; import java.io.IOException; import java.text.DecimalFormat; import java.util.Scanner; import java.lang.*; +import java.io.File; /** * An example Bagel game. @@ -19,7 +19,8 @@ public class ShadowTreasure extends AbstractGame { private Zombie zombie; private Sandwich sandwich; private static int count=0; - private Font dejavu; + private final Font deja = new Font("res/font/DejaVuSans-Bold.ttf",24); + private Image bg; public static void printInfo(double x, double y, int e) { System.out.println(df.format(x) + "," + df.format(y) + "," + e); @@ -29,7 +30,7 @@ public class ShadowTreasure extends AbstractGame { super(1000, 800, "ShadowTreasure"); this.loadEnvironment("res/IO/environment.csv"); // Add code to initialize other attributes as needed - this.dejavu = new Font("res/font/DejaVuSans-Bold.ttf",20); + bg = new Image("res/images/background.png"); } /** @@ -37,34 +38,45 @@ public class ShadowTreasure extends AbstractGame { */ private void loadEnvironment(String filename){ // Code here to read from the file and set up the environment - Scanner scanner = new Scanner(filename); - double xcoor, ycoor; - int level; - while(scanner.hasNext()) { - String item = scanner.next(); - switch (item) { - case "Player": - xcoor = scanner.nextInt(); - ycoor = scanner.nextInt(); - level = scanner.nextInt(); - player = new Player("res/images/player.png", new Point(xcoor, ycoor), level); - break; - case "Zombie": - xcoor = scanner.nextInt(); - ycoor = scanner.nextInt(); - zombie = new Zombie("res/Images/Zombie.png", new Point(xcoor, ycoor)); - break; - case "Sandwich": - xcoor = scanner.nextInt(); - ycoor = scanner.nextInt(); - sandwich = new Sandwich("res/Images/Sandwich.png", new Point(xcoor, ycoor)); - break; + try { + Scanner scanner = new Scanner(new File(filename)); + double xcoor, ycoor; + int level; + while (scanner.hasNextLine()) { + Scanner linescan = new Scanner(scanner.nextLine()); + linescan.useDelimiter(","); + String item = linescan.next(); + System.out.println("Next item: ("+item+")"); + + switch (item.toLowerCase()) { + case "player": + xcoor = Double.parseDouble(linescan.next()); + ycoor = Double.parseDouble(linescan.next()); + level = Integer.parseInt(linescan.next()); + System.out.println("Player: ("+xcoor+","+ycoor+"), "+level); + player = new Player("res/images/player.png", new APoint(xcoor, ycoor), level); + break; + case "zombie": + xcoor = Double.parseDouble(linescan.next()); + ycoor = Double.parseDouble(linescan.next()); + System.out.println("Zombie: ("+xcoor+","+ycoor+")"); + zombie = new Zombie("res/Images/Zombie.png", new APoint(xcoor, ycoor)); + break; + case "zandwich": + xcoor = Double.parseDouble(linescan.next()); + ycoor = Double.parseDouble(linescan.next()); + System.out.println("Sandwich: ("+xcoor+","+ycoor+")"); + sandwich = new Sandwich("res/Images/Sandwich.png", new APoint(xcoor, ycoor)); + break; } + linescan.close(); } - scanner.close(); - // draw background at position 0,0 - Image bg = new Image("res/images/background.png"); - bg.draw(0,0); + scanner.close(); + } catch(FileNotFoundException e) { + System.out.println("File was not found!\n"+e); + } catch(Exception e) { + System.out.println("There is an unexpected error!\n"+e); + } } /** @@ -72,6 +84,9 @@ public class ShadowTreasure extends AbstractGame { */ @Override public void update(Input input) { + bg.draw(0,0); + deja.drawString("Energy",20,760); +// deja.drawString(String.format("energy: %d",player.getEnergy()),20,760); // Logic to update the game, as per specification must go here if(player.getPos().distanceTo(zombie.getPos()) < 50) { player.addEnergy(-3); @@ -81,16 +96,15 @@ public class ShadowTreasure extends AbstractGame { sandwich = null; } if(player.getEnergy() >= 3) { - Point dir = player.getPos().getDirection(zombie.getPos()); + APoint dir = player.getPos().getDirection(zombie.getPos()); player.getPos().setX(player.getPos().getX()+dir.getX()*player.getStepsize()); player.getPos().setY(player.getPos().getY()+dir.getY()*player.getStepsize()); } else { - Point dir = player.getPos().getDirection(sandwich.getPos()); + APoint dir = player.getPos().getDirection(sandwich.getPos()); player.getPos().setX(player.getPos().getX()+dir.getX()*player.getStepsize()); player.getPos().setY(player.getPos().getY()+dir.getY()*player.getStepsize()); } // constant info on window - dejavu.drawString(String.format("energy: %d",player.getEnergy()),20,760); } diff --git a/project-1/src/Zombie.java b/project-1/src/Zombie.java index 34abd6be7b46a67731760a7b165daff0880afab9..e0eab539ac077517b434a7944a6c8c6c2b2337e6 100644 --- a/project-1/src/Zombie.java +++ b/project-1/src/Zombie.java @@ -1,9 +1,8 @@ import java.lang.*; -import bagel.util.*; public class Zombie extends Entity{ - public Zombie(String filename, Point pos) { + public Zombie(String filename, APoint pos) { super(filename, pos); } } diff --git a/project-1/src/test.java b/project-1/src/test.java index 9aacf9e875bea8cb400b53468e7423bdf79d3327..da0decdc18e25b3dd429d0041c1be3bb5013c455 100644 --- a/project-1/src/test.java +++ b/project-1/src/test.java @@ -1,5 +1,7 @@ +import bagel.*; public class test { public static void main(String[] args) { + Font dejavu = new Font("res/font/DejaVuSans-Bold.ttf",24); } }