Files

70 lines
1.7 KiB
C#
Raw Permalink Normal View History

2021-11-09 21:19:38 +01:00
using UnityEngine;
2022-08-09 21:48:58 +03:00
using UnityEngine.InputSystem;
2021-11-09 21:19:38 +01:00
[RequireComponent(typeof(PlayerController))]
[RequireComponent(typeof(CpuController))]
public class BrainController : MonoBehaviour
{
2022-12-24 23:21:00 +02:00
private PlayerController _playerController;
private CpuController _cpuController;
private bool _cpuActive;
2021-11-09 21:19:38 +01:00
2022-12-24 23:21:00 +02:00
public bool IsPlayerOne { get; set; }
public string ControllerInputName { get; set; }
public BaseController ActiveController { get; private set; }
public InputDevice InputDevice { get; set; }
2021-11-09 21:19:38 +01:00
2022-12-24 23:21:00 +02:00
void Awake()
{
_playerController = GetComponent<PlayerController>();
_cpuController = GetComponent<CpuController>();
}
2021-11-09 21:19:38 +01:00
2023-01-23 18:31:58 +02:00
public void SetControllerToPlayer(int index)
2022-12-24 23:21:00 +02:00
{
ActiveController = _playerController;
_cpuActive = false;
2023-01-23 18:31:58 +02:00
GameSimulation._players[index].isAi = false;
2022-12-24 23:21:00 +02:00
_playerController.enabled = true;
_cpuController.enabled = false;
DeactivateCpu();
}
2021-11-09 21:19:38 +01:00
2023-01-23 18:31:58 +02:00
public void SetControllerToCpu(int index)
2022-12-24 23:21:00 +02:00
{
GetComponent<PlayerInput>().enabled = false;
ActiveController = _cpuController;
_cpuActive = true;
2023-01-23 18:31:58 +02:00
GameSimulation._players[index].isAi = true;
2022-12-24 23:21:00 +02:00
_playerController.enabled = false;
_cpuController.enabled = true;
ActivateCpu();
}
2021-11-09 21:19:38 +01:00
2022-12-24 23:21:00 +02:00
public void ActivateCpu()
{
if (_cpuActive)
{
_cpuController.ActivateInput();
}
}
2021-11-10 18:35:06 +01:00
2022-12-24 23:21:00 +02:00
public void DeactivateCpu()
{
_cpuController.DeactivateInput();
}
2021-11-10 18:35:06 +01:00
2022-12-24 23:21:00 +02:00
public void ActivateInput()
{
ActivateCpu();
ActiveController.ActivateInput();
}
2021-11-09 21:19:38 +01:00
2022-12-24 23:21:00 +02:00
public void DeactivateInput()
{
DeactivateCpu();
ActiveController.DeactivateInput();
}
2021-11-09 21:19:38 +01:00
}