Advertisement
rooster5105

Untitled

Jul 7th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.99 KB | None | 0 0
  1. package game.sprites;
  2.  
  3. import game.graphicsEngine.Animation;
  4. import game.graphicsEngine.Sprite;
  5.  
  6. import java.awt.Point;
  7. import java.lang.reflect.Constructor;
  8.  
  9. /*Unit.java
  10.  * @author Robert
  11.  * Empty, placeholder class, that will be used later.
  12.  */
  13. public abstract class Unit extends Sprite {
  14.  
  15.     private static final int DIE_TIME = 1000; //Time it takes for a creature to die.
  16.    
  17.     private static final int STATE_ALIVE = 0;
  18.     private static final int STATE_DYING = 1;
  19.     private static final int STATE_DEAD = 2;
  20.    
  21.     private static final int NUM_OF_STATES = 3;
  22.    
  23.     private Animation left;
  24.     private Animation right;
  25.     private Animation up;
  26.     private Animation down;
  27.     private Animation dyingLeft;
  28.     private Animation dyingRight;
  29.     private Animation dyingUp;
  30.     private Animation dyingDown;
  31.     private Point destination;
  32.    
  33.     private int state;
  34.     private int stateTime;
  35.    
  36.     public Unit(Animation left, Animation right, Animation up, Animation down, Animation dyingLeft, Animation dyingRight,
  37.                 Animation dyingUp, Animation dyingDown){
  38.         super(right);
  39.         this.left = left;
  40.         this.right = right;
  41.         this.down = down;
  42.         this.up = up;
  43.         state = STATE_ALIVE;
  44.     }
  45.    
  46.     public Object clone(){
  47.         Constructor<?> cTor = getClass().getConstructors()[0];
  48.         try{
  49.             return cTor.newInstance(new Object[]{
  50.             (Animation)left.clone(),
  51.             (Animation)right.clone(),
  52.             (Animation)up.clone(),
  53.             (Animation)down.clone(),
  54.             (Animation)dyingLeft.clone(),
  55.             (Animation)dyingRight.clone(),
  56.             (Animation)dyingUp.clone(),
  57.             (Animation)dyingDown.clone()
  58.             });
  59.         }
  60.         catch(Exception ex){
  61.             //should not happen.  Do Nothing.
  62.             ex.printStackTrace();
  63.             return null;
  64.         }
  65.     }
  66.    
  67.     public float getMaxSpeed(){
  68.         return 0;
  69.     }
  70.     public void wakeup() {
  71.         if (getState() == STATE_ALIVE && destination != null){
  72.             moveTowardsDestination(destination);
  73.         }
  74.     }
  75.    
  76.     private void moveTowardsDestination(Point destination2) {
  77.         // TODO Auto-generated method stub
  78.        
  79.     }
  80.  
  81.     public int getState(){
  82.         return state;
  83.     }
  84.    
  85.     public void setState(int state){
  86.         if(this.state != state){
  87.             this.state = state;
  88.             stateTime = 0;
  89.             if (state == STATE_DYING){
  90.                 setVelocityX(0);
  91.                 setVelocityY(0);
  92.             }
  93.         }
  94.     }
  95.    
  96.     public boolean isAlive(){
  97.         return (state == STATE_ALIVE);
  98.     }
  99.    
  100.     public void collideHorizontal(){
  101.         setVelocityX(0);
  102.     }
  103.    
  104.     public void collideVertical(){
  105.         setVelocityY(0);
  106.     }
  107.    
  108.     public void update(long elapsedTime){
  109.         Animation newAnim = anim;
  110.         if(getVelocityX() < 0){
  111.             anim = left;
  112.         }
  113.        
  114.         else if (getVelocityY() < 0){
  115.             anim = up;
  116.         }
  117.        
  118.         else if (getVelocityX() > 0 ){
  119.             anim = right;
  120.         }
  121.         else if (getVelocityY() > 0){
  122.             anim = down;
  123.         }
  124.         else if(getState() == STATE_DYING && newAnim == left){
  125.             anim = dyingLeft;
  126.         }
  127.         else if(getState() == STATE_DYING && newAnim == right){
  128.             anim = dyingRight;
  129.         }
  130.         else if(getState() == STATE_DYING && newAnim == up){
  131.             anim = dyingUp;
  132.         }
  133.         else if(getState() == STATE_DYING && newAnim == down){
  134.             anim = dyingDown;
  135.         }
  136.     }
  137. }
  138.  
  139. //###END UNIT###
  140.  
  141.  
  142. //###UNITTYPE###
  143. package game.sprites;
  144. import game.graphicsEngine.*;
  145.  
  146. public enum UnitType {
  147.     UNIT1(left, right, up, down, dyingLeft, dyingRight, dyingUp, dyingDown); //wants me to make them static.
  148.     Animation left;                              //but if I do, it gets pissed off
  149.     Animation right;                             //that I cant access a member before its defined
  150.     Animation  up;                               //And Yells at me below that I can't modify a Static.
  151.     Animation  down;
  152.     Animation  dyingLeft;
  153.     Animation  dyingRight;
  154.     Animation  dyingUp;
  155.     Animation  dyingDown;
  156.    
  157.     private UnitType(Animation left,
  158.                         Animation right,
  159.                         Animation  up,
  160.                         Animation  down,
  161.                         Animation  dyingLeft,
  162.                         Animation  dyingRight,
  163.                         Animation  dyingUp,
  164.                         Animation  dyingDown){
  165.         this.left = left;
  166.         this.right = right;
  167.         this.up = up;
  168.         this.down = down;
  169.         this.dyingLeft = dyingLeft;
  170.         this.dyingRight = dyingRight;
  171.         this.dyingUp = dyingUp;
  172.         this.dyingDown = dyingDown;
  173.     }
  174.    
  175.    
  176. }
  177. //###End UnitType###
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement