Create static java.util.Random wrapper, add shot hit sound.

This commit is contained in:
Jan Philipp Timme 2014-11-11 14:20:16 +01:00
parent 5d404e37bd
commit 04944e0391
8 changed files with 71 additions and 30 deletions

Binary file not shown.

View File

@ -1,16 +1,15 @@
package de.teamteamteam.spacescooter.entity.enemy;
import java.util.Random;
import de.teamteamteam.spacescooter.entity.ShootingEntity;
import de.teamteamteam.spacescooter.entity.shot.Shot;
import de.teamteamteam.spacescooter.utility.Random;
public abstract class Enemy extends ShootingEntity {
public Enemy(int x, int y) {
super(x, y);
Random r = new Random();
this.name = "EnemyOne";
this.willShoot = r.nextBoolean();
this.willShoot = Random.nextBoolean();
this.setShootDirection(Shot.LEFT);
this.setShootDamage(5);
}

View File

@ -1,7 +1,5 @@
package de.teamteamteam.spacescooter.entity.enemy;
import java.util.Random;
import de.teamteamteam.spacescooter.datastructure.ConcurrentIterator;
import de.teamteamteam.spacescooter.entity.Entity;
import de.teamteamteam.spacescooter.entity.Player;
@ -9,17 +7,16 @@ import de.teamteamteam.spacescooter.entity.explosion.MultiExplosion;
import de.teamteamteam.spacescooter.entity.item.Item;
import de.teamteamteam.spacescooter.screen.Screen;
import de.teamteamteam.spacescooter.utility.GameConfig;
import de.teamteamteam.spacescooter.utility.Random;
public class EnemyThree extends Enemy{
private double newY;
private double ySpeed = 0.4;
private Random random;
private ConcurrentIterator<Entity> entityIterator;
public EnemyThree(int x, int y) {
super(x, y);
random = new Random();
this.setImage("images/enemy02.png");
this.setPrimaryShotImage("images/shots/laser_red.png");
this.setShootSpeed(4);
@ -29,7 +26,7 @@ public class EnemyThree extends Enemy{
this.setHealthPoints(15);
this.setCollisionDamage(10);
this.setScore(30);
this.setPosition(GameConfig.windowWidth, random.nextInt(GameConfig.windowHeight - this.getHeight()));
this.setPosition(GameConfig.windowWidth, Random.nextInt(GameConfig.windowHeight - this.getHeight()));
this.newY = this.getY();
this.entityIterator = Screen.currentScreen.createEntityIterator();
}
@ -39,7 +36,7 @@ public class EnemyThree extends Enemy{
*/
@Override
public void die() {
if(random.nextInt(10) < 5) Item.create(getX(), getY());
if(Random.nextInt(10) < 5) Item.create(getX(), getY());
new EnemyThree(0, 0);
super.die();
}

View File

@ -1,14 +1,12 @@
package de.teamteamteam.spacescooter.entity.enemy;
import java.util.Random;
import de.teamteamteam.spacescooter.utility.GameConfig;
import de.teamteamteam.spacescooter.utility.Random;
public class EnemyTwo extends Enemy{
public EnemyTwo(int x, int y) {
super(x, y);
Random random = new Random();
this.setImage("images/enemy02.png");
this.setPrimaryShotImage("images/shots/laser_green.png");
this.setShootSpeed(4);
@ -19,7 +17,7 @@ public class EnemyTwo extends Enemy{
this.setHealthPoints(5);
this.setScore(20);
this.setCollisionDamage(this.getHealthPoints());
this.setPosition(GameConfig.windowWidth, random.nextInt(GameConfig.windowHeight - this.getHeight()));
this.setPosition(GameConfig.windowWidth, Random.nextInt(GameConfig.windowHeight - this.getHeight()));
}
@Override

View File

@ -1,6 +1,6 @@
package de.teamteamteam.spacescooter.entity.explosion;
import java.util.Random;
import de.teamteamteam.spacescooter.utility.Random;
/**
* Extends the functionality of the simple ExplosionOne to randomly
@ -13,17 +13,12 @@ public class MultiExplosion extends ExplosionOne {
*/
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();
}
/**
@ -32,7 +27,7 @@ public class MultiExplosion extends ExplosionOne {
@Override
public void update() {
if(this.counter % 10 == 0) {
if(this.random.nextBoolean()) {
if(Random.nextBoolean()) {
this.spawnExplosion();
}
}
@ -44,11 +39,11 @@ public class MultiExplosion extends ExplosionOne {
* 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);
int x_offset = Random.nextInt(35);
if(Random.nextBoolean()) x_offset *= -1;
int y_offset = Random.nextInt(35);
if(Random.nextBoolean()) y_offset *= -1;
int explosionType = Random.nextInt(2);
switch(explosionType) {
case 0:
new ExplosionOne(this.getX() + x_offset, this.getY() + y_offset);

View File

@ -1,6 +1,6 @@
package de.teamteamteam.spacescooter.entity.item;
import java.util.Random;
import de.teamteamteam.spacescooter.utility.Random;
public class ItemChance {
@ -21,8 +21,7 @@ public class ItemChance {
public static int choose() {
//dauerhaft
Random random = new Random();
int r = random.nextInt(ItemChance.summe - 1) + 1;
int r = Random.nextInt(ItemChance.summe - 1) + 1;
for(int i=0; i<ItemChance.items.length; i++) {
r -= ItemChance.items[i];

View File

@ -6,6 +6,7 @@ import de.teamteamteam.spacescooter.entity.enemy.Enemy;
import de.teamteamteam.spacescooter.entity.spi.Collidable;
import de.teamteamteam.spacescooter.sound.SoundSystem;
import de.teamteamteam.spacescooter.utility.GameConfig;
import de.teamteamteam.spacescooter.utility.Random;
/**
* This class represents a Shot in the game.
@ -98,7 +99,17 @@ public class Shot extends CollidableEntity {
public void collideWith(Collidable entity) {
if(this.direction == LEFT && entity instanceof Enemy) return;
if(this.direction == RIGHT && entity instanceof Player) return;
SoundSystem.playSound("sounds/shot_hit_something.wav");
int soundToPlay = Random.getRandom().nextInt(2);
switch(soundToPlay) {
case 0:
SoundSystem.playSound("sounds/shot_hit_something.wav");
break;
case 1:
SoundSystem.playSound("sounds/shot_hit_something_loud.wav");
break;
default:
break;
}
this.remove();
}

View File

@ -0,0 +1,42 @@
package de.teamteamteam.spacescooter.utility;
/**
* Provides a single Random instance for everyone who needs it.
* The purpose of this is to avoid creating tons of instances that will
* fall to the garbage collector only seconds later.
*/
public class Random {
/**
* Internal java.util.Random instance.
*/
private static java.util.Random random = new java.util.Random();
/**
* Private constructor.
*/
private Random() {}
/**
* Get the java.util.Random instance.
*/
public static java.util.Random getRandom() {
return Random.random;
}
/**
* Wrap of java.util.Random.nextInt().
*/
public static int nextInt(int boundary) {
return Random.random.nextInt(boundary);
}
/**
* Wrap of java.util.Random.nextBoolean().
*/
public static boolean nextBoolean() {
return Random.random.nextBoolean();
}
}