Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System;
- using System.Reflection;
- using System.Linq;
- using UnityEngine;
- public static class Powerup_Factory
- {
- private static Dictionary<string, Type> powerupsByName;
- private static bool IsInitalized => powerupsByName != null;
- private static void InitalizeFactory()
- {
- if (IsInitalized)
- return;
- var powerupTypes = Assembly.GetAssembly(typeof(Powerup_Base)).GetTypes()
- .Where(myType => !myType.IsAbstract && myType.IsSubclassOf(typeof(Powerup_Base)));
- // Dictionary for finding these by name later (could be an enum/id instead of string
- powerupsByName = new Dictionary<string, Type>();
- foreach (var type in powerupTypes)
- {
- var tempPowerup = Activator.CreateInstance(type) as Powerup_Base;
- powerupsByName.Add(tempPowerup.PowerupName, type);
- }
- Debug.Log("Powerup_Factory Initalized Status: " + IsInitalized);
- }
- public static Powerup_Base GetPowerup(string powerupType)
- {
- InitalizeFactory();
- if (powerupsByName.ContainsKey(powerupType))
- {
- Type type = powerupsByName[powerupType];
- var powerup = Activator.CreateInstance(type) as Powerup_Base;
- return powerup;
- }
- return null;
- }
- internal static IEnumerable<string> GetPowerupNames()
- {
- UnityEngine.Debug.Log("Test");
- InitalizeFactory();
- return powerupsByName.Keys;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement