mirror of
https://github.com/ConjureETS/GameOff2024.git
synced 2026-03-24 13:10:58 +00:00
70 lines
1.8 KiB
C#
70 lines
1.8 KiB
C#
using System;
|
|
using GameOff.Player;
|
|
using GameOff.Quiz;
|
|
using GameOff.UI.Quiz;
|
|
using UnityEngine;
|
|
|
|
namespace GameOff.Core
|
|
{
|
|
public class PlayerMain : QuizTaker
|
|
{
|
|
public static PlayerMain Instance { get; private set; }
|
|
public static void ClearStaticVariables()
|
|
{
|
|
Instance = null;
|
|
}
|
|
|
|
public event EventHandler<bool> OnStateUpdate;
|
|
|
|
private PlayerInputHandler _inputHandler;
|
|
|
|
private IState _currentState;
|
|
private PlayerClassState _classState;
|
|
private PlayerQuizState _quizState;
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance)
|
|
{
|
|
Debug.LogError($"PlayerMain already exist! {transform}");
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
Instance = this;
|
|
_inputHandler = GetComponent<PlayerInputHandler>();
|
|
}
|
|
|
|
protected override void Start()
|
|
{
|
|
_inputHandler.OnChangeState += InputHandler_OnChangeState;
|
|
QuestionUI.OnAnyQuestionChoice += (sender, choice) => SetAnswer(choice.Index - 1, choice.Answer);
|
|
|
|
_classState = new PlayerClassState();
|
|
_quizState = new PlayerQuizState();
|
|
_currentState = _classState;
|
|
|
|
base.Start();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
_currentState.UpdateState();
|
|
}
|
|
|
|
private void InputHandler_OnChangeState(object sender, EventArgs e)
|
|
{
|
|
bool isQuiz = _currentState is PlayerQuizState;
|
|
ChangeState(isQuiz ? _classState: _quizState);
|
|
OnStateUpdate?.Invoke(this, !isQuiz);
|
|
}
|
|
|
|
private void ChangeState(IState next)
|
|
{
|
|
_currentState.ExitState();
|
|
_currentState = next;
|
|
_currentState.EnterState();
|
|
}
|
|
}
|
|
}
|