Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. namespace App
  2. {
  3.     public class World
  4.     {
  5.         private readonly Dictionary<Type, object> Resources = new Dictionary<Type, object>();
  6.  
  7.         public void Insert<T>(T resource)
  8.             where T : class
  9.         {
  10.             Resources.Add(typeof(T), resource);
  11.         }
  12.  
  13.         public T Fetch<T>()
  14.             where T : class
  15.         {
  16.             if (Resources.TryGetValue(typeof(T), out var resource))
  17.                 return (T) resource;
  18.             return null;
  19.         }
  20.  
  21.         public bool Contains<T>()
  22.             where T : class
  23.         {
  24.             return Resources.ContainsKey(typeof(T));
  25.         }
  26.     }
  27.  
  28.     public static class WorldVal
  29.     {
  30.         private class ValueWrapper<T>
  31.         {
  32.             public T Val;
  33.         }
  34.  
  35.         public static T Fetch<T>(this World world)
  36.             where T : struct
  37.         {
  38.             return world.Fetch<ValueWrapper<T>>().Val;
  39.         }
  40.  
  41.         public static void Insert<T>(this World world, T resource)
  42.             where T : struct
  43.         {
  44.             world.Insert(new ValueWrapper<T> {Val = resource});
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement