Skip to content

Commit

Permalink
Replaced Random method to prevent Security Hotspots
Browse files Browse the repository at this point in the history
Replaced all Random class calls to MathUtils.random
  • Loading branch information
Hasakev committed Oct 17, 2023
1 parent d267848 commit 3912125
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.csse3200.game.components.npc;

import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.csse3200.game.components.Component;
import com.csse3200.game.physics.PhysicsEngine;
Expand Down Expand Up @@ -32,7 +33,6 @@ public class DodgingComponent extends Component {
private float dodgeSpeed = 1.75f;
private float originalSpeed; // Original entity vertical speed
private PhysicsEngine physics;
private Random random = new Random();

// Sometimes the raycast mechanic doesn't detect the other entity because of the
// target's (or self) collider size does not match. This value makes sure the
Expand Down Expand Up @@ -95,7 +95,7 @@ public void create() {
* @param mobPos The current Vector2 mob position in the map.
*/
public void changeTraverseDirection(Vector2 mobPos) {
int randDirection = random.nextInt(2) == 1 ? -1 : 1;
int randDirection = MathUtils.random(0,2) == 1 ? -1 : 1;
if (isTargetVisible(mobPos)) {
// If mob is in the top half quadrant of the map grid, make the entity dodge
// downwards.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.csse3200.game.components.tasks.waves;

import com.badlogic.gdx.math.GridPoint2;
import com.badlogic.gdx.math.MathUtils;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.services.GameTime;
import com.csse3200.game.services.ServiceLocator;
Expand All @@ -14,7 +15,6 @@ public class LevelWaves extends Entity {
private float spawnDelay;
private GameTime gameTime;
private long startTime;
private Random rand = new Random();
private int currentRandom = 0;
private int previousRandom = 0;
private int mobIndex;
Expand Down Expand Up @@ -63,7 +63,7 @@ public WaveClass getWave(int index) {
public void spawnWave() {
if (gameTime.getTime() >= startTime + spawnDelay * 1000) {
do {
currentRandom = rand.nextInt(0, ServiceLocator.getMapService().getHeight());
currentRandom = MathUtils.random(0, ServiceLocator.getMapService().getHeight());
} while (currentRandom == previousRandom);
ServiceLocator.getWaveService().setNextLane(currentRandom);
GridPoint2 randomPos = new GridPoint2(19, currentRandom);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.csse3200.game.entities.factories;

import com.badlogic.gdx.math.MathUtils;
import com.csse3200.game.ai.tasks.AITaskComponent;
import com.csse3200.game.components.tasks.waves.LevelWaves;
import com.csse3200.game.components.tasks.waves.WaveClass;
Expand All @@ -26,7 +27,6 @@ public class WaveFactory {
*/

private static final Logger logger = LoggerFactory.getLogger(WaveFactory.class);
private static Random rand = new Random();
private static final ArrayList<String> MELEE_MOBS = new ArrayList<>(Arrays.asList(
"Skeleton", "Coat", "DragonKnight", "Necromancer"
));
Expand Down Expand Up @@ -173,7 +173,7 @@ public static LevelWaves createLevel(int chosenLevel) {
num = minMobs - currentMobs;
System.out.println(num + " for " + mob + " at wave " + atWave);
} else {
num = rand.nextInt(minMobs - currentMobs - (2 * leftToSort) - 2) + 2;
num = MathUtils.random(minMobs - currentMobs - (2 * leftToSort) - 2) + 2;
System.out.println(num + " for " + mob + " at wave " + atWave);
currentMobs += num;
}
Expand Down

0 comments on commit 3912125

Please sign in to comment.