Files
monde-usage-unity/Assets/Scripts/SchoolManager.cs
2024-10-05 18:08:45 +02:00

177 lines
4.1 KiBLFS
C#

using System;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.Events;
using UnityEngine.UI;
[Serializable]
public class StoryPart
{
public string name;
public Clickable[] clickables;
public GameObject root;
public int minVideoPlayCount = 2;
// public VideoClip endVideoClip;
public VideoAsset endVideoAsset;
// public VideoClip onlyPlayVideo;
public VideoAsset onlyPlayVideoAsset;
public UnityEvent OnEnd;
public UnityEvent OnStart;
}
public class SchoolManager : MonoBehaviour
{
public StoryPart[] storyParts;
[SerializeField] private int currentStoryPartIndex = 0;
private StoryPart CurrentStoryPart => currentStoryPartIndex < storyParts.Length ? storyParts[currentStoryPartIndex] : null;
[SerializeField] private int currentVideoPlayCount = 0;
[SerializeField] private bool goBackToMainSceneOnEnd = true;
[SerializeField] private GameObject endState = null;
private Animator animator;
public UnityEvent OnEnd;
private bool completed = false;
void Start()
{
currentStoryPartIndex = 0;
currentVideoPlayCount = 0;
animator = GetComponent<Animator>();
foreach (var storyPart in storyParts)
{
if (storyPart.root == null) continue;
storyPart.root?.SetActive(false);
foreach (var clickable in storyPart.clickables)
{
clickable.OnVideoPlayed += (Clickable gfc) => OnVideoPlayed();
}
}
CurrentStoryPart?.root?.SetActive(true);
if (endState != null)
{
endState.SetActive(false);
}
}
public void TriggerStartAnimation()
{
if (completed) return;
animator.SetTrigger("Start");
}
void OnVideoPlayed()
{
currentVideoPlayCount++;
if (currentVideoPlayCount < CurrentStoryPart.minVideoPlayCount) return;
CurrentStoryPart.OnEnd?.Invoke();
currentStoryPartIndex++;
if (currentStoryPartIndex >= storyParts.Length)
{
currentStoryPartIndex = 0;
CompleteStory();
return;
}
currentVideoPlayCount = 0;
if (animator != null)
{
animator.SetTrigger(CurrentStoryPart.name);
}
else
{
ShowCurrentStoryPart();
}
}
public void NextStoryPart()
{
currentStoryPartIndex++;
if (currentStoryPartIndex >= storyParts.Length)
{
currentStoryPartIndex = 0;
CompleteStory();
return;
}
currentVideoPlayCount = 0;
ShowCurrentStoryPart();
}
private void CompleteStory()
{
completed = true;
OnEnd?.Invoke();
if (goBackToMainSceneOnEnd)
{
CanvasManager.singleton.GoBackToMainScene();
}
if (endState != null)
{
endState.SetActive(true);
foreach (var storyPart in storyParts)
{
storyPart.root.SetActive(false);
}
}
}
public void ShowCurrentStoryPart()
{
if (CurrentStoryPart == null) return;
CurrentStoryPart.OnStart?.Invoke();
foreach (var storyPart in storyParts)
{
storyPart.root.SetActive(false);
}
CurrentStoryPart.root.SetActive(true);
if (CurrentStoryPart.onlyPlayVideoAsset != null)
{
CameraManager.singleton.PlayVideo(CurrentStoryPart.onlyPlayVideoAsset, () =>
{
NextStoryPart();
});
}
}
public void JumpToScene(string scene)
{
var storyPart = Array.Find(storyParts, s => s.name == scene);
if (storyPart == null)
{
Debug.LogError("Scene not found");
return;
}
currentStoryPartIndex = Array.IndexOf(storyParts, storyPart);
ShowCurrentStoryPart();
}
public void Revolution()
{
CanvasManager.singleton.Warning("lancer la révolution ?", () =>
{
NextStoryPart();
});
}
}