Crosshair lock on logic

This commit is contained in:
austin 2021-11-16 00:50:59 -05:00
parent c23cb9870b
commit c571e736cb
4 changed files with 87 additions and 14 deletions

View File

@ -10,6 +10,7 @@ import electrosphere.entity.state.movement.GroundMovementTree;
import electrosphere.entity.state.movement.GroundMovementTree.MovementTreeState;
import electrosphere.entity.types.collision.CollisionObjUtils;
import electrosphere.entity.types.item.ItemUtils;
import electrosphere.game.client.targeting.crosshair.Crosshair;
import electrosphere.main.Globals;
import electrosphere.menu.MenuTransition;
import electrosphere.menu.MenuUtils;
@ -42,6 +43,7 @@ public class ControlHandler {
public static final String DATA_STRING_INPUT_CODE_ATTACK_PRIMARY = "attackPrimary";
public static final String DATA_STRING_INPUT_CODE_DEBUG_SPAWN_ITEM = "debugSpawnItem";
public static final String DATA_STRING_INPUT_CODE_IN_GAME_MAIN_MENU = "inGameMainMenu";
public static final String DATA_STRING_INPUT_CODE_LOCK_CROSSHAIR = "crosshairLock";
public static final String DATA_STRING_INPUT_CODE_MENU_INCREMENT = "menuIncrement";
public static final String DATA_STRING_INPUT_CODE_MENU_DECREMENT = "menuDecrement";
@ -125,6 +127,7 @@ public class ControlHandler {
handler.addControl(DATA_STRING_INPUT_CODE_MOVEMENT_FALL, new Control(true,false,GLFW_KEY_LEFT_CONTROL));
handler.addControl(DATA_STRING_INPUT_CODE_ATTACK_PRIMARY, new Control(false,true,GLFW_MOUSE_BUTTON_LEFT));
handler.addControl(DATA_STRING_INPUT_CODE_IN_GAME_MAIN_MENU, new Control(true,false,GLFW_KEY_ESCAPE));
handler.addControl(DATA_STRING_INPUT_CODE_LOCK_CROSSHAIR, new Control(false,true,GLFW_MOUSE_BUTTON_RIGHT));
/*
Map the menu navigation controls
@ -378,6 +381,28 @@ public class ControlHandler {
}
/*
Lock on crosshair
*/
if(controls.containsKey(DATA_STRING_INPUT_CODE_LOCK_CROSSHAIR)){
if(controls.get(DATA_STRING_INPUT_CODE_LOCK_CROSSHAIR).isIsMouse() && glfwGetMouseButton(Globals.window, controls.get(DATA_STRING_INPUT_CODE_LOCK_CROSSHAIR).getKeyValue()) == GLFW_PRESS){
if(controls.get(DATA_STRING_INPUT_CODE_LOCK_CROSSHAIR).isState() == false){
if(Crosshair.hasTarget()){
Crosshair.setCrosshairActive(true);
}
}
controls.get(DATA_STRING_INPUT_CODE_LOCK_CROSSHAIR).setState(true);
} else {
if(controls.get(DATA_STRING_INPUT_CODE_LOCK_CROSSHAIR).isState() == true){
if(Crosshair.getCrosshairActive()){
Crosshair.setCrosshairActive(false);
}
}
controls.get(DATA_STRING_INPUT_CODE_LOCK_CROSSHAIR).setState(false);
}
}
/*
Main menu dialog toggle
*/

View File

@ -620,8 +620,8 @@ public class LoadingThread extends Thread {
//give evil goblin sword
Entity goblinSword = ItemUtils.spawnBasicItem("Katana");
AttachUtils.attachEntityToEntityAtBone(goblin, goblinSword, "Bone.031");
//attach ai to evil goblin
MindlessAttacker.attachToCreature(goblin);
// //attach ai to evil goblin
// MindlessAttacker.attachToCreature(goblin);
// goblin = CreatureUtils.spawnBasicCreature("Goblin");
// CollisionObjUtils.positionCharacter(goblin, new Vector3f(3, 0, 4));

View File

@ -18,6 +18,7 @@ public class Crosshair {
static float bobMagnitude = 0.1f;
static float offsetVertical = 1;
static Entity currentTarget = null;
static boolean crosshairActive = false;
public static void initCrossHairEntity(){
crossHairEntity = EntityUtils.spawnDrawableEntity("/Models/lockoncrosshair1.fbx");
@ -30,6 +31,7 @@ public class Crosshair {
if(crossHairEntity != null && Globals.playerCharacter != null){
Vector3d parentPos = EntityUtils.getPosition(Globals.playerCharacter);
// if(currentTarget == null){
if(!crosshairActive){
Entity target = null;
double dist = 100;
for(Entity entity : Globals.entityManager.getTargetables()){
@ -45,7 +47,11 @@ public class Crosshair {
// System.out.println("Found target!");
currentTarget = target;
EntityUtils.setVisible(crossHairEntity, true);
} else {
currentTarget = null;
EntityUtils.setVisible(crossHairEntity, false);
}
}
// } else {
// if(parentPos.distance(EntityUtils.getPosition(currentTarget)) > TARGET_MAX_DIST){
// currentTarget = null;
@ -72,5 +78,21 @@ public class Crosshair {
}
}
public static void setCrosshairActive(boolean active){
crosshairActive = active;
}
public static boolean getCrosshairActive(){
return crosshairActive;
}
public static boolean hasTarget(){
return currentTarget != null;
}
public static Vector3d getTargetPosition(){
return EntityUtils.getPosition(currentTarget);
}
}

View File

@ -18,6 +18,7 @@ import electrosphere.entity.types.item.ItemUtils;
import electrosphere.entity.types.attach.AttachUtils;
import electrosphere.engine.LoadingThread;
import electrosphere.game.client.ClientFunctions;
import electrosphere.game.client.targeting.crosshair.Crosshair;
import electrosphere.game.config.UserSettings;
import electrosphere.game.server.saves.SaveUtils;
import electrosphere.game.server.terrain.manager.ServerTerrainManager;
@ -294,22 +295,47 @@ public class Main {
updateMouseVariables();
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));
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();
}
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);
// if(Crosshair.getCrosshairActive()){
// CameraEntityUtils.setCameraEye(Globals.playerCamera, cameraRotationVector);
// } else {
// CameraEntityUtils.setCameraEye(Globals.playerCamera, cameraRotationVector);
// }
// if(Globals.playerCharacter != null){
// Vector3f playerPos = EntityUtils.getPosition(Globals.playerCharacter);
// if(posX == -1 && playerPos.x > 100){