Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.25 KB | None | 0 0
  1. /*
  2.  * Created on Oct 24, 2010
  3.  */
  4.  
  5. package engine.rts;
  6.  
  7. public class BindingTest
  8. {
  9.    public static void main(String[] args)
  10.    {
  11.       Unit unit = new Unit();
  12.       Tree tree = new Tree();
  13.  
  14.       tree.health = 88;
  15.  
  16.       unit.startChopping(tree);
  17.  
  18.       while (true)
  19.       {
  20.          unit.tick();
  21.          tree.tick();
  22.  
  23.          if (unit.choppingTree == null)
  24.             break;
  25.       }
  26.    }
  27.  
  28.    public static class Unit
  29.    {
  30.       Tree choppingTree;
  31.  
  32.       public void startChopping(Tree tree)
  33.       {
  34.          tree.binding.listen("chopped", this);
  35.          this.choppingTree = tree;
  36.       }
  37.  
  38.       public void tick()
  39.       {
  40.          if (this.choppingTree != null)
  41.          {
  42.             System.out.println("chop!");
  43.             this.choppingTree.health -= 10;
  44.          }
  45.       }
  46.  
  47.       private void onTreeChopped(Tree tree)
  48.       {
  49.          System.out.println("tree was chopped!");
  50.          this.choppingTree = null;
  51.       }
  52.    }
  53.  
  54.    public static class Tree
  55.    {
  56.       private final Binding binding = new Binding(this);
  57.  
  58.       public int            health  = 55;
  59.  
  60.       public void tick()
  61.       {
  62.          if (this.health <= 0)
  63.          {
  64.             this.binding.fire("chopped");
  65.          }
  66.       }
  67.    }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement