Initial commit.

This commit is contained in:
Ritchie Cunningham 2022-03-06 23:44:26 +00:00
commit 6582bec5bc
12 changed files with 241 additions and 0 deletions

11
.classpath Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/Users/user/Downloads/jsfml/jsfml.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

28
.project Normal file
View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Game</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<filteredResources>
<filter>
<id>1646606958986</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>

View File

@ -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

BIN
bin/tfg/Direction.class Normal file

Binary file not shown.

BIN
bin/tfg/Game.class Normal file

Binary file not shown.

BIN
bin/tfg/Location.class Normal file

Binary file not shown.

BIN
bin/tfg/Player.class Normal file

Binary file not shown.

BIN
player.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

3
src/tfg/Direction.java Normal file
View File

@ -0,0 +1,3 @@
package tfg;
enum Direction { NORTH, SOUTH, EAST, WEST, NORTH_EAST, NORTH_WEST, SOUTH_EAST, SOUTH_WEST };

71
src/tfg/Game.java Normal file
View File

@ -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);
}
}

57
src/tfg/Location.java Normal file
View File

@ -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;
}
}

57
src/tfg/Player.java Normal file
View File

@ -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;
}
}