Box.java :  » UnTagged » poliandroid » wensday » sensors » Android Open Source

Android Open Source » UnTagged » poliandroid 
poliandroid » wensday » sensors » Box.java
package wensday.sensors;

import android.graphics.Bitmap;

public class Box extends PosSprite implements OnEnterFrame{

  public static final int STAND = 0;
  public static final int HORIZONTAL = 1;
  public static final int VERTICAL = 2;
  public static final int FALL = 3;
  public static final int TRANSIT = 4;
  
  public static final int STAND_FRAME = 0;
  public static final int HORIZONTAL_FRAME = 28;
  public static final int VERTICAL_FRAME = 35;
  
  int state;
  Boolean busy = false;
  
  public int ox, oy;
  
  public Box(Bitmap bitmap) {
    super();
    
    init(bitmap, 250, 188, 1750, 56);
    setPivot(86, 109);
    setOnEnterFrame(this);
    
    ox = 0;
    oy = 0;
    
    resetCube();
    
    updateXY();
  }
  
  public void resetCube() {
    cx = ox;
    cy = oy;
    
    state = FALL;
    busy = true;
    
    visible = true;
    
    gotoAndStop(Box.STAND_FRAME);
  }
  
  public int getState() {
    return state;
  }
  
  public void fall() {
    gotoAndPlay(44);
    state = TRANSIT;
  }
  
  public void left() {
    if(busy)
      return;
    
    busy = true;
    
    switch(state) {
    case STAND:
      gotoAndPlay(21);
      break;
    case VERTICAL:
      cx -= 1;
      gotoAndStop(41);
      playReverse();
      break;
    case HORIZONTAL:
      cx -= 1;
      gotoAndStop(20);
      playReverse();
      break;
    }
    
    state = TRANSIT;
  }
  
  public void right() {
    if(busy)
      return;
    
    busy = true;
    
    switch(state) {
    case STAND:
      gotoAndPlay(14);
      break;
    case VERTICAL:
      gotoAndPlay(35);
      break;
    case HORIZONTAL:
      cx += 2;
      gotoAndStop(27);
      playReverse();
      break;
    }
    
    state = TRANSIT;
  }
  
  public void up() {
    if(busy)
      return;
    
    busy = true;
    
    switch(state) {
    case STAND:
      gotoAndPlay(7);
      break;
    case VERTICAL:
      cy -= 1;
      gotoAndStop(6);
      playReverse();
      break;
    case HORIZONTAL:
      cy -= 1;
      gotoAndStop(34);
      playReverse();
      break;
    }
    
    state = TRANSIT;
  }
  
  public void down() {
    if(busy)
      return;
    
    busy = true;
    
    switch(state) {
    case STAND:
      gotoAndPlay(0);
      break;
    case VERTICAL:
      gotoAndStop(13);
      playReverse();
      cy += 2;
      break;
    case HORIZONTAL:
      gotoAndPlay(28);
      break;
    }
    
    state = TRANSIT;
  }

  @Override
  public void enterFrame(int frame, int sense) {
    
    if(yOffset > 10)
      yOffset -= 10;
    else if(yOffset < -10)
      yOffset += 10;
    else {
      yOffset = 0;
      if(state == FALL) {
        state = STAND;
        busy = false;
      }
    }
  
    if(sense == 1)
      frameForward(frame);
    else if(sense == -1)
      frameReverse(frame);

  }
  
  public void frameForward(int frame) {
    switch(frame) {
    case 27:
      state = HORIZONTAL;
      cx -= 2;
      gotoAndStop(HORIZONTAL_FRAME);
      busy = false;
      break;
    case 20:
      state = HORIZONTAL;
      gotoAndStop(HORIZONTAL_FRAME);
      cx += 1;
      busy = false;
      break;
    case 34:
      state = HORIZONTAL;
      cy += 1;
      gotoAndStop(HORIZONTAL_FRAME);
      busy = false;
      break;
    case 6:
      state = VERTICAL;
      cy += 1;
      gotoAndStop(VERTICAL_FRAME);
      busy = false;
      break;
    case 13:
      state = VERTICAL;
      cy -= 2;
      gotoAndStop(VERTICAL_FRAME);
      busy = false;
      break;
    case 41:
      state = VERTICAL;
      gotoAndStop(VERTICAL_FRAME);
      cx += 1;
      busy = false;
      break;
    case 48:
      state = VERTICAL;
      visible = false;
      stop();
      break;
    }
  }
  
  public void frameReverse(int frame) {
    switch(frame) {
    case 21:
      state = STAND;
      gotoAndStop(STAND_FRAME);
      busy = false;
      break;
    case 14:
      state = STAND;
      gotoAndStop(STAND_FRAME);
      busy = false;
      break;
    case 28:
      state = HORIZONTAL;
      gotoAndStop(HORIZONTAL_FRAME);
      busy = false;
      break;
    case 0:
      state = STAND;
      gotoAndStop(STAND_FRAME);
      busy = false;
      break;
    case 7:
      state = STAND;
      gotoAndStop(STAND_FRAME);
      busy = false;
      break;
    case 35:
      state = VERTICAL;
      gotoAndStop(VERTICAL_FRAME);
      busy = false;
      break;
    }
  }
  
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.