SHARE
TWEET

Default unit Action class

Darker666 Nov 13th, 2014 148 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3. using RTS;
  4. using Defs;
  5. using System;
  6. using System.Reflection;
  7. using Units;
  8. using System.Collections.Generic;
  9. /**
  10.  * Class for unit/building action
  11.  * Contains cursor information, button image and other GUI details as well as the actual function,
  12.  * cooldown, warmup etc.
  13.  * Every world object needs own action instance
  14.  */
  15.  
  16.  
  17. namespace Actions
  18. {
  19.     public abstract class Action
  20.     {
  21.         private static Dictionary<string, ConstructorInfo> constructors = new Dictionary<string, ConstructorInfo>();
  22.         public static Action fromString(string className, string defName, WorldObject actor)
  23.         {
  24.             ConstructorInfo constructor;
  25.             //Try to load cache
  26.             if (constructors.ContainsKey(className))
  27.             {
  28.                 constructor = constructors[className];
  29.             }
  30.             else
  31.             {
  32.                 //Get the Assembly (namespace)
  33.                 //Assembly assembly = Assembly.Load("Actions");
  34.                 //Get the exact class Type
  35.                 Type t = Type.GetType("Actions." + className);
  36.                 if (t == null)
  37.                 {
  38.                     Debug.LogError("Type 'Actions." + className + "' not found!");
  39.                     constructors.Add(className, null);
  40.                     return null;
  41.                 }
  42.                 //Get the info about constructor (using array literal)
  43.                 constructor = t.GetConstructor(new Type[] { typeof(string), typeof(WorldObject) });
  44.                 //Save in cache
  45.                 constructors.Add(className, constructor);
  46.             }
  47.             //Constructor can be (theoretically) null
  48.             if (constructor!=null)
  49.             {
  50.                 //Initialise the Type instance
  51.                 System.Object action = constructor.Invoke(new System.Object[] { defName, actor });
  52.                 //If it's child of the main class
  53.                 if (action is Action)
  54.                     return (Action)action;
  55.                 //Error otherwise
  56.                 else
  57.                 {
  58.                     Debug.LogError("'" + className + "' is not child of Action!");
  59.                     //Nullify the constructor as it obviously sucks
  60.                     constructors[className] = null;
  61.                     return null;
  62.                 }
  63.             }
  64.             else
  65.             {
  66.                 Debug.LogError("No suitable constructor found for 'Actions." + className + "'.");
  67.                 return null;
  68.             }
  69.         }
  70.  
  71.         // TIMING
  72.         //Used to calculate remaining cooldown
  73.         protected float lastUsed = -9999999;
  74.         //Used to calculate warmup delay
  75.         protected float lastStarted = -9999999;
  76.         //Tells that the action is running
  77.         protected bool running = false;
  78.  
  79.         // ACTION TARGETS
  80.         WorldObject targetObj;
  81.         Vector3 targetLoc;
  82.  
  83.         // RESOURCES
  84.         public ActionDef def;
  85.         protected LazyResource<Texture2D> menuImage;
  86.  
  87.         // REFERENCES
  88.         protected WorldObject actor;  //Object that uses (is capable of) this action
  89.  
  90.         //private Texture2D cursorTex = null;
  91.         //public Texture2D cursor { get { return ResourceManager.loadTexture(ref cursorTex, HoverCursor); } }
  92.  
  93.         public Action(string def, WorldObject act)
  94.         {
  95.             if (act)
  96.                 actor = act;
  97.             else
  98.             {
  99.                 Debug.LogError("Action with no parent object!");
  100.             }
  101.  
  102.             if (def.Length > 0)
  103.                 this.def = DefStorage.getDef(def) as ActionDef;
  104.             else
  105.                 this.def = DefStorage.getDef(getDefName()) as ActionDef;
  106.             if (this.def == null)
  107.                 this.def = new ActionDef();
  108.             //this.def = ResourceManager.
  109.             menuImage = new LazyResource<Texture2D>(this.def.MenuIcon);
  110.             //Init def
  111.             //InitDef();
  112.         }
  113.         //Allows classes to alter the definition
  114.         /*protected virtual void InitDef()
  115.         {
  116.            
  117.         }*/
  118.         //Allows classes to override def name
  119.         protected virtual string getDefName()
  120.         {
  121.             return "";
  122.         }
  123.  
  124.         //Start the action
  125.         //Returns on success
  126.         public virtual bool Start(Vector3? targ_location = null, WorldObject target = null)
  127.         {
  128.             if (!CanStart())
  129.             {
  130.                 Debug.Log("Action '" + this.GetName() + "' can't start.");
  131.                 return false;
  132.             }
  133.             else
  134.             {
  135.                 Debug.Log("Action '" + this.GetName() + "' started.");
  136.             }
  137.             //The action already started (now handled at CanStart())
  138.             /*if (running)
  139.             {
  140.                 //Either re-start
  141.                 if (def.cancelable)
  142.                 {
  143.                     lastStarted = Time.time;
  144.                     return true;
  145.                 }
  146.                 //Or ignore the request
  147.                 else
  148.                 {
  149.                     return false;
  150.                 }
  151.             }*/
  152.             if (targ_location.HasValue)
  153.                 targetLoc = targ_location.Value;
  154.             else
  155.                 targetLoc = new Vector3(-99999, -99999, -99999);
  156.             targetObj = target;
  157.             //Start normally
  158.             running = true;
  159.             lastStarted = Time.time;
  160.             //May cause problems, let's see
  161.             if (def.warmup == 0)
  162.                 Execute();
  163.             return true;
  164.         }
  165.         //Execute the action - do whatever the action actually does
  166.         //RESPONSIBLE FOR TURNING OFF THE RUNNING SETTING!!!
  167.         public abstract void Execute();
  168.  
  169.         //Updates the action (on cooldowns etc)
  170.         public virtual void Update()
  171.         {
  172.             if (running && Time.time - lastStarted >= def.warmup)
  173.             {
  174.                 lastUsed = Time.time;
  175.                 Execute();
  176.             }
  177.         }
  178.  
  179.  
  180.         //Function to get menu image - allows to alter icons as needed
  181.         public virtual string GetMenuIconPath()
  182.         {
  183.             return def.MenuIcon;
  184.         }
  185.         //Getter for the menu icon texture
  186.         public virtual Texture2D GetMenuIcon()
  187.         {
  188.             return menuImage.res;
  189.         }
  190.         //Get title
  191.         public virtual string GetName()
  192.         {
  193.             return def.name;
  194.         }
  195.         public override string ToString()
  196.         {
  197.             return GetName();
  198.         }
  199.         //constructor
  200.         /*public Action()
  201.         {
  202.             //Preloads static menu icon
  203.            
  204.         }*/
  205.  
  206.         public virtual bool CanStart()
  207.         {
  208.             float time = Time.time;
  209.             return time - lastUsed >= def.cooldown && (def.cancelable || !running);
  210.         }
  211.     }
  212. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top