From 704ca8e08508516ede660c3d8ffe7a82fb0d3019 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 25 Nov 2014 13:52:26 +0100 Subject: [PATCH] Disable enemies respawning themselves, move itemchance code into item create() method. --- .../entity/enemy/EnemyBossMinion.java | 1 - .../spacescooter/entity/enemy/EnemyThree.java | 4 +- .../spacescooter/entity/enemy/EnemyTwo.java | 4 +- .../spacescooter/entity/item/Item.java | 87 +++++++++++++------ .../spacescooter/entity/item/ItemChance.java | 42 --------- .../spacescooter/level/Level.java | 2 - 6 files changed, 66 insertions(+), 74 deletions(-) delete mode 100644 src/de/teamteamteam/spacescooter/entity/item/ItemChance.java diff --git a/src/de/teamteamteam/spacescooter/entity/enemy/EnemyBossMinion.java b/src/de/teamteamteam/spacescooter/entity/enemy/EnemyBossMinion.java index 3ca1aff..c6b1731 100644 --- a/src/de/teamteamteam/spacescooter/entity/enemy/EnemyBossMinion.java +++ b/src/de/teamteamteam/spacescooter/entity/enemy/EnemyBossMinion.java @@ -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(); } diff --git a/src/de/teamteamteam/spacescooter/entity/enemy/EnemyThree.java b/src/de/teamteamteam/spacescooter/entity/enemy/EnemyThree.java index ca387c6..89a9547 100644 --- a/src/de/teamteamteam/spacescooter/entity/enemy/EnemyThree.java +++ b/src/de/teamteamteam/spacescooter/entity/enemy/EnemyThree.java @@ -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; diff --git a/src/de/teamteamteam/spacescooter/entity/enemy/EnemyTwo.java b/src/de/teamteamteam/spacescooter/entity/enemy/EnemyTwo.java index a2adec3..cdf2189 100644 --- a/src/de/teamteamteam/spacescooter/entity/enemy/EnemyTwo.java +++ b/src/de/teamteamteam/spacescooter/entity/enemy/EnemyTwo.java @@ -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(); } } diff --git a/src/de/teamteamteam/spacescooter/entity/item/Item.java b/src/de/teamteamteam/spacescooter/entity/item/Item.java index 5069cef..7966474 100644 --- a/src/de/teamteamteam/spacescooter/entity/item/Item.java +++ b/src/de/teamteamteam/spacescooter/entity/item/Item.java @@ -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[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; } } } diff --git a/src/de/teamteamteam/spacescooter/entity/item/ItemChance.java b/src/de/teamteamteam/spacescooter/entity/item/ItemChance.java deleted file mode 100644 index c1e2631..0000000 --- a/src/de/teamteamteam/spacescooter/entity/item/ItemChance.java +++ /dev/null @@ -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