TouhouLS/Assets/RealCode/Menu/GameTransition.cs

242 lines
6.9 KiB
C#
Raw Permalink Normal View History

2020-08-22 05:29:00 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
2020-08-22 20:39:53 +00:00
using UnityEngine.UI;
2020-08-22 05:29:00 +00:00
using System.Linq;
public enum GameState { Menu, SinglePlayer, Multiplayer, Lobby, SelectingRegion, ConnectionInProgress, InGame, Continue, Credits, Help }
2020-08-22 05:29:00 +00:00
public class GameTransition : MonoBehaviour {
2020-08-22 20:39:53 +00:00
[Header("Escape")]
public RectTransform escapeButton;
public Vector2 selectedEscapePosition = Vector2.zero;
private Vector2 baseEscapePosition;
public static GameTransition Instance { get; private set; }
2020-08-22 05:29:00 +00:00
2020-08-22 20:39:53 +00:00
[Header("Main")]
2020-08-22 05:29:00 +00:00
[SerializeField]
private GameState _state;
public GameState state {
get => _state;
set {
if (_state == value) return;
_state = value;
EventSystem.current.SetSelectedGameObject(null);
var select = rDisplays.FirstOrDefault(d => d.states.Contains(value) && d.firstSelectedGameObject != null);
if (select != null)
EventSystem.current.SetSelectedGameObject(select.firstSelectedGameObject);
}
}
public bool inMultiplayer;
public float lerpSpeed = 10f;
[System.Serializable]
private class RectTransformDisplay{
public RectTransform target = null;
public GameState[] states = new GameState[] { GameState.Menu };
[System.NonSerialized]
public Vector2 basePosition = Vector2.zero;
public Vector2 selectedPosition = Vector2.zero;
public GameObject firstSelectedGameObject = null;
}
[System.Serializable]
private class TransformDisplay : TransformDefault{
public GameState[] states = new GameState[] { GameState.Menu };
public int playerLoadedCount = 0;
public TransformDisplay(Transform t, Vector3 p, Vector3 r, Vector3 s) : base(t, p, r, s) { }
}
private class TransformDefault{
public Transform target = null;
public Vector3 position = Vector3.zero;
public Vector3 rotation = Vector3.zero;
public Vector3 scale = Vector3.one;
public TransformDefault(Transform t, Vector3 p, Vector3 r, Vector3 s){
target = t;
position = p;
rotation = r;
scale = s;
}
public void Set(float lerp){
target.localPosition = Vector3.Lerp(target.localPosition, position, lerp);
target.localRotation = Quaternion.Slerp(target.localRotation, Quaternion.Euler(rotation), lerp);
target.localScale = Vector3.Lerp(target.localScale, scale, lerp);
}
}
private TransformDefault[] tDefault;
[SerializeField]
private RectTransformDisplay[] rDisplays = new RectTransformDisplay[0];
[SerializeField]
private TransformDisplay[] tDisplays = new TransformDisplay[0];
public GameState[] selectedOrthoStates;
public float selectedCameraOrthoSize = 3f;
private float baseCameraOrthoSize;
private Camera baseCamera;
private void Awake() {
Instance = this;
}
private void Start() {
2020-08-22 20:39:53 +00:00
baseEscapePosition = escapeButton.anchoredPosition;
2020-08-22 05:29:00 +00:00
baseCamera = Camera.main;
baseCameraOrthoSize = baseCamera.orthographicSize;
foreach(var r in rDisplays){
r.basePosition = r.target.anchoredPosition;
}
tDefault = tDisplays.Select(t => t.target).Distinct().Select(t => new TransformDefault(t, t.localPosition, t.localEulerAngles, t.localScale)).ToArray();
}
2020-08-22 20:39:53 +00:00
private bool prevState;
private bool curState;
public float timeSinceInputOnRoot = 0f;
2020-08-22 05:29:00 +00:00
private void Update() {
2020-08-22 20:39:53 +00:00
// obscure fix to input field + esc
prevState = curState;
var sinputfield = EventSystem.current.currentSelectedGameObject;
if (sinputfield){
var comp = sinputfield.GetComponent<TMPro.TMP_InputField>();
curState = comp ? comp.isFocused : false;
} else {
curState = false;
}
// Escape handling
2020-08-22 20:39:53 +00:00
if (Input.GetKeyDown(KeyCode.Escape)) {
if (prevState && !curState) return;
Escape();
}
2020-08-22 20:39:53 +00:00
// in case the player disconnects during any weird times
2020-08-22 05:29:00 +00:00
if (inMultiplayer){
switch(NetworkManager.net.State){
case ExitGames.Client.Photon.LoadBalancing.ClientState.Disconnected:
case ExitGames.Client.Photon.LoadBalancing.ClientState.PeerCreated:
state = GameState.Menu;
inMultiplayer = false;
break;
case ExitGames.Client.Photon.LoadBalancing.ClientState.ConnectedToNameServer:
state = GameState.SelectingRegion;
break;
case ExitGames.Client.Photon.LoadBalancing.ClientState.JoinedLobby:
state = GameState.Multiplayer;
break;
case ExitGames.Client.Photon.LoadBalancing.ClientState.Joined:
2020-08-22 06:07:08 +00:00
if (state != GameState.InGame && state != GameState.Continue)
state = GameState.Lobby;
2020-08-22 05:29:00 +00:00
break;
}
}
if (state == GameState.Menu) {
timeSinceInputOnRoot += Time.deltaTime;
if (timeSinceInputOnRoot > 15) {
//state = GameState.SinglePlayer;
GameBoardInstance.instance.SetupGame(true);
Debug.Log("Starting Demo");
timeSinceInputOnRoot = 0;
}
} else {
timeSinceInputOnRoot = 0f;
}
if (Input.anyKey)
timeSinceInputOnRoot = 0f;
2020-08-22 05:29:00 +00:00
// transform
var dist = Time.deltaTime * lerpSpeed;
var playersLoaded = NetworkManager.inRoom ? NetworkManager.net.CurrentRoom.PlayerCount : 2;
foreach(var d in rDisplays){
var selected = d.states.Contains(state);
var item = d.target;
if (item){
item.anchoredPosition = Vector3.Lerp(item.anchoredPosition, selected ? d.selectedPosition : d.basePosition, dist);
}
}
var set = new Dictionary<Transform, TransformDefault>();
foreach(var t in tDefault)
set.Add(t.target, t);
foreach(var d in tDisplays){
var selected = d.states.Contains(state);
if (selected && d.playerLoadedCount <= playersLoaded){
d.Set(dist);
set.Remove(d.target);
}
}
foreach(var d in set){
d.Value.Set(dist);
}
baseCamera.orthographicSize = Mathf.Lerp(baseCamera.orthographicSize, selectedOrthoStates.Contains(state) ? selectedCameraOrthoSize : baseCameraOrthoSize, dist);
2020-08-22 20:39:53 +00:00
escapeButton.anchoredPosition = Vector2.Lerp(escapeButton.anchoredPosition, ShowEscapeButton() ? selectedEscapePosition : baseEscapePosition, dist);
}
bool ShowEscapeButton(){
return state != GameState.Menu;
}
public void Escape(){
2020-08-22 20:39:53 +00:00
if (!ShowEscapeButton()) return;
switch (state){
case GameState.Help:
case GameState.Credits:
case GameState.SinglePlayer:
2020-08-22 20:39:53 +00:00
state = GameState.Menu;
break;
// must disconnect
case GameState.Multiplayer:
case GameState.SelectingRegion:
state = GameState.ConnectionInProgress;
NetworkManager.net.Service();
NetworkManager.net.Disconnect();
break;
// special case, leave room
case GameState.Lobby:
state = GameState.ConnectionInProgress;
NetworkManager.net.OpLeaveRoom();
break;
// close game
case GameState.InGame:
GameBoardInstance.instance.ExitGame();
break;
case GameState.Continue:
GameBoardInstance.instance.ExitGame(true);
break;
}
2020-08-22 05:29:00 +00:00
}
}