From c1c438a053ff3d34ca9b8d13eec9b41929bffc09 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Tue, 25 Feb 2020 11:41:50 +0100 Subject: [PATCH] Better complexity --- .../ynerant/leveleditor/api/editor/RawMap.java | 12 ++++++++++++ .../fr/ynerant/leveleditor/game/GameFrame.java | 18 ++++-------------- .../java/fr/ynerant/leveleditor/game/Mob.java | 4 ++-- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/main/java/fr/ynerant/leveleditor/api/editor/RawMap.java b/src/main/java/fr/ynerant/leveleditor/api/editor/RawMap.java index 6f42a0f..6cfe48d 100644 --- a/src/main/java/fr/ynerant/leveleditor/api/editor/RawMap.java +++ b/src/main/java/fr/ynerant/leveleditor/api/editor/RawMap.java @@ -4,10 +4,12 @@ import fr.ynerant.leveleditor.editor.Map; import java.awt.image.BufferedImage; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; public class RawMap { private List cases; + private java.util.Map cases_map; private int width; private int height; private transient BufferedImage font; @@ -36,6 +38,16 @@ public class RawMap { return cases; } + public RawCase getCase(int x, int y) { + if (cases_map == null) { + cases_map = new HashMap<>(); + for (RawCase c : getCases()) + cases_map.put(c.getPosY() * width + c.getPosX(), c); + } + + return cases_map.get(y * getWidth() + x); + } + public int getWidth() { return width; } diff --git a/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java b/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java index 3b5f9bb..2e1c889 100644 --- a/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java +++ b/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java @@ -108,25 +108,15 @@ public class GameFrame extends JFrame { return map; } - public RawCase getCase(int x, int y) { - for (RawCase c : getMap().getCases()) { - if (c.getPosX() == x && c.getPosY() == y) - return c; - } - - return null; - } - public void tick() { if (mobs.isEmpty() && round < 4) { ++round; for (int i = 1; i <= RANDOM.nextInt(16) + 1; ++i) { Mob mob = Mob.getRandomMob(); - do { + do mob.move(RANDOM.nextInt(getMap().getWidth() / 16), RANDOM.nextInt(getMap().getHeight() / 16)); - } - while (getCase(mob.getX(), mob.getY()).getCollision() != Collision.ANY); - getCase(mob.getX(), mob.getY()).setCollision(Collision.PARTIAL); + while (getMap().getCase(mob.getX(), mob.getY()).getCollision() != Collision.ANY); + getMap().getCase(mob.getX(), mob.getY()).setCollision(Collision.PARTIAL); mobs.add(mob); } } @@ -218,7 +208,7 @@ public class GameFrame extends JFrame { if (tower == null || tower.getPrice() > reward) return; - RawCase c = getCase(x, y); + RawCase c = getMap().getCase(x, y); if (c == null || c.getCollision() != Collision.ANY) return; c.setCollision(Collision.FULL); diff --git a/src/main/java/fr/ynerant/leveleditor/game/Mob.java b/src/main/java/fr/ynerant/leveleditor/game/Mob.java index daea566..187f279 100644 --- a/src/main/java/fr/ynerant/leveleditor/game/Mob.java +++ b/src/main/java/fr/ynerant/leveleditor/game/Mob.java @@ -73,8 +73,8 @@ public abstract class Mob { --tickRemains; else { tickRemains = getSlowness(); - RawCase c = game.getCase(getX(), getY()); - RawCase other = game.getCase(getX() - 1, getY()); + RawCase c = game.getMap().getCase(getX(), getY()); + RawCase other = game.getMap().getCase(getX() - 1, getY()); if (other == null || other.getCollision() == Collision.ANY) { c.setCollision(Collision.ANY); if (other != null)