Better complexity

This commit is contained in:
Yohann D'ANELLO 2020-02-25 11:41:50 +01:00
parent 97d455756e
commit c1c438a053
3 changed files with 18 additions and 16 deletions

View File

@ -4,10 +4,12 @@ import fr.ynerant.leveleditor.editor.Map;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
public class RawMap { public class RawMap {
private List<RawCase> cases; private List<RawCase> cases;
private java.util.Map<Integer, RawCase> cases_map;
private int width; private int width;
private int height; private int height;
private transient BufferedImage font; private transient BufferedImage font;
@ -36,6 +38,16 @@ public class RawMap {
return cases; 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() { public int getWidth() {
return width; return width;
} }

View File

@ -108,25 +108,15 @@ public class GameFrame extends JFrame {
return map; 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() { public void tick() {
if (mobs.isEmpty() && round < 4) { if (mobs.isEmpty() && round < 4) {
++round; ++round;
for (int i = 1; i <= RANDOM.nextInt(16) + 1; ++i) { for (int i = 1; i <= RANDOM.nextInt(16) + 1; ++i) {
Mob mob = Mob.getRandomMob(); Mob mob = Mob.getRandomMob();
do { do
mob.move(RANDOM.nextInt(getMap().getWidth() / 16), RANDOM.nextInt(getMap().getHeight() / 16)); mob.move(RANDOM.nextInt(getMap().getWidth() / 16), RANDOM.nextInt(getMap().getHeight() / 16));
} while (getMap().getCase(mob.getX(), mob.getY()).getCollision() != Collision.ANY);
while (getCase(mob.getX(), mob.getY()).getCollision() != Collision.ANY); getMap().getCase(mob.getX(), mob.getY()).setCollision(Collision.PARTIAL);
getCase(mob.getX(), mob.getY()).setCollision(Collision.PARTIAL);
mobs.add(mob); mobs.add(mob);
} }
} }
@ -218,7 +208,7 @@ public class GameFrame extends JFrame {
if (tower == null || tower.getPrice() > reward) if (tower == null || tower.getPrice() > reward)
return; return;
RawCase c = getCase(x, y); RawCase c = getMap().getCase(x, y);
if (c == null || c.getCollision() != Collision.ANY) if (c == null || c.getCollision() != Collision.ANY)
return; return;
c.setCollision(Collision.FULL); c.setCollision(Collision.FULL);

View File

@ -73,8 +73,8 @@ public abstract class Mob {
--tickRemains; --tickRemains;
else { else {
tickRemains = getSlowness(); tickRemains = getSlowness();
RawCase c = game.getCase(getX(), getY()); RawCase c = game.getMap().getCase(getX(), getY());
RawCase other = game.getCase(getX() - 1, getY()); RawCase other = game.getMap().getCase(getX() - 1, getY());
if (other == null || other.getCollision() == Collision.ANY) { if (other == null || other.getCollision() == Collision.ANY) {
c.setCollision(Collision.ANY); c.setCollision(Collision.ANY);
if (other != null) if (other != null)