Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #6 from guoguo12/master

Simplify upgrade drop code
  • Loading branch information...
commit 7012758c79077b0e06d4322c308e7c29ba800669 2 parents e05bca5 + 4e07a3c
@SubSage authored
View
20 GDCalaga/src/org/gdc/gdcalaga/Enemy.java
@@ -243,23 +243,13 @@ public void Hurt(float dmg)
*/
private void tryDropUpgrade()
{
- Random rand = new Random(System.currentTimeMillis());
- //percentageChance 1 - 100
- final int percentageChance = 5;
- int max = (100 / percentageChance);
- int min = 1;
- int chance = rand.nextInt(max - min + 1) + min;
- if (chance == (int)(max / 2)) //chance can be compared to any number between min and max
+ final double decimalChance = 0.05; // TODO Make static final
+ if (Math.random() < decimalChance)
{
//generate a random upgrade
- int numUpgrades = UpgradeType.getNumDroppableUpgrades();
- int minUpgrade = 1;
- chance = rand.nextInt(numUpgrades - minUpgrade + 1) + minUpgrade;
-
- Upgrade.UpgradeType type;
- type = UpgradeType.getIndexedUpgrade(chance);
-
- Upgrade upgrade = new Upgrade(entities, this.pos, type);
+ int numUpgrades = UpgradeType.getNumDroppableUpgrades();
+ Upgrade.UpgradeType type = UpgradeType.getIndexedUpgrade((int) (numUpgrades * Math.random()) + 1); // Add 1 because the indices start at 1
+ new Upgrade(entities, this.pos, type);
}
}
}
View
18 GDCalaga/src/org/gdc/gdcalaga/Player.java
@@ -1,5 +1,4 @@
package org.gdc.gdcalaga;
-import org.gdc.gdcalaga.Upgrade.UpgradeType;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
@@ -127,18 +126,13 @@ public boolean fire(float delta)
//TODO add more firing positions and more bullets depending on the upgrades
if (ticksSinceLastBullet >= ticksPerBullet)
{
- int max = 0;
- if (numGuns < gunPositions.getSize())
- max = numGuns;
- else
- max = gunPositions.getSize();
-
- for (int iii = 0; iii < max; iii++)
+ int max = numGuns < gunPositions.getSize() ? numGuns : gunPositions.getSize();
+ for (int i = 0; i < max; i++)
{
- Vector2f position = new Vector2f(gunPositions.getPosition(iii));
+ Vector2f position = new Vector2f(gunPositions.getPosition(i));
position.add(pos);
Bullet newBullet = new Bullet(entities, position, (int)(damage), alliance);
- Vector2f direction = new Vector2f(gunPositions.getDirection(iii));
+ Vector2f direction = gunPositions.getDirection(i);
newBullet.setSpeed(direction.x * 500, direction.y * 500);
}
@@ -216,10 +210,8 @@ public static void increaseTotalPoints(int pointValue)
//my idea here is that ship upgrades will cost points to buy, like in most games.
public static void decreaseTotalPoints(int pointValue)
{
- if( (totalPoints - pointValue) >= 0)
- {
+ if(totalPoints - pointValue >= 0)
totalPoints -= pointValue;
- }
else
System.out.println("Can't spend any more points!"); //TODO Fix error message
}
Please sign in to comment.
Something went wrong with that request. Please try again.