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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class RawMap {
private List<RawCase> cases;
private java.util.Map<Integer, RawCase> 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;
}

View File

@ -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);

View File

@ -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)