Merge commit 'e7f8693f' into SamWorkset

This commit is contained in:
2025-01-22 21:22:58 +02:00
6 changed files with 745 additions and 88 deletions

View File

@@ -7,9 +7,7 @@ public class FloorButtonVisualizer : MonoBehaviour
{
public Sprite InactiveSprite;
public Sprite ActiveSprite;
public float FloorUpperCoordiantes;
public float FloorLowerCoordiantes;
private bool activeState = true;
public bool ActiveState;
private Image buttonImage;
// Start is called before the first frame update
void Start()
@@ -17,25 +15,16 @@ public class FloorButtonVisualizer : MonoBehaviour
buttonImage = gameObject.GetComponent<Image>();
}
// Update is called once per frame
void Update()
public void Activate()
{
float height = gameObject.transform.position.y;
if (height < FloorUpperCoordiantes && height > FloorLowerCoordiantes)
{
if (!activeState) {
activeState = true;
buttonImage.sprite = ActiveSprite;
}
}
else
{
if (activeState)
{
activeState = false;
buttonImage.sprite = InactiveSprite;
}
}
this.ActiveState = true;
buttonImage.sprite = ActiveSprite;
//Debug.Log("Floorbutton of " + gameObject.name + " activated");
}
public void Deactivate()
{
this.ActiveState = false;
buttonImage.sprite = InactiveSprite;
//Debug.Log("Floorbutton of " + gameObject.name + " deactivated");
}
}

View File

@@ -3,6 +3,7 @@ using System.Collections;
using System.Collections.Generic;
using Unity.XR.CoreUtils;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UI;
using UnityEngine.XR.Interaction.Toolkit;
@@ -25,20 +26,38 @@ public class MenuTeleportButton : MonoBehaviour
button.onClick.AddListener(TeleportPlayer);
TeleportLocation[] locations = FindObjectsOfType<TeleportLocation>(); // Fetches all teleport locations from scene.
Debug.Log("The amount of teleport locations is " + locations.Length);
//Debug.Log("The amount of teleport locations is " + locations.Length);
foreach (TeleportLocation location in locations) // Finds the target.
{
if (location.Name.Equals(TargetName))
{
target = location;
Debug.Log("Teleport target of " + target.Name + " found.");
//Debug.Log("Teleport target of " + target.Name + " found.");
}
}
if (target == null) Debug.Log("Teleport target of " + TargetName + " not found.");
}
public void SetStateSelected()
{
if (button != null)
{
if (HoverSprite != null)
{
button.targetGraphic.GetComponent<Image>().sprite = HoverSprite;
}
}
}
void Update()
public void SetStateDefault()
{
// Refresh the button state.
button.interactable = false;
button.interactable = true;
if (NormalSprite != null)
{
button.targetGraphic.GetComponent<Image>().sprite = NormalSprite;
}
}
public void SetStateDefault()
{
@@ -47,16 +66,27 @@ public class MenuTeleportButton : MonoBehaviour
}
private void TeleportPlayer()
{
Debug.Log("Teleport button clicked");
//Debug.Log("Teleport button clicked");
if (target != null && Player != null && Player.Camera != null)
{
// Teleport the XR Origin to the specified coordinates.
Player.transform.position = target.transform.position;
Player.Camera.transform.rotation = target.transform.rotation;
// Calculate the rotation offset needed for the player
Vector3 targetEulerAngles = target.transform.rotation.eulerAngles;
Vector3 currentCameraEulerAngles = Player.Camera.transform.rotation.eulerAngles;
// Determine the rotation delta around the Y-axis
float rotationDeltaY = targetEulerAngles.y - currentCameraEulerAngles.y;
// Apply the rotation delta to the XR Origin
Player.transform.Rotate(0, rotationDeltaY, 0, Space.World);
// Refresh the button state.
button.interactable = false;
button.interactable = true;
}
}
}

View File

