Create Animation, base Explosions on Animation, create MultiExplosion.

This commit is contained in:
Jan Philipp Timme 2014-11-11 11:14:32 +01:00
parent 223612de32
commit 671e9f5ef0
8 changed files with 190 additions and 168 deletions

View File

@ -0,0 +1,69 @@
package de.teamteamteam.spacescooter.entity;
/**
* A very simple image slideshow animation.
* Given a list of images and an interval, it does a (fast) slideshow
* through all the images, changing them after <interval> ticks.
*/
public class Animation extends Entity {
/**
* The list of images to display
*/
private String[] images;
/**
* Internal counter to check whether the interval is over.
*/
private int counter;
/**
* Interval parameter - change the image after each interval.
*/
private int interval;
/**
* Index of the current image that is displayed.
*/
private int currentImage;
/**
* Default constructor.
*/
public Animation(int x, int y) {
super(x, y);
}
/**
* Set the list of images to display plus the interval.
*/
public void configure(String[] images, int interval) {
this.images = images;
this.interval = interval;
this.counter = 0;
this.currentImage = 0;
this.setImage(this.images[this.currentImage]);
}
/**
* Increase the counter every tick, then check whether the interval is over.
* If the interval is over, check whether there are more images to display.
* If there is one, display the next image, else it is just over and the
* Animation removes itself.
*/
public void update() {
this.counter++;
if(this.counter % this.interval == 0) {
this.currentImage++;
if(this.currentImage == this.images.length) {
this.remove();
} else {
this.setImage(this.images[this.currentImage]);
}
}
}
}

View File

