Friday, January 23, 2015

Anansi: Sprites - Part 2


Now that we have a sprite base class, we can subclass it and easily add various objects to the playfield.

We can create the class for the background ...

public class Background extends SpriteBase {

 public Background(Pane layer, Image image, double x, double y, double speed) {
  super(layer, image, x, y, 0, 0, speed, 0, Double.MAX_VALUE, 0);
 }

 public void move() {
  
  super.move();
  
  checkBounds();
  
 }

 private void checkBounds() {
  
     // check bounds. we scroll upwards, so the y position is negative. once it's > 0 we have reached the end of the map and stop scrolling
     if( Double.compare( y, 0) >= 0) {
      y = 0;
     }
     
 }
 
 @Override
 public void checkRemovability() {
  // nothing to do
 }


}

and similarly easy the cloud class which has an additional opacity attribute, depending on the layer ...
public class Cloud extends SpriteBase {

 public Cloud(Pane layer, Image image, double x, double y, double speed, double opacity) {
  
  super(layer, image, x, y, 0.0, 0.0, speed, 0.0, Double.MAX_VALUE, 0.0);

  getView().setOpacity(opacity);
  
 }

 @Override
 public void checkRemovability() {

  if( Double.compare( getY(), Settings.SCENE_HEIGHT) > 0) {
   setRemovable(true);
  }
  
 }


}

... and we create the player bullet class ...
public class PlayerBullet extends SpriteBase {

 public PlayerBullet(Pane layer, Image image, double x, double y, double dx, double dy) {
  super(layer, image, x, y, 0, dx, dy, 0, 1, 1); // TODO: health/damage
 }

 public PlayerBullet(Pane layer, Image image, double x, double y, double r, double dx, double dy, double dr, double health, double damage) {
  super(layer, image, x, y, r, dx, dy, dr, health, damage);
 }

 @Override
 public void checkRemovability() {
  
  // upper bounds exceeded
  if( ( getY() + getHeight()) < 0) {
   setRemovable(true);
  }
  
 }

}

... and the enemy class.
public class Enemy extends SpriteBase {

 public Enemy(Pane layer, Image image, double x, double y, double r, double dx, double dy, double dr, double health, double damage) {
  super(layer, image, x, y, r, dx, dy, dr, 1, 1); // TODO: health/damage
 }

 @Override
 public void checkRemovability() {

  if( Double.compare( getY(), Settings.SCENE_HEIGHT) > 0) {
   setRemovable(true);
  }

  
 }
}

No comments:

Post a Comment