Files
TalesOfNovariel/Assets/Scripts/Controls.cs

254 lines
7.7 KiB
C#

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;
using System;
public class Controls : MonoBehaviour
{
GameObject player;
GameObject fight;
GameObject worldGen;
GameObject playerCam;
UIHandler uihandler;
Vector3 input;
public PlayerInput playerInput;
MoveDirection direction;
public Vector2 sensitivityMouse = new Vector2(0, 0);
private Vector2 lookInput;
private Vector2 currentRotation;
private Vector2 rotationVelocity;
private float yaw;
private float pitch;
private float smoothTime = 0.05f;
private float minPitch = -75f;
private float maxPitch = 80f;
void Start()
{
player = GameObject.Find("Player");
fight = GameObject.Find("Fight");
worldGen = GameObject.Find("WorldGenerator");
playerCam = GameObject.Find("Main Camera");
uihandler = GameObject.Find("UIHandler").GetComponent<UIHandler>();
input = new Vector3();
playerInput = GetComponent<PlayerInput>();
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 (uihandler.canPlayerMove())
{
ControlEvents.Move(input);
}
}
public void FixedUpdate()
{
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 (uihandler.canPlayerRotate())
{
Cursor.lockState = CursorLockMode.Locked;
}
else
{
Cursor.lockState = CursorLockMode.Confined;
}
}
public void LateUpdate()
{
//Stop execution if player is not created yet or if player is dead
if (player.GetComponent<PlayerGameObject>().takeDamage(0)) return;
//Stop execution if player is in fight
if (uihandler.isPlayerInFight()) return;
if (uihandler.canPlayerRotate())
{
lookInput = Mouse.current.delta.ReadValue();
Vector2 targetRotation = lookInput * sensitivityMouse * Time.deltaTime;
currentRotation = Vector2.SmoothDamp(
currentRotation,
targetRotation,
ref rotationVelocity,
smoothTime
);
yaw += currentRotation.x;
pitch -= currentRotation.y;
pitch = Mathf.Clamp(pitch, minPitch, maxPitch);
ControlEvents.Look(pitch, yaw);
}
}
public void OnMovement(InputValue value)
{
try
{
input = value.Get<Vector3>();
}
catch
{
if (value.Get<Vector2>().x < 0)
{
direction = MoveDirection.Left;
}
else if (value.Get<Vector2>().x > 0)
{
direction = MoveDirection.Right;
}
else if (value.Get<Vector2>().y < 0)
{
direction = MoveDirection.Down;
}
else if (value.Get<Vector2>().y > 0)
{
direction = MoveDirection.Up;
}
}
}
public void OnInteraction()
{
if (uihandler.canPlayerMove())
{
GameObject target = playerCam.GetComponent<PlayerCamera>().interactWithObject();
if (target != null)
{
switch (target.tag.Split(':')[1])
{
case "Enemy":
fight.GetComponent<Fight>().startFight(worldGen.GetComponent<WorldGenerator>().getCurrentTile(), target, player, true);
break;
case "Tree":
player.GetComponent<PlayerGameObject>().getPlayer().getStat("TreeCount").changeAmount(1);
GameObject.Find("Inventory").GetComponent<Inventory>().addItem(new Item("Wood"));
break;
case "Stone":
GameObject.Find("Inventory").GetComponent<Inventory>().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<Inventory>().addItem(new Item("Iron ore"));
}
else if (target.name.ToLower().Contains("gold"))
{
GameObject.Find("Inventory").GetComponent<Inventory>().addItem(new Item("Gold ore"));
}
else if (target.name.ToLower().Contains("copper"))
{
GameObject.Find("Inventory").GetComponent<Inventory>().addItem(new Item("Copper ore"));
}
else if (target.name.ToLower().Contains("tin"))
{
GameObject.Find("Inventory").GetComponent<Inventory>().addItem(new Item("Tin ore"));
}
player.GetComponent<PlayerGameObject>().getPlayer().getStat("OreCount").changeAmount(1);
break;
}
target.GetComponent<InteractableObject>().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<Fight>().playerAction(1);
}
}
public void OnSkillTwo()
{
if (uihandler.isPlayerInFight())
{
fight.GetComponent<Fight>().playerAction(2);
}
}
public void OnSkillThree()
{
if (uihandler.isPlayerInFight())
{
fight.GetComponent<Fight>().playerAction(3);
}
}
public void OnSkillFour()
{
if (uihandler.isPlayerInFight())
{
fight.GetComponent<Fight>().playerAction(4);
}
}
public void OnDisarm()
{
if (player.GetComponent<PlayerGameObject>().isArmed)
{
player.GetComponent<Animator>().SetTrigger("WeaponHandling");
player.GetComponent<Animator>().SetBool("isArmed", true);
player.GetComponent<PlayerGameObject>().isArmed = false;
player.GetComponent<Animator>().SetInteger("objectCategory", 0);
}
else
{
player.GetComponent<Animator>().SetTrigger("WeaponHandling");
player.GetComponent<Animator>().SetBool("isArmed", false);
player.GetComponent<PlayerGameObject>().isArmed = true;
player.GetComponent<Animator>().SetInteger("objectCategory", 0);
}
}
}