@ -3,7 +3,7 @@ package de.teamteamteam.spacescooter.entity;
import java.awt.Rectangle;
import de.teamteamteam.spacescooter.entity.enemy.Enemy;
import de.teamteamteam.spacescooter.entity.explosion.Explosion;
import de.teamteamteam.spacescooter.entity.explosion.ExplosionTwo;
import de.teamteamteam.spacescooter.entity.shot.Shot;
import de.teamteamteam.spacescooter.entity.spi.Collidable;
import de.teamteamteam.spacescooter.entity.spi.Hittable;
@ -60,6 +60,8 @@ public abstract class LivingEntity extends Entity implements Collidable, Hittabl
public void collideWith(Collidable entity) {
if(entity instanceof Shot) {
Shot s = (Shot) entity;
if(this instanceof Enemy && s.getDirection() == Shot.LEFT) return;
if(this instanceof Player && s.getDirection() == Shot.RIGHT) return;
this.takeDamage(s.getDamageValue());
}
if(entity instanceof Player && (!(this instanceof Player))) {
@ -120,7 +122,7 @@ public abstract class LivingEntity extends Entity implements Collidable, Hittabl
* Override this method for a different explosion behaviour.
*/
public void explode() {
new Explosion(this.getX(), this.getY());
new ExplosionTwo(this.getX(), this.getY());
}
/**

View File

@ -5,6 +5,7 @@ import java.util.Random;
import de.teamteamteam.spacescooter.datastructure.ConcurrentIterator;
import de.teamteamteam.spacescooter.entity.Entity;
import de.teamteamteam.spacescooter.entity.Player;
import de.teamteamteam.spacescooter.entity.explosion.MultiExplosion;
import de.teamteamteam.spacescooter.entity.item.Items;
import de.teamteamteam.spacescooter.screen.Screen;
import de.teamteamteam.spacescooter.utility.GameConfig;
@ -42,6 +43,14 @@ public class EnemyThree extends Enemy{
super.die();
}
/**
* Custom MultiExplosion for this enemy.
*/
@Override
public void explode() {
new MultiExplosion(this.getX(), this.getY());
}
@Override
public void update() {
super.update();

View File

@ -1,67 +0,0 @@
package de.teamteamteam.spacescooter.entity.explosion;
import java.util.Random;
import de.teamteamteam.spacescooter.entity.Entity;
public class Explosion extends Entity {
/**
* Time to 'live' in update ticks. Will be decreased with each update()-call.
*/
private int timeToLive;
/**
* Attributes for the 'spawn more' constructor.
*/
private int count;
private int height;
private int width;
/**
* Instance of Random, so we get good random numbers.
*/
private Random random;
/**
* Just be a single explosion.
*/
public Explosion(int x, int y) {
super(x, y);
this.setImage("images/explosions/explosion_proto.png");
this.setPosition(x - (this.getWidth()/2), y - (this.getHeight()/2));
Random rand = new Random();
if (rand.nextInt(99) <= 70) {
new ExplosionOne(x, y);
} else {
new ExplosionTwo(x, y);
}
}
/**
* Be an explosion that spawns even more explosions! :O
*/
public Explosion(int x, int y, int count, int width, int height) {
super(x, y);
System.out.println("Explosion with: " + count);
this.setImage("images/explosions/explosion_proto.png");
this.setPosition(x, y);
this.random = new Random();
this.count = 10;
this.width = width;
this.height = height;
this.timeToLive = 100 * this.count; //Just a random value of ticks the explosion will take to spawn more explosions.
}
/**
* Here, a countdown is happening. Every X ticks, a new Explosion is born.
*/
public void update() {
this.timeToLive--;
if(this.timeToLive <= 0) this.remove();
if(this.count > 0 && this.timeToLive % this.count == 0) {
new Explosion(this.getX() + (int) (this.width * this.random.nextDouble()), this.getY() + (int) (this.height * this.random.nextDouble()));
this.count--;
}
}
}

View File

@ -1,45 +1,21 @@
package de.teamteamteam.spacescooter.entity.explosion;
import de.teamteamteam.spacescooter.entity.Entity;
import de.teamteamteam.spacescooter.entity.Animation;
public class ExplosionOne extends Entity {
private int count = 71;
public class ExplosionOne extends Animation {
public ExplosionOne(int x, int y) {
super(x, y);
this.setImage("images/explosions/explosion_proto.png");
String[] images = {
"images/explosions/01/explosion1.png",
"images/explosions/01/explosion2.png",
"images/explosions/01/explosion3.png",
"images/explosions/01/explosion4.png",
"images/explosions/01/explosion5.png",
"images/explosions/01/explosion6.png",
"images/explosions/01/explosion7.png"
};
this.configure(images, 10);
this.setPosition(x - (this.getWidth()/2), y - (this.getHeight()/2));
}
public void update() {
if (count >= 0) {
count--;
} else {
this.remove();
}
switch (count) {
case 70:
this.setImage("images/explosions/01/explosion1.png");
break;
case 60:
this.setImage("images/explosions/01/explosion2.png");
break;
case 50:
this.setImage("images/explosions/01/explosion3.png");
break;
case 40:
this.setImage("images/explosions/01/explosion4.png");
break;
case 30:
this.setImage("images/explosions/01/explosion5.png");
break;
case 20:
this.setImage("images/explosions/01/explosion6.png");
break;
case 10:
this.setImage("images/explosions/01/explosion7.png");
break;
}
}
}

View File

@ -1,73 +1,31 @@
package de.teamteamteam.spacescooter.entity.explosion;
import de.teamteamteam.spacescooter.entity.Entity;
import de.teamteamteam.spacescooter.entity.Animation;
public class ExplosionTwo extends Entity {
private int count = 141;
public class ExplosionTwo extends Animation {
public ExplosionTwo(int x, int y) {
super(x, y);
this.setImage("images/explosions/explosion_proto.png");
String[] images = {
"images/explosions/02/explosion2_1.png",
"images/explosions/02/explosion2_2.png",
"images/explosions/02/explosion2_3.png",
"images/explosions/02/explosion2_4.png",
"images/explosions/02/explosion2_5.png",
"images/explosions/02/explosion2_6.png",
"images/explosions/02/explosion2_7.png",
"images/explosions/02/explosion2_8.png",
"images/explosions/02/explosion2_9.png",
"images/explosions/02/explosion2_10.png",
"images/explosions/02/explosion2_11.png",
"images/explosions/02/explosion2_12.png",
"images/explosions/02/explosion2_13.png",
"images/explosions/02/explosion2_14.png",
"images/explosions/02/explosion2_15.png",
"images/explosions/02/explosion2_16.png"
};
this.configure(images, 10);
this.setPosition(x - (this.getWidth()/2), y - (this.getHeight()/2));
}
public void update() {
if (count >= 0) {
count--;
} else {
this.remove();
}
switch (count) {
case 150:
this.setImage("images/explosions/02/explosion2_1.png");
break;
case 140:
this.setImage("images/explosions/02/explosion2_2.png");
break;
case 130:
this.setImage("images/explosions/02/explosion2_3.png");
break;
case 120:
this.setImage("images/explosions/02/explosion2_4.png");
break;
case 110:
this.setImage("images/explosions/02/explosion2_5.png");
break;
case 100:
this.setImage("images/explosions/02/explosion2_6.png");
break;
case 90:
this.setImage("images/explosions/02/explosion2_7.png");
break;
case 80:
this.setImage("images/explosions/02/explosion2_8.png");
break;
case 70:
this.setImage("images/explosions/02/explosion2_9.png");
break;
case 60:
this.setImage("images/explosions/02/explosion2_10.png");
break;
case 50:
this.setImage("images/explosions/02/explosion2_11.png");
break;
case 40:
this.setImage("images/explosions/02/explosion2_12.png");
break;
case 30:
this.setImage("images/explosions/02/explosion2_13.png");
break;
case 20:
this.setImage("images/explosions/02/explosion2_14.png");
break;
case 10:
this.setImage("images/explosions/02/explosion2_15.png");
break;
case 1:
this.setImage("images/explosions/02/explosion2_16.png");
break;
}
}
}

View File

@ -0,0 +1,63 @@
package de.teamteamteam.spacescooter.entity.explosion;
import java.util.Random;
/**
* Extends the functionality of the simple ExplosionOne to randomly
* spawn more explosions in the area.
*/
public class MultiExplosion extends ExplosionOne {
/**
* Internal tick counter.
*/
private int counter;
/**
* Instance of Random, so we get good random numbers.
*/
private Random random;
/**
* Just be a single explosion.
*/
public MultiExplosion(int x, int y) {
super(x, y);
this.random = new Random();
}
/**
* Decide every 10 ticks whether or not to spawn a random explosion.
*/
@Override
public void update() {
if(this.counter % 10 == 0) {
if(this.random.nextBoolean()) {
this.spawnExplosion();
}
}
this.counter++;
super.update();
}
/**
* Randomly spawn a new random explosion at a random location.
*/
private void spawnExplosion() {
int x_offset = this.random.nextInt(35);
if(this.random.nextBoolean()) x_offset *= -1;
int y_offset = this.random.nextInt(35);
if(this.random.nextBoolean()) y_offset *= -1;
int explosionType = this.random.nextInt(2);
switch(explosionType) {
case 0:
new ExplosionOne(this.getX() + x_offset, this.getY() + y_offset);
break;
case 1:
new ExplosionTwo(this.getX() + x_offset, this.getY() + y_offset);
break;
default:
break;
}
}
}

View File

@ -1,6 +1,8 @@
package de.teamteamteam.spacescooter.entity.shot;
import de.teamteamteam.spacescooter.entity.LivingEntity;
import de.teamteamteam.spacescooter.entity.Player;
import de.teamteamteam.spacescooter.entity.enemy.Enemy;
import de.teamteamteam.spacescooter.entity.spi.Collidable;
import de.teamteamteam.spacescooter.utility.GameConfig;
@ -50,6 +52,14 @@ public class Shot extends LivingEntity {
this.setPosition(this.getX() - this.getImage().getWidth() / 2, this.getY() - this.getImage().getHeight() / 2);
}
/**
* Return the direction the Shot is traveling into.
* Returns Shot.LEFT or Shot.RIGHT
*/
public int getDirection() {
return this.direction;
}
/**
* Returns the damage this shot does to LivingEntities it hits.
*/
@ -85,6 +95,8 @@ public class Shot extends LivingEntity {
*/
@Override
public void collideWith(Collidable entity) {
if(this.direction == LEFT && entity instanceof Enemy) return;
if(this.direction == RIGHT && entity instanceof Player) return;
super.collideWith(entity);
this.explode();
this.remove();