@@ -3,6 +3,7 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.LowLevel;
using UnityEngine.UI;
public class Menu : MonoBehaviour
@@ -10,19 +11,29 @@ public class Menu : MonoBehaviour
public GameObject MenuRotator;
public GameObject Floor1Panel;
public GameObject Floor2Panel;
public Button Floor1Button;
public Button Floor2Button;
public FloorButtonVisualizer Floor1Button;
public FloorButtonVisualizer Floor2Button;
public Camera Camera;
public Image Playericon;
public Image PlayericonFloor1;
public Image PlayericonFloor2;
public GameObject PlayericonFloor1Parent;
public GameObject PlayericonFloor2Parent;
public InputActionReference openMenuAction;
private Canvas canvas;
public Vector2 rotatedPlayerPos = new Vector2();
public float worldToMapAngle = 130.0f;
public Vector2 VR_P1 = new Vector2(-77.4f, 10.1f); // - 71.2f (alumine punkt)
public Vector2 VR_P2 = new Vector2(95.7f, -82.8f);
public Vector2 IMG_P1 = new Vector2(-387.2f, 193.1f); //
public Vector2 IMG_P2 = new Vector2(389.7f, -221.4f); //
// Values for setting the position of the player marker. Do not touch them without great cause.
private Vector2 rotatedPlayerPos = new Vector2();
private float worldToMapAngle = 130.0f;
private Vector2 VR_P1 = new Vector2(-77.4f, 10.1f); // - 71.2f (alumine punkt)
private Vector2 VR_P2 = new Vector2(95.7f, -82.8f);
private Vector2 IMG_P1 = new Vector2(-387.2f, 193.1f); //
private Vector2 IMG_P2 = new Vector2(389.7f, -221.4f); //
protected static float cameraToMapIconRotationOffset = -50;
private float floor2UpperLimit = 9;
private float floorsMidpointLimit = 2.5f;
private float floor1LowerLimit = -5;
private void Awake()
{
@@ -52,8 +63,8 @@ public class Menu : MonoBehaviour
// Start is called before the first frame update
void Start()
{
Floor1Button.onClick.AddListener(DisplayFloor1);
Floor2Button.onClick.AddListener(DisplayFloor2);
Floor1Button.GetComponent<Button>().onClick.AddListener(DisplayFloor1);
Floor2Button.GetComponent<Button>().onClick.AddListener(DisplayFloor2);
DisplayFloor2();
}
private void DisplayFloor1()
@@ -61,13 +72,39 @@ public class Menu : MonoBehaviour
//Debug.Log("Dispaling floor 1");
Floor1Panel.gameObject.SetActive(true);
Floor2Panel.gameObject.SetActive(false);
Button buttonComponent = Floor1Button.GetComponent<Button>();
// Refresh the state of the button.
buttonComponent.interactable = false;
buttonComponent.interactable = true;
}
private void DisplayFloor2()
{
//Debug.Log("Dispaling floor 2");
Floor1Panel.gameObject.SetActive(false);
Floor2Panel.gameObject.SetActive(true);
Button buttonComponent = Floor2Button.GetComponent<Button>();
// Refresh the state of the button.
buttonComponent.interactable = false;
buttonComponent.interactable = true;
}
private int DeterminePlayerCurrentFloor()
{
float yPos = gameObject.transform.position.y;
if (yPos > floorsMidpointLimit && yPos < floor2UpperLimit) // Floor 2
{
return 2;
}
if (yPos > floor1LowerLimit && yPos <= floorsMidpointLimit) // Floor 1
{
return 1;
}
return -1; // If the player should not be on the map
}
private void OnDeviceChange(InputDevice device, InputDeviceChange change) // To avoid bugs with controllers disconnecting.
{
switch (change)
@@ -82,7 +119,7 @@ public class Menu : MonoBehaviour
break;
}
}
// Update is called once per frame
void Update()
{
@@ -93,7 +130,82 @@ public class Menu : MonoBehaviour
ToggleMenu(new InputAction.CallbackContext()); // Activate menu.
}
}
UpdatePlayerIconPosition();
UpdatePlayerIconRotation();
UpdateActveFloor();
}
private void UpdatePlayerIconRotation()
{
float cameraRotationY = -Camera.transform.eulerAngles.y;
float iconRotation = cameraRotationY + cameraToMapIconRotationOffset;
if (PlayericonFloor1Parent.activeSelf)
{
PlayericonFloor1.transform.rotation = Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, iconRotation);
}
if (PlayericonFloor2Parent.activeSelf)
{
PlayericonFloor2.transform.rotation = Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, iconRotation);
}
}
private void UpdateActveFloor()
{
int playerCurrentFloor = DeterminePlayerCurrentFloor();
//Debug.Log("Player current floor is " + playerCurrentFloor);
switch (playerCurrentFloor)
{
case 1:
if (!Floor1Button.ActiveState)
{
//Debug.Log("Activating floor 1 Button.");
Floor1Button.Activate();
Floor2Button.Deactivate();
}
if (!PlayericonFloor1Parent.activeSelf)
{
PlayericonFloor1Parent.SetActive(true);
PlayericonFloor2Parent.SetActive(false);
}
break;
case 2:
if (!Floor2Button.ActiveState)
{
//Debug.Log("Activating floor 2 Button.");
Floor1Button.Deactivate();
Floor2Button.Activate();
}
if (!PlayericonFloor2Parent.activeSelf)
{
Debug.Log("PlayericonFloor2Parent is inactive");
PlayericonFloor1Parent.SetActive(false);
PlayericonFloor2Parent.SetActive(true);
Debug.Log(PlayericonFloor2Parent.activeSelf);
}
break;
default:
if (PlayericonFloor1Parent.activeSelf || PlayericonFloor2Parent.activeSelf)
{
//Debug.Log("Deactivating floor Buttons.");
Floor1Button.Deactivate();
Floor2Button.Deactivate();
}
if (PlayericonFloor1.enabled || PlayericonFloor2.enabled)
{
PlayericonFloor1Parent.SetActive(false);
PlayericonFloor2Parent.SetActive(false);
}
break;
}
}
private void UpdatePlayerIconPosition()
{
float worldToMapAngleRad = worldToMapAngle * Mathf.Deg2Rad;
//Vector2 VR_Player = new Vector2(transform.position.x, transform.position.z);
@@ -121,7 +233,14 @@ public class Menu : MonoBehaviour
Vector2 IMG_L = IMG_P2 - IMG_P1;
Vector2 IMG_Player = (VR_Player - VR_P1) / VR_L * IMG_L + IMG_P1;
Playericon.GetComponent<RectTransform>().anchoredPosition = IMG_Player;
if (PlayericonFloor1Parent.activeSelf)
{
PlayericonFloor1.GetComponent<RectTransform>().anchoredPosition = IMG_Player;
}
if (PlayericonFloor2Parent.activeSelf)
{
PlayericonFloor2.GetComponent<RectTransform>().anchoredPosition = IMG_Player;
}
/*
float VR_X = transform.position.x;
@@ -142,14 +261,13 @@ public class Menu : MonoBehaviour
//Playericon.transform.position.Set(IMG_X,0,0);
//Playericon.GetComponent<RectTransform>().anchoredPosition = new Vector2(IMG_X, IMG_Y);
//Debug.Log(IMG_X + " vs " + Playericon.GetComponent<RectTransform>().anchoredPosition.x);
}
private void OnDestroy()
{
openMenuAction.action.Disable();
openMenuAction.action.performed -= ToggleMenu;
InputSystem.onDeviceChange -= OnDeviceChange;
Floor1Button.onClick.RemoveListener(DisplayFloor1);
Floor2Button.onClick.RemoveListener(DisplayFloor2);
Floor1Button.GetComponent<Button>().onClick.RemoveListener(DisplayFloor1);
Floor2Button.GetComponent<Button>().onClick.RemoveListener(DisplayFloor2);
}
}