Advertisement
Zidinjo

Bullet

Nov 14th, 2015
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. class Bullet extends DynamicGameObject {
  2.  
  3.   private GameManager gameManager;
  4.   private int bulletShipID;
  5.   public float bulletSize;
  6.   public color fColor;
  7.  
  8.   Bullet(GameManager gameManager, int bulletShipID) {
  9.     // call the constructor of the superclass
  10.     super(gameManager);
  11.     this.gameManager = gameManager;
  12.     this.bulletShipID = bulletShipID;
  13.   }
  14.  
  15.   /*
  16.    The debugInfo method is called in the method runGameLoopCycle
  17.    of the GameManager class for every game object once per frame.
  18.    Use it for debug information on the console.
  19.    */
  20.  
  21.   public void debugInfo() {
  22.     // call this method of superclass
  23.     super.debugInfo();
  24.     // .. implement me ...
  25.   }
  26.  
  27.   public void setBulletSize(float bulletSize){
  28.     this.bulletSize = bulletSize;
  29.   }
  30.  
  31.   /*
  32.    The update method is called in the method runGameLoopCycle
  33.    of the GameManager class for every game object once per frame.
  34.    */
  35.  
  36.   void update() {  
  37.     super.update();
  38.     position.set(position.x+speed*cos(orientation),position.y+speed*sin(orientation));
  39.     if (position.x < 0 || position.x > width || position.y < 0 || position.y > height){
  40.       this.gameManager.removeObject(this);
  41.     }
  42.   }
  43.  
  44.   /*
  45.    This method is called by the gameManager when this object
  46.    collides with another GameObject (object).
  47.    Often the response depends on the class of the colliding object.
  48.    */
  49.  
  50.   void collidesWith(GameObject object) {
  51.     if(object instanceof RedSpaceShip && bulletShipID == 2){
  52.       object.hitted = true;
  53.       gameManager.removeObject(this);
  54.     }
  55.    
  56.     if(object instanceof BlueSpaceShip && bulletShipID == 1){
  57.       object.hitted = true;
  58.       gameManager.removeObject(this);
  59.     }
  60.    
  61.     if(object instanceof EnemySpaceShip && bulletShipID == 1){
  62.       gameManager.removeObject(this);
  63.       gameManager.removeObject(object);
  64.     }
  65.    
  66.     if(object instanceof BlueSpaceShip && bulletShipID == 3){
  67.       gameManager.removeObject(this);
  68.     }
  69.    
  70.     if(object instanceof Bullet){
  71.       gameManager.removeObject(object);
  72.       gameManager.removeObject(this);
  73.     }
  74.   }
  75.  
  76.   void renderShape() {
  77.     fill(fColor);
  78.     beginShape();
  79.     ellipse(0,0,bulletSize,bulletSize);
  80.     endShape();
  81.   }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement