Add towers

This commit is contained in:
Yohann D'ANELLO 2020-02-25 03:59:31 +01:00
parent 927d98c977
commit dc44220ea6
6 changed files with 156 additions and 3 deletions

View File

@ -0,0 +1,38 @@
package fr.ynerant.leveleditor.game;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class BasicTower extends Tower {
public BasicTower(int x, int y) {
super(x, y);
}
@Override
public int getDamagePerShot() {
return 1;
}
@Override
public long getPeriod() {
return 5;
}
@Override
public int getPrice() {
return 10;
}
@Override
Collection<Mob> _filterDetectedMobs(Collection<Mob> mobs) {
List<Mob> filtered = new ArrayList<>();
for (Mob mob : mobs) {
if (mob.getX() == getX() || mob.getY() == getY())
filtered.add(mob);
}
return filtered;
}
}

View File

@ -19,7 +19,10 @@ public class GameFrame extends JFrame implements WindowListener {
private final RawMap map; private final RawMap map;
private int round = 0; private int round = 0;
private int hp = 20;
private int reward = 20;
private List<Mob> mobs = new ArrayList<>(); private List<Mob> mobs = new ArrayList<>();
private List<Tower> towers = new ArrayList<>();
public GameFrame(RawMap map) { public GameFrame(RawMap map) {
super("Jeu"); super("Jeu");
@ -72,10 +75,20 @@ public class GameFrame extends JFrame implements WindowListener {
} }
} }
for (Tower tower : towers) {
for (Mob mob : tower.filterDetectedMobs(mobs))
mob.hit();
}
for (Mob mob : new ArrayList<>(mobs)) { for (Mob mob : new ArrayList<>(mobs)) {
mob.tick(); mob.tick();
if (mob.getX() < 0 || mob.isDead()) if (mob.getX() < 0 || mob.isDead()) {
mobs.remove(mob); mobs.remove(mob);
if (mob.getX() < 0)
--hp;
else
reward += mob.getReward();
}
else { else {
Sprite s = mob.getSprite(); Sprite s = mob.getSprite();
g.drawImage(s.getImage(), SPRITE_SIZE * mob.getX(), SPRITE_SIZE * mob.getY(), SPRITE_SIZE, SPRITE_SIZE, null, null); g.drawImage(s.getImage(), SPRITE_SIZE * mob.getX(), SPRITE_SIZE * mob.getY(), SPRITE_SIZE, SPRITE_SIZE, null, null);

View File

@ -3,8 +3,6 @@ package fr.ynerant.leveleditor.game;
import fr.ynerant.leveleditor.api.editor.sprites.Sprite; import fr.ynerant.leveleditor.api.editor.sprites.Sprite;
import fr.ynerant.leveleditor.api.editor.sprites.SpriteRegister; import fr.ynerant.leveleditor.api.editor.sprites.SpriteRegister;
import java.util.Arrays;
import java.util.List;
import java.util.Random; import java.util.Random;
public abstract class Mob { public abstract class Mob {

View File

@ -0,0 +1,30 @@
package fr.ynerant.leveleditor.game;
import java.util.ArrayList;
import java.util.Collection;
public class NullTower extends Tower {
public NullTower(int x, int y) {
super(x, y);
}
@Override
public int getDamagePerShot() {
return Integer.MAX_VALUE;
}
@Override
public long getPeriod() {
return 1;
}
@Override
public int getPrice() {
return 0;
}
@Override
Collection<Mob> _filterDetectedMobs(Collection<Mob> mobs) {
return new ArrayList<>();
}
}

View File

@ -0,0 +1,29 @@
package fr.ynerant.leveleditor.game;
import java.util.Collection;
public class SuperTower extends Tower {
public SuperTower(int x, int y) {
super(x, y);
}
@Override
public int getDamagePerShot() {
return 20;
}
@Override
public long getPeriod() {
return 10;
}
@Override
public int getPrice() {
return 142;
}
@Override
Collection<Mob> _filterDetectedMobs(Collection<Mob> mobs) {
return mobs;
}
}

View File

@ -0,0 +1,45 @@
package fr.ynerant.leveleditor.game;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;
public abstract class Tower {
private final int x;
private final int y;
private long remainingTicks;
public Tower(int x, int y) {
this.x = x;
this.y = y;
}
private static final Random RANDOM = new Random();
public abstract int getDamagePerShot();
public abstract long getPeriod();
public abstract int getPrice();
abstract Collection<Mob> _filterDetectedMobs(Collection<Mob> mobs);
public Collection<Mob> filterDetectedMobs(Collection<Mob> mobs) {
if (remainingTicks > 0) {
--remainingTicks;
return new ArrayList<>();
}
else {
remainingTicks = getPeriod();
return filterDetectedMobs(mobs);
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}