Skip to content

Commit

Permalink
Use CollectionUtilities and ArrayUtilities for random access.
Browse files Browse the repository at this point in the history
Use ThreadLocalRandom instead of individual Random objects.
  • Loading branch information
steffen-wilke committed Jul 20, 2019
1 parent e726e06 commit d6c976f
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package de.gurkenlabs.litiengine.abilities.effects;

import java.util.Random;

import de.gurkenlabs.litiengine.Game;
import de.gurkenlabs.litiengine.abilities.Ability;
import de.gurkenlabs.litiengine.entities.ICombatEntity;
import de.gurkenlabs.litiengine.sound.Sound;
import de.gurkenlabs.litiengine.util.ArrayUtilities;

public class SoundEffect extends Effect {
private final Sound[] sounds;
Expand All @@ -30,7 +29,6 @@ private Sound getRandomSound() {
return null;
}

final int randomIndex = new Random().nextInt(this.sounds.length);
return this.sounds[randomIndex];
return ArrayUtilities.getRandom(this.sounds);
}
}
8 changes: 3 additions & 5 deletions src/de/gurkenlabs/litiengine/environment/EntitySpawner.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package de.gurkenlabs.litiengine.environment;

import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

import de.gurkenlabs.litiengine.Game;
import de.gurkenlabs.litiengine.IGameLoop;
import de.gurkenlabs.litiengine.entities.IEntity;
import de.gurkenlabs.litiengine.entities.Spawnpoint;
import de.gurkenlabs.litiengine.util.CollectionUtilities;

