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

136 lines
4.0 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-12-02 21:23:01 +00:00
public class InstrumentsManager : PlayerSyncObject
{
private Transform rootObj;
private AnimationType _savedType;
private GameObject ChertDrum;
2020-10-27 22:59:19 +00:00
2020-12-02 21:23:01 +00:00
public void InitLocal(Transform root)
{
rootObj = root;
gameObject.AddComponent<CameraManager>();
2020-11-11 08:31:20 +00:00
2020-12-02 21:23:01 +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);
QSBInputManager.ExitTaunt += () => ReturnToPlayer();
2020-11-20 19:50:31 +00:00
2020-12-02 21:23:01 +00:00
QSB.Helper.Events.Unity.RunWhen(() => Locator.GetPlayerBody() != null, SetupInstruments);
2020-11-20 19:50:31 +00:00
2020-12-02 21:23:01 +00:00
QSBPlayerManager.PlayerSyncObjects.Add(this);
}
2020-11-20 19:50:31 +00:00
2020-12-02 21:23:01 +00:00
public void InitRemote(Transform root)
{
rootObj = root;
QSB.Helper.Events.Unity.RunWhen(() => Locator.GetPlayerBody() != null, SetupInstruments);
2020-11-20 19:50:31 +00:00
2020-12-02 21:23:01 +00:00
QSBPlayerManager.PlayerSyncObjects.Add(this);
}
2020-11-11 08:31:20 +00:00
2020-12-02 21:23:01 +00:00
private void OnDestroy()
{
if (!IsLocalPlayer)
{
return;
}
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);
QSBInputManager.ExitTaunt -= () => ReturnToPlayer();
}
2020-11-11 08:31:20 +00:00
2020-12-02 21:23:01 +00:00
private void SetupInstruments()
{
var bundle = QSB.InstrumentAssetBundle;
ChertDrum = MakeChertDrum(bundle);
}
2020-11-20 19:50:31 +00:00
2020-12-02 21:23:01 +00:00
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>();
2020-12-06 23:39:25 +00:00
if (QSBSceneManager.CurrentScene == OWScene.SolarSystem)
{
mr.sharedMaterial = GameObject.Find("NewDrum:polySurface2").GetComponent<MeshRenderer>().material;
}
else if (QSBSceneManager.CurrentScene == OWScene.EyeOfTheUniverse)
{
//mr.sharedMaterial = GameObject.Find("Props_HEA_Drums").GetComponent<MeshRenderer>().material;
// TODO : fix for instrument release
mr.sharedMaterial = null;
}
2020-12-02 21:23:01 +00:00
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);
2020-11-20 19:50:31 +00:00
2020-12-02 21:23:01 +00:00
return drum;
}
2020-11-20 19:50:31 +00:00
2020-12-02 21:23:01 +00:00
public void StartInstrument(AnimationType type)
{
if (!IsLocalPlayer)
{
DebugLog.DebugWrite("Error - Tried to start instrument on non-local player!", MessageType.Error);
return;
}
if (Player.PlayingInstrument || !Locator.GetPlayerController().IsGrounded())
{
return;
}
_savedType = Player.AnimationSync.CurrentType;
CameraManager.Instance.SwitchTo3rdPerson();
SwitchToType(type);
}
2020-10-27 22:59:19 +00:00
2020-12-02 21:23:01 +00:00
public void ReturnToPlayer()
{
if (!Player.PlayingInstrument)
{
return;
}
CameraManager.Instance.SwitchTo1stPerson();
SwitchToType(_savedType);
}
2020-11-01 15:56:48 +00:00
2020-12-02 21:23:01 +00:00
public void SwitchToType(AnimationType type)
{
DebugLog.DebugWrite($"switch to type {type} player {PlayerId}");
GlobalMessenger<uint, AnimationType>.FireEvent(EventNames.QSBChangeAnimType, QSBPlayerManager.LocalPlayerId, type);
QSBPlayerManager.LocalPlayer.AnimationSync.SetAnimationType(type);
CheckInstrumentProps(type);
}
2020-11-20 19:50:31 +00:00
2020-12-02 21:23:01 +00:00
public void CheckInstrumentProps(AnimationType type)
{
switch (type)
{
case AnimationType.Chert:
ChertDrum.SetActive(true);
break;
2020-12-03 08:28:05 +00:00
2020-12-02 21:23:01 +00:00
case AnimationType.PlayerSuited:
case AnimationType.PlayerUnsuited:
ChertDrum.SetActive(false);
break;
}
}
}
2020-12-03 08:28:05 +00:00
}