diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..93991d9ad86cc684e8f62e30a2e077b79784a687 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/../../../../../../../../../:\Users\14159\Desktop\study\oosd\project2\yiya-project-2\.idea/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..639900d13c6182e452e33a3bd638e70a0146c785 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectRootManager"> + <output url="file://$PROJECT_DIR$/out" /> + </component> +</project> \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..8d9dd66b04762f9320b7c2d28f991c560b32427b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectModuleManager"> + <modules> + <module fileurl="file://$PROJECT_DIR$/yiya-project-2.iml" filepath="$PROJECT_DIR$/yiya-project-2.iml" /> + </modules> + </component> +</project> \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="" vcs="Git" /> + </component> +</project> \ No newline at end of file diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..24957dce42144cc115095b85bb08b4dcbdca5642 Binary files /dev/null and b/src/.DS_Store differ diff --git a/src/Energy.java b/src/Energy.java new file mode 100644 index 0000000000000000000000000000000000000000..3278b00592500739c67893d19cd4102770e2521b --- /dev/null +++ b/src/Energy.java @@ -0,0 +1,15 @@ +public class Energy { + private int value; + + protected Energy(int energy) { + this.value = energy; + } + + protected int getEnergy() { + return this.value; + } + + protected void add(int amount) { + this.value += amount; + } +} diff --git a/src/Entity.java b/src/Entity.java new file mode 100644 index 0000000000000000000000000000000000000000..8d6968f703ad59ee00c2b112799ad775135c24a8 --- /dev/null +++ b/src/Entity.java @@ -0,0 +1,32 @@ +import bagel.Image; +import bagel.util.Point; + +public abstract class Entity extends Image { + private double x, y; + + protected Entity(String filename, double x, double y) { + super(filename); + this.x = x; + this.y = y; + } + + protected Point getPosition() { + return new Point(this.x, this.y); + } + + protected double getX() { + return x; + } + + protected void setX(double x) { + this.x = x; + } + + protected double getY() { + return y; + } + + protected void setY(double y) { + this.y = y; + } +} \ No newline at end of file diff --git a/src/Player.java b/src/Player.java new file mode 100644 index 0000000000000000000000000000000000000000..15f8e888e21cc0bcb9125a11524dd6beb0dfc1c4 --- /dev/null +++ b/src/Player.java @@ -0,0 +1,23 @@ +import bagel.Window; + +public class Player extends Entity{ + private Energy energy; + + protected Player(String filename, double x, double y, int energy) { + super(filename, x, y); + this.energy = new Energy(energy); + } + + protected int getEnergy() { + return energy.getEnergy(); + } + + protected void updateEnergy(int amount) { + this.energy.add(amount); + } + + protected void eat(Sandwich sandwich) { + this.updateEnergy(+5); + sandwich.eaten(); + } +} diff --git a/src/Sandwich.java b/src/Sandwich.java new file mode 100644 index 0000000000000000000000000000000000000000..52fc084c511c741073eac9b04905bbe19fc5718f --- /dev/null +++ b/src/Sandwich.java @@ -0,0 +1,15 @@ +public class Sandwich extends Entity{ + private boolean eaten = false; + + protected Sandwich(String filename, double x, double y) { + super(filename, x, y); + } + + protected void eaten() { + this.eaten = true; + } + + protected boolean isEaten() { + return eaten; + } +} \ No newline at end of file diff --git a/src/ShadowTreasure.java b/src/ShadowTreasure.java new file mode 100644 index 0000000000000000000000000000000000000000..814a4bf73a0c175eeccfd7aa70c3fdcc91846893 --- /dev/null +++ b/src/ShadowTreasure.java @@ -0,0 +1,153 @@ +import bagel.*; +import bagel.util.Colour; +import bagel.util.Vector2; + +import java.io.*; +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Scanner; + + +/** + * An example Bagel game. + */ +public class ShadowTreasure extends AbstractGame { + private String filename = "res/IO/environment.csv"; + private final Image background = new Image("res/images/background.png"); + private Player player; + private Zombie zombie; + private Sandwich sandwich; + private final Font energyLevel = new Font("res/font/DejaVuSans-Bold.ttf", 20); + private final DrawOptions black = new DrawOptions().setBlendColour(Colour.BLACK); + private int frame = 0; + + // for rounding double number; use this to print the location of the player + private static DecimalFormat df = new DecimalFormat("0.00"); + + public static void printInfo(double x, double y, int e) { + System.out.println(df.format(x) + "," + df.format(y) + "," + e); + } + + public ShadowTreasure() throws IOException { + this.loadEnvironment(filename); + // Add code to initialize other attributes as needed + } + + /** + * Load from input file + */ + private void loadEnvironment(String filename) throws FileNotFoundException { + // Code here to read from the file and set up the environment + Scanner scan = new Scanner(new File(filename)); + String[] row; + while (scan.hasNext()) { + row = scan.next().split(",",4); + switch (row[0]) { + case "Player" : + this.player = new Player("res/images/player.png", + Double.parseDouble(row[1]), + Double.parseDouble(row[2]), + Integer.parseInt(row[3])); + break; + case "Zombie" : + this.zombie = new Zombie("res/images/zombie.png", + Double.parseDouble(row[1]), + Double.parseDouble(row[2])); + break; + case "Sandwich" : + this.sandwich = new Sandwich("res/images/sandwich.png", + Double.parseDouble(row[1]), + Double.parseDouble(row[2])); + break; + } + } + printInfo(player.getX(), player.getY(), player.getEnergy()); + } + + /** + * Performs a state update. + */ + @Override + public void update(Input input) { + // Logic to update the game, as per specification must go here + if (input.wasPressed(Keys.ESCAPE)) { + Window.close(); + + } else if (frame<10) { + background.draw(Window.getWidth() / 2.0, Window.getHeight() / 2.0); + player.draw(player.getX(), player.getY()); + zombie.draw(zombie.getX(), zombie.getY()); + if (!sandwich.isEaten()) { + sandwich.draw(sandwich.getX(), sandwich.getY()); + } + energyLevel.drawString("energy: " + Integer.toString(player.getEnergy()), + 20, 760, black); + frame++; + return; + } + + double speed = 10; + double playerToZombie = player.getPosition().distanceTo(zombie.getPosition()); + double playerToSandwich = player.getPosition().distanceTo(sandwich.getPosition()); + double distDiff = playerToZombie - playerToSandwich; + int energy = player.getEnergy(); + Vector2 direction; + + if (sandwich.isEaten() || (distDiff <= 0 && energy >= 3)) { +// if (sandwich.isEaten() || energy >= 3) { + // player move to zombie + direction = zombie.getPosition().asVector().sub(player.getPosition().asVector()); + direction = direction.normalised(); + player.setX(player.getX() + speed * direction.x); + player.setY(player.getY() + speed * direction.y); + playerToZombie = player.getPosition().distanceTo(zombie.getPosition()); + + if (playerToZombie <= 50) { + player.updateEnergy(-3); + Window.close(); // terminate game if player meets zombie + } +// if (playerToSandwich <= 50) { +// player.eat(sandwich); +// } + + } else if (distDiff > 0 || player.getEnergy() < 3) { +// } else if (player.getEnergy() < 3) { + // player move to sandwich + direction = sandwich.getPosition().asVector().sub(player.getPosition().asVector()); + direction = direction.normalised(); + player.setX(player.getX() + speed * direction.x); + player.setY(player.getY() + speed * direction.y); + playerToSandwich = player.getPosition().distanceTo(sandwich.getPosition()); + +// if (playerToZombie <= 50) { +// player.updateEnergy(-3); +// Window.close(); // terminate game if player meets zombie + if (playerToSandwich <= 50) { + player.eat(sandwich); + } + } + + + background.draw(Window.getWidth() / 2.0, Window.getHeight() / 2.0); + player.draw(player.getX(), player.getY()); + zombie.draw(zombie.getX(), zombie.getY()); + if (!sandwich.isEaten()) { + sandwich.draw(sandwich.getX(), sandwich.getY()); + } + energyLevel.drawString("energy: " + Integer.toString(player.getEnergy()), + 20, 760, black); + + frame = 0; + printInfo(player.getX(), player.getY(), player.getEnergy()); + } + + + /** + * The entry point for the program. + */ + public static void main(String[] args) throws IOException { + ShadowTreasure game = new ShadowTreasure(); + game.run(); + } +} diff --git a/src/Zombie.java b/src/Zombie.java new file mode 100644 index 0000000000000000000000000000000000000000..43246a11a760bf078266310ff646140ed1a4b8bd --- /dev/null +++ b/src/Zombie.java @@ -0,0 +1,5 @@ +public class Zombie extends Entity{ + protected Zombie(String filename, double x, double y) { + super(filename, x, y); + } +} \ No newline at end of file diff --git a/yiya-project-2.iml b/yiya-project-2.iml new file mode 100644 index 0000000000000000000000000000000000000000..d5162cfeaa107bf8464eab7fb8f227bd870485d0 --- /dev/null +++ b/yiya-project-2.iml @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="JAVA_MODULE" version="4"> + <component name="NewModuleRootManager" inherit-compiler-output="true"> + <exclude-output /> + <content url="file://$MODULE_DIR$"> + <sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" /> + <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> + <sourceFolder url="file://$MODULE_DIR$/test" type="java-resource" /> + </content> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + </component> +</module> \ No newline at end of file