Added controls for gamepad, changed input & UI

This commit is contained in:
TAASONI3
2023-07-04 20:15:06 +02:00
parent 0b058f9248
commit 7aba856b17
48 changed files with 24176 additions and 15427 deletions

View File

@@ -3,6 +3,7 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class Controls : MonoBehaviour
@@ -13,6 +14,11 @@ public class Controls : MonoBehaviour
GameObject playerCam;
UIHandler uihandler;
GameObject currentHouse;
Vector3 input;
Vector2 view;
PlayerInput playerInput;
MoveDirection direction;
void Start()
{
@@ -21,122 +27,185 @@ public class Controls : MonoBehaviour
worldGen = GameObject.Find("WorldGenerator");
playerCam = GameObject.Find("Main Camera");
uihandler = GameObject.Find("UIHandler").GetComponent<UIHandler>();
input = new Vector3();
view = new Vector2();
playerInput = GetComponent<PlayerInput>();
direction = MoveDirection.None;
}
// Update is called once per frame
void Update()
{
if(playerInput.currentControlScheme == "Controller"){
if(Cursor.lockState != CursorLockMode.Locked){
Cursor.lockState = CursorLockMode.Locked;
}
}
else{
if(uihandler.canPlayerRotate()){
Cursor.lockState = CursorLockMode.Locked;
}
else{
Cursor.lockState = CursorLockMode.Confined;
}
}
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<Player>().takeDamage(0))
{
EventSystem.current.SetSelectedGameObject(null);
if (!uihandler.isPlayerInFight())
{
checkNormalControls();
}
else
{
checkFightControls();
if (uihandler.canPlayerRotate()){
playerCam.GetComponent<PlayerCamera>().lookAround(view, playerInput.currentControlScheme == "Controller");
}
}
}
}
private void checkNormalControls()
{
if (uihandler.canPlayerMove())
public void FixedUpdate(){
if (!player.GetComponent<Player>().takeDamage(0))
{
player.GetComponent<Player>().move();
if (Input.GetKeyDown(KeyCode.E))
if (!uihandler.isPlayerInFight())
{
GameObject target = playerCam.GetComponent<PlayerCamera>().interactWithObject();
if (target != null)
if (uihandler.canPlayerMove())
{
switch (target.tag.Split(':')[1])
{
case "Enemy":
fight.GetComponent<Fight>().startFight(worldGen.GetComponent<WorldGenerator>().getCurrentTile(), target, player);
break;
case "Tree":
GameObject.Find("Inventory").GetComponent<Inventory>().addItem(new Item("Wood"));
Destroy(target);
break;
case "Stone":
GameObject.Find("Inventory").GetComponent<Inventory>().addItem(new Item("Rock"));
Destroy(target);
break;
case "NPC":
target.GetComponent<NPC>().interact();
break;
case "Door":
target.GetComponent<Door>().interact();
break;
case "Chest":
target.GetComponent<Chest>().interact();
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"));
}
Destroy(target);
break;
}
player.GetComponent<Player>().move(input);
}
}
else if (Input.GetKeyDown(KeyCode.C))
{
uihandler.switchCharactersheet();
}
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);
}
}
public void OnLooking(InputValue value){
view = value.Get<Vector2>();
}
public void OnMovement(InputValue value){
try{
input = value.Get<Vector3>();
}catch{
if(value.Get<Vector2>().x < 0){
direction = MoveDirection.Left;
}
else if (Input.GetKeyDown(KeyCode.Q))
{
uihandler.switchQuestLog();
else if(value.Get<Vector2>().x > 0){
direction = MoveDirection.Right;
}
else if (Input.GetKeyDown(KeyCode.I))
{
uihandler.switchInventory();
else if(value.Get<Vector2>().y < 0){
direction = MoveDirection.Down;
}
else if(value.Get<Vector2>().y > 0){
direction = MoveDirection.Up;
}
}
if (Input.GetKeyDown(KeyCode.Escape))
}
public void OnInteraction(){
if (uihandler.canPlayerMove())
{
uihandler.switchPauseMenu();
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);
break;
case "Tree":
GameObject.Find("Inventory").GetComponent<Inventory>().addItem(new Item("Wood"));
Destroy(target);
break;
case "Stone":
GameObject.Find("Inventory").GetComponent<Inventory>().addItem(new Item("Rock"));
Destroy(target);
break;
case "NPC":
target.GetComponent<NPC>().interact();
break;
case "Door":
target.GetComponent<Door>().interact();
break;
case "Chest":
target.GetComponent<Chest>().interact();
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"));
}
Destroy(target);
break;
}
}
}
}
private void checkFightControls()
{
if (Input.GetKeyDown(KeyCode.Alpha1) || Input.GetKeyDown(KeyCode.Keypad1))
{
public void OnCharsheet(){
uihandler.switchCharactersheet();
}
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);
}
else if (Input.GetKeyDown(KeyCode.Alpha2) || Input.GetKeyDown(KeyCode.Keypad2))
{
}
public void OnSkillTwo(){
if(uihandler.isPlayerInFight()){
fight.GetComponent<Fight>().playerAction(2);
}
else if (Input.GetKeyDown(KeyCode.Alpha3) || Input.GetKeyDown(KeyCode.Keypad3))
{
}
public void OnSkillThree(){
if(uihandler.isPlayerInFight()){
fight.GetComponent<Fight>().playerAction(3);
}
else if (Input.GetKeyDown(KeyCode.Alpha4) || Input.GetKeyDown(KeyCode.Keypad4))
{
}
public void OnSkillFour(){
if(uihandler.isPlayerInFight()){
fight.GetComponent<Fight>().playerAction(4);
}
else if (Input.GetKeyDown(KeyCode.Alpha5) || Input.GetKeyDown(KeyCode.Keypad5))
{
}
public void OnSkillFive(){
if(uihandler.isPlayerInFight()){
fight.GetComponent<Fight>().playerAction(5);
}
else if (Input.GetKeyDown(KeyCode.Alpha6) || Input.GetKeyDown(KeyCode.Keypad6))
{
}
public void OnSkillSix(){
if(uihandler.isPlayerInFight()){
fight.GetComponent<Fight>().playerAction(6);
}
}
}