Collisions

This commit is contained in:
Yohann D'ANELLO 2020-02-25 11:30:22 +01:00
parent 1c7762c40c
commit 97d455756e
4 changed files with 42 additions and 11 deletions

View File

@ -53,4 +53,8 @@ public class RawCase {
public Collision getCollision() {
return collision;
}
public void setCollision(Collision collision) {
this.collision = collision;
}
}

View File

@ -1,5 +1,6 @@
package fr.ynerant.leveleditor.game;
import fr.ynerant.leveleditor.api.editor.Collision;
import fr.ynerant.leveleditor.api.editor.RawCase;
import fr.ynerant.leveleditor.api.editor.RawMap;
import fr.ynerant.leveleditor.api.editor.sprites.Sprite;
@ -107,12 +108,25 @@ 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();
mob.move(RANDOM.nextInt(getMap().getWidth() / 16), RANDOM.nextInt(getMap().getHeight() / 16));
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);
mobs.add(mob);
}
}
@ -123,7 +137,7 @@ public class GameFrame extends JFrame {
}
for (Mob mob : new ArrayList<>(mobs)) {
mob.tick();
mob.tick(this);
if (mob.getX() < 0 || mob.isDead()) {
mobs.remove(mob);
if (mob.getX() < 0) {
@ -139,16 +153,15 @@ public class GameFrame extends JFrame {
}
}
if (round == 4 && mobs.isEmpty()) {
winLabel.setForeground(Color.green.darker());
winLabel.setText("Vous avez gagné !");
return;
}
waveLabel.setText("Vague " + round);
nbMobsLabel.setText(mobs.size() + " mob" + (mobs.size() > 1 ? "s" : "") + " restant" + (mobs.size() > 1 ? "s" : ""));
hpLabel.setText("Points de vie : " + hp);
rewardLabel.setText("Butin : " + reward);
if (round == 4 && mobs.isEmpty()) {
winLabel.setForeground(Color.green.darker());
winLabel.setText("Vous avez gagné !");
}
}
private class Grid extends JComponent implements MouseListener {
@ -205,6 +218,11 @@ public class GameFrame extends JFrame {
if (tower == null || tower.getPrice() > reward)
return;
RawCase c = getCase(x, y);
if (c == null || c.getCollision() != Collision.ANY)
return;
c.setCollision(Collision.FULL);
reward -= tower.getPrice();
towers.add(tower);
}

View File

@ -1,5 +1,7 @@
package fr.ynerant.leveleditor.game;
import fr.ynerant.leveleditor.api.editor.Collision;
import fr.ynerant.leveleditor.api.editor.RawCase;
import fr.ynerant.leveleditor.api.editor.sprites.Sprite;
import fr.ynerant.leveleditor.api.editor.sprites.SpriteRegister;
@ -66,12 +68,19 @@ public abstract class Mob {
return false;
}
public void tick() {
public void tick(GameFrame game) {
if (tickRemains > 0)
--tickRemains;
else {
tickRemains = getSlowness();
move(getX() - 1, getY());
RawCase c = game.getCase(getX(), getY());
RawCase other = game.getCase(getX() - 1, getY());
if (other == null || other.getCollision() == Collision.ANY) {
c.setCollision(Collision.ANY);
if (other != null)
other.setCollision(Collision.PARTIAL);
move(getX() - 1, getY());
}
}
}

View File

@ -25,7 +25,7 @@ public class NullTower extends Tower {
@Override
public int getPrice() {
return 0;
return 5;
}
@Override