BorisKotlyar

Fabric

Apr 19th, 2019
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEngine;
  6.  
  7. namespace TankDemo.Model
  8. {
  9.     /// <summary>
  10.     /// Main basic pattern class - 'Factory'
  11.     /// Author: Boris Kotlyar
  12.     /// </summary>
  13.     public static class Factory
  14.     {
  15.         // cached types
  16.         private static Dictionary<string, Type> _entitiesByName;
  17.         private static bool IsInitialized => _entitiesByName != null;
  18.  
  19.         private static void Init()
  20.         {
  21.             // if already initialized, just skip
  22.             if (IsInitialized)
  23.                 return;
  24.  
  25.             // get all entities
  26.             var entitiesTypes = Assembly.GetAssembly(typeof(Entity)).GetTypes().Where(myType =>
  27.                 myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(Entity)));
  28.  
  29.             // dictionary for finding these by name later
  30.             _entitiesByName = new Dictionary<string, Type>();
  31.  
  32.             // get the names and put them to dictionary
  33.             foreach (var entitiesType in entitiesTypes)
  34.             {
  35.                 _entitiesByName.Add(entitiesType.Name, entitiesType);
  36.             }
  37.         }
  38.  
  39.         /// <summary>
  40.         /// Return component from MonoBehaviour
  41.         /// </summary>
  42.         /// <param name="obj">MonoBehaviour</param>
  43.         /// <param name="entityName">entity combonent based class type</param>
  44.         /// <returns></returns>
  45.         public static Entity GetComponentEntity(GameObject obj, string entityName)
  46.         {
  47.             Init();
  48.  
  49.             if (_entitiesByName.ContainsKey(entityName))
  50.             {
  51.                 var entity = obj.AddComponent(_entitiesByName[entityName]) as Entity;
  52.                 return entity;
  53.             }
  54.  
  55.             return null;
  56.         }
  57.  
  58.         /// <summary>
  59.         /// Get model entity
  60.         /// </summary>
  61.         /// <param name="entityName">entity class type</param>
  62.         /// <returns></returns>
  63.         public static Entity GetEntity(string entityName)
  64.         {
  65.             Init();
  66.  
  67.             if (_entitiesByName.ContainsKey(entityName))
  68.             {
  69.                 var type = _entitiesByName[entityName];
  70.                 var entity = Activator.CreateInstance(type) as Entity;
  71.                 return entity;
  72.             }
  73.  
  74.             return null;
  75.         }
  76.  
  77.         /// <summary>
  78.         /// Get all stored entities
  79.         /// </summary>
  80.         /// <returns></returns>
  81.         internal static IEnumerable<string> GetNames()
  82.         {
  83.             Init();
  84.             return _entitiesByName.Keys;
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment