[Change] Documented Camera

This commit is contained in:
Ritchie Cunningham 2022-03-08 22:20:43 +00:00
parent 7eb171b491
commit 4b2803c7b9
6 changed files with 63 additions and 16 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -4,32 +4,61 @@ import org.jsfml.graphics.RenderWindow;
import org.jsfml.graphics.View; import org.jsfml.graphics.View;
import org.jsfml.system.Vector2f; import org.jsfml.system.Vector2f;
/**
* Camera that can center on various objects.
* @author Ritchie Cunningham
*/
public class Camera { 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(); * Center the camera on the default view.
View view = new View(vectorLerp(defaultView.getCenter(), */
toLoc, step), defaultView.getSize()); public void centerOnDefault() {
window.setView(view); 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); 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)); return new Vector2f(Lerp(v0.x, v1.x, m), Lerp(v0.y, v1.y, m));
} }
public static void Rotate() {}
} }

View File

@ -4,6 +4,7 @@ import org.jsfml.graphics.Color;
import org.jsfml.graphics.RenderWindow; import org.jsfml.graphics.RenderWindow;
import org.jsfml.graphics.TextStyle; import org.jsfml.graphics.TextStyle;
import org.jsfml.system.Clock; import org.jsfml.system.Clock;
import org.jsfml.system.Vector2f;
import org.jsfml.system.Vector2i; import org.jsfml.system.Vector2i;
import org.jsfml.window.Keyboard; import org.jsfml.window.Keyboard;
import org.jsfml.window.Keyboard.Key; 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. * Repesents whether or not the user has the window opened and in focus.
*/ */
private boolean windowFocus = true; 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), window.create(new VideoMode(windowDimensions.x, windowDimensions.y),
windowTitle); windowTitle);
player.changeMap(new Map(10, 10, Tile.SAND)); 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. */ /* The window has automatic double-buffering. */
window.clear(); window.clear();
/* Draw each object like layers, background to foreground. */ /* Draw each object like layers, background to foreground. */
Vector2f playerPos = player.getSprite().getPosition();
camera.centerOn(playerPos, 0);
window.draw(player.getMap()); window.draw(player.getMap());
window.draw(player); window.draw(player);
camera.centerOnDefault();
window.draw(fpsUI); window.draw(fpsUI);
window.display(); window.display();
} }

View File

@ -101,7 +101,7 @@ public class Player extends Entity implements Drawable {
* e.g. NORTH would return the location above the current location. * e.g. NORTH would return the location above the current location.
*/ */
Location newLoc = entityLoc.getRelativeLocation(d); Location newLoc = entityLoc.getRelativeLocation(d);
playerLoc.setDirection(d); entityLoc.setDirection(d);
if(currentMap.isValidLocation(newLoc)) { if(currentMap.isValidLocation(newLoc)) {
currentAction = PlayerAction.MOVING; currentAction = PlayerAction.MOVING;
entityLoc.setDirection(d); 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. * Draw the player on screen.
*/ */