using Assets.Scripts; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.InputSystem; using UnityEngine.UI; using Assets.Scripts.Player; using Assets.Scripts.InteractableObjects; public class Controls : MonoBehaviour { GameObject player; GameObject fight; GameObject worldGen; GameObject playerCam; UIHandler uihandler; Vector3 input; Vector2 view; PlayerInput playerInput; MoveDirection direction; public Vector2 sensitivityController = new Vector2(0,0); public Vector2 sensitivityMouse = new Vector2(0,0); float multiplier = 0.01f; //DEV Purpose only void Start() { player = GameObject.Find("Player"); fight = GameObject.Find("Fight"); worldGen = GameObject.Find("WorldGenerator"); playerCam = GameObject.Find("Main Camera"); uihandler = GameObject.Find("UIHandler").GetComponent(); input = new Vector3(); view = new Vector2(); playerInput = GetComponent(); direction = MoveDirection.None; } // Update is called once per frame void Update() { if (uihandler.state == UIState.GAME && playerInput.currentActionMap.name != "MainGame") { playerInput.SwitchCurrentActionMap("MainGame"); } if (uihandler.state != UIState.GAME && playerInput.currentActionMap.name != "Menu") { playerInput.SwitchCurrentActionMap("Menu"); } if (!player.GetComponent().takeDamage(0)) { if (!uihandler.isPlayerInFight()) { if (uihandler.canPlayerRotate()) { if(playerInput.currentControlScheme == "Controller"){ playerCam.GetComponent().LookAround(view, sensitivityController * multiplier); player.GetComponent().rotate(view, sensitivityController * multiplier); } else{ playerCam.GetComponent().LookAround(view, sensitivityMouse * multiplier); player.GetComponent().rotate(view, sensitivityMouse * multiplier); } } if (uihandler.canPlayerMove()) { player.GetComponent().move(input); } } } } public void FixedUpdate() { Debug.Log(playerInput.currentControlScheme); if (direction != MoveDirection.None) { AxisEventData data = new AxisEventData(EventSystem.current); data.moveDir = direction; data.selectedObject = EventSystem.current.currentSelectedGameObject; ExecuteEvents.Execute(data.selectedObject, data, ExecuteEvents.moveHandler); } if (playerInput.currentControlScheme == "Controller") { if (Cursor.lockState != CursorLockMode.Locked) { Cursor.lockState = CursorLockMode.Locked; } GameObject.Find("txtInteract").GetComponent().text = GameObject.Find("txtInteract").GetComponent().text.Replace("[E]", "[ButtonEast]"); GameObject.Find("txtInteraction_Tutorial").GetComponent().text = GameObject.Find("txtInteraction_Tutorial").GetComponent().text.Replace("[E]", "[ButtonEast]"); GameObject.Find("txtTutorialGoal").GetComponent().text = GameObject.Find("txtTutorialGoal").GetComponent().text.Replace("[ESC]", "[Start]"); } else { if (uihandler.canPlayerRotate()) { Cursor.lockState = CursorLockMode.Locked; } else { Cursor.lockState = CursorLockMode.Confined; } GameObject.Find("txtInteract").GetComponent().text = GameObject.Find("txtInteract").GetComponent().text.Replace("[ButtonEast]", "[E]"); GameObject.Find("txtInteraction_Tutorial").GetComponent().text = GameObject.Find("txtInteraction_Tutorial").GetComponent().text.Replace("[E]", "[ButtonEast]"); GameObject.Find("txtTutorialGoal").GetComponent().text = GameObject.Find("txtTutorialGoal").GetComponent().text.Replace("[Start]", "[ESC]"); } } public void OnLooking(InputValue value) { view = value.Get(); } public void OnMovement(InputValue value) { try { input = value.Get(); } catch { if (value.Get().x < 0) { direction = MoveDirection.Left; } else if (value.Get().x > 0) { direction = MoveDirection.Right; } else if (value.Get().y < 0) { direction = MoveDirection.Down; } else if (value.Get().y > 0) { direction = MoveDirection.Up; } } } public void OnInteraction() { if (uihandler.canPlayerMove()) { GameObject target = playerCam.GetComponent().interactWithObject(); if (target != null) { switch (target.tag.Split(':')[1]) { case "Enemy": fight.GetComponent().startFight(worldGen.GetComponent().getCurrentTile(), target, player); break; case "Tree": player.GetComponent().getPlayer().getStat("TreeCount").changeAmount(1); GameObject.Find("Inventory").GetComponent().addItem(new Item("Wood")); break; case "Stone": GameObject.Find("Inventory").GetComponent().addItem(new Item("Rock")); break; case "NPC": break; case "Door": break; case "Chest": break; case "Ore": if (target.name.ToLower().Contains("iron")) { GameObject.Find("Inventory").GetComponent().addItem(new Item("Iron ore")); } else if (target.name.ToLower().Contains("gold")) { GameObject.Find("Inventory").GetComponent().addItem(new Item("Gold ore")); } else if (target.name.ToLower().Contains("copper")) { GameObject.Find("Inventory").GetComponent().addItem(new Item("Copper ore")); } else if (target.name.ToLower().Contains("tin")) { GameObject.Find("Inventory").GetComponent().addItem(new Item("Tin ore")); } player.GetComponent().getPlayer().getStat("OreCount").changeAmount(1); break; } target.GetComponent().OnInteraction(player); } } } public void OnInventory() { uihandler.switchInventory(); } public void OnQuestlog() { uihandler.switchQuestLog(); } public void OnPause() { uihandler.switchPauseMenu(); } public void OnSkillOne() { if (uihandler.isPlayerInFight()) { fight.GetComponent().playerAction(1); } } public void OnSkillTwo() { if (uihandler.isPlayerInFight()) { fight.GetComponent().playerAction(2); } } public void OnSkillThree() { if (uihandler.isPlayerInFight()) { fight.GetComponent().playerAction(3); } } public void OnSkillFour() { if (uihandler.isPlayerInFight()) { fight.GetComponent().playerAction(4); } } public void OnDisarm() { if (player.GetComponent().isArmed) { player.GetComponent().SetTrigger("WeaponHandling"); player.GetComponent().SetBool("isArmed", true); player.GetComponent().isArmed = false; player.GetComponent().SetInteger("objectCategory", 0); } else { player.GetComponent().SetTrigger("WeaponHandling"); player.GetComponent().SetBool("isArmed", false); player.GetComponent().isArmed = true; player.GetComponent().SetInteger("objectCategory", 0); } } }