From 39121252589fa32efdd5d978d7099699b6e75841 Mon Sep 17 00:00:00 2001 From: Kevin <104761532+Hasakev@users.noreply.github.com> Date: Tue, 17 Oct 2023 12:02:18 +1000 Subject: [PATCH] Replaced Random method to prevent Security Hotspots Replaced all Random class calls to MathUtils.random --- .../com/csse3200/game/components/npc/DodgingComponent.java | 4 ++-- .../com/csse3200/game/components/tasks/waves/LevelWaves.java | 4 ++-- .../com/csse3200/game/entities/factories/WaveFactory.java | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/source/core/src/main/com/csse3200/game/components/npc/DodgingComponent.java b/source/core/src/main/com/csse3200/game/components/npc/DodgingComponent.java index 96cefd608..6e4756757 100644 --- a/source/core/src/main/com/csse3200/game/components/npc/DodgingComponent.java +++ b/source/core/src/main/com/csse3200/game/components/npc/DodgingComponent.java @@ -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; @@ -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 @@ -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. diff --git a/source/core/src/main/com/csse3200/game/components/tasks/waves/LevelWaves.java b/source/core/src/main/com/csse3200/game/components/tasks/waves/LevelWaves.java index cca65b2e5..19d6e910c 100644 --- a/source/core/src/main/com/csse3200/game/components/tasks/waves/LevelWaves.java +++ b/source/core/src/main/com/csse3200/game/components/tasks/waves/LevelWaves.java @@ -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; @@ -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; @@ -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); diff --git a/source/core/src/main/com/csse3200/game/entities/factories/WaveFactory.java b/source/core/src/main/com/csse3200/game/entities/factories/WaveFactory.java index de95970f8..bd97d0ee2 100644 --- a/source/core/src/main/com/csse3200/game/entities/factories/WaveFactory.java +++ b/source/core/src/main/com/csse3200/game/entities/factories/WaveFactory.java @@ -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; @@ -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 MELEE_MOBS = new ArrayList<>(Arrays.asList( "Skeleton", "Coat", "DragonKnight", "Necromancer" )); @@ -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; }