partway to iron sight

This commit is contained in:
austin 2022-03-23 00:23:59 -04:00
parent aa64f8f002
commit 1c0ea25fb5
12 changed files with 251 additions and 64 deletions

View File

@ -1,3 +1,4 @@
{
"java.configuration.updateBuildConfiguration": "automatic"
"java.configuration.updateBuildConfiguration": "automatic",
"java.jdt.ls.vmargs": "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx2G -Xms100m"
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -0,0 +1,90 @@
package electrosphere.controls;
import org.joml.Vector3d;
import org.joml.Vector3f;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.types.camera.CameraEntityUtils;
import electrosphere.game.client.targeting.crosshair.Crosshair;
import electrosphere.main.Globals;
import electrosphere.main.Main;
import electrosphere.renderer.ui.events.MouseEvent;
public class CameraHandler {
float mouseSensitivity = .1f;
float cameraSpeed;
float yaw = 150;
float pitch = 50;
Vector3f cameraRotationVector = new Vector3f();
Vector3f radialOffset = new Vector3f(0,1,0);
public void handleMouseEvent(MouseEvent event){
if(Globals.controlHandler != null && !Globals.controlHandler.isMouseVisible()){
yaw = yaw + event.getDeltaX() * mouseSensitivity;
pitch = pitch - event.getDeltaY() * mouseSensitivity;
if (pitch > 100.0f) {
pitch = 100.0f;
}
if (pitch < -99.0f) {
pitch = -99.0f;
}
}
updateGlobalCamera();
}
public void updateRadialOffset(Vector3f offset){
radialOffset = offset;
}
public void updateGlobalCamera(){
cameraSpeed = 2.5f * Main.deltaTime;
if(Crosshair.getCrosshairActive()){
// if(Globals.playerCharacter != null){
// Vector3d charPos = EntityUtils.getPosition(Globals.playerCharacter);
// CameraEntityUtils.setCameraCenter(Globals.playerCamera, new Vector3f((float)charPos.x,(float)charPos.y,(float)charPos.z));
// }
Vector3d characterPos = EntityUtils.getPosition(Globals.playerCharacter);
Vector3d targetPos = Crosshair.getTargetPosition();
Vector3d diffed = new Vector3d(targetPos).sub(characterPos).mul(-1).normalize();
cameraRotationVector.set((float)diffed.x, 0.5f, (float)diffed.z).normalize();
yaw = (float)Math.toDegrees(Math.atan2(diffed.z, diffed.x));
CameraEntityUtils.setCameraPitch(Globals.playerCamera, pitch);
CameraEntityUtils.setCameraYaw(Globals.playerCamera, yaw);
} else {
CameraEntityUtils.setCameraPitch(Globals.playerCamera, pitch);
CameraEntityUtils.setCameraYaw(Globals.playerCamera, yaw);
// if(Globals.playerCharacter != null){
// Vector3d charPos = EntityUtils.getPosition(Globals.playerCharacter);
// CameraEntityUtils.setCameraCenter(Globals.playerCamera, new Vector3f((float)charPos.x,(float)charPos.y,(float)charPos.z));
// }
cameraRotationVector.x = 0 + (float) Math.cos(yaw / 180.0f * Math.PI) * 1;
cameraRotationVector.y = 0 + (float) Math.sin(pitch / 180.0f * Math.PI) * 1;
cameraRotationVector.z = 0 + (float) Math.sin(yaw / 180.0f * Math.PI) * 1;
cameraRotationVector.normalize();
}
//update view matrix offset
float xFactor = (float)Math.cos(yaw / 180.0f * Math.PI);
float yFactor = (float)Math.sin(yaw / 180.0f * Math.PI);
// Vector3f radialOffset = CameraEntityUtils.getOrbitalCameraRadialOffset(Globals.playerCamera);
Vector3f trueOffset = new Vector3f(radialOffset).mul(xFactor,1.0f,yFactor);
CameraEntityUtils.setOrbitalCameraRadialOffset(Globals.playerCamera, trueOffset);
// float cam_Player_Orbit_Magnitude = CameraEntityUtils.getCameraOrbitRadius(Globals.playerCamera);
cameraRotationVector.mul(CameraEntityUtils.getOrbitalCameraDistance(Globals.playerCamera));
CameraEntityUtils.setCameraEye(Globals.playerCamera, cameraRotationVector);
Globals.viewMatrix = CameraEntityUtils.getCameraViewMatrix(Globals.playerCamera);
}
}

View File

@ -10,6 +10,7 @@ import electrosphere.entity.state.AttackTree;
import electrosphere.entity.state.equip.EquipState;
import electrosphere.entity.state.inventory.InventoryUtils;
import electrosphere.entity.state.inventory.UnrelationalInventoryState;
import electrosphere.entity.state.ironsight.IronSightTree;
import electrosphere.entity.state.movement.GroundMovementTree;
import electrosphere.entity.state.movement.GroundMovementTree.MovementRelativeFacing;
import electrosphere.entity.state.movement.GroundMovementTree.MovementTreeState;
@ -72,6 +73,7 @@ public class ControlHandler {
public static final String INPUT_CODE_DROP = "drop";
public static final String INPUT_CODE_INVENTORY_OPEN = "inventoryOpen";
public static final String INPUT_CODE_CHARACTER_OPEN = "characterOpen";
public static final String INPUT_CODE_IRON_SIGHT = "ironSight";
public static final String DATA_STRING_INPUT_CODE_MENU_INCREMENT = "menuIncrement";
public static final String DATA_STRING_INPUT_CODE_MENU_DECREMENT = "menuDecrement";
@ -187,12 +189,13 @@ public class ControlHandler {
handler.addControl(DATA_STRING_INPUT_CODE_MOVEMENT_FALL, new Control(ControlType.KEY,GLFW_KEY_LEFT_CONTROL));
handler.addControl(DATA_STRING_INPUT_CODE_ATTACK_PRIMARY, new Control(ControlType.MOUSE_BUTTON,GLFW_MOUSE_BUTTON_LEFT));
handler.addControl(DATA_STRING_INPUT_CODE_IN_GAME_MAIN_MENU, new Control(ControlType.KEY,GLFW_KEY_ESCAPE));
handler.addControl(DATA_STRING_INPUT_CODE_LOCK_CROSSHAIR, new Control(ControlType.MOUSE_BUTTON,GLFW_MOUSE_BUTTON_RIGHT));
handler.addControl(DATA_STRING_INPUT_CODE_LOCK_CROSSHAIR, new Control(ControlType.KEY,GLFW_KEY_CAPS_LOCK));
handler.addControl(INPUT_CODE_SPRINT, new Control(ControlType.KEY,GLFW_KEY_LEFT_SHIFT));
handler.addControl(INPUT_CODE_INTERACT, new Control(ControlType.KEY,GLFW_KEY_E));
handler.addControl(INPUT_CODE_DROP, new Control(ControlType.KEY,GLFW_KEY_Y));
handler.addControl(INPUT_CODE_INVENTORY_OPEN, new Control(ControlType.KEY,GLFW_KEY_I));
handler.addControl(INPUT_CODE_CHARACTER_OPEN, new Control(ControlType.KEY,GLFW_KEY_C));
handler.addControl(INPUT_CODE_IRON_SIGHT, new Control(ControlType.MOUSE_BUTTON,GLFW_MOUSE_BUTTON_RIGHT));
/*
Map the menu navigation controls
@ -332,64 +335,8 @@ public class ControlHandler {
Camera rotation
*/
mainGameControlList.add(controls.get(INPUT_CODE_CAMERA_ROTATION));
controls.get(INPUT_CODE_CAMERA_ROTATION).setOnMove(new Control.MouseCallback(){
float mouseSensitivity = .1f;
float cameraSpeed;
float yaw = 150;
float pitch = 50;
Vector3f cameraRotationVector = new Vector3f();
public void execute(MouseEvent event){
cameraSpeed = 2.5f * Main.deltaTime;
if(Globals.controlHandler != null && !Globals.controlHandler.isMouseVisible()){
yaw = yaw + event.getDeltaX() * mouseSensitivity;
pitch = pitch - event.getDeltaY() * mouseSensitivity;
if (pitch > 100.0f) {
pitch = 100.0f;
}
if (pitch < -99.0f) {
pitch = -99.0f;
}
}
if(Crosshair.getCrosshairActive()){
// if(Globals.playerCharacter != null){
// Vector3d charPos = EntityUtils.getPosition(Globals.playerCharacter);
// CameraEntityUtils.setCameraCenter(Globals.playerCamera, new Vector3f((float)charPos.x,(float)charPos.y,(float)charPos.z));
// }
Vector3d characterPos = EntityUtils.getPosition(Globals.playerCharacter);
Vector3d targetPos = Crosshair.getTargetPosition();
Vector3d diffed = new Vector3d(targetPos).sub(characterPos).mul(-1).normalize();
cameraRotationVector.set((float)diffed.x, 0.5f, (float)diffed.z).normalize();
yaw = (float)Math.toDegrees(Math.atan2(diffed.z, diffed.x));
CameraEntityUtils.setCameraPitch(Globals.playerCamera, pitch);
CameraEntityUtils.setCameraYaw(Globals.playerCamera, yaw);
} else {
CameraEntityUtils.setCameraPitch(Globals.playerCamera, pitch);
CameraEntityUtils.setCameraYaw(Globals.playerCamera, yaw);
// if(Globals.playerCharacter != null){
// Vector3d charPos = EntityUtils.getPosition(Globals.playerCharacter);
// CameraEntityUtils.setCameraCenter(Globals.playerCamera, new Vector3f((float)charPos.x,(float)charPos.y,(float)charPos.z));
// }
float cam_Player_Orbit_Magnitude = 5f;
cameraRotationVector.x = 0 + (float) Math.cos(yaw / 180.0f * Math.PI) * cam_Player_Orbit_Magnitude;
cameraRotationVector.y = 0 + (float) Math.sin(pitch / 180.0f * Math.PI) * cam_Player_Orbit_Magnitude;
cameraRotationVector.z = 0 + (float) Math.sin(yaw / 180.0f * Math.PI) * cam_Player_Orbit_Magnitude;
cameraRotationVector.normalize();
}
CameraEntityUtils.setCameraEye(Globals.playerCamera, cameraRotationVector);
Globals.viewMatrix = CameraEntityUtils.getCameraViewMatrix(Globals.playerCamera);
controls.get(INPUT_CODE_CAMERA_ROTATION).setOnMove(new Control.MouseCallback(){public void execute(MouseEvent event){
Globals.cameraHandler.handleMouseEvent(event);
}});
/*
Move forward
@ -658,6 +605,24 @@ public class ControlHandler {
}
}
}});
mainGameControlList.add(controls.get(INPUT_CODE_IRON_SIGHT));
controls.get(INPUT_CODE_IRON_SIGHT).setOnPress(new ControlMethod() {public void execute() {
if(Globals.playerCharacter != null){
IronSightTree ironSightTree = IronSightTree.getIronSightTree(Globals.playerCharacter);
if(ironSightTree != null){
ironSightTree.start();
}
}
}});
controls.get(INPUT_CODE_IRON_SIGHT).setOnRelease(new ControlMethod() {public void execute() {
if(Globals.playerCharacter != null){
IronSightTree ironSightTree = IronSightTree.getIronSightTree(Globals.playerCharacter);
if(ironSightTree != null){
ironSightTree.release();
}
}
}});
/*
Interact

View File

@ -62,6 +62,7 @@ public class EntityDataStrings {
*/
public static final String DATA_STRING_CAMERA_ORBIT_TARGET = "cameraOrbitTarget";
public static final String DATA_STRING_CAMERA_ORBIT_DISTANCE = "cameraOrbitDistance";
public static final String CAMERA_ORBIT_RADIAL_OFFSET = "cameraOrbitRadialOffset";
/*
Light Entity
@ -205,6 +206,11 @@ public class EntityDataStrings {
Inventory in general
*/
public static final String NATURAL_INVENTORY = "inventoryNatural";
/*
Iron sight
*/
public static final String IRON_SIGHT_TREE = "ironSightTree";
/*
Entity categories

View File

@ -0,0 +1,70 @@
package electrosphere.entity.state.ironsight;
import org.joml.Vector3f;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.state.BehaviorTree;
import electrosphere.entity.types.camera.CameraEntityUtils;
import electrosphere.main.Globals;
public class IronSightTree implements BehaviorTree {
static enum IronSightTreeState {
ACTIVE,
INACTIVE,
}
IronSightTreeState state = IronSightTreeState.INACTIVE;
boolean cameraZoomedIn = false;
float regularRadius = 1.0f;
float zoomedInRadius = 0.1f;
Vector3f offcenterOffset = new Vector3f(-0.1f,1,0);
public void start(){
state = IronSightTreeState.ACTIVE;
}
public void release(){
state = IronSightTreeState.INACTIVE;
}
@Override
public void simulate() {
switch(state){
case ACTIVE:
if(!cameraZoomedIn){
cameraZoomedIn = true;
CameraEntityUtils.setOrbitalCameraDistance(Globals.playerCamera, zoomedInRadius);
Globals.cameraHandler.updateRadialOffset(new Vector3f(0,1,0.5f));
Globals.cameraHandler.updateGlobalCamera();
}
break;
case INACTIVE:
if(cameraZoomedIn){
cameraZoomedIn = false;
CameraEntityUtils.setOrbitalCameraDistance(Globals.playerCamera, regularRadius);
Globals.cameraHandler.updateRadialOffset(new Vector3f(0,1,0));
Globals.cameraHandler.updateGlobalCamera();
}
break;
}
}
public static IronSightTree getIronSightTree(Entity creature){
Object rVal;
if((rVal = creature.getData(EntityDataStrings.IRON_SIGHT_TREE)) != null && rVal instanceof IronSightTree){
return (IronSightTree) rVal;
}
return null;
}
public static void attachIronSightTree(Entity player){
IronSightTree ironSightTree = new IronSightTree();
player.putData(EntityDataStrings.IRON_SIGHT_TREE, ironSightTree);
Globals.entityManager.registerBehaviorTree(ironSightTree);
}
}

View File

@ -58,7 +58,7 @@ public class RotatorTree {
// currentRotation.
}
if(followsView){
// Quaternionf currentRotation = entityActor.getBoneRotation(parentBone);
}
}
//recurse to children

View File

@ -23,6 +23,8 @@ public class CameraEntityUtils {
rVal.putData(EntityDataStrings.DATA_STRING_CAMERA_TYPE, EntityDataStrings.DATA_STRING_CAMERA_TYPE_BASIC);
rVal.putData(EntityDataStrings.DATA_STRING_CAMERA_CENTER, center);
rVal.putData(EntityDataStrings .DATA_STRING_CAMERA_EYE, eye);
rVal.putData(EntityDataStrings.DATA_STRING_CAMERA_ORBIT_DISTANCE, 1.0f);
rVal.putData(EntityDataStrings.CAMERA_ORBIT_RADIAL_OFFSET, new Vector3f(0,1,0));
rVal.putData(EntityDataStrings.CAMERA_PITCH, 0.0f);
rVal.putData(EntityDataStrings.CAMERA_YAW, 0.0f);
return rVal;
@ -33,7 +35,9 @@ public class CameraEntityUtils {
Globals.entityManager.registerEntity(rVal);
rVal.putData(EntityDataStrings.DATA_STRING_CAMERA_TYPE, EntityDataStrings.DATA_STRING_CAMERA_TYPE_ORBIT);
rVal.putData(EntityDataStrings.DATA_STRING_CAMERA_CENTER, center);
rVal.putData(EntityDataStrings .DATA_STRING_CAMERA_EYE, eye);
rVal.putData(EntityDataStrings.DATA_STRING_CAMERA_EYE, eye);
rVal.putData(EntityDataStrings.DATA_STRING_CAMERA_ORBIT_DISTANCE, 1.0f);
rVal.putData(EntityDataStrings.CAMERA_ORBIT_RADIAL_OFFSET, new Vector3f(0,1,0));
rVal.putData(EntityDataStrings.CAMERA_PITCH, 0.0f);
rVal.putData(EntityDataStrings.CAMERA_YAW, 0.0f);
BehaviorTree entityTrackingTree = new BehaviorTree() {
@ -42,7 +46,7 @@ public class CameraEntityUtils {
// TODO Auto-generated method stub
if(toTrack != null){
Vector3d entityPos = EntityUtils.getPosition(toTrack);
CameraEntityUtils.setCameraCenter(rVal, new Vector3f((float)entityPos.x,(float)entityPos.y,(float)entityPos.z));
CameraEntityUtils.setCameraCenter(rVal, new Vector3f((float)entityPos.x,(float)entityPos.y,(float)entityPos.z).add(getOrbitalCameraRadialOffset(rVal)));
}
}
};
@ -57,6 +61,18 @@ public class CameraEntityUtils {
public static float getOrbitalCameraDistance(Entity camera){
return (float)camera.getData(EntityDataStrings.DATA_STRING_CAMERA_ORBIT_DISTANCE);
}
public static void setOrbitalCameraDistance(Entity camera, float distance){
camera.putData(EntityDataStrings.DATA_STRING_CAMERA_ORBIT_DISTANCE, distance);
}
public static Vector3f getOrbitalCameraRadialOffset(Entity camera){
return (Vector3f)camera.getData(EntityDataStrings.CAMERA_ORBIT_RADIAL_OFFSET);
}
public static void setOrbitalCameraRadialOffset(Entity camera, Vector3f offset){
camera.putData(EntityDataStrings.CAMERA_ORBIT_RADIAL_OFFSET, offset);
}
public static void setCameraCenter(Entity camera, Vector3f center){
camera.putData(EntityDataStrings.DATA_STRING_CAMERA_CENTER, center);
@ -97,7 +113,7 @@ public class CameraEntityUtils {
}
public static Matrix4f getCameraViewMatrix(Entity camera){
Vector3f cameraCenter = new Vector3f(0,1,0);
Vector3f cameraCenter = new Vector3f(0,0,0);//getViewMatrixCenterOffset(camera);
Vector3f cameraEye = new Vector3f(cameraCenter).add(getCameraEye(camera));
Vector3f cameraUp = new Vector3f(0,1.0f,0);
// System.out.println("eye: " + cameraEye);

View File

@ -8,6 +8,7 @@ import electrosphere.renderer.texture.Texture;
import electrosphere.renderer.texture.TextureMap;
import com.google.gson.Gson;
import electrosphere.audio.AudioEngine;
import electrosphere.controls.CameraHandler;
import electrosphere.controls.ControlCallback;
import electrosphere.controls.ControlHandler;
import electrosphere.controls.MouseCallback;
@ -151,6 +152,11 @@ public class Globals {
// Database stuff
//
public static DatabaseController dbController = new DatabaseController();
//
//Camera handler stuff
//
public static CameraHandler cameraHandler = new CameraHandler();
//
//current world

View File

@ -3,6 +3,7 @@ package electrosphere.net.server;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.ironsight.IronSightTree;
import electrosphere.entity.types.item.ItemUtils;
import electrosphere.entity.types.attach.AttachUtils;
import electrosphere.entity.types.collision.CollisionObjUtils;
@ -106,6 +107,7 @@ public class ServerConnectionHandler implements Runnable {
newPlayerObject.setWorldY(Globals.serverWorldData.convertRealToChunkSpace(Globals.spawnPoint.z));
Globals.dataCellManager.addPlayer(newPlayerObject);
Globals.dataCellManager.movePlayer(newPlayerObject, Globals.serverWorldData.convertRealToChunkSpace(Globals.spawnPoint.x), Globals.serverWorldData.convertRealToChunkSpace(Globals.spawnPoint.z));
IronSightTree.attachIronSightTree(newPlayerCharacter);
// //spawn player sword
// Entity sword = ItemUtils.spawnBasicItem("Katana");
// AttachUtils.attachEntityToEntityAtBone(newPlayerCharacter, sword, "Bone.031");

View File

@ -29,6 +29,7 @@ public class Actor {
ActorMeshMask meshMask = new ActorMeshMask();
List<ActorShaderMask> shaderMasks = new LinkedList<ActorShaderMask>();
Map<String,ActorTextureMask> textureMap = null;
Map<String,ActorNodeTransformMask> nodeTransformMaskMap;
public Actor(String modelPath){
this.modelPath = modelPath;

View File

@ -0,0 +1,30 @@
package electrosphere.renderer.actor;
import org.joml.Quaternionf;
import org.joml.Vector3f;
public class ActorNodeTransformMask {
Vector3f translation;
Quaternionf rotation;
Vector3f scale;
public ActorNodeTransformMask(Vector3f translation, Quaternionf rotation, Vector3f scale){
this.translation = translation;
this.rotation = rotation;
this.scale = scale;
}
public Vector3f getTranslation(){
return translation;
}
public Quaternionf getRotation(){
return rotation;
}
public Vector3f getScale(){
return scale;
}
}