// ¥¤¥Ù¥ó¥È¤Î¼ïÎà¤òɽ¤¹Îóµó·¿ public enum EventType { Battle, Exploration, Shop }
using System.Collections; // ¥¤¥Ù¥ó¥È¤Î¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹ public interface IEvent { IEnumerator ExecuteEvent(); EventType GetEventType(); }
using UnityEngine; using System.Collections; // ¥¤¥Ù¥ó¥È¤ÎÃê¾Ý¥¯¥é¥¹ public abstract class EventBase : MonoBehaviour, IEvent { public abstract IEnumerator ExecuteEvent(); // ExecuteEvent ¥á¥½¥Ã¥É¤òÃê¾Ý¥á¥½¥Ã¥É¤È¤·¤Æ¼ÂÁõ public abstract EventType GetEventType(); }
using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Linq; public class GameController : MonoBehaviour { public Transform player; // ¥×¥ì¥¤¥ä¡¼¤Î¥È¥é¥ó¥¹¥Õ¥©¡¼¥à public List<EventBase> events; // ¥Þ¥¹¤Ë´ØÏ¢ÉÕ¤±¤é¤ì¤¿¥¤¥Ù¥ó¥È¤Î¥ê¥¹¥È private int currentPosition = 0; // ¸½ºß¤Î°ÌÃÖ private bool isEventRunning = false; // ¥¤¥Ù¥ó¥È¤¬¼Â¹ÔÃ椫¤É¤¦¤«¤Î¥Õ¥é¥° private bool isEventRunning = false; private Coroutine currentEventCoroutine; private void Start() { MoveToNextPosition(); } private void Update() { if (isEventRunning) return; if (Input.GetKeyDown(KeyCode.Space)) { // ¥×¥ì¥¤¥ä¡¼¤¬¥Ü¥¿¥ó¤ò²¡¤¹¤È¥¤¥Ù¥ó¥È¤ò³«»Ï StartEvent(); } } private void StartEvent() { if (currentPosition >= events.Count) { // ¥²¡¼¥à½ªÎ» Debug.Log("¥²¡¼¥à½ªÎ»"); return; } EventBase currentEvent = events[currentPosition]; currentEventCoroutine = StartCoroutine(RunEvent(currentEvent)); } private IEnumerator RunEvent(EventBase eventObj) { isEventRunning = true; // ¥¤¥Ù¥ó¥È¼Â¹ÔÃæ¤Ï¥²¡¼¥à¤Î¿Ê¹Ô¤ò°ì»þÄä»ß Time.timeScale = 0f; yield return StartCoroutine(eventObj.ExecuteEvent()); // ¥¤¥Ù¥ó¥È½ªÎ»¤Þ¤ÇÂÔµ¡ while (isEventRunning) { yield return null; } // ¥¤¥Ù¥ó¥È¤¬½ªÎ»¤·¤¿¤é¥²¡¼¥à¤Î¿Ê¹Ô¤òºÆ³« Time.timeScale = 1f; currentPosition++; MoveToNextPosition(); } public void EndEvent() { isEventRunning = false; currentPosition++; // ¼¡¤Î¥Þ¥¹¤Ë°ÜÆ°¤·¡¢¥Ü¥¿¥ó¤òɽ¼¨ MoveToNextPosition(); } private void MoveToNextPosition() { if (currentPosition < events.Count) { Vector3 nextPosition = new Vector3(currentPosition, 0, 0); // ¥Þ¥¹¤´¤È¤ËXºÂɸ¤òÊѤ¨¤ë player.position = nextPosition; // ¼¡¤Î¥Þ¥¹¤Ë¥Ü¥¿¥ó¤òɽ¼¨ events[currentPosition].gameObject.SetActive(true); } } }
private IEnumerator RunEvent(EventBase eventObj) { isEventRunning = true; // ¥¤¥Ù¥ó¥È¼Â¹ÔÃæ¤Ï¥²¡¼¥à¤Î¿Ê¹Ô¤¬Ää»ß // UniTask¤äyield return StartCoroutine¤ò»È¤Ã¤Æ¤â¤è¤¤ eventObj.ExecuteEvent(); // ¥¤¥Ù¥ó¥È½ªÎ»¤Þ¤ÇÂÔµ¡ while (isEventRunning) { yield return null; } }
private IEnumerator RunEvent(EventBase eventObj) { isEventRunning = true; // ¥¤¥Ù¥ó¥È¼Â¹ÔÃæ¤Ï¥²¡¼¥à¤Î¿Ê¹Ô¤¬Ää»ß yield retrurn StartCoroutine(eventObj.ExecuteEvent()); // ¥¤¥Ù¥ó¥È½ªÎ»¤Þ¤ÇÂÔµ¡ yield return new WaitUntil(() => !isEventRunning); }
private IEnumerator RunEvent(EventBase eventObj) { isEventRunning = true; // ¥¤¥Ù¥ó¥È¼Â¹ÔÃæ¤Ï¥²¡¼¥à¤Î¿Ê¹Ô¤¬Ää»ß eventObj.ExecuteEvent(); // ¥¤¥Ù¥ó¥È½ªÎ»¤Þ¤ÇÂÔµ¡ yield return new WaitUntil(() => !isEventRunning); }
using UnityEngine; using System.Collections; public class BattleEvent : EventBase { public override IEnumerator ExecuteEvent() { Debug.Log("¥Ð¥È¥ë¥¤¥Ù¥ó¥È¤¬È¯À¸¤·¤Þ¤·¤¿¡£Å¨¤ÈÀ襤¤Þ¤¹¡ª"); // ¥Ð¥È¥ë¤Î¼ÂºÝ¤Î½èÍý¤ò¤³¤³¤ËÄɲà // ¥Ð¥È¥ë¤¬½ªÎ»¤¹¤ë¤Þ¤ÇÂÔµ¡ yield return new WaitForSeconds(2.0f); // ²¾¤ÎÂÔµ¡»þ´Ö // ¥Ð¥È¥ë¤¬½ªÎ»¤·¤¿¤é¥¤¥Ù¥ó¥È½ªÎ»¤òÄÌÃÎ EndEvent(); } public override EventType GetEventType() { return EventType.Battle; } }