SHARE
TWEET

Untitled

a guest Feb 28th, 2016 48 Never
  1. using System.Collections.Generic;
  2. using System;
  3.  
  4. namespace Actions
  5. {
  6.     public abstract class Action
  7.     {
  8.         private static Dictionary<string, Action> allActions = null;
  9.  
  10.         private string actionName = "BaseAction";
  11.         private List<Type> possibleCallers = new List<Type>();
  12.         private List<Type> allowedTargets = new List<Type>();
  13.  
  14.         /// <summary>
  15.         /// this is the list of requirements that caller and target must shatisfy in order to be executed
  16.         /// </summary>
  17.         protected List<Requirement> requirements = new List<Requirement>();
  18.         /// <summary>
  19.         /// if a action is enable by default, every actor instantiated in the list of allowed types
  20.         /// will include this as allowed action.
  21.         /// </summary>
  22.         protected bool enabledByDefault = false;
  23.         /// <summary>
  24.         /// if target in the possible target list should be allowed by default, set this to true.
  25.         /// </summary>
  26.         protected bool allowedByDefault = true;
  27.  
  28.         /// <summary>
  29.         /// return true if the type of the actor is one of those that can call this action.
  30.         /// </summary>
  31.         /// <param name="actorType"></param>
  32.         /// <returns></returns>
  33.         public bool canBeCalledBy(Type actorType)
  34.         {
  35.             bool foundOne = false;
  36.             foreach (Type t in possibleCallers)
  37.             {
  38.                 if (actorType == t)
  39.                 {
  40.                     foundOne = true;
  41.                     break;
  42.                 }
  43.             }
  44.  
  45.             if (!foundOne)
  46.                 return false;
  47.  
  48.             return true;
  49.         }
  50.  
  51.         /// <summary>
  52.         /// returns the list of requirements that are not shatisfied by a particular activation settings
  53.         /// </summary>
  54.         /// <param name="caller"></param>
  55.         /// <param name="targets"></param>
  56.         /// <param name="otherArgs"></param>
  57.         /// <returns></returns>
  58.         public List<Requirement> getNotShatishiedRequirements(Actor caller, List<Target> targets, List<string> otherArgs)
  59.         {
  60.             List<Requirement> diRitorno = new List<Requirement>();
  61.             foreach (Requirement rq in requirements)
  62.                 if (!rq.isShatishied(caller, targets, otherArgs))
  63.                     diRitorno.Add(rq);
  64.             return diRitorno;
  65.         }
  66.  
  67.         /// <summary>
  68.         /// add a type to the list of the possible callers.
  69.         /// it does not influence already instantiated callers.
  70.         /// </summary>
  71.         /// <param name="t"></param>
  72.         public void addPossibleCaller(Type t)
  73.         {
  74.             if (t.IsSubclassOf(typeof(Actor)))
  75.                 possibleCallers.Add(t);
  76.         }
  77.  
  78.         /// <summary>
  79.         /// remove a type from the list of possible callers.
  80.         /// it does not influence already instantiated callers.
  81.         /// </summary>
  82.         /// <param name="t"></param>
  83.         public void removePossibleCaller(Type t)
  84.         {
  85.             if (t.IsSubclassOf(typeof(Actor)))
  86.                 possibleCallers.Remove(t);
  87.         }
  88.  
  89.         /// <summary>
  90.         /// remove a possible target from the possible target list
  91.         /// does not influence already instantiatied targets.
  92.         /// </summary>
  93.         /// <param name="t"></param>
  94.         public void removePossibleTarget(Type t)
  95.         {
  96.             if (t.IsSubclassOf(typeof(Target)))
  97.                 allowedTargets.Remove(t);
  98.         }
  99.  
  100.         /// <summary>
  101.         /// add a possible target from the possible target list
  102.         /// does not influence already instantiatied targets.
  103.         /// </summary>
  104.         /// <param name="t"></param>
  105.         public void addPossibleTarget(Type t)
  106.         {
  107.             if (t.IsSubclassOf(typeof(Target)))
  108.                 allowedTargets.Add(t);
  109.         }
  110.  
  111.         /// <summary>
  112.         /// return if the target type is one of the one that can be taken as target of this action.
  113.         /// </summary>
  114.         /// <param name="targetType"></param>
  115.         /// <returns></returns>
  116.         public bool canBeAppliedAt(Type targetType)
  117.         {
  118.             bool foundOne = false;
  119.             foreach (Type t in allowedTargets)
  120.             {
  121.                 if (targetType == t)
  122.                 {
  123.                     foundOne = true;
  124.                     break;
  125.                 }
  126.             }
  127.             if (!foundOne)
  128.                 return false;
  129.             //throw new ArgumentException(actor.getType().Name +" cannot call "+getName()+" action")
  130.             return true;
  131.         }
  132.  
  133.         /// <summary>
  134.         /// check if every possible condition is shatisfied.
  135.         /// </summary>
  136.         /// <param name="caller"></param>
  137.         /// <param name="targets"></param>
  138.         /// <param name="otherArgs"></param>
  139.         /// <returns></returns>
  140.         public bool everythingShatishied(Actor caller, List<Target> targets, List<string> otherArgs)
  141.         {
  142.             if (!caller.canCallAction(actionName))
  143.                 return false;
  144.  
  145.             foreach (Target t in targets)
  146.                 if (!t.canBeInteractedBy(this, caller))
  147.                     return false;
  148.            
  149.             if (getNotShatishiedRequirements(caller, targets, otherArgs).Count != 0)
  150.                 return false;
  151.  
  152.             return true;
  153.         }
  154.  
  155.         /// <summary>
  156.         /// effetuate the action. if it cannot be called for any reason, it will not be executed.
  157.         /// </summary>
  158.         /// <param name="caller"></param>
  159.         /// <param name="targets"></param>
  160.         /// <param name="otherArgs"></param>
  161.         public void call(Actor caller, List<Target> targets, List<string> otherArgs)
  162.         {
  163.             if (everythingShatishied(caller, targets, otherArgs))
  164.                 apply(caller, targets, otherArgs);
  165.         }
  166.  
  167.         protected Action()
  168.         {
  169.             Action.allActions.Add(actionName, this);
  170.             actionName = GetType().Name;
  171.         }
  172.  
  173.         /// <summary>
  174.         /// return the action from his name
  175.         /// </summary>
  176.         /// <param name="name"></param>
  177.         /// <returns></returns>
  178.         public static Action getAction(string name)
  179.         {
  180.             if (Action.allActions == null)
  181.                 Action.loadActions();
  182.             return Action.allActions["name"];
  183.         }
  184.  
  185.         private static void loadActions()
  186.         {
  187.             allActions = new Dictionary<string, Action>();
  188.             List<Type> listOfType = new List<Type>();
  189.             foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
  190.             {
  191.                 foreach (var type in asm.GetTypes())
  192.                 {
  193.                     if (type.BaseType == typeof(Action))
  194.                         listOfType.Add(type);
  195.                 }
  196.             }
  197.             foreach (Type t in listOfType)
  198.             {
  199.                 if (t.IsAbstract)
  200.                     continue;
  201.                 var ctors = t.GetConstructors();
  202.                 ctors[0].Invoke(new object[] { });
  203.             }
  204.         }
  205.  
  206.         /// <summary>
  207.         /// return the name of this particular action.
  208.         /// </summary>
  209.         /// <returns></returns>
  210.         public string getName()
  211.         {
  212.             return actionName;
  213.         }
  214.  
  215.         /// <summary>
  216.         /// you must implement this, when this is called requirements are alaready checked.
  217.         /// </summary>
  218.         /// <param name="caller"></param>
  219.         /// <param name="targets"></param>
  220.         /// <param name="otherArgs"></param>
  221.         protected abstract void apply(Actor caller, List<Target> targets, List<string> otherArgs);
  222.  
  223.         /// <summary>
  224.         /// return the list of actions that by default can be exectuted by an actor
  225.         /// </summary>
  226.         /// <param name="actor"></param>
  227.         /// <returns></returns>
  228.         public static List<Action> getDefaultEnabledActions(Actor actor)
  229.         {
  230.             List<Action> diRitorno = new List<Action>();
  231.             if (Action.allActions == null)
  232.                 Action.loadActions();
  233.             foreach (Action action in allActions.Values)
  234.             {
  235.                 if (action.canBeCalledBy(actor.GetType()) && action.enabledByDefault)
  236.                     diRitorno.Add(action);
  237.             }
  238.  
  239.             return diRitorno;
  240.         }
  241.  
  242.         /// <summary>
  243.         /// return the list of actions that by default can be applied to a target
  244.         /// </summary>
  245.         /// <param name="actor"></param>
  246.         /// <returns></returns>
  247.         public static List<Action> getDefaultAllowedActions(Target target)
  248.         {
  249.             if (allActions == null)
  250.                 loadActions();
  251.             List<Action> diRitorno = new List<Action>();
  252.             if (Action.allActions == null)
  253.                 Action.loadActions();
  254.  
  255.             foreach (Action action in allActions.Values)
  256.             {
  257.                 if (action.canBeAppliedAt(target.GetType()) && action.allowedByDefault)
  258.                     diRitorno.Add(action);
  259.             }
  260.  
  261.             return diRitorno;
  262.         }
  263.     }
  264. }
RAW Paste Data
Top