added range to cursor
This commit is contained in:
parent
5ef254b75f
commit
3b55f16b24
@ -1,13 +1,22 @@
|
||||
package eu.mhsl.minenet.minigames.instance.game.stateless.types.towerdefense;
|
||||
|
||||
import eu.mhsl.minenet.minigames.instance.game.stateless.types.towerdefense.towers.Tower;
|
||||
import net.minestom.server.coordinate.Point;
|
||||
import net.minestom.server.coordinate.Pos;
|
||||
import net.minestom.server.coordinate.Vec;
|
||||
import net.minestom.server.entity.Entity;
|
||||
import net.minestom.server.entity.EntityType;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.instance.block.Block;
|
||||
import net.minestom.server.item.Material;
|
||||
import net.minestom.server.network.packet.server.SendablePacket;
|
||||
import net.minestom.server.network.packet.server.play.ParticlePacket;
|
||||
import net.minestom.server.particle.Particle;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
public class Cursor extends Entity {
|
||||
private boolean cursorActive = false;
|
||||
private final int reach;
|
||||
@ -30,8 +39,10 @@ public class Cursor extends Entity {
|
||||
return;
|
||||
}
|
||||
if(!this.getInstance().getBlock(newTargetBlockPosition).equals(Block.BLACK_WOOL)) this.setCursorEnabled(false);
|
||||
if(!this.getInstance().getGame().getAvailableTowers().containsKey(this.getInstance().getPlayer().getItemInMainHand().material())) this.setCursorEnabled(false);
|
||||
Material holdingMaterial = this.getInstance().getPlayerHandMaterial();
|
||||
if(!this.getInstance().getGame().getAvailableTowers().containsKey(holdingMaterial)) this.setCursorEnabled(false);
|
||||
this.teleport(new Pos(newTargetBlockPosition.add(0.5,1,0.5)));
|
||||
this.showRange(this.getInstance().getGame().getAvailableTowers().get(holdingMaterial));
|
||||
}
|
||||
|
||||
private void setCursorEnabled(boolean enabled) {
|
||||
@ -39,6 +50,26 @@ public class Cursor extends Entity {
|
||||
this.setInvisible(!enabled);
|
||||
}
|
||||
|
||||
private void showRange(@Nullable Class<? extends Tower> towerClass) {
|
||||
if(towerClass == null) return;
|
||||
if(!this.isCursorActive()) return;
|
||||
try {
|
||||
int range = towerClass.getConstructor().newInstance().getRange();
|
||||
Collection<SendablePacket> particles = new ArrayList<>();
|
||||
double circumference = 2 * Math.PI * range;
|
||||
int count = (int) (circumference * 1.5);
|
||||
for (int i = 0; i < count; i++) {
|
||||
double radians = ((2 * Math.PI) / count) * i;
|
||||
Vec relativePosition = new Vec(Math.sin(radians)*range, 0, Math.cos(radians)*range);
|
||||
ParticlePacket particle = new ParticlePacket(Particle.COMPOSTER, this.position.add(relativePosition), Pos.ZERO, 0, 1);
|
||||
particles.add(particle);
|
||||
}
|
||||
this.getInstance().getPlayer().sendPackets(particles);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TowerdefenseRoom getInstance() {
|
||||
return (TowerdefenseRoom) super.getInstance();
|
||||
|
@ -126,9 +126,9 @@ public class TowerdefenseRoom extends InstanceContainer {
|
||||
projectile.setAerodynamics(new Aerodynamics(0, 1, 0));
|
||||
|
||||
Vec projectileVelocity = p.getPosition().direction().normalize().mul(20);
|
||||
projectile.setVelocity(projectileVelocity.add(this.player.getVelocity()));
|
||||
projectile.setVelocity(projectileVelocity);
|
||||
projectile.scheduleRemove(Duration.ofSeconds(5));
|
||||
projectile.setInstance(this, p.getPosition().add(0, p.getEyeHeight(), 0));
|
||||
projectile.setInstance(this, p.getPosition().add(0, p.getEyeHeight()-0.5, 0));
|
||||
projectile.eventNode()
|
||||
.addListener(ProjectileCollideWithEntityEvent.class, hitEvent -> {
|
||||
if(!(hitEvent.getTarget() instanceof EntityCreature target)) return;
|
||||
@ -145,6 +145,10 @@ public class TowerdefenseRoom extends InstanceContainer {
|
||||
this.player.setLevel(this.money);
|
||||
}
|
||||
|
||||
public Material getPlayerHandMaterial() {
|
||||
return this.player.getItemInMainHand().material();
|
||||
}
|
||||
|
||||
private synchronized void placeTower() {
|
||||
if(!this.canPlaceActiveTower()) {
|
||||
if(this.cursor.isCursorActive() && !this.enoughMoneyForActiveTower()) {
|
||||
@ -163,8 +167,8 @@ public class TowerdefenseRoom extends InstanceContainer {
|
||||
.newInstance();
|
||||
this.setBlock(this.cursor.getTargetBlockPosition(), Block.BLUE_WOOL);
|
||||
tower.setInstance(this, this.cursor.getPosition());
|
||||
System.out.println("PLACE");
|
||||
this.towers.add(tower);
|
||||
tower.startShooting();
|
||||
this.addMoney(-this.game.getPrices().get(tower.getClass()));
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
|
@ -47,6 +47,7 @@ public abstract class ShootingTower extends Tower {
|
||||
if(!this.getRoomInstance().getEnemies().contains(target)) return;
|
||||
this.causeDamage(target);
|
||||
this.onProjectileHit(target);
|
||||
target.setFlyingWithElytra(true);
|
||||
projectile.remove();
|
||||
})
|
||||
.addListener(ProjectileCollideWithBlockEvent.class, event -> projectile.remove());
|
||||
|
@ -45,7 +45,7 @@ public abstract class Tower extends EntityCreature {
|
||||
protected int range;
|
||||
protected double attacksPerSecond;
|
||||
protected TaskSchedule attackDelay;
|
||||
private final Task attackTask;
|
||||
private @Nullable Task attackTask;
|
||||
private float sellingPriceMultiplier = 1;
|
||||
|
||||
public Tower(@NotNull EntityType entityType, int damage, double attacksPerSecond, int range) {
|
||||
@ -54,6 +54,9 @@ public abstract class Tower extends EntityCreature {
|
||||
this.range = range;
|
||||
this.attacksPerSecond = attacksPerSecond;
|
||||
this.attackDelay = TaskSchedule.millis((long) (1000/attacksPerSecond));
|
||||
}
|
||||
|
||||
public void startShooting() {
|
||||
this.attackTask = MinecraftServer.getSchedulerManager().scheduleTask(() -> {
|
||||
EntityCreature nextEnemy = this.getNextEnemy();
|
||||
if(nextEnemy == null) return this.attackDelay;
|
||||
@ -65,7 +68,7 @@ public abstract class Tower extends EntityCreature {
|
||||
|
||||
@Override
|
||||
protected void remove(boolean permanent) {
|
||||
this.attackTask.cancel();
|
||||
if(this.attackTask != null) this.attackTask.cancel();
|
||||
super.remove(permanent);
|
||||
}
|
||||
|
||||
@ -115,6 +118,10 @@ public abstract class Tower extends EntityCreature {
|
||||
return this.damage * this.attacksPerSecond * this.range * 2;
|
||||
}
|
||||
|
||||
public int getRange() {
|
||||
return this.range;
|
||||
}
|
||||
|
||||
protected void causeDamage(EntityCreature enemy) {
|
||||
this.causeDamage(enemy, this.damage);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user