Select Git revision
ShadowDimension.java
ShadowDimension.java 4.91 KiB
import bagel.*;
import bagel.util.Colour;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* Skeleton Code for SWEN20003 Project 1, Semester 2, 2022
* <p>
* Init and show Welcome.
* Load CSV file.
* Get keyboard input and act accordingly.
* Game state manage.
*
* @JIAXI3
*/
public class ShadowDimension extends AbstractGame {
private final static int WINDOW_WIDTH = 1024;
private final static int WINDOW_HEIGHT = 768;
private final static String GAME_TITLE = "SHADOW DIMENSION";
private static int game_state = 0;
private final Image BACKGROUND_IMAGE = new Image("res/background0.png");
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();
public ShadowDimension() {
super(WINDOW_WIDTH, WINDOW_HEIGHT, GAME_TITLE);
}
/**
* The entry point for the program.
*/
public static void main(String[] args) {
ShadowDimension game = new ShadowDimension();
game.run();
}
/**
* Method used to read file and create objects (You can change this
* method as you wish).
*/
private void readCSV() {
String str = null;
String[] csv;
try {
/*Read CSV file*/
str = new String(Files.readAllBytes(Paths.get("res/level0.csv")));
} catch (IOException e) {
throw new RuntimeException(e);
}
/*Split csv text by \r\n or \n, get all line.*/
csv = str.split("\r\n|\n");
/*For all line*/
for (int i = 0; i < csv.length; i++) {
/*Split by ',' , get data.*/
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]);
} else if (line[0].equals("Wall")) {
/*Set wall pos*/
wall.Add(Integer.parseInt(line[1]), Integer.parseInt(line[2]));
} else if (line[0].equals("Sinkhole")) {
/*Set sinkhole pos*/
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]);
} 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]);
}
//System.out.println(csv[i]);
}
}
/**
* Performs a state update.
* allows the game to exit when the escape key is pressed.
*/
@Override
protected void update(Input input) {
/*State for game
* 0:Init and show welcom page
* 1:Show Play page
* */
switch (game_state) {
case 0: {
/*Draw Text*/
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,
(Window.getHeight() - 80 + 190) / 2);
/*Wait for space key*/
if (input.wasPressed(Keys.SPACE)) {
/*Change game state to 1(play page)*/
game_state = 1;
/*Read csv file and get info of player & wall & hole & world size*/
readCSV();
}
break;
}
case 1: {
/*Get key input and move player*/
if (input.isDown(Keys.UP)) {
player.MoveUp();
} else if (input.isDown(Keys.DOWN)) {
player.MoveDown();
} else if (input.isDown(Keys.RIGHT)) {
player.MoveRight();
} else if (input.isDown(Keys.LEFT)) {
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: {
break;
}
}
/*Game exit*/
if (input.wasPressed(Keys.ESCAPE)) {
Window.close();
}
}
}