commit 6582bec5bcd057269b3e45baaca7f3f15231cafd Author: Ritchie Cunningham Date: Sun Mar 6 23:44:26 2022 +0000 Initial commit. diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..d82323d --- /dev/null +++ b/.classpath @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..da7a034 --- /dev/null +++ b/.project @@ -0,0 +1,28 @@ + + + Game + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + + + 1646606958986 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..8c9943d --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=17 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=17 diff --git a/bin/tfg/Direction.class b/bin/tfg/Direction.class new file mode 100644 index 0000000..419a0dc Binary files /dev/null and b/bin/tfg/Direction.class differ diff --git a/bin/tfg/Game.class b/bin/tfg/Game.class new file mode 100644 index 0000000..3ba635a Binary files /dev/null and b/bin/tfg/Game.class differ diff --git a/bin/tfg/Location.class b/bin/tfg/Location.class new file mode 100644 index 0000000..762ca0f Binary files /dev/null and b/bin/tfg/Location.class differ diff --git a/bin/tfg/Player.class b/bin/tfg/Player.class new file mode 100644 index 0000000..bf7d789 Binary files /dev/null and b/bin/tfg/Player.class differ diff --git a/player.png b/player.png new file mode 100644 index 0000000..9f47ee9 Binary files /dev/null and b/player.png differ diff --git a/src/tfg/Direction.java b/src/tfg/Direction.java new file mode 100644 index 0000000..6c46e0d --- /dev/null +++ b/src/tfg/Direction.java @@ -0,0 +1,3 @@ +package tfg; + +enum Direction { NORTH, SOUTH, EAST, WEST, NORTH_EAST, NORTH_WEST, SOUTH_EAST, SOUTH_WEST }; diff --git a/src/tfg/Game.java b/src/tfg/Game.java new file mode 100644 index 0000000..47771a3 --- /dev/null +++ b/src/tfg/Game.java @@ -0,0 +1,71 @@ +package tfg; + +import org.jsfml.graphics.Color; +import org.jsfml.graphics.RenderWindow; +import org.jsfml.system.Vector2i; +import org.jsfml.window.VideoMode; +import org.jsfml.window.event.Event; +import org.jsfml.window.event.KeyEvent; + + +public class Game { + private RenderWindow window = new RenderWindow(); + private final String windowTitle = "TFG Game"; + private final Vector2i screenResolution = new Vector2i(640, 480); + + private Player player = new Player(); + + public static void main(String[] args) { + Game g = new Game(); /* Create temp object of self. */ + g.run(); /* Invoke run. */ + } + + /** + * Run the game. + */ + public void run() { + initialize(); + + /* Main loop. */ + while(window.isOpen()) { + window.clear(Color.BLACK); + window.draw(player.getSprite()); + window.display(); + + /* Event handler. */ + for(Event event : window.pollEvents()) { + if(event.type == Event.Type.CLOSED) { + /* User clicked window close. */ + window.close(); + } else if(event.type == Event.Type.KEY_PRESSED) { + KeyEvent keyEv = event.asKeyEvent(); + switch(keyEv.key) { + case ESCAPE: + window.close(); + break; + case W: + player.move(Direction.NORTH); + break; + case S: + player.move(Direction.SOUTH); + break; + case A: + player.move(Direction.WEST); + break; + case D: + player.move(Direction.EAST); + } + } + } + } + } + + /** + * Configure settings once at startup. + */ + public void initialize() { + window.create(new VideoMode(screenResolution.x, screenResolution.y), + windowTitle); + } + +} \ No newline at end of file diff --git a/src/tfg/Location.java b/src/tfg/Location.java new file mode 100644 index 0000000..c97d712 --- /dev/null +++ b/src/tfg/Location.java @@ -0,0 +1,57 @@ +package tfg; +import org.jsfml.system.Vector2i; + +public class Location { + private Vector2i position = new Vector2i(0,0); + private Direction facing = Direction.SOUTH; + + /** + * Create new location at x, y. + * @param x Starting position. + * @param y Starting position. + */ + public Location(int x, int y) { + position = new Vector2i(x,y); + } + + /** + * Get x Coordinate. + * @return x + */ + public int getX() { + return position.x; + } + + /** + * Get y coordinate. + * @return y + */ + public int getY() { + return position.y; + } + + /** + * Get Direction facing. + * @return direction + */ + public Direction getDirection() { + return facing; + } + + /** + * Update Position. + * @param x + * @param y + */ + public void setPosition(int x, int y) { + position = new Vector2i(x,y); + } + + /** + * Update direction. + * @param dir + */ + public void setDirection(Direction dir) { + facing = dir; + } +} diff --git a/src/tfg/Player.java b/src/tfg/Player.java new file mode 100644 index 0000000..9f8477e --- /dev/null +++ b/src/tfg/Player.java @@ -0,0 +1,57 @@ +package tfg; + +import java.io.IOException; +import java.nio.file.Paths; + +import org.jsfml.graphics.IntRect; +import org.jsfml.graphics.Sprite; +import org.jsfml.graphics.Texture; +import org.jsfml.system.Vector2f; + +public class Player { + private Location loc = new Location(0,0); + private Sprite sprite; + + /** + * Create a new player. + * Load texture and create sprite. + */ + public Player() { + Texture playerTex = new Texture(); + try { + playerTex.loadFromFile(Paths.get("player.png")); + } catch(IOException ex) { + ex.printStackTrace(); + } + sprite = new Sprite(playerTex); + } + + public void move(Direction dir) { + loc.setDirection(dir); + IntRect texRect = new IntRect(0,0,0,0); + switch(dir) { + case NORTH: + texRect = new IntRect(0, 144, 32, 48); + loc.setPosition(loc.getX(), loc.getY()-1); + break; + case SOUTH: + texRect = new IntRect(0, 0, 32, 48); + loc.setPosition(loc.getX(), loc.getY()+1); + break; + case WEST: + texRect = new IntRect(0, 48, 32, 48); + loc.setPosition(loc.getX()-1, loc.getY()); + break; + case EAST: + texRect = new IntRect(0, 96, 32, 48); + loc.setPosition(loc.getX()+1, loc.getY()); + break; + } + sprite.setTextureRect(texRect); + sprite.setPosition(new Vector2f(loc.getX() * 32, loc.getY()*32)); + } + + public Sprite getSprite() { + return sprite; + } +}