55import com .james090500 .blocks .Blocks ;
66import com .james090500 .renderer .gui .ArmOverlay ;
77import com .james090500 .renderer .gui .BlockOverlay ;
8+ import com .james090500 .renderer .gui .CrosshairOverlay ;
9+ import com .james090500 .utils .AABB ;
810import com .james090500 .utils .Clock ;
911import com .james090500 .utils .SoundManager ;
1012import lombok .Getter ;
@@ -28,8 +30,9 @@ public class LocalPlayer {
2830 private boolean swimming = false ;
2931
3032 Clock clock = new Clock ();
33+ private final AABB aabb ;
3134 private final float playerWidth = 0.4f ;
32- private final float playerHeight = 1.5f ;
35+ private final float playerHeight = 1.75f ;
3336 private final Vector3f velocity = new Vector3f ();
3437 private final Vector3f fallVelocity = new Vector3f ();
3538 private double jumpStartTime = 0 ;
@@ -38,50 +41,58 @@ public class LocalPlayer {
3841
3942 BlockOverlay blockOverlay = new BlockOverlay ();
4043 ArmOverlay armOverlay = new ArmOverlay ();
44+ CrosshairOverlay crosshairOverlay = new CrosshairOverlay ();
4145
42- public LocalPlayer (String worldName ) {
43- this .worldName = worldName ;
44-
45- File playerPath = new File ("worlds/" + worldName + "/players" );
46- File playerData = new File (playerPath + "/player.dat" );
46+ public LocalPlayer () {
47+ this .aabb = new AABB (playerWidth , playerHeight );
48+ this .worldName = BlockGame .getInstance ().getWorld ().getWorldName ();
4749 Camera camera = BlockGame .getInstance ().getCamera ();
4850
49- if (!playerPath .exists ()) {
50- playerPath .mkdirs ();
51-
52- savePlayer ();
51+ if (BlockGame .getInstance ().getWorld ().isRemote ()) {
52+ camera .setPosition (0 , 100 , 0 );
5353 } else {
54- try (RandomAccessFile raf = new RandomAccessFile (playerData , "rw" )) {
55- float x = raf .readFloat ();
56- float y = raf .readFloat ();
57- float z = raf .readFloat ();
58- float pitch = raf .readFloat ();
59- float yaw = raf .readFloat ();
54+ File playerPath = new File ("worlds/" + worldName + "/players" );
55+ File playerData = new File (playerPath + "/player.dat" );
6056
61- camera . setPosition ( x , y , z );
62- camera . setRotation ( pitch , yaw );
57+ if (! playerPath . exists ()) {
58+ playerPath . mkdirs ( );
6359
64- } catch (IOException e ) {
65- e .printStackTrace ();
60+ savePlayer ();
61+ } else {
62+ try (RandomAccessFile raf = new RandomAccessFile (playerData , "rw" )) {
63+ float x = raf .readFloat ();
64+ float y = raf .readFloat ();
65+ float z = raf .readFloat ();
66+ float pitch = raf .readFloat ();
67+ float yaw = raf .readFloat ();
68+
69+ camera .setPosition (x , y , z );
70+ camera .setRotation (pitch , yaw );
71+
72+ } catch (IOException e ) {
73+ e .printStackTrace ();
74+ }
6675 }
6776 }
6877 }
6978
7079 public void savePlayer () {
71- File playerPath = new File ("worlds/" + worldName + "/players" );
72- File playerData = new File (playerPath + "/player.dat" );
73- Camera camera = BlockGame .getInstance ().getCamera ();
80+ if (!BlockGame .getInstance ().getWorld ().isRemote ()) {
81+ File playerPath = new File ("worlds/" + worldName + "/players" );
82+ File playerData = new File (playerPath + "/player.dat" );
83+ Camera camera = BlockGame .getInstance ().getCamera ();
7484
75- // Write to file
76- try (RandomAccessFile raf = new RandomAccessFile (playerData , "rw" )) {
77- raf .setLength (0 );
78- raf .writeFloat (camera .getPosition ().x );
79- raf .writeFloat (camera .getPosition ().y );
80- raf .writeFloat (camera .getPosition ().z );
81- raf .writeFloat (camera .pitch );
82- raf .writeFloat (camera .yaw );
83- } catch (IOException e ) {
84- e .printStackTrace ();
85+ // Write to file
86+ try (RandomAccessFile raf = new RandomAccessFile (playerData , "rw" )) {
87+ raf .setLength (0 );
88+ raf .writeFloat (camera .getPosition ().x );
89+ raf .writeFloat (camera .getPosition ().y );
90+ raf .writeFloat (camera .getPosition ().z );
91+ raf .writeFloat (camera .pitch );
92+ raf .writeFloat (camera .yaw );
93+ } catch (IOException e ) {
94+ e .printStackTrace ();
95+ }
8596 }
8697 }
8798
@@ -216,19 +227,16 @@ private void updateMovement(double delta) {
216227
217228 float yVelocity = (float ) (this .fallVelocity .y * delta );
218229
219- // Half the players width
220- float halfWidth = this .playerWidth / 2 ;
221-
222- if (!this .tryMove (new Vector3f (0 , yVelocity , 0 ), 1 , halfWidth , this .playerHeight )) {
230+ if (!this .tryMove (new Vector3f (0 , yVelocity , 0 ), 1 )) {
223231 this .fallVelocity .y = 0 ;
224232 this .falling = false ;
225233 } else if (!this .noclip ) {
226234 this .falling = true ;
227235 }
228236
229237 // Try horizontal movement along X and Z axes
230- boolean canMove1 = this .tryMove (this .velocity , 0 , halfWidth , this . playerHeight );
231- boolean canMove2 = this .tryMove (this .velocity , 2 , halfWidth , this . playerHeight );
238+ boolean canMove1 = this .tryMove (this .velocity , 0 );
239+ boolean canMove2 = this .tryMove (this .velocity , 2 );
232240
233241 if (canMove1 && canMove2 ) {
234242 // Play the movement sound
@@ -237,8 +245,7 @@ private void updateMovement(double delta) {
237245 if (stepCooldown <= 0f ) {
238246 Block blockAtFeet = BlockGame .getInstance ().getWorld ().getBlock (camera .getPosition ().sub (new Vector3f (0 , 2 , 0 )));
239247 if (blockAtFeet != null && blockAtFeet .getSound () != null ) {
240- int sound = 1 + (int ) (Math .random () * 4 );
241- SoundManager .play ("assets/sound/block/" + blockAtFeet .getSound () + sound + ".ogg" );
248+ SoundManager .play ("assets/sound/block/" + blockAtFeet .getSound (), 4 );
242249 stepCooldown = 0.5f ; // play every half second while moving
243250 }
244251 }
@@ -250,9 +257,8 @@ private void updateMovement(double delta) {
250257
251258 /**
252259 * Update interaction via mouse picking
253- * @param delta
254260 */
255- private void updateInteraction (double delta ) {
261+ private void updateInteraction () {
256262 Camera camera = BlockGame .getInstance ().getCamera ();
257263 Vector3f origin = new Vector3f (camera .getPosition ());
258264 Vector3f dir = new Vector3f (camera .getDirection ()).normalize ();
@@ -267,52 +273,26 @@ private void updateInteraction(double delta) {
267273
268274 HashMap <Integer , Boolean > mouse = BlockGame .getInstance ().getClientWindow ().getClientInput ().getMouse ();
269275 if (mouse .getOrDefault (GLFW_MOUSE_BUTTON_LEFT , false )) {
276+ Block currentBlock = BlockGame .getInstance ().getWorld ().getBlock (raycast [1 ].x , raycast [1 ].y , raycast [1 ].z );
270277 BlockGame .getInstance ().getWorld ().setBlock (raycast [1 ].x , raycast [1 ].y , raycast [1 ].z , (byte ) 0 );
278+ SoundManager .play ("assets/sound/block/" + currentBlock .getSound (), 4 );
271279 mouse .put (GLFW_MOUSE_BUTTON_LEFT , false );
272280 }
273281
274282 if (mouse .getOrDefault (GLFW_MOUSE_BUTTON_RIGHT , false )) {
275- BlockGame .getInstance ().getWorld ().setBlock (raycast [0 ].x , raycast [0 ].y , raycast [0 ].z , (byte ) this .currentBlock );
283+ if (!aabb .isColliding (origin , raycast [0 ])) {
284+ Block currentBlock = Blocks .ids [this .currentBlock ];
285+ BlockGame .getInstance ().getWorld ().setBlock (raycast [0 ].x , raycast [0 ].y , raycast [0 ].z , currentBlock .getId ());
286+ SoundManager .play ("assets/sound/block/" + currentBlock .getSound (), 4 );
287+ }
276288 mouse .put (GLFW_MOUSE_BUTTON_RIGHT , false );
277289 }
278-
279- // if(mouse.getOrDefault(GLFW_MOUSE_))
280290 }
281291 }
282292
283293 public void render () {
284294 armOverlay .render ();
285- }
286-
287- /**
288- * Checks if the player's AABB collides with any solid blocks in the world.
289- * @param {Vector3} min - Minimum bounds of the AABB.
290- * @param {Vector3} max - Maximum bounds of the AABB.
291- * @param {Object} futureBlock - Are we attempting to place a block?
292- * @returns {boolean} If the player is colliding
293- */
294- private boolean isAABBColliding (Vector3f min , Vector3f max ) {
295- for (int x = (int ) Math .floor (min .x ); x <= Math .floor (max .x ); x ++) {
296- for (int y = (int ) Math .floor (min .y ); y <= Math .floor (max .y ); y ++) {
297- for (int z = (int ) Math .floor (min .z ); z <= Math .floor (max .z ); z ++) {
298- // if (futureBlock) {
299- // if (
300- // futureBlock[0] === x &&
301- // futureBlock[1] === y &&
302- // futureBlock[2] === z
303- // ) {
304- // return true
305- // }
306- // }
307-
308- Block block = BlockGame .getInstance ().getWorld ().getBlock (x , y , z );
309- if (block != null && block .isSolid ()) {
310- return true ;
311- }
312- }
313- }
314- }
315- return false ;
295+ crosshairOverlay .render ();
316296 }
317297
318298 /**
@@ -325,19 +305,11 @@ private boolean isAABBColliding(Vector3f min, Vector3f max) {
325305 * @param {Object} world - The world object to query blocks from.
326306 * @returns {boolean} True if the move was successful.
327307 */
328- public boolean tryMove (Vector3f velocity , int axis , float halfWidth , float playerHeight ) {
308+ public boolean tryMove (Vector3f velocity , int axis ) {
329309 Vector3f pos = new Vector3f (BlockGame .getInstance ().getCamera ().getPosition ());
330310 pos .setComponent (axis ,pos .get (axis ) + velocity .get (axis ));
331311
332- Vector3f min = new Vector3f (
333- pos .x - halfWidth ,
334- pos .y - playerHeight ,
335- pos .z - halfWidth
336- );
337-
338- Vector3f max = new Vector3f (pos .x + halfWidth , pos .y , pos .z + halfWidth );
339-
340- if (!this .isAABBColliding (min , max ) || this .noclip ) {
312+ if (!aabb .isColliding (pos ) || this .noclip ) {
341313 BlockGame .getInstance ().getCamera ().setPosition (axis , pos .get (axis ));
342314 return true ;
343315 }
@@ -428,7 +400,7 @@ private float intBound(float s, float ds) {
428400
429401 public void update (double delta ) {
430402 this .updateControls ();
431- this .updateInteraction (delta );
403+ this .updateInteraction ();
432404 this .updateMovement (delta );
433405 }
434406
0 commit comments