From 0c44362994e3682fb9033b54df04019b17c74b4c Mon Sep 17 00:00:00 2001
From: JIAXI3 <jiaxi3@student.unimelb.edu.au>
Date: Sun, 4 Sep 2022 10:39:15 +0800
Subject: [PATCH] 1. Complete the wall blocking function. 2. Add player image
 direction.

---
 src/Player.java          | 49 +++++++++++++++++++++++++-----------
 src/ShadowDimension.java | 54 ++++++++++++++++++++++++++--------------
 src/Sinkhole.java        |  5 +++-
 src/Wall.java            | 23 +++++++++++++++--
 4 files changed, 95 insertions(+), 36 deletions(-)

diff --git a/src/Player.java b/src/Player.java
index 4213e76..dbbb79f 100644
--- a/src/Player.java
+++ b/src/Player.java
@@ -2,6 +2,7 @@ import bagel.DrawOptions;
 import bagel.Font;
 import bagel.Image;
 import bagel.util.Point;
+import bagel.util.Rectangle;
 
 /**
  * Player Code for SWEN20003 Project 1, Semester 2, 2022
@@ -16,7 +17,9 @@ import bagel.util.Point;
 public class Player {
     private final static Image FaceLeftImage = new Image("res/faeLeft.png");
     private final static Image FaceRightImage = new Image("res/faeRight.png");
-    private static final int Life = 100;
+
+    private static final int LIFE_MAX = 100;
+    private static int Life = LIFE_MAX;
     private static Image Img;
     private final Font LifeFont = new Font("res/frostbite.ttf", 30);
     private final DrawOptions LifeHighColor = new DrawOptions();
@@ -40,10 +43,32 @@ public class Player {
         LifeLowColor.setBlendColour(1, 0, 0);
     }
 
+    public void Clear() {
+        X = 0;
+        Y = 0;
+        WORLD_TOP = 0;
+        WORLD_LEFT = 0;
+        WORLD_BOTTOM = 0;
+        WORLD_RIGHT = 0;
+        Life = LIFE_MAX;
+    }
+
     public Point GetPos() {
+        /*Get the position of player*/
         return new Point(X, Y);
     }
 
+    public void SetPos(Point p) {
+        /*Set the position of player*/
+        X = (int) p.x;
+        Y = (int) p.y;
+    }
+
+    public Rectangle GetRectangle() {
+        /*Get Rectangle of player*/
+        return Img.getBoundingBoxAt(GetPos());
+    }
+
     public void MoveUp() {
         /*Player moves up in world, step = 2*/
         if (Y > (WORLD_TOP + 2)) {
@@ -62,6 +87,7 @@ public class Player {
         /*Player moves left in world, step = 2*/
         if (X > (WORLD_LEFT + 2)) {
             X -= 2;
+            Img = FaceLeftImage;
         }
     }
 
@@ -69,6 +95,7 @@ public class Player {
         /*Player moves right in world, step = 2*/
         if (X < (WORLD_RIGHT - 2)) {
             X += 2;
+            Img = FaceRightImage;
         }
     }
 
@@ -76,23 +103,15 @@ public class Player {
         /*Show play image*/
         Img.drawFromTopLeft(X, Y);
 
+        int life_percent = Life * 100 / LIFE_MAX;
         /*Show life text*/
-        if (Life >= 65) {
-            LifeFont.drawString(Life + "%", 20, 25, LifeHighColor);
-        } else if (Life >= 35) {
-            LifeFont.drawString(Life + "%", 20, 25, LifeMidColor);
+        if (life_percent >= 65) {
+            LifeFont.drawString(life_percent + "%", 20, 25, LifeHighColor);
+        } else if (life_percent >= 35) {
+            LifeFont.drawString(life_percent + "%", 20, 25, LifeMidColor);
         } else {
-            LifeFont.drawString(Life + "%", 20, 25, LifeLowColor);
+            LifeFont.drawString(life_percent + "%", 20, 25, LifeLowColor);
         }
     }
 
-    public void ToLeft() {
-        /*Changing user orientation to left*/
-        Img = FaceLeftImage;
-    }
-
-    public void ToRight() {
-        /*Changing user orientation to right*/
-        Img = FaceRightImage;
-    }
 }
diff --git a/src/ShadowDimension.java b/src/ShadowDimension.java
index d931a4c..f4be1dd 100644
--- a/src/ShadowDimension.java
+++ b/src/ShadowDimension.java
@@ -1,5 +1,6 @@
 import bagel.*;
 import bagel.util.Colour;
+import bagel.util.Point;
 
 import java.io.IOException;
 import java.nio.file.Files;
@@ -82,10 +83,24 @@ public class ShadowDimension extends AbstractGame {
                 player.WORLD_RIGHT = Integer.parseInt(line[1]);
                 player.WORLD_BOTTOM = Integer.parseInt(line[2]);
             }
-            //System.out.println(csv[i]);
         }
     }
 
