1212import com .james090500 .utils .SoundManager ;
1313import com .james090500 .world .ChunkStatus ;
1414import lombok .Getter ;
15+ import lombok .Setter ;
1516import org .joml .Vector3f ;
1617import org .joml .Vector3i ;
1718
@@ -32,6 +33,7 @@ public class LocalPlayer {
3233 private boolean swimming = false ;
3334
3435 Clock clock = new Clock ();
36+ private final Camera camera ;
3537 private final AABB aabb ;
3638 private final float playerWidth = 0.4f ;
3739 private final float playerHeight = 1.75f ;
@@ -40,6 +42,10 @@ public class LocalPlayer {
4042 private double jumpStartTime = 0 ;
4143 private float stepCooldown ;
4244 private String worldName ;
45+ @ Getter @ Setter
46+ private int lastChunkX ;
47+ @ Getter @ Setter
48+ private int lastChunkZ ;
4349
4450 BlockOverlay blockOverlay = new BlockOverlay ();
4551 ArmOverlay armOverlay = new ArmOverlay ();
@@ -48,10 +54,10 @@ public class LocalPlayer {
4854 public LocalPlayer () {
4955 this .aabb = new AABB (playerWidth , playerHeight );
5056 this .worldName = BlockGame .getInstance ().getWorld ().getWorldName ();
51- Camera camera = BlockGame .getInstance ().getCamera ();
57+ camera = BlockGame .getInstance ().getCamera ();
5258
5359 if (BlockGame .getInstance ().getWorld ().isRemote ()) {
54- camera . setPosition (0 , 100 , 0 );
60+ setPosition (0 , 100 , 0 );
5561 } else {
5662 File playerPath = new File ("worlds/" + worldName + "/players" );
5763 File playerData = new File (playerPath + "/player.dat" );
@@ -68,7 +74,7 @@ public LocalPlayer() {
6874 float pitch = raf .readFloat ();
6975 float yaw = raf .readFloat ();
7076
71- camera . setPosition (x , y , z );
77+ setPosition (x , y , z );
7278 camera .setRotation (pitch , yaw );
7379
7480 } catch (IOException e ) {
@@ -82,14 +88,13 @@ public void savePlayer() {
8288 if (!BlockGame .getInstance ().getWorld ().isRemote ()) {
8389 File playerPath = new File ("worlds/" + worldName + "/players" );
8490 File playerData = new File (playerPath + "/player.dat" );
85- Camera camera = BlockGame .getInstance ().getCamera ();
8691
8792 // Write to file
8893 try (RandomAccessFile raf = new RandomAccessFile (playerData , "rw" )) {
8994 raf .setLength (0 );
90- raf .writeFloat (camera . getPosition ().x );
91- raf .writeFloat (camera . getPosition ().y );
92- raf .writeFloat (camera . getPosition ().z );
95+ raf .writeFloat (getPosition ().x );
96+ raf .writeFloat (getPosition ().y );
97+ raf .writeFloat (getPosition ().z );
9398 raf .writeFloat (camera .pitch );
9499 raf .writeFloat (camera .yaw );
95100 } catch (IOException e ) {
@@ -98,6 +103,14 @@ public void savePlayer() {
98103 }
99104 }
100105
106+ public Vector3f getPosition () {
107+ return camera .getPosition ();
108+ }
109+
110+ public void setPosition (float x , float y , float z ) {
111+ camera .setPosition (x , y , z );
112+ }
113+
101114 private void updateControls () {
102115 HashMap <Integer , Boolean > keys = BlockGame .getInstance ().getClientWindow ().getClientInput ().getKeys ();
103116
@@ -113,6 +126,8 @@ private void updateControls() {
113126 * @param {number} delta - Time since last frame.
114127 */
115128 private void updateMovement (double delta ) {
129+ if (!BlockGame .getInstance ().getWorld ().isChunkStatus (lastChunkX , lastChunkZ , ChunkStatus .FINISHED )) return ;
130+
116131 float moveSpeed = 0.9f ;
117132 if (this .noclip ) {
118133 moveSpeed = 10f ;
@@ -121,11 +136,10 @@ private void updateMovement(double delta) {
121136 }
122137
123138 // Get necessary references
124- Camera camera = BlockGame . getInstance (). getCamera ();
139+ Vector3f playerPos = getPosition ();
125140 HashMap <Integer , Boolean > keys = BlockGame .getInstance ().getClientWindow ().getClientInput ().getKeys ();
126141
127142 // Current Block the player is in
128- Vector3f playerPos = new Vector3f (camera .getPosition ());
129143 playerPos .y -= 1 ;
130144 Block currentBlock = BlockGame .getInstance ().getWorld ().getBlock (playerPos );
131145
@@ -137,8 +151,8 @@ private void updateMovement(double delta) {
137151 dir .cross (new Vector3f (0 , 1 , 0 ), right );
138152
139153 // Stop playing falling through the world
140- if (camera . getPosition ().y < -30 ) {
141- camera . getPosition ().y = 100 ;
154+ if (getPosition ().y < -30 ) {
155+ getPosition ().y = 100 ;
142156 }
143157
144158 // Dampen movement
@@ -245,7 +259,7 @@ private void updateMovement(double delta) {
245259 if (velocity .lengthSquared () > 0.0004f && !this .falling && !this .jumping ) {
246260 stepCooldown -= delta ; // deltaTime is the time since last frame
247261 if (stepCooldown <= 0f ) {
248- Block blockAtFeet = BlockGame .getInstance ().getWorld ().getBlock (camera . getPosition ().sub (new Vector3f (0 , 2 , 0 )));
262+ Block blockAtFeet = BlockGame .getInstance ().getWorld ().getBlock (getPosition ().sub (new Vector3f (0 , 2 , 0 )));
249263 if (blockAtFeet != null && blockAtFeet .getSound () != null ) {
250264 SoundManager .play ("assets/sound/block/" + blockAtFeet .getSound (), 4 );
251265 stepCooldown = 0.5f ; // play every half second while moving
@@ -261,8 +275,7 @@ private void updateMovement(double delta) {
261275 * Update interaction via mouse picking
262276 */
263277 private void updateInteraction () {
264- Camera camera = BlockGame .getInstance ().getCamera ();
265- Vector3f origin = new Vector3f (camera .getPosition ());
278+ Vector3f origin = new Vector3f (getPosition ());
266279 Vector3f dir = new Vector3f (camera .getDirection ()).normalize ();
267280
268281 Vector3i [] raycast = Raycast .block (origin , dir , 5f );
@@ -312,11 +325,11 @@ public void render() {
312325 * @returns {boolean} True if the move was successful.
313326 */
314327 public boolean tryMove (Vector3f velocity , int axis ) {
315- Vector3f pos = new Vector3f (BlockGame . getInstance (). getCamera (). getPosition ());
328+ Vector3f pos = new Vector3f (getPosition ());
316329 pos .setComponent (axis ,pos .get (axis ) + velocity .get (axis ));
317330
318331 if (!aabb .isColliding (pos ) || this .noclip ) {
319- BlockGame . getInstance (). getCamera () .setPosition (axis , pos .get (axis ));
332+ camera .setPosition (axis , pos .get (axis ));
320333 return true ;
321334 }
322335 return false ;
0 commit comments