[Add] Camera follows player.
This commit is contained in:
		
							parent
							
								
									566ac8e48f
								
							
						
					
					
						commit
						9f7127d9bd
					
				
							
								
								
									
										
											BIN
										
									
								
								bin/tfg/Camera.class
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								bin/tfg/Camera.class
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										35
									
								
								src/tfg/Camera.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/tfg/Camera.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,35 @@ | |||||||
|  | package tfg; | ||||||
|  | 
 | ||||||
|  | import org.jsfml.graphics.RenderWindow; | ||||||
|  | import org.jsfml.graphics.View; | ||||||
|  | import org.jsfml.system.Vector2f; | ||||||
|  | 
 | ||||||
|  | public class Camera { | ||||||
|  |     public int[] currentPos = {0, 0}; | ||||||
|  |     private static RenderWindow window; | ||||||
|  | 
 | ||||||
|  |     public Camera(RenderWindow newWindow) { | ||||||
|  |         window = newWindow; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static void SetWindow(RenderWindow newWindow) { | ||||||
|  |         window = newWindow; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     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); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static float Lerp(float x0, float x1, float m) { | ||||||
|  |         return x0 + m * (x1-x0); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static 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() {} | ||||||
|  | } | ||||||
| @ -11,8 +11,6 @@ import org.jsfml.window.Keyboard.Key; | |||||||
| import org.jsfml.window.VideoMode; | import org.jsfml.window.VideoMode; | ||||||
| import org.jsfml.window.event.Event; | import org.jsfml.window.event.Event; | ||||||
| 
 | 
 | ||||||
| import java.util.Collections; |  | ||||||
| 
 |  | ||||||
| /** | /** | ||||||
|  * TFG Game's main class. |  * TFG Game's main class. | ||||||
|  * @author Ritchie Cunningham |  * @author Ritchie Cunningham | ||||||
| @ -22,6 +20,7 @@ public class Game { | |||||||
| 	private final String renderWindowTitle = "TFG Game"; | 	private final String renderWindowTitle = "TFG Game"; | ||||||
| 	private final Vector2i renderWindowDimensions = new Vector2i(640, 480); | 	private final Vector2i renderWindowDimensions = new Vector2i(640, 480); | ||||||
| 	private Player player; | 	private Player player; | ||||||
|  | 	private Camera camera; | ||||||
| 	private boolean renderWindowFocused = true; | 	private boolean renderWindowFocused = true; | ||||||
| 	private Font pixel = new Font(); | 	private Font pixel = new Font(); | ||||||
| 	private int fps; | 	private int fps; | ||||||
| @ -47,6 +46,7 @@ public class Game { | |||||||
| 		player = new Player(); | 		player = new Player(); | ||||||
| 
 | 
 | ||||||
| 		player.changeMap(new Map(10, 10, Tile.SAND)); | 		player.changeMap(new Map(10, 10, Tile.SAND)); | ||||||
|  | 		camera = new Camera(renderWindow); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	/** | 	/** | ||||||
| @ -116,6 +116,7 @@ public class Game { | |||||||
| 
 | 
 | ||||||
| 	public void handleLogic() { | 	public void handleLogic() { | ||||||
| 		player.update(); | 		player.update(); | ||||||
|  | 		Camera.MoveTo(player.getPosition(), 0.5f); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	public void handleDrawing() { | 	public void handleDrawing() { | ||||||
|  | |||||||
| @ -71,6 +71,10 @@ public class Player implements Drawable { | |||||||
|         playerLoc = new Location(0, 0); |         playerLoc = new Location(0, 0); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     public Vector2f getPosition() { | ||||||
|  |         return playerSprite.getPosition(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     public IntRect getTextureCoords() { |     public IntRect getTextureCoords() { | ||||||
|         IntRect textureCoordsRect = new IntRect(0,0,0,0); |         IntRect textureCoordsRect = new IntRect(0,0,0,0); | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user