+
+    private void ImgRefresh() {
+        /*Draw the world, walls, sinkholes, players.*/
+        BACKGROUND_IMAGE.draw(Window.getWidth() / 2.0, Window.getHeight() / 2.0);
+        wall.Update();
+        hole.Update();
+        player.Update();
+    }
+
+    private void Init() {
+        /*Init for new turn.*/
+        player.Clear();
+        wall.Clear();
+    }
+
     /**
      * Performs a state update.
      * allows the game to exit when the escape key is pressed.
@@ -94,12 +109,15 @@ public class ShadowDimension extends AbstractGame {
     protected void update(Input input) {
         /*State for game
          * 0:Init and show welcom page
-         * 1:Show Play page
+         * 1:First show play page
+         * 2:Play
          * */
         switch (game_state) {
             case 0: {
                 /*Draw Text*/
-                Drawing.drawRectangle(0, 0, 1024, 768, new Colour(103.0 / 256, 153.0 / 256, 231.0 / 256));
+                Init();
+                Drawing.drawRectangle(0, 0, 1024, 768,
+                        new Colour(103.0 / 256, 153.0 / 256, 231.0 / 256));
                 title.drawString("SHADOW DIMENSION", 260, 250);
                 hint.drawString("PRESS SPACE TO START\nUSE ARROW KEYS TO FIND GATE",
                         (Window.getWidth() - hint.getWidth("USE ARROW KEYS TO FIND GATE") + 90) / 2,
@@ -116,6 +134,15 @@ public class ShadowDimension extends AbstractGame {
             }
 
             case 1: {
+                ImgRefresh();
+                game_state = 2;
+                break;
+            }
+            case 2: {
+                /*After the player hits the wall,
+                he should return to the original position and copy the player's current position.*/
+                Point old_p = player.GetPos();
+
                 /*Get key input and move player*/
                 if (input.isDown(Keys.UP)) {
                     player.MoveUp();
@@ -127,22 +154,13 @@ public class ShadowDimension extends AbstractGame {
                     player.MoveLeft();
                 }
 
-                /*Show world image*/
-                BACKGROUND_IMAGE.draw(Window.getWidth() / 2.0, Window.getHeight() / 2.0);
-
-                /*Show wall image*/
-                wall.Update();
-
-                /*Show sinkhole image*/
-                hole.Update();
-
-                /*Show player image*/
-                player.Update();
-                break;
-            }
-
-            default: {
+                /*Restore the original position after hitting the wall.*/
+                if (wall.InWallCheck(player.GetRectangle())) {
+                    player.SetPos(old_p);
+                }
 
+				/*Show image*/
+                ImgRefresh();
                 break;
             }
         }
diff --git a/src/Sinkhole.java b/src/Sinkhole.java
index afa84be..d8b4bca 100644
--- a/src/Sinkhole.java
+++ b/src/Sinkhole.java
@@ -1,4 +1,5 @@
 import bagel.Image;
+import bagel.util.Rectangle;
 
 import java.util.ArrayList;
 
@@ -19,14 +20,16 @@ public class Sinkhole {
     public void Sinkhole() {
     }
 
+
     public void Update() {
-        /*Gets all the hole position of the ArrayList for drawing*/
+        /*Gets all the hole position of the ArrayList and draw*/
         for (int i = 0; i < Pos.size(); i++) {
             bagel.util.Point p = (bagel.util.Point) Pos.get(i);
             Img.drawFromTopLeft(p.x, p.y);
         }
     }
 
+
     public void Add(int x, int y) {
         /*Add the hole position to arraylist*/
         Pos.add(new bagel.util.Point(x, y));
diff --git a/src/Wall.java b/src/Wall.java
index edf2514..a9ea06a 100644
--- a/src/Wall.java
+++ b/src/Wall.java
@@ -1,4 +1,5 @@
 import bagel.Image;
+import bagel.util.Rectangle;
 
 import java.util.ArrayList;
 
@@ -14,20 +15,38 @@ import java.util.ArrayList;
 public class Wall {
     private final Image Img = new Image("res/wall.png");
     private final ArrayList Pos = new ArrayList();
+    private final ArrayList rect = new ArrayList();
 
-    public void Wall() {
+
+    public void Clear() {
+        /*Clear all position and Rectangle list*/
+        Pos.clear();
+        rect.clear();
     }
 
     public void Update() {
         /*Gets all the wall locations of the ArrayList for drawing*/
+        rect.clear();
         for (int i = 0; i < Pos.size(); i++) {
             bagel.util.Point p = (bagel.util.Point) Pos.get(i);
             Img.drawFromTopLeft(p.x, p.y);
+
+            /*Record all wall Rectangle for overlaps checking.*/
+            rect.add(Img.getBoundingBoxAt(p));
         }
     }
 
-    public void Add(int x, int y) {
+    public boolean InWallCheck(Rectangle r) {
+        /*Check that each wall overlaps the r(player) position*/
+        for (int i = 0; i < Pos.size(); i++) {
+            if (r.intersects((Rectangle) rect.get(i))) {
+                return true;
+            }
+        }
+        return false;
+    }
 
+    public void Add(int x, int y) {
         /*Add the wall position to arraylist*/
         Pos.add(new bagel.util.Point(x, y));
     }
-- 
GitLab