Ginger3D/src/main/java/com/halotroop/litecraft/types/entity/PlayerEntity.java

101 lines
2.9 KiB
Java
Raw Normal View History

2020-03-09 04:08:09 +00:00
package com.halotroop.litecraft.types.entity;
2019-05-26 04:05:34 +00:00
2020-02-28 03:44:46 +00:00
import org.joml.Vector3f;
2019-05-26 04:05:34 +00:00
2020-03-02 04:33:27 +00:00
import com.github.hydos.ginger.engine.common.Constants;
import com.github.hydos.ginger.engine.common.api.GingerRegister;
import com.github.hydos.ginger.engine.common.io.Window;
2020-03-03 06:27:53 +00:00
import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel;
2020-03-09 04:08:09 +00:00
import com.halotroop.litecraft.Litecraft;
import com.halotroop.litecraft.util.RelativeDirection;
import com.halotroop.litecraft.world.gen.WorldGenConstants;
2019-05-26 04:05:34 +00:00
public class PlayerEntity extends Entity implements WorldGenConstants
2020-02-25 07:00:42 +00:00
{
2019-05-26 04:05:34 +00:00
private boolean isInAir = false;
2020-02-27 19:53:02 +00:00
private double upwardsSpeed;
private boolean noWeight = true;
2020-02-28 09:42:00 +00:00
private int chunkX, chunkY, chunkZ;
2020-02-25 07:00:42 +00:00
2020-03-03 06:27:53 +00:00
public PlayerEntity(GLTexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, Vector3f scale)
2020-02-28 09:42:00 +00:00
{
super(model, position, rotX, rotY, rotZ, scale);
this.chunkX = (int) position.x >> POS_SHIFT;
this.chunkY = (int) position.y >> POS_SHIFT;
this.chunkZ = (int) position.z >> POS_SHIFT;
}
2020-02-25 07:00:42 +00:00
2020-02-28 04:58:14 +00:00
public void move(RelativeDirection direction)
2020-02-25 07:00:42 +00:00
{
2020-02-28 06:41:53 +00:00
float ry = (float) Math.toRadians(GingerRegister.getInstance().game.data.camera.getYaw());
2020-02-28 04:58:14 +00:00
switch (direction)
2020-02-25 07:00:42 +00:00
{
2020-02-28 04:58:14 +00:00
default:
case FORWARD:
2020-02-27 19:53:02 +00:00
position.z -= Math.cos(ry) * Constants.movementSpeed;
position.x += Math.sin(ry) * Constants.movementSpeed;
2020-02-28 04:58:14 +00:00
break;
case BACKWARD:
2020-02-27 19:53:02 +00:00
position.z += Math.cos(ry) * Constants.movementSpeed;
position.x -= Math.sin(ry) * Constants.movementSpeed;
2020-02-28 04:58:14 +00:00
break;
2020-02-28 08:04:35 +00:00
case LEFT:
2020-02-28 09:42:00 +00:00
ry -= RIGHT_ANGLE;
2020-02-28 08:10:54 +00:00
position.z -= Math.cos(ry) * Constants.movementSpeed;
position.x += Math.sin(ry) * Constants.movementSpeed;
2020-02-28 08:04:35 +00:00
break;
2020-02-28 04:58:14 +00:00
case RIGHT:
2020-02-28 09:42:00 +00:00
ry += RIGHT_ANGLE;
2020-02-28 08:10:54 +00:00
position.z -= Math.cos(ry) * Constants.movementSpeed;
position.x += Math.sin(ry) * Constants.movementSpeed;
2020-02-28 04:58:14 +00:00
break;
case UP:
if (this.noWeight)
position.y += Constants.movementSpeed;
2020-02-28 04:58:14 +00:00
else this.jump();
break;
case DOWN:
position.y -= Constants.movementSpeed;
break;
2020-02-26 19:59:00 +00:00
}
2020-02-25 07:00:42 +00:00
}
2020-02-28 09:42:00 +00:00
private static final float RIGHT_ANGLE = (float) (Math.PI / 2f);
2020-02-28 08:10:54 +00:00
2020-02-25 07:00:42 +00:00
private void jump()
{
if (!isInAir)
{
isInAir = true;
this.upwardsSpeed = Constants.jumpPower;
}
2019-05-26 04:05:34 +00:00
}
2020-02-28 09:42:00 +00:00
public int getChunkX()
{ return this.chunkX; }
public int getChunkY()
{ return this.chunkY; }
public int getChunkZ()
{ return this.chunkZ; }
public void updateMovement()
2020-02-25 07:00:42 +00:00
{
2020-02-25 06:47:53 +00:00
super.increasePosition(0, (float) (upwardsSpeed * (Window.getTime())), 0);
upwardsSpeed += Constants.gravity.y() * Window.getTime(); // TODO: Implement 3D gravity
isInAir = false;
upwardsSpeed = 0;
2020-02-28 09:42:00 +00:00
int newChunkX = (int) position.x >> POS_SHIFT;
int newChunkY = (int) position.y >> POS_SHIFT;
int newChunkZ = (int) position.z >> POS_SHIFT;
if (newChunkX != this.chunkX || newChunkY != this.chunkY || newChunkZ != this.chunkZ)
{
Litecraft.getInstance().getWorld().updateLoadedChunks(newChunkX, newChunkY, newChunkZ);
this.chunkX = newChunkX;
this.chunkY = newChunkY;
this.chunkZ = newChunkZ;
}
2019-05-26 04:05:34 +00:00
}
}