Unity/TopdownEngine

MMEvent 동작 원리

lipnus 2023. 1. 29. 00:12
반응형

싱글톤 패턴에 Listener 등록하는 방식.

싱글톤 클래스에서는 누가 듣던가말던가 등록한 것들 Broadcast

 

Android에서 EventBus처럼 쓰면 될듯함.

 

Example (Achievement)

돌 밟으면 Achevement 이벤트 발생
요기 들어오면 발생

 

Checkpoint.cs

/// <summary>
	/// An event to trigger when a checkpoint is reached
	/// </summary>
	public struct CheckPointEvent
	{
		public int Order;
		public CheckPointEvent(int order)
		{
			Order = order;
		}

		static CheckPointEvent e;
		public static void Trigger(int order)
		{
			e.Order = order;
			MMEventManager.TriggerEvent(e); // ★★★★★★★
		}
	}

	/// <summary>
	/// Checkpoint class. Will make the player respawn at this point if it dies.
	/// </summary>
	[AddComponentMenu("TopDown Engine/Spawn/Checkpoint")]
	public class CheckPoint : TopDownMonoBehaviour 
	{
    
    
    //...
    
    
    
    
    protected virtual void TriggerEnter(GameObject collider)
		{
			Character character = collider.GetComponent<Character>();

			if (character == null) { return; }
			if (character.CharacterType != Character.CharacterTypes.Player) { return; }
			if (!LevelManager.HasInstance) { return; }
			LevelManager.Instance.SetCurrentCheckpoint(this);
			CheckPointEvent.Trigger(CheckPointOrder);
		}

 

MMEventManager.cs

싱글톤 클래스인 MMEventManager

_subscribersList에 이벤트들이 저장되는 듯?

 

 

AchievementRules.cs

	public class AchievementRules : MMAchievementRules, 
		MMEventListener<MMGameEvent>, 
		MMEventListener<MMCharacterEvent>, 
		MMEventListener<TopDownEngineEvent>,
		MMEventListener<MMStateChangeEvent<CharacterStates.MovementStates>>,
		MMEventListener<MMStateChangeEvent<CharacterStates.CharacterConditions>>,
		MMEventListener<PickableItemEvent>,
		MMEventListener<CheckPointEvent>,
		MMEventListener<MMInventoryEvent>
        
        
    /// <summary>
		/// On enable, we start listening for MMGameEvents. You may want to extend that to listen to other types of events.
		/// </summary>
		protected override void OnEnable()
		{
			base.OnEnable ();
			this.MMEventStartListening<MMCharacterEvent>();
			this.MMEventStartListening<TopDownEngineEvent>();
			this.MMEventStartListening<MMStateChangeEvent<CharacterStates.MovementStates>>();
			this.MMEventStartListening<MMStateChangeEvent<CharacterStates.CharacterConditions>>();
			this.MMEventStartListening<PickableItemEvent>();
			this.MMEventStartListening<CheckPointEvent>();
			this.MMEventStartListening<MMInventoryEvent>();
		}

		/// <summary>
		/// On disable, we stop listening for MMGameEvents. You may want to extend that to stop listening to other types of events.
		/// </summary>
		protected override void OnDisable()
		{
			base.OnDisable ();
			this.MMEventStopListening<MMCharacterEvent>();
			this.MMEventStopListening<TopDownEngineEvent>();
			this.MMEventStopListening<MMStateChangeEvent<CharacterStates.MovementStates>>();
			this.MMEventStopListening<MMStateChangeEvent<CharacterStates.CharacterConditions>>();
			this.MMEventStopListening<PickableItemEvent>();
			this.MMEventStopListening<CheckPointEvent>();
			this.MMEventStopListening<MMInventoryEvent>();
		}

해당 클래스에서 필요한 이벤트들을 저렇게 상속하면 된다

 

		/// <summary>
		/// Grabs checkpoints events. If the checkpoint's order is > 0, we unlock our achievement
		/// </summary>
		/// <param name="checkPointEvent"></param>
		public virtual void OnMMEvent(CheckPointEvent checkPointEvent)
		{
			if (checkPointEvent.Order > 0)
			{
				MMAchievementManager.UnlockAchievement("SteppingStone");
			}
		}

그다음에 요렇게 해당 부분을 구현.

 

 

반응형

'Unity > TopdownEngine' 카테고리의 다른 글

Inventory에 아이템 집어넣기 & 초기 Weapon 설정  (0) 2023.01.29
MMEvnet 예제  (0) 2023.01.29
데미지 숫자 표시  (0) 2023.01.28
주인공 죽음 및 GameOver Canvas 처리  (0) 2023.01.28
특정 Ability On/Off 하기  (0) 2023.01.28