Skip to content
Snippets Groups Projects
Commit 9385340d authored by jiaxi liu's avatar jiaxi liu
Browse files

1. Packaging improvements. Changed the player class X & Y var to private and...

1. Packaging improvements. Changed the player class X & Y var to private and added the public interface for setting the player position.
2. Improve code reuse. Replace Sinkhole and Wall class with GameObj classes with higher code repetition.
parent 56e58ffa
No related branches found
No related tags found
No related merge requests found
......@@ -4,50 +4,78 @@ import bagel.util.Rectangle;
import java.util.ArrayList;
/**
* Wall Code for SWEN20003 Project 1, Semester 2, 2022
* object Code for SWEN20003 Project 1, Semester 2, 2022
* <p>
* Manage walls info.
* Draw walls.
* Determine the overlap between player and wall.
* Manage object info.
* Draw object.
* Determine the overlap between player and object.
*
* @JIAXI3
*/
public class Wall {
private final Image Img = new Image("res/wall.png");
public class GameObj {
private Image Img;
private final ArrayList Pos = new ArrayList();
private final ArrayList rect = new ArrayList();
GameObj(String img_path) {
Img = new Image(img_path);
}
/**
* Clear all objects.
*/
public void Clear() {
/*Clear all position and Rectangle list*/
Pos.clear();
rect.clear();
}
/**
* Draw all objects.
*/
public void Update() {
/*Gets all the wall locations of the ArrayList for drawing*/
/*Gets all the object 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.*/
/*Record all object Rectangle for overlaps checking.*/
rect.add(Img.getBoundingBoxAt(p));
}
}
public boolean InWallCheck(Rectangle r) {
/**
* Intersect check on all objects.
*/
public boolean IntersectCheck(Rectangle r, boolean remove) {
/*Check that each wall overlaps the r(player) position*/
for (int i = 0; i < Pos.size(); i++) {
if (r.intersects((Rectangle) rect.get(i))) {
/*Remove the object if the overlap is found, which means the target can only be hit once.*/
if (remove) {
Pos.remove(i);
}
return true;
}
}
return false;
}
/**
* Intersect check on all objects.
*/
public boolean IntersectCheck(Rectangle r) {
return IntersectCheck(r, false);
}
/**
* Add a object.
*/
public void Add(int x, int y) {
/*Add the wall position to arraylist*/
/*Add the object position to arraylist*/
Pos.add(new bagel.util.Point(x, y));
}
}
......@@ -25,13 +25,13 @@ public class Player {
private final DrawOptions LifeHighColor = new DrawOptions();
private final DrawOptions LifeMidColor = new DrawOptions();
private final DrawOptions LifeLowColor = new DrawOptions();
public int X = 0;
public int Y = 0;
private int X = 0;
private int Y = 0;
public int WORLD_TOP = 0;
public int WORLD_LEFT = 0;
public int WORLD_BOTTOM = 0;
public int WORLD_RIGHT = 0;
private int WORLD_TOP = 0;
private int WORLD_LEFT = 0;
private int WORLD_BOTTOM = 0;
private int WORLD_RIGHT = 0;
public Player() {
/*Init player image*/
......@@ -43,6 +43,9 @@ public class Player {
LifeLowColor.setBlendColour(1, 0, 0);
}
/**
* Reset all properties.
*/
public void Clear() {
X = 0;
Y = 0;
......@@ -53,17 +56,44 @@ public class Player {
Life = LIFE_MAX;
}
/**
* Get player position.
*/
public Point GetPos() {
/*Get the position of player*/
return new Point(X, Y);
}
/**
* Set World top left position.
*/
public void SetWorldTopLeft(int top, int left) {
/*Set the position of world TopLeft*/
WORLD_TOP = top;
WORLD_LEFT = left;
}
/**
* Set World bottom right position.
*/
public void SetWorldBottomRight(int bottom, int right) {
/*Set the position of world BottomRight*/
WORLD_BOTTOM = bottom;
WORLD_RIGHT = right;
}
/**
* Set player position.
*/
public void SetPos(Point p) {
/*Set the position of player*/
X = (int) p.x;
Y = (int) p.y;
}
/**
* Check whether the player falls into a hole.
*/
public int IntoHole() {
/*Life handling and system output for players falling into sinkholes*/
Life -= 30;
......@@ -74,11 +104,17 @@ public class Player {
return Life;
}
/**
* Get player Rectangle.
*/
public Rectangle GetRectangle() {
/*Get Rectangle of player*/
return Img.getBoundingBoxAt(GetPos());
}
/**
* Player takes a step up.
*/
public void MoveUp() {
/*Player moves up in world, step = 2*/
if (Y > (WORLD_TOP + 2)) {
......@@ -86,6 +122,9 @@ public class Player {
}
}
/**
* Player takes a step down.
*/
public void MoveDown() {
/*Player moves down in world, step = 2*/
if (Y < (WORLD_BOTTOM - 2)) {
......@@ -93,6 +132,9 @@ public class Player {
}
}
/**
* Player takes a step left.
*/
public void MoveLeft() {
/*Player moves left in world, step = 2*/
if (X > (WORLD_LEFT + 2)) {
......@@ -101,6 +143,9 @@ public class Player {
}
}
/**
* Player takes a step right.
*/
public void MoveRight() {
/*Player moves right in world, step = 2*/
if (X < (WORLD_RIGHT - 2)) {
......@@ -109,6 +154,9 @@ public class Player {
}
}
/**
* Draw play and life percent.
*/
public void Update() {
/*Show play image*/
Img.drawFromTopLeft(X, Y);
......
......@@ -27,8 +27,8 @@ public class ShadowDimension extends AbstractGame {
private final Font title = new Font("res/frostbite.ttf", 75);
private final Font hint = new Font("res/frostbite.ttf", 40);
Player player = new Player();
Wall wall = new Wall();
Sinkhole hole = new Sinkhole();
GameObj wall = new GameObj("res/wall.png");
GameObj hole = new GameObj("res/sinkhole.png");
public ShadowDimension() {
......@@ -66,8 +66,7 @@ public class ShadowDimension extends AbstractGame {
String[] line = csv[i].split(",");
if (line[0].equals("Player")) {
/*Set player pos*/
player.X = Integer.parseInt(line[1]);
player.Y = Integer.parseInt(line[2]);
player.SetPos(new Point(Integer.parseInt(line[1]), Integer.parseInt(line[2])));
} else if (line[0].equals("Wall")) {
/*Set wall pos*/
wall.Add(Integer.parseInt(line[1]), Integer.parseInt(line[2]));
......@@ -76,16 +75,17 @@ public class ShadowDimension extends AbstractGame {
hole.Add(Integer.parseInt(line[1]), Integer.parseInt(line[2]));
} else if (line[0].equals("TopLeft")) {
/*Set world top left pos*/
player.WORLD_LEFT = Integer.parseInt(line[1]);
player.WORLD_TOP = Integer.parseInt(line[2]);
player.SetWorldTopLeft(Integer.parseInt(line[2]), Integer.parseInt(line[1]));
} else if (line[0].equals("BottomRight")) {
/*Set world top bottom right pos*/
player.WORLD_RIGHT = Integer.parseInt(line[1]);
player.WORLD_BOTTOM = Integer.parseInt(line[2]);
player.SetWorldBottomRight(Integer.parseInt(line[2]), Integer.parseInt(line[1]));
}
}
}
/**
* GameOver function.
*/
private void GameOver(boolean win) {
/*Modify the game state at the end of the game*/
if (win) {
......@@ -96,18 +96,23 @@ public class ShadowDimension extends AbstractGame {
}
/**
* Player in hole check function.
*/
private void InHoleCheck() {
/*Check if the player falls into a sinkhole.
If the player runs out of health, the game fails.*/
if (hole.InHoldCheck(player.GetRectangle())) {
if (hole.IntersectCheck(player.GetRectangle(), true)) {
if (player.IntoHole() <= 0) {
GameOver(false);
}
}
}
/**
* Draw the world, walls, sinkholes, players.
*/
private void ImgRefresh() {
/*Draw the world, walls, sinkholes, players.*/
BACKGROUND_IMAGE.draw(Window.getWidth() / 2.0, Window.getHeight() / 2.0);
wall.Update();
hole.Update();
......@@ -122,8 +127,8 @@ public class ShadowDimension extends AbstractGame {
}
/**
* Performs a state update.
* allows the game to exit when the escape key is pressed.
* Process the corresponding keyboard input and information according
* to the game state and update the display screen.
*/
@Override
protected void update(Input input) {
......@@ -177,12 +182,12 @@ public class ShadowDimension extends AbstractGame {
}
/*Restore the original position after hitting the wall.*/
if (wall.InWallCheck(player.GetRectangle())) {
if (wall.IntersectCheck(player.GetRectangle())) {
player.SetPos(old_p);
}
/*Win check*/
if ((player.X >= 950) && (player.Y >= 670)) {
if ((player.GetPos().x >= 950) && (player.GetPos().y >= 670)) {
GameOver(true);
}
......
import bagel.Image;
import bagel.util.Rectangle;
import java.util.ArrayList;
/**
* Sinkhole Code for SWEN20003 Project 1, Semester 2, 2022
* <p>
* Manage sinkhole info.
* Draw sinkhole.
* Determine the overlap between player and sinkhole.
*
* @JIAXI3
*/
public class Sinkhole {
private final Image Img = new Image("res/sinkhole.png");
private final ArrayList Pos = new ArrayList();
private final ArrayList rect = new ArrayList();
public void Sinkhole() {
}
public void Clear() {
/*Clear all position and Rectangle list*/
Pos.clear();
rect.clear();
}
public void Update() {
/*Sinkholes will decrease after the player crashes,
so you need to clear the boundary record and re-add the ones that exist.*/
rect.clear();
/*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);
/*Record all sinkhole Rectangle for overlaps checking.*/
rect.add(Img.getBoundingBoxAt(p));
}
}
public boolean InHoldCheck(Rectangle r) {
/*Check that each hole overlaps the r(player) position*/
for (int i = 0; i < Pos.size(); i++) {
if (r.intersects((Rectangle) rect.get(i))) {
/*Remove sinkholes that players fall into.*/
Pos.remove(i);
return true;
}
}
return false;
}
public void Add(int x, int y) {
/*Add the hole position to arraylist*/
Pos.add(new bagel.util.Point(x, y));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment