Programming 8. Game programming patterns

8. Game programming patterns

Observer pattern

Game programming patterns: Observer

gameObject.SendMessage("HitByLightning", SendMessageOptions.DontRequireReceiver);

Singleton pattern

Game programming patterns: Singleton Game programming patterns: Singleton

void Awake ()
{
	// If there is more than ONE instance of ScoreManager
	// disabled the old instance to avoid errors
	if(instance != null)
	{
		Debug.LogError("More than ONE ScoreManager instance in game! Disabling old one.");
		instance.gameObject.SetActive(false);
	}

	// Set instance to this so class can be used everywhere
	// Make sure there is only ONE ScoreManager in the game
	instance = this;
}
ScoreManager.instance.AddScore(1);

State pattern

Game programming patterns: State

switch (PlayerState)
	case State.Walking:
	{
		if (Input.down("Fire1"))
			PlayerState = State.Jumping;
	}
	case State.Jumping:
	{

	}
	case State.Hurting
	{
		
	}