Disable enemies respawning themselves, move itemchance code into item create() method.

This commit is contained in:
Jan Philipp Timme 2014-11-25 13:52:26 +01:00
parent 63f87b5a4a
commit 704ca8e085
6 changed files with 66 additions and 74 deletions

View File

@ -31,7 +31,6 @@ public class EnemyBossMinion extends Enemy{
@Override
public void die() {
if(Random.nextInt(10) < 5) Item.create(getX(), getY());
new EnemyBossMinion(0, 0);
super.die();
}

View File

@ -33,7 +33,6 @@ public class EnemyThree extends Enemy{
@Override
public void die() {
if(Random.nextInt(10) < 5) Item.create(getX(), getY());
new EnemyThree(0, 0);
super.die();
}
@ -49,6 +48,9 @@ public class EnemyThree extends Enemy{
public void update() {
super.update();
this.setPosition(this.getX()-1, this.getY());
if(this.getX() < 0-getWidth()){
this.remove();
}
Player player = GameScreen.getPlayer();
if(this.getY() < player.getY()){
this.newY += ySpeed;

View File

@ -24,8 +24,8 @@ public class EnemyTwo extends Enemy{
public void update() {
super.update();
this.setPosition(this.getX()-1, this.getY());
if(!this.isAlive()){
new EnemyTwo(0, 0);
if(this.getX() < 0-getWidth()){
this.remove();
}
}

View File

@ -4,10 +4,13 @@ import de.teamteamteam.spacescooter.entity.CollidableEntity;
import de.teamteamteam.spacescooter.entity.Player;
import de.teamteamteam.spacescooter.entity.spi.Collidable;
import de.teamteamteam.spacescooter.sound.SoundSystem;
import de.teamteamteam.spacescooter.utility.Random;
public abstract class Item extends CollidableEntity {
/**
* Default constructor.
*/
public Item(int x, int y) {
super(x, y);
}
@ -23,11 +26,15 @@ public abstract class Item extends CollidableEntity {
}
}
/**
* Default update method for all items.
* They smoothly scroll along and vanish when they're off the screen.
*/
public void update(){
this.transpose(-1, 0);
if(this.getX() < 0-this.getWidth()){
if(this.getX() < this.getWidth()) {
this.remove();
};
}
}
/**
@ -37,31 +44,59 @@ public abstract class Item extends CollidableEntity {
public abstract void itemCollected(Player player);
/**
* Selects which item spawns.
*
* (If you add a new item, you must also add it in ItemChance.java)
* Spawns a random item using the weighted probabilities..
*/
public static void create(int x, int y){
int auswahl = ItemChance.choose();
switch (auswahl) {
case 0:
new ItemNuke(x, y);
break;
case 1:
new ItemCredit(x, y);
break;
case 2:
new ItemHeal(x, y);
break;
case 3:
new ItemShield(x, y);
break;
case 4:
new ItemRocket(x, y);
break;
case 5:
new ItemIncreaseDamage(x, y);
break;
int i;
int sum = 0;
int choice = -1;
//List of items with their weighted probabilities.
int[] items = new int[6];
items[0] = 1; //ItemNuke
items[1] = 4; //ItemCredit
items[2] = 2; //ItemHeal
items[3] = 2; //ItemShield
items[4] = 2; //ItemRocket
items[5] = 3; //ItemIncreaseDamage
//Add them all up
for(i=0; i<items.length; i++) {
sum += items[i];
}
//Get a random number between 0 and sum
int randomNumber = Random.nextInt(sum);
//Check out which one is the current choice.
for(i=0; i<items.length; i++) {
if(randomNumber >= items[i]) {
randomNumber -= items[i];
} else {
//A choice has been made. Break away now.
choice = i;
break;
}
}
//Actually spawn the item now
switch (choice) {
case 0:
new ItemNuke(x, y);
break;
case 1:
new ItemCredit(x, y);
break;
case 2:
new ItemHeal(x, y);
break;
case 3:
new ItemShield(x, y);
break;
case 4:
new ItemRocket(x, y);
break;
case 5:
new ItemIncreaseDamage(x, y);
break;
default:
System.err.println("Could not determine which item to spawn!");
break;
}
}
}

View File

@ -1,42 +0,0 @@
package de.teamteamteam.spacescooter.entity.item;
import de.teamteamteam.spacescooter.utility.Random;
public class ItemChance {
private static int summe = 0;
private static int[] items;
/**
* Item spawn probability,
* higher number, higher spawn rate,
* same number, same spawn rate.
*
* New items must be registered here!!
*/
public ItemChance() {
ItemChance.items = new int[6];
items[0] = 1; //ItemNuke
items[1] = 4; //ItemCredit
items[2] = 2; //ItemHeal
items[3] = 2; //ItemShield
items[4] = 2; //ItemRocket
items[5] = 3; //ItemIncreaseDamage
for(int i=0; i<ItemChance.items.length; i++) {
ItemChance.summe += ItemChance.items[i];
}
}
public static int choose() {
int r = Random.nextInt(ItemChance.summe - 1) + 1;
for(int i=0; i<ItemChance.items.length; i++) {
r -= ItemChance.items[i];
if(r <= 0) {
return i;
}
}
return -1;
}
}

View File

@ -8,7 +8,6 @@ import de.teamteamteam.spacescooter.entity.enemy.EnemyBoss;
import de.teamteamteam.spacescooter.entity.enemy.EnemyOne;
import de.teamteamteam.spacescooter.entity.enemy.EnemyThree;
import de.teamteamteam.spacescooter.entity.enemy.EnemyTwo;
import de.teamteamteam.spacescooter.entity.item.ItemChance;
import de.teamteamteam.spacescooter.entity.Entity;
import de.teamteamteam.spacescooter.screen.GameScreen;
import de.teamteamteam.spacescooter.utility.GameConfig;
@ -50,7 +49,6 @@ public final class Level {
* Initialize the level based on the LevelConfig attributes.
*/
public void doBuildUp() {
new ItemChance();
new StarBackground(0, 50);
GameScreen.setPlayer(new Player(200, 300));
}