Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (736)
Games in Android Showcase (223)
games submitted by our members
Games in WIP (813)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Roquen (posted 2012-07-19 10:41:39, viewed 293 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182  
package entity;

import java.util.*;


/**
 * <h2>User Variables</h2>
 * <p>
 * User variables allow designers to assign additional data to the various
 * exposed {@link GameObject}. This allows designing scripts which are
 * data-driven and building of functionality of which the code base is not
 * specifically aware.
 * <p>
 * These <i>user variables</i> are logically <i>typeless</i>, that is to 
 * say that no specific type associated with a given variable. At any point
 * in time the active type will be that of the most recent assignment. Any
 * unassigned variable query will return a default value of that type and
 * any existing variable which is assigned this default value will automatically
 * be deleted.
 * <p>
 *
 */
abstract class GameObject<T extends GameObject<T>>
{ 
  //***
  static final String EMPTY_STRING = "";
  
  static final String stringFilter(String s)
  {
    if (s != null) return s;
    return EMPTY_STRING;
  }
  
  
  //*** dynamic variable support stuff
  
  HashMap<String, UserVar> dvars = null;
  
  final UserVar getVar(String name)
  {
    if (dvars != null) {
      UserVar v = dvars.get(name);
      
      if (v != null)
        return v;
    }
    
    return UserVar.NILL;
  }
  
  /**
   * Returns <i>true</i> if there is a dynamic variable of the
   * specified name defined.
   */
  public final boolean isVarDefined(String name)
  {
    return getVar(name) != UserVar.NILL;
  }
  
  /**
   * Returns the value of the variable as an integer.
   * <p>
   * If the name is currently assigned to a <tt>float</tt> then
   * the result is the same as: <tt>(int)getFloat(name)</tt>.  In
   * all other cases, including undefined, the result is zero.
   */
  public final int getInt(String name)
  {
    return getVar(name).getInt();
  }
  
  /**
   * Returns the value of the variable as a float.
   * <p>
   * If the name is currently assigned to a <tt>int</tt> then
   * the result is the same as: <tt>getInt(name)</tt>.  In
   * all other cases, including undefined, the result is zero.
   */
  public final float getFloat(String name)
  {
    return getVar(name).getFloat();
  }
  
  /**
   * Return the value of the variable as a {@link Entity}.
   * <p>
   * If the variable is question is not entity or is
   * undefined, then the result is {@link Entity.NILL}.
   */
  public final Entity getEntity(String name)
  {
    return getVar(name).getEntity();
  }
  
  /**
   * Returns the value of the variable as a {@link String}.
   * <p>
   * If the variable is currently assigned an integer or a float, then
   * the result is that value converted into a string.  In all other
   * cases, including undefined, the result is an empty string.
   */
  public final String getString(String name)
  {
    return  getVar(name).getString();
  }

  private final void deleteVar(String name)
  {
    if (dvars != null) {
      dvars.remove(name);
      
      if (dvars.size() != 0)
        return;
      
      dvars = null;
    }
  }

  private final void setVar(String name, UserVar value)
  {
    if (dvars == null) dvars = new HashMap<String, UserVar>();
    dvars.put(name, value);
  }

  // NOTE: All the sets could be optimized. Such as not creating
  // a new UserVar.TYPE object if one already exists and is of
  // the same type. Additionally if the new value if a different
  // type in int vs. float from current but is perfectly
  // representable in current, then current type could be used.
  
  /**
   * Sets or creates a dynamic variable with the specified name and value.
   * <p>
   * <tt>setInt(0)</tt> automatically deletes.
   */
  public final void setInt(String name, int value)
  {
    if (value != 0)
      setVar(name, new UserVar.INT(value));
    else
      deleteVar(name);
  }
  
  /**
   * Sets or creates a dynamic variable with the specified name and value.
   * <p>
   * <tt>setFloat(0)</tt> automatically deletes.
   */
  public final void setFloat(String name, float value)
  {
    if (value != 0)
      setVar(name, new UserVar.FLOAT(value));
    else
      deleteVar(name);
  }
  
  /**
   * Sets or creates a dynamic variable with the specified name and value.
   * <p>
   * <tt>setEntity(null)</tt> automatically deletes.
   */
  public final void setEntity(String name, Entity value)
  {
    if (value != null && value != Entity.NILL)
      setVar(name, new UserVar.ENTITY(value));
    else
      deleteVar(name);
  }
  
  /**
   * Sets or creates a dynamic variable with the specified name and value.
   * <p>
   * <tt>setString(null)</tt> automatically deletes.
   */
  public final void setString(String name, String value)
  {
    if (value != null && value.length() != 0)
      setVar(name, new UserVar.STRING(value));
    else
      deleteVar(name);
  }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
cybrmynd (120 views)
2017-08-02 12:28:51

cybrmynd (144 views)
2017-08-02 12:19:43

cybrmynd (138 views)
2017-08-02 12:18:09

Sralse (154 views)
2017-07-25 17:13:48

Archive (624 views)
2017-04-27 17:45:51

buddyBro (733 views)
2017-04-05 03:38:00

CopyableCougar4 (1260 views)
2017-03-24 15:39:42

theagentd (1239 views)
2017-03-24 15:32:08

Rule (1216 views)
2017-03-19 12:43:22

Rule (1268 views)
2017-03-19 12:42:17
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!