diff --git a/bin/tfg/Camera.class b/bin/tfg/Camera.class index 4034950..aefc46a 100644 Binary files a/bin/tfg/Camera.class and b/bin/tfg/Camera.class differ diff --git a/bin/tfg/Game.class b/bin/tfg/Game.class index 531999c..b650aa9 100644 Binary files a/bin/tfg/Game.class and b/bin/tfg/Game.class differ diff --git a/bin/tfg/Player.class b/bin/tfg/Player.class index 47c0638..39ac2b5 100644 Binary files a/bin/tfg/Player.class and b/bin/tfg/Player.class differ diff --git a/src/tfg/Camera.java b/src/tfg/Camera.java index 0b735af..aa7bb87 100644 --- a/src/tfg/Camera.java +++ b/src/tfg/Camera.java @@ -4,32 +4,61 @@ import org.jsfml.graphics.RenderWindow; import org.jsfml.graphics.View; import org.jsfml.system.Vector2f; +/** + * Camera that can center on various objects. + * @author Ritchie Cunningham + */ public class Camera { - public int[] currentPos = {0, 0}; - private static RenderWindow window; + /** + * The window the camera is affecting. + */ + private RenderWindow window; - public Camera(RenderWindow newWindow) { - window = newWindow; + /** + * Default view. + */ + private View defaultView; + + /** + * Create a new camera for window w. + * @param w Window for which to create the camera. + */ + public Camera(RenderWindow w) { + window = w; + defaultView = (View) window.getDefaultView(); } - public static void SetWindow(RenderWindow newWindow) { - window = newWindow; + /** + * Center the camera on the following position. + * @param pos Position to center the camera on. + * @param step + */ + public void centerOn(Vector2f pos, float step) { + View newView = new View(defaultView.getCenter(), defaultView.getSize()); + newView.setCenter(pos); + window.setView(newView); } - public static void MoveTo(Vector2f toLoc, float step) { - View defaultView = (View) window.getDefaultView(); - View view = new View(vectorLerp(defaultView.getCenter(), - toLoc, step), defaultView.getSize()); - window.setView(view); + /** + * Center the camera on the default view. + */ + public void centerOnDefault() { + window.setView(defaultView); } - public static float Lerp(float x0, float x1, float m) { + + /** + * + * @param x0 + * @param x1 + * @param m + * @return + */ + public float Lerp(float x0, float x1, float m) { return x0 + m * (x1-x0); } - public static Vector2f vectorLerp(Vector2f v0, Vector2f v1, float m) { + public Vector2f vectorLerp(Vector2f v0, Vector2f v1, float m) { return new Vector2f(Lerp(v0.x, v1.x, m), Lerp(v0.y, v1.y, m)); } - - public static void Rotate() {} } diff --git a/src/tfg/Game.java b/src/tfg/Game.java index 42e5b15..26ae0ed 100644 --- a/src/tfg/Game.java +++ b/src/tfg/Game.java @@ -4,6 +4,7 @@ import org.jsfml.graphics.Color; import org.jsfml.graphics.RenderWindow; import org.jsfml.graphics.TextStyle; import org.jsfml.system.Clock; +import org.jsfml.system.Vector2f; import org.jsfml.system.Vector2i; import org.jsfml.window.Keyboard; import org.jsfml.window.Keyboard.Key; @@ -42,6 +43,11 @@ public class Game { * Repesents whether or not the user has the window opened and in focus. */ private boolean windowFocus = true; + + /** + * Allows the window to shift focus. + */ + private Camera camera; /** @@ -60,6 +66,7 @@ public class Game { window.create(new VideoMode(windowDimensions.x, windowDimensions.y), windowTitle); player.changeMap(new Map(10, 10, Tile.SAND)); + camera = new Camera(window); } /** @@ -151,8 +158,11 @@ public class Game { /* The window has automatic double-buffering. */ window.clear(); /* Draw each object like layers, background to foreground. */ + Vector2f playerPos = player.getSprite().getPosition(); + camera.centerOn(playerPos, 0); window.draw(player.getMap()); window.draw(player); + camera.centerOnDefault(); window.draw(fpsUI); window.display(); } diff --git a/src/tfg/Player.java b/src/tfg/Player.java index 7b8ae73..47c49ab 100644 --- a/src/tfg/Player.java +++ b/src/tfg/Player.java @@ -101,7 +101,7 @@ public class Player extends Entity implements Drawable { * e.g. NORTH would return the location above the current location. */ Location newLoc = entityLoc.getRelativeLocation(d); - playerLoc.setDirection(d); + entityLoc.setDirection(d); if(currentMap.isValidLocation(newLoc)) { currentAction = PlayerAction.MOVING; entityLoc.setDirection(d); @@ -130,6 +130,14 @@ public class Player extends Entity implements Drawable { } } + /** + * Get animated sprite. + * @return Animated sprite. + */ + public Sprite getSprite() { + return playerSprite.getSprite(); + } + /** * Draw the player on screen. */