Let's look at the steps for implementation:
- We'll start by writing a core element of the pattern, the Visitor interface:
namespace Pattern.Visitor
{
public interface IVisitor
{
void Visit(BikeShield bikeShield);
void Visit(BikeEngine bikeEngine);
void Visit(BikeWeapon bikeWeapon);
}
}
- Next up, we are going to code an interface that each visitable element will have to implement:
namespace Pattern.Visitor
{
public interface IBikeElement
{
void Accept(IVisitor visitor);
}
}
- Now that we have our primary interfaces, let's implement the main class that makes our power-up mechanism work; because of its length, we will review it in two parts:
using UnityEngine;
namespace Pattern.Visitor
{
[CreateAssetMenu(fileName = "PowerUp", menuName = "PowerUp")]
public class PowerUp : ScriptableObject, IVisitor
{
public string powerupName;
public GameObject powerupPrefab...