quantum-space-buddies/QSB/Instruments/InstrumentsManager.cs

129 lines
4.6 KiB
C#
Raw Normal View History

2020-11-20 19:50:31 +00:00
using OWML.Common;
using QSB.Animation;
2020-11-09 21:05:50 +00:00
using QSB.EventsCore;
2020-11-01 12:26:09 +00:00
using QSB.Instruments.QSBCamera;
2020-11-03 21:33:48 +00:00
using QSB.Player;
2020-11-20 19:50:31 +00:00
using QSB.Utility;
2020-10-27 22:59:19 +00:00
using UnityEngine;
namespace QSB.Instruments
{
2020-11-20 19:50:31 +00:00
public class InstrumentsManager : PlayerSyncObject
2020-10-27 22:59:19 +00:00
{
2020-11-20 19:50:31 +00:00
private Transform rootObj;
2020-11-08 19:32:04 +00:00
private AnimationType _savedType;
2020-11-20 19:50:31 +00:00
private GameObject ChertDrum;
2020-10-27 22:59:19 +00:00
2020-11-20 19:50:31 +00:00
public void InitLocal(Transform root)
2020-10-27 22:59:19 +00:00
{
2020-11-20 19:50:31 +00:00
rootObj = root;
2020-10-27 22:59:19 +00:00
gameObject.AddComponent<CameraManager>();
2020-11-11 08:31:20 +00:00
QSBInputManager.ChertTaunt += () => StartInstrument(AnimationType.Chert);
QSBInputManager.EskerTaunt += () => StartInstrument(AnimationType.Esker);
QSBInputManager.FeldsparTaunt += () => StartInstrument(AnimationType.Feldspar);
QSBInputManager.GabbroTaunt += () => StartInstrument(AnimationType.Gabbro);
QSBInputManager.RiebeckTaunt += () => StartInstrument(AnimationType.Riebeck);
2020-11-11 08:56:03 +00:00
QSBInputManager.ExitTaunt += () => ReturnToPlayer();
2020-11-20 19:50:31 +00:00
QSB.Helper.Events.Unity.RunWhen(() => Locator.GetPlayerBody() != null, SetupInstruments);
QSBPlayerManager.PlayerSyncObjects.Add(this);
}
public void InitRemote(Transform root)
{
rootObj = root;
QSB.Helper.Events.Unity.RunWhen(() => Locator.GetPlayerBody() != null, SetupInstruments);
QSBPlayerManager.PlayerSyncObjects.Add(this);
2020-11-11 08:31:20 +00:00
}
private void OnDestroy()
{
2020-12-02 09:51:53 +00:00
if (!IsLocalPlayer)
2020-11-20 19:50:31 +00:00
{
return;
}
DebugLog.DebugWrite($"OnDestroy {PlayerId}");
2020-11-11 08:31:20 +00:00
QSBInputManager.ChertTaunt -= () => StartInstrument(AnimationType.Chert);
QSBInputManager.EskerTaunt -= () => StartInstrument(AnimationType.Esker);
QSBInputManager.FeldsparTaunt -= () => StartInstrument(AnimationType.Feldspar);
QSBInputManager.GabbroTaunt -= () => StartInstrument(AnimationType.Gabbro);
QSBInputManager.RiebeckTaunt -= () => StartInstrument(AnimationType.Riebeck);
2020-11-11 08:56:03 +00:00
QSBInputManager.ExitTaunt -= () => ReturnToPlayer();
2020-11-11 08:31:20 +00:00
}
2020-11-20 19:50:31 +00:00
private void SetupInstruments()
{
var bundle = QSB.InstrumentAssetBundle;
ChertDrum = MakeChertDrum(bundle);
}
// EyeCompatibility : Need to find right object.
private GameObject MakeChertDrum(AssetBundle bundle)
{
var drum = new GameObject();
var mf = drum.AddComponent<MeshFilter>();
mf.sharedMesh = bundle.LoadAsset("assets/Chert/hourglasstwinsmeshescharacters2.asset") as Mesh;
var mr = drum.AddComponent<MeshRenderer>();
mr.sharedMaterial = GameObject.Find("NewDrum:polySurface2").GetComponent<MeshRenderer>().material;
drum.transform.parent = rootObj;
drum.transform.rotation = rootObj.rotation;
drum.transform.localPosition = Vector3.zero;
drum.transform.localScale = new Vector3(16.0f, 16.5f, 16.0f);
drum.SetActive(false);
return drum;
}
2020-11-11 08:31:20 +00:00
public void StartInstrument(AnimationType type)
{
2020-12-02 09:51:53 +00:00
if (!IsLocalPlayer)
2020-11-11 08:31:20 +00:00
{
2020-11-20 19:50:31 +00:00
DebugLog.DebugWrite("Error - Tried to start instrument on non-local player!", MessageType.Error);
2020-11-11 08:31:20 +00:00
return;
}
2020-11-20 19:50:31 +00:00
if (Player.PlayingInstrument || !Locator.GetPlayerController().IsGrounded())
{
return;
}
2020-11-22 13:57:31 +00:00
_savedType = Player.AnimationSync.CurrentType;
2020-11-11 08:31:20 +00:00
CameraManager.Instance.SwitchTo3rdPerson();
SwitchToType(type);
2020-10-27 22:59:19 +00:00
}
2020-11-11 08:31:20 +00:00
public void ReturnToPlayer()
2020-10-27 22:59:19 +00:00
{
2020-11-20 19:50:31 +00:00
if (!Player.PlayingInstrument)
2020-10-27 22:59:19 +00:00
{
2020-11-11 08:31:20 +00:00
return;
2020-10-27 22:59:19 +00:00
}
2020-11-11 08:31:20 +00:00
CameraManager.Instance.SwitchTo1stPerson();
SwitchToType(_savedType);
2020-10-27 22:59:19 +00:00
}
2020-11-01 15:56:48 +00:00
2020-11-08 19:32:04 +00:00
public void SwitchToType(AnimationType type)
2020-11-01 15:56:48 +00:00
{
2020-11-20 19:50:31 +00:00
DebugLog.DebugWrite($"switch to type {type} player {PlayerId}");
2020-11-09 21:05:50 +00:00
GlobalMessenger<uint, AnimationType>.FireEvent(EventNames.QSBChangeAnimType, QSBPlayerManager.LocalPlayerId, type);
2020-11-22 13:57:31 +00:00
QSBPlayerManager.LocalPlayer.AnimationSync.SetAnimationType(type);
2020-11-20 19:50:31 +00:00
CheckInstrumentProps(type);
}
public void CheckInstrumentProps(AnimationType type)
{
switch (type)
{
case AnimationType.Chert:
ChertDrum.SetActive(true);
break;
case AnimationType.PlayerSuited:
case AnimationType.PlayerUnsuited:
ChertDrum.SetActive(false);
break;
}
2020-11-01 15:56:48 +00:00
}
2020-10-27 22:59:19 +00:00
}
}