public abstract class EntitySpawner<T extends IEntity> implements IEntitySpawner<T> {
private static final Logger log = Logger.getLogger(EntitySpawner.class.getName());
Expand Down Expand Up @@ -126,13 +126,11 @@ protected void spawnNewEntities() {
}
break;
case ONERANDOMSPAWNPOINT:
final int rnd = new Random().nextInt(this.getSpawnPoints().size());
this.spawn(this.getSpawnPoints().get(rnd), this.getSpawnAmount());
this.spawn(CollectionUtilities.random(this.getSpawnPoints()), this.getSpawnAmount());
break;
case RANDOMSPAWNPOINTS:
for (int i = 0; i < this.getSpawnAmount(); i++) {
final int rnd2 = new Random().nextInt(this.getSpawnPoints().size());
this.spawn(this.getSpawnPoints().get(rnd2), 1);
this.spawn(CollectionUtilities.random(this.getSpawnPoints()), 1);
}

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -11,6 +10,7 @@
import de.gurkenlabs.litiengine.IUpdateable;
import de.gurkenlabs.litiengine.graphics.Spritesheet;
import de.gurkenlabs.litiengine.resources.Resources;
import de.gurkenlabs.litiengine.util.CollectionUtilities;

public class Animation implements IUpdateable, ILaunchable {
public static final int DEFAULT_FRAME_DURATION = 120;
Expand Down Expand Up @@ -43,7 +43,7 @@ public Animation(final String name, final Spritesheet spritesheet, final boolean
this(name, spritesheet, loop, keyFrameDurations);

if (randomizeStart && !this.keyframes.isEmpty()) {
this.firstFrame = this.getKeyframes().get(new Random().nextInt(this.getKeyframes().size()));
this.firstFrame = CollectionUtilities.random(this.getKeyframes());
}
}

Expand Down
12 changes: 5 additions & 7 deletions src/de/gurkenlabs/litiengine/graphics/emitters/Emitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Consumer;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -38,8 +38,6 @@ public abstract class Emitter extends Entity implements IUpdateable, ITimeToLive
public static final int DEFAULT_SPAWNAMOUNT = 1;
public static final int DEFAULT_MAXPARTICLES = 100;

private static final Random RANDOM = new Random();

private final List<Consumer<Emitter>> finishedConsumer;
private final CopyOnWriteArrayList<Particle> particles;
private final List<Color> colors;
Expand Down Expand Up @@ -431,7 +429,7 @@ protected Color getRandomParticleColor() {
return DEFAULT_PARTICLE_COLOR;
}

return this.colors.get(RANDOM.nextInt(this.colors.size()));
return this.colors.get(ThreadLocalRandom.current().nextInt(this.colors.size()));
}

protected int getRandomParticleTTL() {
Expand All @@ -444,15 +442,15 @@ protected int getRandomParticleTTL() {
return this.getParticleMaxTTL();
}

return RANDOM.nextInt(this.getParticleMaxTTL() - this.getParticleMinTTL()) + this.getParticleMinTTL();
return ThreadLocalRandom.current().nextInt(this.getParticleMaxTTL() - this.getParticleMinTTL()) + this.getParticleMinTTL();
}

protected int getRandomParticleX() {
return RANDOM.nextInt((int) this.getWidth());
return ThreadLocalRandom.current().nextInt((int) this.getWidth());
}

protected int getRandomParticleY() {
return RANDOM.nextInt((int) this.getHeight());
return ThreadLocalRandom.current().nextInt((int) this.getHeight());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.awt.Image;
import java.awt.geom.Point2D;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import de.gurkenlabs.litiengine.graphics.Spritesheet;
import de.gurkenlabs.litiengine.graphics.emitters.particles.Particle;
Expand All @@ -23,9 +23,9 @@ public Spritesheet getSpritesheet() {
}

protected Image getRandomSprite() {
return this.getSpritesheet().getSprite(new Random().nextInt(this.getSpritesheet().getTotalNumberOfSprites()));
return this.getSpritesheet().getSprite(ThreadLocalRandom.current().nextInt(this.getSpritesheet().getTotalNumberOfSprites()));
}

@Override
protected Particle createNewParticle() {
return new SpriteParticle(this.getRandomSprite(), this.getRandomParticleTTL());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.gurkenlabs.litiengine.graphics.emitters;

import java.awt.Image;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import de.gurkenlabs.litiengine.entities.IEntity;
import de.gurkenlabs.litiengine.graphics.Spritesheet;
Expand All @@ -23,7 +23,7 @@ public Spritesheet getSpritesheet() {
}

protected Image getRandomSprite() {
return this.getSpritesheet().getSprite(new Random().nextInt(this.getSpritesheet().getTotalNumberOfSprites()));
return this.getSpritesheet().getSprite(ThreadLocalRandom.current().nextInt(this.getSpritesheet().getTotalNumberOfSprites()));
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/de/gurkenlabs/litiengine/util/ArrayUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public static <T> List<T> toList(T[][] arr) {
public static <T> T getRandom(T[] arr) {
return getRandom(arr, ThreadLocalRandom.current());
}

public static <T> T getRandom(T[] arr, Random rand) {
if (arr.length == 0) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions src/de/gurkenlabs/litiengine/util/CollectionUtilities.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package de.gurkenlabs.litiengine.util;

import java.util.Collection;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public final class CollectionUtilities {
public static <T> T random(Collection<T> coll) {
int num = (new Random().nextInt(coll.size()));
int num = (ThreadLocalRandom.current().nextInt(coll.size()));
for (T t : coll) {
if (--num < 0) {
return t;
Expand Down
60 changes: 39 additions & 21 deletions src/de/gurkenlabs/litiengine/util/MathUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,28 @@ public static double round(double value, int places) {
/**
* Modifies the value (if necessary) such that it lies within the boundaries of the specified minimum and maximum.
*
* @param value The value to be clamped.
* @param min The minimum value to be accepted.
* @param max The maximum value to be accepted.
* @param value
* The value to be clamped.
* @param min
* The minimum value to be accepted.
* @param max
* The maximum value to be accepted.
*
* @return A value clamped to the specified boundaries.
*/
public static double clamp(final double value, final double min, final double max) {
return Math.max(min, Math.min(max, value));
}

/**
* Modifies the value (if necessary) such that it lies within the boundaries of the specified minimum and maximum.
*
* @param value The value to be clamped.
* @param min The minimum value to be accepted.
* @param max The maximum value to be accepted.
* @param value
* The value to be clamped.
* @param min
* The minimum value to be accepted.
* @param max
* The maximum value to be accepted.
*
* @return A value clamped to the specified boundaries.
*/
Expand All @@ -56,9 +62,12 @@ public static float clamp(final float value, final float min, final float max) {
/**
* Modifies the value (if necessary) such that it lies within the boundaries of the specified minimum and maximum.
*
* @param value The value to be clamped.
* @param min The minimum value to be accepted.
* @param max The maximum value to be accepted.
* @param value
* The value to be clamped.
* @param min
* The minimum value to be accepted.
* @param max
* The maximum value to be accepted.
*
* @return A value clamped to the specified boundaries.
*/
Expand All @@ -73,13 +82,16 @@ public static byte clamp(final byte value, final byte min, final byte max) {

return value;
}

/**
* Modifies the value (if necessary) such that it lies within the boundaries of the specified minimum and maximum.
*
* @param value The value to be clamped.
* @param min The minimum value to be accepted.
* @param max The maximum value to be accepted.
* @param value
* The value to be clamped.
* @param min
* The minimum value to be accepted.
* @param max
* The maximum value to be accepted.
*
* @return A value clamped to the specified boundaries.
*/
Expand All @@ -95,13 +107,16 @@ public static short clamp(final short value, final short min, final short max) {

return value;
}

/**
* Modifies the value (if necessary) such that it lies within the boundaries of the specified minimum and maximum.
*
* @param value The value to be clamped.
* @param min The minimum value to be accepted.
* @param max The maximum value to be accepted.
* @param value
* The value to be clamped.
* @param min
* The minimum value to be accepted.
* @param max
* The maximum value to be accepted.
*
* @return A value clamped to the specified boundaries.
*/
Expand All @@ -120,9 +135,12 @@ public static int clamp(final int value, final int min, final int max) {
/**
* Modifies the value (if necessary) such that it lies within the boundaries of the specified minimum and maximum.
*
* @param value The value to be clamped.
* @param min The minimum value to be accepted.
* @param max The maximum value to be accepted.
* @param value
* The value to be clamped.
* @param min
* The minimum value to be accepted.
* @param max
* The maximum value to be accepted.
*
* @return A value clamped to the specified boundaries.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

public class GeometricUtilities {
private static final double RAYCAST_EPSILON = 0.01;
Expand Down Expand Up @@ -430,8 +431,8 @@ public static List<Point2D> getPointsBetweenPoints(final Point2D point1, final P
}

public static Point2D getRandomLocation(final double x, final double y, final double width, final double height) {
final double xOffset = Math.random() * width;
final double yOffset = Math.random() * height;
final double xOffset = ThreadLocalRandom.current().nextDouble(width);
final double yOffset = ThreadLocalRandom.current().nextDouble(height);

return new Point2D.Double(x + xOffset, y + yOffset);
}
Expand Down

0 comments on commit d6c976f

Please sign in to comment.