Perform general code cleaning & refactoring
This commit is contained in:
@@ -1,42 +1,40 @@
|
|||||||
package com.bartlomiejpluta.demo.ai;
|
package com.bartlomiejpluta.demo.ai;
|
||||||
|
|
||||||
import lombok.*;
|
import com.bartlomiejpluta.base.api.ai.AI;
|
||||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||||
import com.bartlomiejpluta.base.api.ai.*;
|
import com.bartlomiejpluta.base.lib.ai.RandomMovementAI;
|
||||||
import com.bartlomiejpluta.base.api.move.Direction;
|
import com.bartlomiejpluta.base.lib.ai.RunawayAI;
|
||||||
import com.bartlomiejpluta.base.lib.ai.*;
|
|
||||||
|
|
||||||
import com.bartlomiejpluta.demo.entity.Enemy;
|
|
||||||
import com.bartlomiejpluta.demo.entity.Creature;
|
import com.bartlomiejpluta.demo.entity.Creature;
|
||||||
|
import com.bartlomiejpluta.demo.entity.Enemy;
|
||||||
|
|
||||||
public class AnimalAI implements AI {
|
public class AnimalAI implements AI {
|
||||||
private final Enemy animal;
|
private final Enemy animal;
|
||||||
private final Creature creature;
|
private final Creature creature;
|
||||||
private final int range;
|
private final int range;
|
||||||
private final AI idleAI;
|
private final AI idleAI;
|
||||||
private final AI runawayAI;
|
private final AI runawayAI;
|
||||||
|
|
||||||
public AnimalAI(Enemy animal, Creature creature, int range) {
|
public AnimalAI(Enemy animal, Creature creature, int range) {
|
||||||
this.animal = animal;
|
this.animal = animal;
|
||||||
this.creature = creature;
|
this.creature = creature;
|
||||||
this.range = range;
|
this.range = range;
|
||||||
this.idleAI = new RandomMovementAI<>(animal, 4);
|
this.idleAI = new RandomMovementAI<>(animal, 4);
|
||||||
this.runawayAI = new RunawayAI<>(animal, creature);
|
this.runawayAI = new RunawayAI<>(animal, creature);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void nextActivity(ObjectLayer layer, float dt) {
|
public void nextActivity(ObjectLayer layer, float dt) {
|
||||||
if(animal.isMoving()) {
|
if (animal.isMoving()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var distance = animal.manhattanDistance(creature);
|
var distance = animal.manhattanDistance(creature);
|
||||||
|
|
||||||
if(animal.getHp() < animal.getMaxHp() && distance < range) {
|
if (animal.getHp() < animal.getMaxHp() && distance < range) {
|
||||||
runawayAI.nextActivity(layer, dt);
|
runawayAI.nextActivity(layer, dt);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
idleAI.nextActivity(layer, dt);
|
idleAI.nextActivity(layer, dt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,53 +1,42 @@
|
|||||||
package com.bartlomiejpluta.demo.ai;
|
package com.bartlomiejpluta.demo.ai;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.ai.AI;
|
import com.bartlomiejpluta.base.api.ai.AI;
|
||||||
import com.bartlomiejpluta.base.api.ai.NPC;
|
|
||||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
|
||||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||||
import com.bartlomiejpluta.base.api.move.MoveEvent;
|
|
||||||
import com.bartlomiejpluta.base.lib.ai.KeepStraightDistanceAI;
|
import com.bartlomiejpluta.base.lib.ai.KeepStraightDistanceAI;
|
||||||
import com.bartlomiejpluta.base.lib.ai.RandomMovementAI;
|
import com.bartlomiejpluta.base.lib.ai.RandomMovementAI;
|
||||||
import com.bartlomiejpluta.base.util.path.MovementPath;
|
|
||||||
import com.bartlomiejpluta.base.util.path.PathExecutor;
|
|
||||||
import com.bartlomiejpluta.base.util.pathfinder.AstarPathFinder;
|
import com.bartlomiejpluta.base.util.pathfinder.AstarPathFinder;
|
||||||
import com.bartlomiejpluta.base.util.pathfinder.PathFinder;
|
|
||||||
import lombok.NonNull;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import org.joml.Vector2i;
|
|
||||||
import org.joml.Vector2ic;
|
|
||||||
|
|
||||||
import com.bartlomiejpluta.demo.entity.Enemy;
|
|
||||||
import com.bartlomiejpluta.demo.entity.Creature;
|
import com.bartlomiejpluta.demo.entity.Creature;
|
||||||
|
import com.bartlomiejpluta.demo.entity.Enemy;
|
||||||
|
|
||||||
public class ArcherAI extends KeepStraightDistanceAI<Enemy, Creature> {
|
public class ArcherAI extends KeepStraightDistanceAI<Enemy, Creature> {
|
||||||
private static final int ASTAR_MAX_NODES = 100;
|
private static final int ASTAR_MAX_NODES = 100;
|
||||||
private static final int IDLE_MOVEMENT_INTERVAL = 4;
|
private static final int IDLE_MOVEMENT_INTERVAL = 4;
|
||||||
private final int range;
|
private final int range;
|
||||||
private final AI idle;
|
private final AI idle;
|
||||||
|
|
||||||
public ArcherAI(Enemy enemy, Creature target, int minRange, int maxRange, int range) {
|
public ArcherAI(Enemy enemy, Creature target, int minRange, int maxRange, int range) {
|
||||||
super(new AstarPathFinder(ASTAR_MAX_NODES), enemy, target, minRange, maxRange);
|
super(new AstarPathFinder(ASTAR_MAX_NODES), enemy, target, minRange, maxRange);
|
||||||
this.range = range;
|
this.range = range;
|
||||||
this.idle = new RandomMovementAI<>(enemy, IDLE_MOVEMENT_INTERVAL);
|
this.idle = new RandomMovementAI<>(enemy, IDLE_MOVEMENT_INTERVAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean sees(Enemy enemy, Creature target, ObjectLayer layer) {
|
protected boolean sees(Enemy enemy, Creature target, ObjectLayer layer) {
|
||||||
return enemy.manhattanDistance(target) < range;
|
return enemy.manhattanDistance(target) < range;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void interact(Enemy enemy, Creature target, ObjectLayer layer, float dt) {
|
protected void interact(Enemy enemy, Creature target, ObjectLayer layer, float dt) {
|
||||||
enemy.attack();
|
enemy.attack();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void follow(Enemy enemy, Creature target, ObjectLayer layer, float dt) {
|
protected void follow(Enemy enemy, Creature target, ObjectLayer layer, float dt) {
|
||||||
// noop
|
// noop
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void idle(Enemy enemy, Creature target, ObjectLayer layer, float dt) {
|
protected void idle(Enemy enemy, Creature target, ObjectLayer layer, float dt) {
|
||||||
idle.nextActivity(layer, dt);
|
idle.nextActivity(layer, dt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,43 +1,42 @@
|
|||||||
package com.bartlomiejpluta.demo.ai;
|
package com.bartlomiejpluta.demo.ai;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.api.ai.AI;
|
||||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||||
|
import com.bartlomiejpluta.base.lib.ai.FollowObjectAI;
|
||||||
import com.bartlomiejpluta.base.util.pathfinder.*;
|
import com.bartlomiejpluta.base.lib.ai.RandomMovementAI;
|
||||||
import com.bartlomiejpluta.base.api.ai.*;
|
import com.bartlomiejpluta.base.util.pathfinder.AstarPathFinder;
|
||||||
import com.bartlomiejpluta.base.lib.ai.*;
|
|
||||||
|
|
||||||
import com.bartlomiejpluta.demo.entity.Enemy;
|
|
||||||
import com.bartlomiejpluta.demo.entity.Creature;
|
import com.bartlomiejpluta.demo.entity.Creature;
|
||||||
|
import com.bartlomiejpluta.demo.entity.Enemy;
|
||||||
|
|
||||||
public class SimpleEnemyAI extends FollowObjectAI<Enemy, Creature> {
|
public class SimpleEnemyAI extends FollowObjectAI<Enemy, Creature> {
|
||||||
private static final int ASTAR_MAX_NODES = 100;
|
private static final int ASTAR_MAX_NODES = 100;
|
||||||
private static final int IDLE_MOVEMENT_INTERVAL = 4;
|
private static final int IDLE_MOVEMENT_INTERVAL = 4;
|
||||||
private final AI idle;
|
private final AI idle;
|
||||||
private final int range;
|
private final int range;
|
||||||
|
|
||||||
public SimpleEnemyAI(Enemy enemy, Creature target, int range) {
|
public SimpleEnemyAI(Enemy enemy, Creature target, int range) {
|
||||||
super(new AstarPathFinder(ASTAR_MAX_NODES), enemy, target);
|
super(new AstarPathFinder(ASTAR_MAX_NODES), enemy, target);
|
||||||
this.range = range;
|
this.range = range;
|
||||||
this.idle = new RandomMovementAI<>(enemy, IDLE_MOVEMENT_INTERVAL);
|
this.idle = new RandomMovementAI<>(enemy, IDLE_MOVEMENT_INTERVAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean sees(Enemy enemy, Creature target, ObjectLayer layer, int distance) {
|
protected boolean sees(Enemy enemy, Creature target, ObjectLayer layer, int distance) {
|
||||||
return distance < range;
|
return distance < range;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void interact(Enemy enemy, Creature target, ObjectLayer layer, float dt) {
|
protected void interact(Enemy enemy, Creature target, ObjectLayer layer, float dt) {
|
||||||
enemy.attack();
|
enemy.attack();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void follow(Enemy enemy, Creature target, ObjectLayer layer, float dt) {
|
protected void follow(Enemy enemy, Creature target, ObjectLayer layer, float dt) {
|
||||||
// noop
|
// noop
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void idle(Enemy enemy, Creature target, ObjectLayer layer, float dt) {
|
protected void idle(Enemy enemy, Creature target, ObjectLayer layer, float dt) {
|
||||||
idle.nextActivity(layer, dt);
|
idle.nextActivity(layer, dt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,27 +1,25 @@
|
|||||||
package com.bartlomiejpluta.demo.ai;
|
package com.bartlomiejpluta.demo.ai;
|
||||||
|
|
||||||
import lombok.*;
|
import com.bartlomiejpluta.base.api.ai.AI;
|
||||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||||
import com.bartlomiejpluta.base.api.ai.*;
|
|
||||||
import com.bartlomiejpluta.base.lib.ai.*;
|
|
||||||
|
|
||||||
import com.bartlomiejpluta.demo.entity.Enemy;
|
|
||||||
import com.bartlomiejpluta.demo.entity.Creature;
|
import com.bartlomiejpluta.demo.entity.Creature;
|
||||||
|
import com.bartlomiejpluta.demo.entity.Enemy;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class SimpleSniperAI implements AI {
|
public class SimpleSniperAI implements AI {
|
||||||
private final Enemy enemy;
|
private final Enemy enemy;
|
||||||
private Creature target;
|
private Creature target;
|
||||||
private int range;
|
private int range;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void nextActivity(ObjectLayer layer, float dt) {
|
public void nextActivity(ObjectLayer layer, float dt) {
|
||||||
var enemyCoords = enemy.getCoordinates();
|
var enemyCoords = enemy.getCoordinates();
|
||||||
var targetCoords = target.getCoordinates();
|
var targetCoords = target.getCoordinates();
|
||||||
if(enemy.manhattanDistance(target) <= range && (enemyCoords.x() == targetCoords.x() || enemyCoords.y() == targetCoords.y())) {
|
if (enemy.manhattanDistance(target) <= range && (enemyCoords.x() == targetCoords.x() || enemyCoords.y() == targetCoords.y())) {
|
||||||
var direction = enemy.getDirectionTowards(target);
|
var direction = enemy.getDirectionTowards(target);
|
||||||
enemy.setFaceDirection(direction);
|
enemy.setFaceDirection(direction);
|
||||||
enemy.attack();
|
enemy.attack();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,78 +1,77 @@
|
|||||||
package com.bartlomiejpluta.demo.ai;
|
package com.bartlomiejpluta.demo.ai;
|
||||||
|
|
||||||
import lombok.*;
|
|
||||||
import com.bartlomiejpluta.base.api.ai.AI;
|
import com.bartlomiejpluta.base.api.ai.AI;
|
||||||
import com.bartlomiejpluta.base.api.move.MoveEvent;
|
|
||||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||||
import com.bartlomiejpluta.base.lib.ai.*;
|
import com.bartlomiejpluta.base.api.move.MoveEvent;
|
||||||
|
import com.bartlomiejpluta.base.lib.ai.RunawayAI;
|
||||||
import com.bartlomiejpluta.demo.entity.Creature;
|
import com.bartlomiejpluta.demo.entity.Creature;
|
||||||
import com.bartlomiejpluta.demo.entity.Enemy;
|
import com.bartlomiejpluta.demo.entity.Enemy;
|
||||||
import com.bartlomiejpluta.demo.world.weapon.*;
|
import com.bartlomiejpluta.demo.world.weapon.MeleeWeapon;
|
||||||
|
import lombok.NonNull;
|
||||||
|
|
||||||
public class WeaponBasedAI implements AI {
|
public class WeaponBasedAI implements AI {
|
||||||
private static final int RANGE = 10;
|
private static final int RANGE = 10;
|
||||||
private static final int MIN_RANGE = 3;
|
private static final int MIN_RANGE = 3;
|
||||||
private static final int MAX_RANGE = 12;
|
private static final int MAX_RANGE = 12;
|
||||||
private final Enemy enemy;
|
private final Enemy enemy;
|
||||||
private final Creature target;
|
private final Creature target;
|
||||||
private final RunawayAI<Enemy, Creature> runawayAI;
|
private final RunawayAI<Enemy, Creature> runawayAI;
|
||||||
private final SimpleEnemyAI meleeAI;
|
private final SimpleEnemyAI meleeAI;
|
||||||
private final ArcherAI archerAI;
|
private final ArcherAI archerAI;
|
||||||
|
|
||||||
public WeaponBasedAI(@NonNull Enemy enemy, @NonNull Creature target) {
|
public WeaponBasedAI(@NonNull Enemy enemy, @NonNull Creature target) {
|
||||||
this.enemy = enemy;
|
this.enemy = enemy;
|
||||||
this.target = target;
|
this.target = target;
|
||||||
this.runawayAI = new RunawayAI<>(enemy, target);
|
this.runawayAI = new RunawayAI<>(enemy, target);
|
||||||
this.meleeAI = new SimpleEnemyAI(enemy, target, RANGE);
|
this.meleeAI = new SimpleEnemyAI(enemy, target, RANGE);
|
||||||
this.archerAI = new ArcherAI(enemy, target, MIN_RANGE, MAX_RANGE, RANGE);
|
this.archerAI = new ArcherAI(enemy, target, MIN_RANGE, MAX_RANGE, RANGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void recomputePath() {
|
public void recomputePath() {
|
||||||
meleeAI.recomputePath();
|
meleeAI.recomputePath();
|
||||||
archerAI.recomputePath();
|
archerAI.recomputePath();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void recomputePath(@NonNull MoveEvent event) {
|
public void recomputePath(@NonNull MoveEvent event) {
|
||||||
meleeAI.recomputePath(event);
|
meleeAI.recomputePath(event);
|
||||||
archerAI.recomputePath(event);
|
archerAI.recomputePath(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void nextActivity(ObjectLayer layer, float dt) {
|
public void nextActivity(ObjectLayer layer, float dt) {
|
||||||
var lastAttacker = enemy.getLastAttacker();
|
var lastAttacker = enemy.getLastAttacker();
|
||||||
if(lastAttacker != null && lastAttacker instanceof Creature) {
|
if (lastAttacker != null && lastAttacker instanceof Creature) {
|
||||||
var attacker = (Creature) lastAttacker;
|
var attacker = (Creature) lastAttacker;
|
||||||
if(attacker.isAlive()) {
|
if (attacker.isAlive()) {
|
||||||
runawayAI.setDanger(attacker);
|
runawayAI.setDanger(attacker);
|
||||||
meleeAI.setTarget(attacker);
|
meleeAI.setTarget(attacker);
|
||||||
archerAI.setTarget(attacker);
|
archerAI.setTarget(attacker);
|
||||||
} else {
|
} else {
|
||||||
runawayAI.setDanger(target);
|
runawayAI.setDanger(target);
|
||||||
meleeAI.setTarget(target);
|
meleeAI.setTarget(target);
|
||||||
archerAI.setTarget(target);
|
archerAI.setTarget(target);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var meleeWeapon = enemy.getMeleeWeapon();
|
var meleeWeapon = enemy.getMeleeWeapon();
|
||||||
var rangedWeapon = enemy.getRangedWeapon();
|
var rangedWeapon = enemy.getRangedWeapon();
|
||||||
|
|
||||||
if(meleeWeapon == null && rangedWeapon == null) {
|
if (meleeWeapon == null && rangedWeapon == null) {
|
||||||
runawayAI.nextActivity(layer, dt);
|
runawayAI.nextActivity(layer, dt);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(rangedWeapon == null || enemy.manhattanDistance(target) == 1) {
|
if (rangedWeapon == null || enemy.manhattanDistance(target) == 1) {
|
||||||
enemy.setWeapon(meleeWeapon);
|
enemy.setWeapon(meleeWeapon);
|
||||||
meleeAI.nextActivity(layer, dt);
|
meleeAI.nextActivity(layer, dt);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(enemy.getWeapon() instanceof MeleeWeapon) {
|
if (enemy.getWeapon() instanceof MeleeWeapon) {
|
||||||
meleeAI.nextActivity(layer, dt);
|
meleeAI.nextActivity(layer, dt);
|
||||||
}
|
}
|
||||||
|
|
||||||
enemy.setWeapon(rangedWeapon);
|
enemy.setWeapon(rangedWeapon);
|
||||||
archerAI.nextActivity(layer, dt);
|
archerAI.nextActivity(layer, dt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,85 +1,82 @@
|
|||||||
package com.bartlomiejpluta.demo.entity;
|
package com.bartlomiejpluta.demo.entity;
|
||||||
|
|
||||||
import lombok.*;
|
|
||||||
import org.slf4j.*;
|
|
||||||
import org.joml.Vector2i;
|
|
||||||
import com.bartlomiejpluta.base.api.context.*;
|
|
||||||
import com.bartlomiejpluta.base.api.character.Character;
|
import com.bartlomiejpluta.base.api.character.Character;
|
||||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.lib.animation.AnimationRunner;
|
|
||||||
|
|
||||||
import com.bartlomiejpluta.demo.world.weapon.Weapon;
|
import com.bartlomiejpluta.demo.world.weapon.Weapon;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NonNull;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public abstract class Creature extends NamedCharacter {
|
public abstract class Creature extends NamedCharacter {
|
||||||
private static final Logger log = LoggerFactory.getLogger(Creature.class);
|
private static final Logger log = LoggerFactory.getLogger(Creature.class);
|
||||||
|
|
||||||
protected int attackCooldown = 0;
|
protected int attackCooldown = 0;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
protected boolean alive = true;
|
protected boolean alive = true;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
protected boolean immortal = false;
|
protected boolean immortal = false;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
protected int maxHp;
|
protected int maxHp;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
protected int hp;
|
protected int hp;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
private Weapon weapon;
|
private Weapon weapon;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private NamedCharacter lastAttacker;
|
private NamedCharacter lastAttacker;
|
||||||
|
|
||||||
public Creature(@NonNull Character entity) {
|
public Creature(@NonNull Character entity) {
|
||||||
super(entity);
|
super(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void attack() {
|
public void attack() {
|
||||||
if(weapon == null) {
|
if (weapon == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(attackCooldown >= weapon.getCooldown()) {
|
if (attackCooldown >= weapon.getCooldown()) {
|
||||||
if(weapon.attack(this)) {
|
if (weapon.attack(this)) {
|
||||||
attackCooldown = 0;
|
attackCooldown = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void hit(NamedCharacter source, int dmg) {
|
public void hit(NamedCharacter source, int dmg) {
|
||||||
this.lastAttacker = source;
|
this.lastAttacker = source;
|
||||||
|
|
||||||
if(immortal) {
|
if (immortal) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
hp -= dmg;
|
hp -= dmg;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(float dt) {
|
public void update(float dt) {
|
||||||
super.update(dt);
|
super.update(dt);
|
||||||
|
|
||||||
if(weapon != null && attackCooldown < weapon.getCooldown()) {
|
if (weapon != null && attackCooldown < weapon.getCooldown()) {
|
||||||
attackCooldown += (int) (dt * 1000f);
|
attackCooldown += (int) (dt * 1000f);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(hp <= 0 && alive && getLayer() != null) {
|
if (hp <= 0 && alive && getLayer() != null) {
|
||||||
alive = false;
|
alive = false;
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void die() {
|
protected void die() {
|
||||||
if(isMoving()) {
|
if (isMoving()) {
|
||||||
abortMove();
|
abortMove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract String getName();
|
public abstract String getName();
|
||||||
}
|
}
|
||||||
@@ -1,131 +1,124 @@
|
|||||||
package com.bartlomiejpluta.demo.entity;
|
package com.bartlomiejpluta.demo.entity;
|
||||||
|
|
||||||
import lombok.*;
|
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.context.*;
|
|
||||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
|
||||||
import com.bartlomiejpluta.base.api.character.Character;
|
|
||||||
import com.bartlomiejpluta.base.api.ai.AI;
|
import com.bartlomiejpluta.base.api.ai.AI;
|
||||||
import com.bartlomiejpluta.base.api.ai.NPC;
|
import com.bartlomiejpluta.base.api.ai.NPC;
|
||||||
import com.bartlomiejpluta.base.api.move.*;
|
import com.bartlomiejpluta.base.api.context.ContextHolder;
|
||||||
|
import com.bartlomiejpluta.base.api.move.MoveEvent;
|
||||||
import com.bartlomiejpluta.base.lib.ai.*;
|
import com.bartlomiejpluta.base.lib.ai.NoopAI;
|
||||||
import com.bartlomiejpluta.base.lib.animation.*;
|
import com.bartlomiejpluta.base.lib.animation.AnimationRunner;
|
||||||
|
import com.bartlomiejpluta.base.lib.animation.SimpleAnimationRunner;
|
||||||
import com.bartlomiejpluta.base.util.random.DiceRoller;
|
import com.bartlomiejpluta.base.util.random.DiceRoller;
|
||||||
import com.bartlomiejpluta.base.util.path.*;
|
|
||||||
|
|
||||||
import com.bartlomiejpluta.demo.runner.DemoRunner;
|
|
||||||
import com.bartlomiejpluta.demo.world.weapon.*;
|
|
||||||
import com.bartlomiejpluta.demo.event.EnemyDiedEvent;
|
|
||||||
import com.bartlomiejpluta.demo.ai.*;
|
import com.bartlomiejpluta.demo.ai.*;
|
||||||
import com.bartlomiejpluta.demo.ai.ArcherAI;
|
import com.bartlomiejpluta.demo.event.EnemyDiedEvent;
|
||||||
|
import com.bartlomiejpluta.demo.runner.DemoRunner;
|
||||||
|
import com.bartlomiejpluta.demo.world.weapon.MeleeWeapon;
|
||||||
|
import com.bartlomiejpluta.demo.world.weapon.RangedWeapon;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NonNull;
|
||||||
|
|
||||||
|
|
||||||
public class Enemy extends Creature implements NPC {
|
public class Enemy extends Creature implements NPC {
|
||||||
private final DB.model.EnemyModel template;
|
private final DB.model.EnemyModel template;
|
||||||
private AI ai = NoopAI.INSTANCE;
|
private final AnimationRunner dieAnimation;
|
||||||
private final AnimationRunner dieAnimation;
|
@Getter
|
||||||
|
private final String name;
|
||||||
|
private AI ai = NoopAI.INSTANCE;
|
||||||
|
@Getter
|
||||||
|
private MeleeWeapon meleeWeapon;
|
||||||
|
@Getter
|
||||||
|
private RangedWeapon rangedWeapon;
|
||||||
|
|
||||||
@Getter
|
public Enemy(@NonNull String id) {
|
||||||
private MeleeWeapon meleeWeapon;
|
this(DB.dao.enemy.find(id));
|
||||||
|
}
|
||||||
|
|
||||||
@Getter
|
public Enemy(@NonNull DB.model.EnemyModel template) {
|
||||||
private RangedWeapon rangedWeapon;
|
super(ContextHolder.INSTANCE.getContext().createCharacter(A.charsets.get(template.getCharset()).uid));
|
||||||
|
this.template = template;
|
||||||
|
name = template.getName();
|
||||||
|
maxHp = DiceRoller.of(template.getHp()).roll();
|
||||||
|
hp = maxHp;
|
||||||
|
var speed = DiceRoller.of(template.getSpeed()).roll() / 10f;
|
||||||
|
setSpeed(speed);
|
||||||
|
setAnimationSpeed(speed / 2.0f);
|
||||||
|
setBlocking(template.isBlocking());
|
||||||
|
var runner = (DemoRunner) context.getGameRunner();
|
||||||
|
var meleeWeaponTemplate = template.getMeleeWeapon();
|
||||||
|
var rangedWeaponTemplate = template.getRangedWeapon();
|
||||||
|
|
||||||
@Getter
|
if (meleeWeaponTemplate != null) {
|
||||||
private final String name;
|
this.meleeWeapon = new MeleeWeapon(meleeWeaponTemplate);
|
||||||
|
}
|
||||||
|
|
||||||
public Enemy(@NonNull String id) {
|
if (rangedWeaponTemplate != null) {
|
||||||
this(DB.dao.enemy.find(id));
|
this.rangedWeapon = new RangedWeapon(rangedWeaponTemplate);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Enemy(@NonNull DB.model.EnemyModel template) {
|
this.dieAnimation = new SimpleAnimationRunner(A.animations.get(template.getDieAnimation()).uid);
|
||||||
super(ContextHolder.INSTANCE.getContext().createCharacter(A.charsets.get(template.getCharset()).uid));
|
}
|
||||||
this.template = template;
|
|
||||||
name = template.getName();
|
|
||||||
maxHp = DiceRoller.of(template.getHp()).roll();
|
|
||||||
hp = maxHp;
|
|
||||||
var speed = DiceRoller.of(template.getSpeed()).roll()/10f;
|
|
||||||
setSpeed(speed);
|
|
||||||
setAnimationSpeed(speed/2.0f);
|
|
||||||
setBlocking(template.isBlocking());
|
|
||||||
var runner = (DemoRunner) context.getGameRunner();
|
|
||||||
var meleeWeaponTemplate = template.getMeleeWeapon();
|
|
||||||
var rangedWeaponTemplate = template.getRangedWeapon();
|
|
||||||
|
|
||||||
if(meleeWeaponTemplate != null) {
|
@Override
|
||||||
this.meleeWeapon = new MeleeWeapon(meleeWeaponTemplate);
|
public AI getStrategy() {
|
||||||
}
|
return ai;
|
||||||
|
}
|
||||||
|
|
||||||
if(rangedWeaponTemplate != null) {
|
@Override
|
||||||
this.rangedWeapon = new RangedWeapon(rangedWeaponTemplate);
|
public void die() {
|
||||||
}
|
super.die();
|
||||||
|
changeCharacterSet(A.charsets.get(template.getDeadCharset()).uid);
|
||||||
|
setScale(0.5f);
|
||||||
|
setBlocking(false);
|
||||||
|
setZIndex(-1);
|
||||||
|
|
||||||
this.dieAnimation = new SimpleAnimationRunner(A.animations.get(template.getDieAnimation()).uid);
|
ai = NoopAI.INSTANCE;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
dieAnimation.run(context, getLayer(), this);
|
||||||
public AI getStrategy() {
|
context.playSound(A.sounds.get(template.getDieSound()).uid);
|
||||||
return ai;
|
context.fireEvent(new EnemyDiedEvent(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public Enemy followAndAttack(Creature target, int range) {
|
||||||
public void die() {
|
var ai = new SimpleEnemyAI(this, target, range);
|
||||||
super.die();
|
|
||||||
changeCharacterSet(A.charsets.get(template.getDeadCharset()).uid);
|
|
||||||
setScale(0.5f);
|
|
||||||
setBlocking(false);
|
|
||||||
setZIndex(-1);
|
|
||||||
|
|
||||||
ai = NoopAI.INSTANCE;
|
addEventListener(MoveEvent.TYPE, ai::recomputePath);
|
||||||
|
addEventListener(EnemyDiedEvent.TYPE, e -> ai.recomputePath());
|
||||||
|
|
||||||
dieAnimation.run(context, getLayer(), this);
|
this.ai = ai;
|
||||||
context.playSound(A.sounds.get(template.getDieSound()).uid);
|
|
||||||
context.fireEvent(new EnemyDiedEvent(this));
|
|
||||||
}
|
|
||||||
|
|
||||||
public Enemy followAndAttack(Creature target, int range) {
|
return this;
|
||||||
var ai = new SimpleEnemyAI(this, target, range);
|
}
|
||||||
|
|
||||||
addEventListener(MoveEvent.TYPE, ai::recomputePath);
|
public Enemy campAndHunt(Creature target, int range) {
|
||||||
addEventListener(EnemyDiedEvent.TYPE, e -> ai.recomputePath());
|
this.ai = new SimpleSniperAI(this, target, range);
|
||||||
|
|
||||||
this.ai = ai;
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
return this;
|
public Enemy asAnimal(Creature source, int range) {
|
||||||
}
|
this.ai = new AnimalAI(this, source, range);
|
||||||
|
|
||||||
public Enemy campAndHunt(Creature target, int range) {
|
return this;
|
||||||
this.ai = new SimpleSniperAI(this, target, range);
|
}
|
||||||
|
|
||||||
return this;
|
public Enemy archer(Creature target, int minRange, int maxRange, int range) {
|
||||||
}
|
var ai = new ArcherAI(this, target, minRange, maxRange, range);
|
||||||
|
|
||||||
public Enemy asAnimal(Creature source, int range) {
|
addEventListener(MoveEvent.TYPE, ai::recomputePath);
|
||||||
this.ai = new AnimalAI(this, source, range);
|
addEventListener(EnemyDiedEvent.TYPE, e -> ai.recomputePath());
|
||||||
|
|
||||||
return this;
|
this.ai = ai;
|
||||||
}
|
|
||||||
|
|
||||||
public Enemy archer(Creature target, int minRange, int maxRange, int range) {
|
return this;
|
||||||
var ai = new ArcherAI(this, target, minRange, maxRange, range);
|
}
|
||||||
|
|
||||||
addEventListener(MoveEvent.TYPE, ai::recomputePath);
|
public Enemy defaultAI() {
|
||||||
addEventListener(EnemyDiedEvent.TYPE, e -> ai.recomputePath());
|
var ai = new WeaponBasedAI(this, runner.getPlayer());
|
||||||
|
|
||||||
this.ai = ai;
|
addEventListener(MoveEvent.TYPE, ai::recomputePath);
|
||||||
|
addEventListener(EnemyDiedEvent.TYPE, e -> ai.recomputePath());
|
||||||
|
|
||||||
return this;
|
this.ai = ai;
|
||||||
}
|
|
||||||
|
|
||||||
public Enemy defaultAI() {
|
return this;
|
||||||
var ai = new WeaponBasedAI(this, runner.getPlayer());
|
}
|
||||||
|
|
||||||
addEventListener(MoveEvent.TYPE, ai::recomputePath);
|
|
||||||
addEventListener(EnemyDiedEvent.TYPE, e -> ai.recomputePath());
|
|
||||||
|
|
||||||
this.ai = ai;
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,79 +1,80 @@
|
|||||||
package com.bartlomiejpluta.demo.entity;
|
package com.bartlomiejpluta.demo.entity;
|
||||||
|
|
||||||
import lombok.*;
|
import com.bartlomiejpluta.base.api.context.ContextHolder;
|
||||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
import com.bartlomiejpluta.base.api.move.Direction;
|
||||||
import com.bartlomiejpluta.base.api.context.*;
|
import com.bartlomiejpluta.base.util.path.CharacterPath;
|
||||||
import com.bartlomiejpluta.base.api.move.*;
|
import com.bartlomiejpluta.base.util.path.PathExecutor;
|
||||||
import com.bartlomiejpluta.base.util.path.*;
|
import lombok.Getter;
|
||||||
|
import lombok.NonNull;
|
||||||
|
|
||||||
public class MapObject extends NamedCharacter {
|
public class MapObject extends NamedCharacter {
|
||||||
private final PathExecutor<MapObject> pathExecutor = new PathExecutor<>(this);
|
private final PathExecutor<MapObject> pathExecutor = new PathExecutor<>(this);
|
||||||
private final DB.model.MapObjectModel template;
|
private final DB.model.MapObjectModel template;
|
||||||
private final Short frame;
|
private final Short frame;
|
||||||
private final String interactSound;
|
private final String interactSound;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
private boolean interacting = false;
|
private boolean interacting = false;
|
||||||
|
|
||||||
public MapObject(@NonNull String id) {
|
public MapObject(@NonNull String id) {
|
||||||
this(DB.dao.map_object.find(id));
|
this(DB.dao.map_object.find(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
public MapObject(@NonNull DB.model.MapObjectModel template) {
|
public MapObject(@NonNull DB.model.MapObjectModel template) {
|
||||||
super(ContextHolder.INSTANCE.getContext().createCharacter(A.charsets.get(template.getCharset()).uid));
|
super(ContextHolder.INSTANCE.getContext().createCharacter(A.charsets.get(template.getCharset()).uid));
|
||||||
this.template = template;
|
this.template = template;
|
||||||
this.frame = template.getFrame();
|
this.frame = template.getFrame();
|
||||||
this.name = template.getName();
|
this.name = template.getName();
|
||||||
this.interactSound = A.sounds.get(template.getInteractSound()).uid;
|
this.interactSound = A.sounds.get(template.getInteractSound()).uid;
|
||||||
|
|
||||||
setBlocking(true);
|
setBlocking(true);
|
||||||
disableAnimation();
|
disableAnimation();
|
||||||
|
|
||||||
if(frame != null) {
|
if (frame != null) {
|
||||||
setAnimationFrame(frame);
|
setAnimationFrame(frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
pathExecutor.setPath(
|
pathExecutor.setPath(
|
||||||
frame != null
|
frame != null
|
||||||
? new CharacterPath<MapObject>()
|
? new CharacterPath<MapObject>()
|
||||||
.run(this::startInteraction)
|
.run(this::startInteraction)
|
||||||
.turn(Direction.LEFT, frame)
|
.turn(Direction.LEFT, frame)
|
||||||
.wait(0.05f)
|
.wait(0.05f)
|
||||||
.turn(Direction.RIGHT, frame)
|
.turn(Direction.RIGHT, frame)
|
||||||
.wait(0.05f)
|
.wait(0.05f)
|
||||||
.turn(Direction.UP, frame)
|
.turn(Direction.UP, frame)
|
||||||
.wait(0.5f)
|
.wait(0.5f)
|
||||||
.turn(Direction.RIGHT, frame)
|
.turn(Direction.RIGHT, frame)
|
||||||
.wait(0.05f)
|
.wait(0.05f)
|
||||||
.turn(Direction.LEFT, frame)
|
.turn(Direction.LEFT, frame)
|
||||||
.wait(0.05f)
|
.wait(0.05f)
|
||||||
.turn(Direction.DOWN, frame)
|
.turn(Direction.DOWN, frame)
|
||||||
.wait(0.5f)
|
.wait(0.5f)
|
||||||
.run(this::finishInteraction)
|
.run(this::finishInteraction)
|
||||||
: new CharacterPath<MapObject>()
|
: new CharacterPath<MapObject>()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void interact(Creature creature) {
|
public void interact(Creature creature) {
|
||||||
interacting = true;
|
interacting = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startInteraction() {
|
private void startInteraction() {
|
||||||
if(interactSound != null) {
|
if (interactSound != null) {
|
||||||
context.playSound(interactSound);
|
context.playSound(interactSound);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void finishInteraction() {
|
private void finishInteraction() {
|
||||||
interacting = false;
|
interacting = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(float dt) {
|
public void update(float dt) {
|
||||||
if(interacting) {
|
if (interacting) {
|
||||||
pathExecutor.execute(getLayer(), dt);
|
pathExecutor.execute(getLayer(), dt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,21 +1,20 @@
|
|||||||
package com.bartlomiejpluta.demo.entity;
|
package com.bartlomiejpluta.demo.entity;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.context.*;
|
|
||||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
|
||||||
import com.bartlomiejpluta.base.lib.character.CharacterDelegate;
|
|
||||||
import com.bartlomiejpluta.base.api.character.Character;
|
import com.bartlomiejpluta.base.api.character.Character;
|
||||||
|
import com.bartlomiejpluta.base.api.context.Context;
|
||||||
|
import com.bartlomiejpluta.base.api.context.ContextHolder;
|
||||||
|
import com.bartlomiejpluta.base.lib.character.CharacterDelegate;
|
||||||
import com.bartlomiejpluta.demo.runner.DemoRunner;
|
import com.bartlomiejpluta.demo.runner.DemoRunner;
|
||||||
|
|
||||||
public abstract class NamedCharacter extends CharacterDelegate {
|
public abstract class NamedCharacter extends CharacterDelegate {
|
||||||
protected final Context context;
|
protected final Context context;
|
||||||
protected final DemoRunner runner;
|
protected final DemoRunner runner;
|
||||||
|
|
||||||
public NamedCharacter(Character character) {
|
public NamedCharacter(Character character) {
|
||||||
super(character);
|
super(character);
|
||||||
this.context = ContextHolder.INSTANCE.getContext();
|
this.context = ContextHolder.INSTANCE.getContext();
|
||||||
this.runner = (DemoRunner) context.getGameRunner();
|
this.runner = (DemoRunner) context.getGameRunner();
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract String getName();
|
public abstract String getName();
|
||||||
}
|
}
|
||||||
@@ -1,36 +1,34 @@
|
|||||||
package com.bartlomiejpluta.demo.entity;
|
package com.bartlomiejpluta.demo.entity;
|
||||||
|
|
||||||
import lombok.*;
|
|
||||||
import org.joml.Vector2i;
|
|
||||||
import com.bartlomiejpluta.base.api.context.Context;
|
|
||||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
|
||||||
import com.bartlomiejpluta.base.api.character.Character;
|
import com.bartlomiejpluta.base.api.character.Character;
|
||||||
|
import lombok.NonNull;
|
||||||
|
import org.joml.Vector2i;
|
||||||
|
|
||||||
public class Player extends Creature {
|
public class Player extends Creature {
|
||||||
|
|
||||||
public Player(@NonNull Character entity) {
|
public Player(@NonNull Character entity) {
|
||||||
super(entity);
|
super(entity);
|
||||||
this.hp = 500;
|
this.hp = 500;
|
||||||
this.maxHp = 500;
|
this.maxHp = 500;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void interact() {
|
public void interact() {
|
||||||
var coords = getCoordinates().add(getFaceDirection().vector, new Vector2i());
|
var coords = getCoordinates().add(getFaceDirection().vector, new Vector2i());
|
||||||
for(var entity : getLayer().getEntities()) {
|
for (var entity : getLayer().getEntities()) {
|
||||||
if(entity.getCoordinates().equals(coords) && entity instanceof MapObject) {
|
if (entity.getCoordinates().equals(coords) && entity instanceof MapObject) {
|
||||||
((MapObject) entity).interact(this);
|
((MapObject) entity).interact(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void die() {
|
public void die() {
|
||||||
super.die();
|
super.die();
|
||||||
runner.returnToStartMenu();
|
runner.returnToStartMenu();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return "Player";
|
return "Player";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,20 +1,20 @@
|
|||||||
package com.bartlomiejpluta.demo.event;
|
package com.bartlomiejpluta.demo.event;
|
||||||
|
|
||||||
import lombok.*;
|
import com.bartlomiejpluta.base.api.event.EventType;
|
||||||
|
import com.bartlomiejpluta.base.lib.event.BaseEvent;
|
||||||
import com.bartlomiejpluta.base.api.event.*;
|
|
||||||
import com.bartlomiejpluta.base.lib.event.*;
|
|
||||||
import com.bartlomiejpluta.demo.entity.Enemy;
|
import com.bartlomiejpluta.demo.entity.Enemy;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class EnemyDiedEvent extends BaseEvent {
|
public class EnemyDiedEvent extends BaseEvent {
|
||||||
public static final EventType<EnemyDiedEvent> TYPE = new EventType<>("ENEMY_DIED_EVENT");
|
public static final EventType<EnemyDiedEvent> TYPE = new EventType<>("ENEMY_DIED_EVENT");
|
||||||
|
|
||||||
private final Enemy enemy;
|
private final Enemy enemy;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EventType<EnemyDiedEvent> getType() {
|
public EventType<EnemyDiedEvent> getType() {
|
||||||
return TYPE;
|
return TYPE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,22 +1,22 @@
|
|||||||
package com.bartlomiejpluta.demo.event;
|
package com.bartlomiejpluta.demo.event;
|
||||||
|
|
||||||
import lombok.*;
|
import com.bartlomiejpluta.base.api.event.EventType;
|
||||||
|
import com.bartlomiejpluta.base.lib.event.BaseEvent;
|
||||||
import com.bartlomiejpluta.base.api.event.*;
|
|
||||||
import com.bartlomiejpluta.base.lib.event.*;
|
|
||||||
import com.bartlomiejpluta.demo.entity.Creature;
|
import com.bartlomiejpluta.demo.entity.Creature;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class HitEvent extends BaseEvent {
|
public class HitEvent extends BaseEvent {
|
||||||
public static final EventType<HitEvent> TYPE = new EventType<>("HIT_EVENT");
|
public static final EventType<HitEvent> TYPE = new EventType<>("HIT_EVENT");
|
||||||
|
|
||||||
private final Creature attacker;
|
private final Creature attacker;
|
||||||
private final Creature target;
|
private final Creature target;
|
||||||
private final int damage;
|
private final int damage;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EventType<HitEvent> getType() {
|
public EventType<HitEvent> getType() {
|
||||||
return TYPE;
|
return TYPE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,65 +1,63 @@
|
|||||||
package com.bartlomiejpluta.demo.gui;
|
package com.bartlomiejpluta.demo.gui;
|
||||||
|
|
||||||
import lombok.*;
|
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.gui.*;
|
|
||||||
import com.bartlomiejpluta.base.lib.gui.*;
|
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.screen.*;
|
|
||||||
import com.bartlomiejpluta.base.api.context.Context;
|
import com.bartlomiejpluta.base.api.context.Context;
|
||||||
import com.bartlomiejpluta.base.api.input.*;
|
import com.bartlomiejpluta.base.api.gui.Color;
|
||||||
|
import com.bartlomiejpluta.base.api.gui.GUI;
|
||||||
|
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||||
|
import com.bartlomiejpluta.base.lib.gui.BaseComponent;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
public class Bar extends BaseComponent {
|
public class Bar extends BaseComponent {
|
||||||
|
|
||||||
@Setter
|
private final Color stroke;
|
||||||
private float value = 1.0f;
|
private final Color fill;
|
||||||
private float actualValue = 1.0f;
|
@Setter
|
||||||
private float speed = 0.05f;
|
private float value = 1.0f;
|
||||||
private final Color stroke;
|
private float actualValue = 1.0f;
|
||||||
private final Color fill;
|
private final float speed = 0.05f;
|
||||||
|
|
||||||
public Bar(Context context, GUI gui) {
|
public Bar(Context context, GUI gui) {
|
||||||
super(context, gui);
|
super(context, gui);
|
||||||
|
|
||||||
this.stroke = gui.createColor();
|
this.stroke = gui.createColor();
|
||||||
this.fill = gui.createColor();
|
this.fill = gui.createColor();
|
||||||
|
|
||||||
stroke.setAlpha(1f);
|
stroke.setAlpha(1f);
|
||||||
fill.setAlpha(1f);
|
fill.setAlpha(1f);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setStrokeColor(Integer hex) {
|
public void setStrokeColor(Integer hex) {
|
||||||
stroke.setRGB(hex);
|
stroke.setRGB(hex);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFillColor(Integer hex) {
|
public void setFillColor(Integer hex) {
|
||||||
fill.setRGB(hex);
|
fill.setRGB(hex);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public float getContentWidth() {
|
public float getContentWidth() {
|
||||||
return width;
|
return width;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public float getContentHeight() {
|
public float getContentHeight() {
|
||||||
return height;
|
return height;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void draw(Screen screen, GUI gui) {
|
public void draw(Screen screen, GUI gui) {
|
||||||
var remainingDistance = value - actualValue;
|
var remainingDistance = value - actualValue;
|
||||||
actualValue += remainingDistance * speed;
|
actualValue += remainingDistance * speed;
|
||||||
|
|
||||||
gui.beginPath();
|
gui.beginPath();
|
||||||
gui.drawRectangle(x, y, Math.max(width * actualValue, 0), height);
|
gui.drawRectangle(x, y, Math.max(width * actualValue, 0), height);
|
||||||
gui.setFillColor(fill);
|
gui.setFillColor(fill);
|
||||||
gui.fill();
|
gui.fill();
|
||||||
gui.closePath();
|
gui.closePath();
|
||||||
gui.beginPath();
|
gui.beginPath();
|
||||||
gui.drawRectangle(x, y, width, height);
|
gui.drawRectangle(x, y, width, height);
|
||||||
gui.setStrokeColor(stroke);
|
gui.setStrokeColor(stroke);
|
||||||
gui.stroke();
|
gui.stroke();
|
||||||
gui.closePath();
|
gui.closePath();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,47 +1,50 @@
|
|||||||
package com.bartlomiejpluta.demo.gui;
|
package com.bartlomiejpluta.demo.gui;
|
||||||
|
|
||||||
import lombok.*;
|
|
||||||
import com.bartlomiejpluta.base.api.context.Context;
|
import com.bartlomiejpluta.base.api.context.Context;
|
||||||
|
import com.bartlomiejpluta.base.api.gui.Color;
|
||||||
|
import com.bartlomiejpluta.base.api.gui.GUI;
|
||||||
|
import com.bartlomiejpluta.base.api.input.Key;
|
||||||
|
import com.bartlomiejpluta.base.api.input.KeyAction;
|
||||||
|
import com.bartlomiejpluta.base.api.input.KeyEvent;
|
||||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||||
import com.bartlomiejpluta.base.api.input.*;
|
import com.bartlomiejpluta.base.lib.gui.Label;
|
||||||
import com.bartlomiejpluta.base.api.gui.*;
|
import lombok.Setter;
|
||||||
import com.bartlomiejpluta.base.lib.gui.*;
|
|
||||||
|
|
||||||
public class Button extends Label {
|
public class Button extends Label {
|
||||||
private Color color;
|
private final Color color;
|
||||||
|
|
||||||
@Setter
|
@Setter
|
||||||
private Runnable action;
|
private Runnable action;
|
||||||
|
|
||||||
public Button(Context context, GUI gui) {
|
public Button(Context context, GUI gui) {
|
||||||
super(context, gui);
|
super(context, gui);
|
||||||
this.color = gui.createColor();
|
this.color = gui.createColor();
|
||||||
this.color.setRGBA(1, 1, 1, 0);
|
this.color.setRGBA(1, 1, 1, 0);
|
||||||
|
|
||||||
setText("");
|
setText("");
|
||||||
setFontSize(17f);
|
setFontSize(17f);
|
||||||
setAlignment(GUI.ALIGN_TOP | GUI.ALIGN_CENTER);
|
setAlignment(GUI.ALIGN_TOP | GUI.ALIGN_CENTER);
|
||||||
setColor(0.4f, 0.7f, 0.0f, 1f);
|
setColor(0.4f, 0.7f, 0.0f, 1f);
|
||||||
setPadding(10f);
|
setPadding(10f);
|
||||||
addEventListener(KeyEvent.TYPE, this::handleKeyEvent);
|
addEventListener(KeyEvent.TYPE, this::handleKeyEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleKeyEvent(KeyEvent event) {
|
private void handleKeyEvent(KeyEvent event) {
|
||||||
if(event.getKey() == Key.KEY_ENTER && event.getAction() == KeyAction.PRESS && action != null) {
|
if (event.getKey() == Key.KEY_ENTER && event.getAction() == KeyAction.PRESS && action != null) {
|
||||||
event.consume();
|
event.consume();
|
||||||
action.run();
|
action.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void draw(Screen screen, GUI gui) {
|
public void draw(Screen screen, GUI gui) {
|
||||||
color.setAlpha(focused ? 0.7f : 0f);
|
color.setAlpha(focused ? 0.7f : 0f);
|
||||||
|
|
||||||
gui.beginPath();
|
gui.beginPath();
|
||||||
gui.drawRectangle(x, y, getWidth(), getHeight());
|
gui.drawRectangle(x, y, getWidth(), getHeight());
|
||||||
gui.setFillColor(color);
|
gui.setFillColor(color);
|
||||||
gui.fill();
|
gui.fill();
|
||||||
|
|
||||||
super.draw(screen, gui);
|
super.draw(screen, gui);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,35 +1,37 @@
|
|||||||
package com.bartlomiejpluta.demo.gui;
|
package com.bartlomiejpluta.demo.gui;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.context.Context;
|
import com.bartlomiejpluta.base.api.context.Context;
|
||||||
|
import com.bartlomiejpluta.base.api.gui.Color;
|
||||||
|
import com.bartlomiejpluta.base.api.gui.GUI;
|
||||||
|
import com.bartlomiejpluta.base.api.gui.Paint;
|
||||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||||
import com.bartlomiejpluta.base.api.gui.*;
|
import com.bartlomiejpluta.base.lib.gui.BaseWindow;
|
||||||
import com.bartlomiejpluta.base.lib.gui.*;
|
|
||||||
|
|
||||||
public abstract class DecoratedWindow extends BaseWindow {
|
public abstract class DecoratedWindow extends BaseWindow {
|
||||||
private Paint paint;
|
private final Paint paint;
|
||||||
private Color inner;
|
private final Color inner;
|
||||||
private Color outer;
|
private final Color outer;
|
||||||
|
|
||||||
public DecoratedWindow(Context context, GUI gui) {
|
public DecoratedWindow(Context context, GUI gui) {
|
||||||
super(context, gui);
|
super(context, gui);
|
||||||
|
|
||||||
this.inner = gui.createColor();
|
this.inner = gui.createColor();
|
||||||
this.outer = gui.createColor();
|
this.outer = gui.createColor();
|
||||||
this.paint = gui.createPaint();
|
this.paint = gui.createPaint();
|
||||||
|
|
||||||
inner.setRGBA(0.1f, 0.1f, 0.1f, 1f);
|
inner.setRGBA(0.1f, 0.1f, 0.1f, 1f);
|
||||||
outer.setRGBA(0.2f, 0.2f, 0.2f, 1f);
|
outer.setRGBA(0.2f, 0.2f, 0.2f, 1f);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void draw(Screen screen, GUI gui) {
|
public void draw(Screen screen, GUI gui) {
|
||||||
gui.beginPath();
|
gui.beginPath();
|
||||||
gui.drawRectangle(x, y, getWidth(), getHeight());
|
gui.drawRectangle(x, y, getWidth(), getHeight());
|
||||||
gui.setFillPaint(paint);
|
gui.setFillPaint(paint);
|
||||||
gui.boxGradient(x, y, getWidth(), getHeight(), 10f, 100f, inner, outer, paint);
|
gui.boxGradient(x, y, getWidth(), getHeight(), 10f, 100f, inner, outer, paint);
|
||||||
gui.fill();
|
gui.fill();
|
||||||
gui.stroke();
|
gui.stroke();
|
||||||
|
|
||||||
super.draw(screen, gui);
|
super.draw(screen, gui);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,14 +1,12 @@
|
|||||||
package com.bartlomiejpluta.demo.gui;
|
package com.bartlomiejpluta.demo.gui;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.context.Context;
|
import com.bartlomiejpluta.base.api.context.Context;
|
||||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
import com.bartlomiejpluta.base.api.gui.GUI;
|
||||||
import com.bartlomiejpluta.base.api.gui.*;
|
|
||||||
import com.bartlomiejpluta.base.lib.gui.*;
|
|
||||||
|
|
||||||
|
|
||||||
public class EquipmentWindow extends DecoratedWindow {
|
public class EquipmentWindow extends DecoratedWindow {
|
||||||
|
|
||||||
public EquipmentWindow(Context context, GUI gui) {
|
public EquipmentWindow(Context context, GUI gui) {
|
||||||
super(context, gui);
|
super(context, gui);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,32 +1,32 @@
|
|||||||
package com.bartlomiejpluta.demo.gui;
|
package com.bartlomiejpluta.demo.gui;
|
||||||
|
|
||||||
import lombok.*;
|
|
||||||
import com.bartlomiejpluta.base.api.context.Context;
|
import com.bartlomiejpluta.base.api.context.Context;
|
||||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
import com.bartlomiejpluta.base.api.gui.GUI;
|
||||||
import com.bartlomiejpluta.base.api.gui.*;
|
import com.bartlomiejpluta.base.api.gui.Inflatable;
|
||||||
import com.bartlomiejpluta.base.lib.gui.*;
|
import com.bartlomiejpluta.base.api.gui.Ref;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
|
||||||
public class GameMenuWindow extends DecoratedWindow implements Inflatable {
|
public class GameMenuWindow extends DecoratedWindow implements Inflatable {
|
||||||
|
|
||||||
@Ref("resume_game")
|
@Ref("resume_game")
|
||||||
@Getter
|
@Getter
|
||||||
private Button resumeGameBtn;
|
private Button resumeGameBtn;
|
||||||
|
|
||||||
@Ref("start_menu")
|
@Ref("start_menu")
|
||||||
@Getter
|
@Getter
|
||||||
private Button startMenuBtn;
|
private Button startMenuBtn;
|
||||||
|
|
||||||
@Ref("exit")
|
@Ref("exit")
|
||||||
@Getter
|
@Getter
|
||||||
private Button exitBtn;
|
private Button exitBtn;
|
||||||
|
|
||||||
public GameMenuWindow(Context context, GUI gui) {
|
public GameMenuWindow(Context context, GUI gui) {
|
||||||
super(context, gui);
|
super(context, gui);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInflate() {
|
public void onInflate() {
|
||||||
resumeGameBtn.focus();
|
resumeGameBtn.focus();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
package com.bartlomiejpluta.demo.gui;
|
package com.bartlomiejpluta.demo.gui;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.context.Context;
|
import com.bartlomiejpluta.base.api.context.Context;
|
||||||
import com.bartlomiejpluta.base.api.gui.*;
|
import com.bartlomiejpluta.base.api.gui.GUI;
|
||||||
import com.bartlomiejpluta.base.api.input.*;
|
import com.bartlomiejpluta.base.api.gui.Ref;
|
||||||
import com.bartlomiejpluta.base.api.screen.*;
|
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||||
import com.bartlomiejpluta.base.lib.gui.*;
|
import com.bartlomiejpluta.base.lib.gui.BorderLayout;
|
||||||
|
import com.bartlomiejpluta.base.lib.gui.IconView;
|
||||||
|
import com.bartlomiejpluta.base.lib.gui.Label;
|
||||||
import com.bartlomiejpluta.demo.entity.Player;
|
import com.bartlomiejpluta.demo.entity.Player;
|
||||||
import com.bartlomiejpluta.demo.event.EnemyDiedEvent;
|
import com.bartlomiejpluta.demo.event.EnemyDiedEvent;
|
||||||
import com.bartlomiejpluta.demo.event.HitEvent;
|
import com.bartlomiejpluta.demo.event.HitEvent;
|
||||||
@@ -17,82 +19,82 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class HUD extends BorderLayout {
|
public class HUD extends BorderLayout {
|
||||||
private static final int MAX_LOG_SIZE = 10;
|
private static final int MAX_LOG_SIZE = 10;
|
||||||
private static final float LOG_VISIBILITY_DURATION = 8000f;
|
private static final float LOG_VISIBILITY_DURATION = 8000f;
|
||||||
private static final float LOG_VISIBILITY_FADING_OUT = 1000f;
|
private static final float LOG_VISIBILITY_FADING_OUT = 1000f;
|
||||||
private final DemoRunner runner;
|
private final DemoRunner runner;
|
||||||
private final Player player;
|
private final Player player;
|
||||||
private final Runtime runtime;
|
private final Runtime runtime;
|
||||||
private LimitedQueue<String> logger = new LimitedQueue<>(MAX_LOG_SIZE);
|
private final LimitedQueue<String> logger = new LimitedQueue<>(MAX_LOG_SIZE);
|
||||||
|
|
||||||
private float logVisibilityDuration = 0f;
|
private float logVisibilityDuration = 0f;
|
||||||
|
|
||||||
private Weapon currentWeapon;
|
private Weapon currentWeapon;
|
||||||
|
|
||||||
@Ref("hp")
|
@Ref("hp")
|
||||||
private Bar hp;
|
private Bar hp;
|
||||||
|
|
||||||
@Ref("debug")
|
@Ref("debug")
|
||||||
private Label debugLbl;
|
private Label debugLbl;
|
||||||
|
|
||||||
@Ref("log")
|
@Ref("log")
|
||||||
private Label logLbl;
|
private Label logLbl;
|
||||||
|
|
||||||
@Ref("weapon")
|
@Ref("weapon")
|
||||||
private IconView weapon;
|
private IconView weapon;
|
||||||
|
|
||||||
public HUD(Context context, GUI gui) {
|
public HUD(Context context, GUI gui) {
|
||||||
super(context, gui);
|
super(context, gui);
|
||||||
this.runner = (DemoRunner) context.getGameRunner();
|
this.runner = (DemoRunner) context.getGameRunner();
|
||||||
this.player = runner.getPlayer();
|
this.player = runner.getPlayer();
|
||||||
this.runtime = Runtime.getRuntime();
|
this.runtime = Runtime.getRuntime();
|
||||||
context.addEventListener(HitEvent.TYPE, this::logHitEvent);
|
context.addEventListener(HitEvent.TYPE, this::logHitEvent);
|
||||||
context.addEventListener(EnemyDiedEvent.TYPE, this::logEnemyDiedEvent);
|
context.addEventListener(EnemyDiedEvent.TYPE, this::logEnemyDiedEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void logHitEvent(HitEvent event) {
|
private void logHitEvent(HitEvent event) {
|
||||||
log(String.format("%s hits %s with damage = %d", event.getAttacker().getName(), event.getTarget().getName(), event.getDamage()));
|
log(String.format("%s hits %s with damage = %d", event.getAttacker().getName(), event.getTarget().getName(), event.getDamage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void log(String message) {
|
private void log(String message) {
|
||||||
logger.add(message);
|
logger.add(message);
|
||||||
log.info(message);
|
log.info(message);
|
||||||
logLbl.setText(logger.stream().collect(Collectors.joining("\n")));
|
logLbl.setText(logger.stream().collect(Collectors.joining("\n")));
|
||||||
logVisibilityDuration = LOG_VISIBILITY_DURATION;
|
logVisibilityDuration = LOG_VISIBILITY_DURATION;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void logEnemyDiedEvent(EnemyDiedEvent event) {
|
private void logEnemyDiedEvent(EnemyDiedEvent event) {
|
||||||
log(String.format("%s has died with HP = %d", event.getEnemy().getName(), event.getEnemy().getHp()));
|
log(String.format("%s has died with HP = %d", event.getEnemy().getName(), event.getEnemy().getHp()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(float dt) {
|
public void update(float dt) {
|
||||||
super.update(dt);
|
super.update(dt);
|
||||||
|
|
||||||
hp.setValue((float) player.getHp() / (float) player.getMaxHp());
|
hp.setValue((float) player.getHp() / (float) player.getMaxHp());
|
||||||
|
|
||||||
if (logVisibilityDuration > 0) {
|
if (logVisibilityDuration > 0) {
|
||||||
logVisibilityDuration -= dt * 1000;
|
logVisibilityDuration -= dt * 1000;
|
||||||
} else {
|
} else {
|
||||||
logVisibilityDuration = 0;
|
logVisibilityDuration = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (player.getWeapon() != null && player.getWeapon() != currentWeapon) {
|
if (player.getWeapon() != null && player.getWeapon() != currentWeapon) {
|
||||||
weapon.setIcon(player.getWeapon().getIcon());
|
weapon.setIcon(player.getWeapon().getIcon());
|
||||||
this.currentWeapon = player.getWeapon();
|
this.currentWeapon = player.getWeapon();
|
||||||
} else if (player.getWeapon() == null) {
|
} else if (player.getWeapon() == null) {
|
||||||
this.currentWeapon = null;
|
this.currentWeapon = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void draw(Screen screen, GUI gui) {
|
public void draw(Screen screen, GUI gui) {
|
||||||
var coords = player.getCoordinates();
|
var coords = player.getCoordinates();
|
||||||
var pos = player.getPosition();
|
var pos = player.getPosition();
|
||||||
debugLbl.setText(String.format("FPS: %.2f\n" + "Mem: %.2f / %.2f [MB]\n" + "Coords: %d : %d\n" + "Pos: %.2f : %.2f\n" + "Entities: %d", runner.instantFPS(), runtime.totalMemory() / 1024f / 1024f, runtime.maxMemory() / 1024f / 1024f, coords.x(), coords.y(), pos.x(), pos.y(), player.getLayer().getEntities().size() - 1));
|
debugLbl.setText(String.format("FPS: %.2f\n" + "Mem: %.2f / %.2f [MB]\n" + "Coords: %d : %d\n" + "Pos: %.2f : %.2f\n" + "Entities: %d", runner.instantFPS(), runtime.totalMemory() / 1024f / 1024f, runtime.maxMemory() / 1024f / 1024f, coords.x(), coords.y(), pos.x(), pos.y(), player.getLayer().getEntities().size() - 1));
|
||||||
|
|
||||||
logLbl.setAlpha(Math.min(1f, logVisibilityDuration / LOG_VISIBILITY_FADING_OUT));
|
logLbl.setAlpha(Math.min(1f, logVisibilityDuration / LOG_VISIBILITY_FADING_OUT));
|
||||||
|
|
||||||
super.draw(screen, gui);
|
super.draw(screen, gui);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,27 +1,27 @@
|
|||||||
package com.bartlomiejpluta.demo.gui;
|
package com.bartlomiejpluta.demo.gui;
|
||||||
|
|
||||||
import lombok.*;
|
|
||||||
import com.bartlomiejpluta.base.api.context.Context;
|
import com.bartlomiejpluta.base.api.context.Context;
|
||||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
import com.bartlomiejpluta.base.api.gui.GUI;
|
||||||
import com.bartlomiejpluta.base.api.gui.*;
|
import com.bartlomiejpluta.base.api.gui.Inflatable;
|
||||||
import com.bartlomiejpluta.base.lib.gui.*;
|
import com.bartlomiejpluta.base.api.gui.Ref;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
public class StartMenuWindow extends DecoratedWindow implements Inflatable {
|
public class StartMenuWindow extends DecoratedWindow implements Inflatable {
|
||||||
|
|
||||||
@Ref("new_game")
|
@Ref("new_game")
|
||||||
@Getter
|
@Getter
|
||||||
private Button newGameBtn;
|
private Button newGameBtn;
|
||||||
|
|
||||||
@Ref("exit")
|
@Ref("exit")
|
||||||
@Getter
|
@Getter
|
||||||
private Button exitBtn;
|
private Button exitBtn;
|
||||||
|
|
||||||
public StartMenuWindow(Context context, GUI gui) {
|
public StartMenuWindow(Context context, GUI gui) {
|
||||||
super(context, gui);
|
super(context, gui);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInflate() {
|
public void onInflate() {
|
||||||
newGameBtn.focus();
|
newGameBtn.focus();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,138 +1,134 @@
|
|||||||
package com.bartlomiejpluta.demo.map;
|
package com.bartlomiejpluta.demo.map;
|
||||||
|
|
||||||
import lombok.*;
|
import com.bartlomiejpluta.base.api.camera.Camera;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.map.handler.MapHandler;
|
|
||||||
import com.bartlomiejpluta.base.api.map.model.GameMap;
|
|
||||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
|
||||||
import com.bartlomiejpluta.base.api.context.Context;
|
import com.bartlomiejpluta.base.api.context.Context;
|
||||||
|
import com.bartlomiejpluta.base.api.icon.Icon;
|
||||||
|
import com.bartlomiejpluta.base.api.input.Input;
|
||||||
|
import com.bartlomiejpluta.base.api.input.Key;
|
||||||
|
import com.bartlomiejpluta.base.api.map.handler.MapHandler;
|
||||||
|
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||||
|
import com.bartlomiejpluta.base.api.map.model.GameMap;
|
||||||
import com.bartlomiejpluta.base.api.move.Direction;
|
import com.bartlomiejpluta.base.api.move.Direction;
|
||||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||||
import com.bartlomiejpluta.base.api.camera.Camera;
|
import com.bartlomiejpluta.base.lib.camera.CameraController;
|
||||||
import com.bartlomiejpluta.base.api.input.*;
|
import com.bartlomiejpluta.base.lib.camera.FollowingCameraController;
|
||||||
import com.bartlomiejpluta.base.api.icon.Icon;
|
import com.bartlomiejpluta.base.util.world.CharacterSpawner;
|
||||||
|
import com.bartlomiejpluta.base.util.world.Warp;
|
||||||
import com.bartlomiejpluta.base.lib.camera.*;
|
import com.bartlomiejpluta.demo.entity.Enemy;
|
||||||
|
import com.bartlomiejpluta.demo.entity.MapObject;
|
||||||
import com.bartlomiejpluta.base.util.world.*;
|
import com.bartlomiejpluta.demo.entity.Player;
|
||||||
|
import com.bartlomiejpluta.demo.event.EnemyDiedEvent;
|
||||||
import com.bartlomiejpluta.demo.runner.DemoRunner;
|
import com.bartlomiejpluta.demo.runner.DemoRunner;
|
||||||
import com.bartlomiejpluta.demo.entity.*;
|
import lombok.NonNull;
|
||||||
import com.bartlomiejpluta.demo.event.*;
|
|
||||||
|
|
||||||
import com.bartlomiejpluta.demo.util.*;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.function.*;
|
|
||||||
|
|
||||||
public abstract class BaseMapHandler implements MapHandler {
|
public abstract class BaseMapHandler implements MapHandler {
|
||||||
protected Screen screen;
|
protected Screen screen;
|
||||||
protected Context context;
|
protected Context context;
|
||||||
protected DemoRunner runner;
|
protected DemoRunner runner;
|
||||||
protected Camera camera;
|
protected Camera camera;
|
||||||
protected GameMap map;
|
protected GameMap map;
|
||||||
protected Player player;
|
protected Player player;
|
||||||
protected ObjectLayer mainLayer;
|
protected ObjectLayer mainLayer;
|
||||||
protected CameraController cameraController;
|
protected CameraController cameraController;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Context context, GameMap map) {
|
public void onCreate(Context context, GameMap map) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.screen = context.getScreen();
|
this.screen = context.getScreen();
|
||||||
this.runner = (DemoRunner) context.getGameRunner();
|
this.runner = (DemoRunner) context.getGameRunner();
|
||||||
this.camera = context.getCamera();
|
this.camera = context.getCamera();
|
||||||
this.map = map;
|
this.map = map;
|
||||||
this.player = runner.getPlayer();
|
this.player = runner.getPlayer();
|
||||||
this.cameraController = FollowingCameraController
|
this.cameraController = FollowingCameraController
|
||||||
.on(screen, camera, map)
|
.on(screen, camera, map)
|
||||||
.follow(player.getPosition());
|
.follow(player.getPosition());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void input(Input input) {
|
public void input(Input input) {
|
||||||
if(context.isPaused()) {
|
if (context.isPaused()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(runner.openedWindows() > 0) {
|
if (runner.openedWindows() > 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(input.isKeyPressed(Key.KEY_SPACE)) {
|
if (input.isKeyPressed(Key.KEY_SPACE)) {
|
||||||
player.attack();
|
player.attack();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(input.isKeyPressed(Key.KEY_ENTER)) {
|
if (input.isKeyPressed(Key.KEY_ENTER)) {
|
||||||
player.interact();
|
player.interact();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(input.isKeyPressed(Key.KEY_LEFT_CONTROL)) {
|
if (input.isKeyPressed(Key.KEY_LEFT_CONTROL)) {
|
||||||
if(input.isKeyPressed(Key.KEY_DOWN)) {
|
if (input.isKeyPressed(Key.KEY_DOWN)) {
|
||||||
player.setFaceDirection(Direction.DOWN);
|
player.setFaceDirection(Direction.DOWN);
|
||||||
} else if(input.isKeyPressed(Key.KEY_UP)) {
|
} else if (input.isKeyPressed(Key.KEY_UP)) {
|
||||||
player.setFaceDirection(Direction.UP);
|
player.setFaceDirection(Direction.UP);
|
||||||
} else if(input.isKeyPressed(Key.KEY_LEFT)) {
|
} else if (input.isKeyPressed(Key.KEY_LEFT)) {
|
||||||
player.setFaceDirection(Direction.LEFT);
|
player.setFaceDirection(Direction.LEFT);
|
||||||
} else if(input.isKeyPressed(Key.KEY_RIGHT)) {
|
} else if (input.isKeyPressed(Key.KEY_RIGHT)) {
|
||||||
player.setFaceDirection(Direction.RIGHT);
|
player.setFaceDirection(Direction.RIGHT);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if(input.isKeyPressed(Key.KEY_DOWN)) {
|
if (input.isKeyPressed(Key.KEY_DOWN)) {
|
||||||
player.getLayer().pushMovement(player.prepareMovement(Direction.DOWN));
|
player.getLayer().pushMovement(player.prepareMovement(Direction.DOWN));
|
||||||
} else if(input.isKeyPressed(Key.KEY_UP)) {
|
} else if (input.isKeyPressed(Key.KEY_UP)) {
|
||||||
player.getLayer().pushMovement(player.prepareMovement(Direction.UP));
|
player.getLayer().pushMovement(player.prepareMovement(Direction.UP));
|
||||||
} else if(input.isKeyPressed(Key.KEY_LEFT)) {
|
} else if (input.isKeyPressed(Key.KEY_LEFT)) {
|
||||||
player.getLayer().pushMovement(player.prepareMovement(Direction.LEFT));
|
player.getLayer().pushMovement(player.prepareMovement(Direction.LEFT));
|
||||||
} else if(input.isKeyPressed(Key.KEY_RIGHT)) {
|
} else if (input.isKeyPressed(Key.KEY_RIGHT)) {
|
||||||
player.getLayer().pushMovement(player.prepareMovement(Direction.RIGHT));
|
player.getLayer().pushMovement(player.prepareMovement(Direction.RIGHT));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(Context context, GameMap map, float dt) {
|
public void update(Context context, GameMap map, float dt) {
|
||||||
cameraController.update();
|
cameraController.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Enemy enemy(@NonNull String id) {
|
public Enemy enemy(@NonNull String id) {
|
||||||
return new Enemy(id);
|
return new Enemy(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Enemy enemy(ObjectLayer layer, int x, int y, @NonNull String id) {
|
public Enemy enemy(ObjectLayer layer, int x, int y, @NonNull String id) {
|
||||||
var enemy = new Enemy(id);
|
var enemy = new Enemy(id);
|
||||||
enemy.setCoordinates(x, y);
|
enemy.setCoordinates(x, y);
|
||||||
layer.addEntity(enemy);
|
layer.addEntity(enemy);
|
||||||
return enemy;
|
return enemy;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MapObject object(ObjectLayer layer, int x, int y, @NonNull String id) {
|
public MapObject object(ObjectLayer layer, int x, int y, @NonNull String id) {
|
||||||
var object = new MapObject(id);
|
var object = new MapObject(id);
|
||||||
object.setCoordinates(x, y);
|
object.setCoordinates(x, y);
|
||||||
layer.addEntity(object);
|
layer.addEntity(object);
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CharacterSpawner spawner(ObjectLayer layer, int x, int y) {
|
public CharacterSpawner spawner(ObjectLayer layer, int x, int y) {
|
||||||
var spawner = new CharacterSpawner().trackEntities(EnemyDiedEvent.TYPE);
|
var spawner = new CharacterSpawner().trackEntities(EnemyDiedEvent.TYPE);
|
||||||
spawner.setCoordinates(x, y);
|
spawner.setCoordinates(x, y);
|
||||||
layer.addEntity(spawner);
|
layer.addEntity(spawner);
|
||||||
return spawner;
|
return spawner;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Icon icon(ObjectLayer layer, int x, int y, String iconSetUid, int row, int column) {
|
public Icon icon(ObjectLayer layer, int x, int y, String iconSetUid, int row, int column) {
|
||||||
var icon = context.createIcon(iconSetUid, row, column);
|
var icon = context.createIcon(iconSetUid, row, column);
|
||||||
icon.setScale(1f);
|
icon.setScale(1f);
|
||||||
icon.setZIndex(-1);
|
icon.setZIndex(-1);
|
||||||
icon.setCoordinates(x, y);
|
icon.setCoordinates(x, y);
|
||||||
layer.addEntity(icon);
|
layer.addEntity(icon);
|
||||||
return icon;
|
return icon;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Warp warp(ObjectLayer layer, int x, int y, String targetMap, String targetLayer, int targetX, int targetY) {
|
public Warp warp(ObjectLayer layer, int x, int y, String targetMap, String targetLayer, int targetX, int targetY) {
|
||||||
var warp = new Warp(A.maps.get(targetMap).uid, A.maps.getLayer(targetMap, targetLayer), targetX, targetY);
|
var warp = new Warp(A.maps.get(targetMap).uid, A.maps.getLayer(targetMap, targetLayer), targetX, targetY);
|
||||||
warp.setEntity(player);
|
warp.setEntity(player);
|
||||||
warp.setCoordinates(x, y);
|
warp.setCoordinates(x, y);
|
||||||
layer.addEntity(warp);
|
layer.addEntity(warp);
|
||||||
return warp;
|
return warp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,5 @@
|
|||||||
package com.bartlomiejpluta.demo.map;
|
package com.bartlomiejpluta.demo.map;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.context.Context;
|
|
||||||
import com.bartlomiejpluta.base.api.input.Input;
|
|
||||||
import com.bartlomiejpluta.base.api.map.model.GameMap;
|
|
||||||
import com.bartlomiejpluta.base.api.map.handler.MapHandler;
|
|
||||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
|
||||||
|
|
||||||
public class ForrestHandler extends BaseMapHandler {
|
public class ForrestHandler extends BaseMapHandler {
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,19 +1,17 @@
|
|||||||
package com.bartlomiejpluta.demo.map;
|
package com.bartlomiejpluta.demo.map;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.context.Context;
|
import com.bartlomiejpluta.base.api.context.Context;
|
||||||
import com.bartlomiejpluta.base.api.input.Input;
|
|
||||||
import com.bartlomiejpluta.base.api.map.model.GameMap;
|
import com.bartlomiejpluta.base.api.map.model.GameMap;
|
||||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
|
||||||
|
|
||||||
public class ForrestTempleHandler extends BaseMapHandler {
|
public class ForrestTempleHandler extends BaseMapHandler {
|
||||||
public static final String UID = "f845355e-b9ad-4884-a217-dd3a4c18a3fa";
|
public static final String UID = "f845355e-b9ad-4884-a217-dd3a4c18a3fa";
|
||||||
public static final A.maps.GameMapAsset_Layers_ForrestTemple LAYERS = A.maps.forrest_temple.layers;
|
public static final A.maps.GameMapAsset_Layers_ForrestTemple LAYERS = A.maps.forrest_temple.layers;
|
||||||
|
|
||||||
public static final int MAIN_LAYER = 4;
|
public static final int MAIN_LAYER = 4;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Context context, GameMap map) {
|
public void onCreate(Context context, GameMap map) {
|
||||||
super.onCreate(context, map);
|
super.onCreate(context, map);
|
||||||
this.mainLayer = map.getObjectLayer(MAIN_LAYER);
|
this.mainLayer = map.getObjectLayer(MAIN_LAYER);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,105 +1,108 @@
|
|||||||
package com.bartlomiejpluta.demo.menu;
|
package com.bartlomiejpluta.demo.menu;
|
||||||
|
|
||||||
import lombok.*;
|
|
||||||
|
|
||||||
import java.util.function.*;
|
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.context.Context;
|
import com.bartlomiejpluta.base.api.context.Context;
|
||||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
import com.bartlomiejpluta.base.api.gui.DisplayMode;
|
||||||
import com.bartlomiejpluta.base.api.input.*;
|
import com.bartlomiejpluta.base.api.gui.GUI;
|
||||||
import com.bartlomiejpluta.base.api.gui.*;
|
import com.bartlomiejpluta.base.api.gui.UpdateMode;
|
||||||
import com.bartlomiejpluta.base.lib.gui.*;
|
import com.bartlomiejpluta.base.api.gui.WindowManager;
|
||||||
|
import com.bartlomiejpluta.base.api.input.Key;
|
||||||
|
import com.bartlomiejpluta.base.api.input.KeyAction;
|
||||||
|
import com.bartlomiejpluta.base.api.input.KeyEvent;
|
||||||
|
import com.bartlomiejpluta.demo.gui.EquipmentWindow;
|
||||||
|
import com.bartlomiejpluta.demo.gui.GameMenuWindow;
|
||||||
|
import com.bartlomiejpluta.demo.gui.StartMenuWindow;
|
||||||
import com.bartlomiejpluta.demo.runner.DemoRunner;
|
import com.bartlomiejpluta.demo.runner.DemoRunner;
|
||||||
import com.bartlomiejpluta.demo.gui.*;
|
import lombok.NonNull;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class MenuManager {
|
public class MenuManager {
|
||||||
private final DemoRunner runner;
|
private final DemoRunner runner;
|
||||||
private final Context context;
|
private final Context context;
|
||||||
private final GUI gui;
|
private final GUI gui;
|
||||||
private final WindowManager manager;
|
private final WindowManager manager;
|
||||||
|
|
||||||
private final StartMenuWindow startMenu;
|
private final StartMenuWindow startMenu;
|
||||||
private final GameMenuWindow gameMenu;
|
private final GameMenuWindow gameMenu;
|
||||||
private final EquipmentWindow equipment;
|
private final EquipmentWindow equipment;
|
||||||
|
|
||||||
private final Consumer<KeyEvent> gameMenuHandler = this::handleGameMenuKeyEvent;
|
private final Consumer<KeyEvent> gameMenuHandler = this::handleGameMenuKeyEvent;
|
||||||
|
|
||||||
public MenuManager(@NonNull DemoRunner runner, @NonNull Context context) {
|
public MenuManager(@NonNull DemoRunner runner, @NonNull Context context) {
|
||||||
this.runner = runner;
|
this.runner = runner;
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.gui = context.newGUI();
|
this.gui = context.newGUI();
|
||||||
this.manager = new WindowManager(context, DisplayMode.DISPLAY_TOP, UpdateMode.UPDATE_TOP);
|
this.manager = new WindowManager(context, DisplayMode.DISPLAY_TOP, UpdateMode.UPDATE_TOP);
|
||||||
|
|
||||||
this.gui.setRoot(this.manager);
|
this.gui.setRoot(this.manager);
|
||||||
|
|
||||||
this.startMenu = (StartMenuWindow) gui.inflateWindow(A.widgets.start_menu.uid);
|
this.startMenu = (StartMenuWindow) gui.inflateWindow(A.widgets.start_menu.uid);
|
||||||
this.startMenu.getNewGameBtn().setAction(runner::newGame);
|
this.startMenu.getNewGameBtn().setAction(runner::newGame);
|
||||||
this.startMenu.getExitBtn().setAction(runner::exit);
|
this.startMenu.getExitBtn().setAction(runner::exit);
|
||||||
|
|
||||||
this.gameMenu = (GameMenuWindow) gui.inflateWindow(A.widgets.game_menu.uid);
|
this.gameMenu = (GameMenuWindow) gui.inflateWindow(A.widgets.game_menu.uid);
|
||||||
this.gameMenu.getResumeGameBtn().setAction(this::resumeGame);
|
this.gameMenu.getResumeGameBtn().setAction(this::resumeGame);
|
||||||
this.gameMenu.getStartMenuBtn().setAction(runner::returnToStartMenu);
|
this.gameMenu.getStartMenuBtn().setAction(runner::returnToStartMenu);
|
||||||
this.gameMenu.getExitBtn().setAction(runner::exit);
|
this.gameMenu.getExitBtn().setAction(runner::exit);
|
||||||
|
|
||||||
this.equipment = (EquipmentWindow) gui.inflateWindow(A.widgets.equipment.uid);
|
this.equipment = (EquipmentWindow) gui.inflateWindow(A.widgets.equipment.uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleGameMenuKeyEvent(KeyEvent event) {
|
private void handleGameMenuKeyEvent(KeyEvent event) {
|
||||||
if (event.getKey() == Key.KEY_E && event.getAction() == KeyAction.PRESS) {
|
if (event.getKey() == Key.KEY_E && event.getAction() == KeyAction.PRESS) {
|
||||||
if(manager.isEmpty()) {
|
if (manager.isEmpty()) {
|
||||||
manager.open(equipment);
|
manager.open(equipment);
|
||||||
} else if (manager.top() == equipment) {
|
} else if (manager.top() == equipment) {
|
||||||
manager.close();
|
manager.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
event.consume();
|
event.consume();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.getKey() == Key.KEY_ESCAPE && event.getAction() == KeyAction.PRESS) {
|
if (event.getKey() == Key.KEY_ESCAPE && event.getAction() == KeyAction.PRESS) {
|
||||||
if(manager.size() > 0) {
|
if (manager.size() > 0) {
|
||||||
manager.close();
|
manager.close();
|
||||||
} else {
|
} else {
|
||||||
manager.open(gameMenu);
|
manager.open(gameMenu);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(manager.size() > 0) {
|
if (manager.size() > 0) {
|
||||||
context.pause();
|
context.pause();
|
||||||
} else {
|
} else {
|
||||||
context.resume();
|
context.resume();
|
||||||
}
|
}
|
||||||
|
|
||||||
event.consume();
|
event.consume();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int openedWindows() {
|
public int openedWindows() {
|
||||||
return manager.size();
|
return manager.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showStartMenu() {
|
public void showStartMenu() {
|
||||||
manager.closeAll();
|
manager.closeAll();
|
||||||
manager.open(startMenu);
|
manager.open(startMenu);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void enableGameMenu() {
|
public void enableGameMenu() {
|
||||||
manager.closeAll();
|
manager.closeAll();
|
||||||
|
|
||||||
context.getInput().addKeyEventHandler(gameMenuHandler);
|
context.getInput().addKeyEventHandler(gameMenuHandler);
|
||||||
manager.setDisplayMode(DisplayMode.DISPLAY_STACK);
|
manager.setDisplayMode(DisplayMode.DISPLAY_STACK);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disableGameMenu() {
|
public void disableGameMenu() {
|
||||||
context.getInput().removeKeyEventHandler(gameMenuHandler);
|
context.getInput().removeKeyEventHandler(gameMenuHandler);
|
||||||
manager.setDisplayMode(DisplayMode.DISPLAY_TOP);
|
manager.setDisplayMode(DisplayMode.DISPLAY_TOP);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void closeAll() {
|
public void closeAll() {
|
||||||
manager.closeAll();
|
manager.closeAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void resumeGame() {
|
private void resumeGame() {
|
||||||
manager.closeAll();
|
manager.closeAll();
|
||||||
context.resume();
|
context.resume();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,112 +8,110 @@ import com.bartlomiejpluta.base.util.profiler.FPSProfiler;
|
|||||||
import com.bartlomiejpluta.demo.entity.Player;
|
import com.bartlomiejpluta.demo.entity.Player;
|
||||||
import com.bartlomiejpluta.demo.menu.MenuManager;
|
import com.bartlomiejpluta.demo.menu.MenuManager;
|
||||||
import com.bartlomiejpluta.demo.world.weapon.RangedWeapon;
|
import com.bartlomiejpluta.demo.world.weapon.RangedWeapon;
|
||||||
import lombok.*;
|
import lombok.Getter;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
|
||||||
public class DemoRunner implements GameRunner {
|
public class DemoRunner implements GameRunner {
|
||||||
private static final Logger log = LoggerFactory.getLogger(DemoRunner.class);
|
private static final Logger log = LoggerFactory.getLogger(DemoRunner.class);
|
||||||
private Screen screen;
|
private final FPSProfiler fpsProfiler = FPSProfiler.create(20);
|
||||||
private Context context;
|
private Screen screen;
|
||||||
private MenuManager menu;
|
private Context context;
|
||||||
private GUI hud;
|
private MenuManager menu;
|
||||||
|
private GUI hud;
|
||||||
|
@Getter
|
||||||
|
private Player player;
|
||||||
|
|
||||||
@Getter
|
@Override
|
||||||
private Player player;
|
public void init(Context context) {
|
||||||
|
this.context = context;
|
||||||
|
this.screen = context.getScreen();
|
||||||
|
|
||||||
private final FPSProfiler fpsProfiler = FPSProfiler.create(20);
|
configureScreen();
|
||||||
|
configureCamera();
|
||||||
|
initPlayer();
|
||||||
|
initHUD();
|
||||||
|
initMenu();
|
||||||
|
|
||||||
@Override
|
menu.showStartMenu();
|
||||||
public void init(Context context) {
|
|
||||||
this.context = context;
|
|
||||||
this.screen = context.getScreen();
|
|
||||||
|
|
||||||
configureScreen();
|
screen.show();
|
||||||
configureCamera();
|
}
|
||||||
initPlayer();
|
|
||||||
initHUD();
|
|
||||||
initMenu();
|
|
||||||
|
|
||||||
menu.showStartMenu();
|
private void configureScreen() {
|
||||||
|
var resolution = screen.getCurrentResolution();
|
||||||
|
screen.setSize(1800, 1000);
|
||||||
|
screen.setPosition((resolution.x() - 1800) / 2, (resolution.y() - 1000) / 2);
|
||||||
|
}
|
||||||
|
|
||||||
screen.show();
|
private void configureCamera() {
|
||||||
}
|
context.getCamera().setScale(2f);
|
||||||
|
}
|
||||||
|
|
||||||
private void configureScreen() {
|
private void initMenu() {
|
||||||
var resolution = screen.getCurrentResolution();
|
this.menu = new MenuManager(this, context);
|
||||||
screen.setSize(1800, 1000);
|
}
|
||||||
screen.setPosition((resolution.x() - 1800) / 2, (resolution.y() - 1000) / 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void configureCamera() {
|
public int openedWindows() {
|
||||||
context.getCamera().setScale(2f);
|
return this.menu.openedWindows();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initMenu() {
|
private void initHUD() {
|
||||||
this.menu = new MenuManager(this, context);
|
hud = context.newGUI();
|
||||||
}
|
hud.hide();
|
||||||
|
var hudComponent = hud.inflateComponent(A.widgets.hud.uid);
|
||||||
|
hud.setRoot(hudComponent);
|
||||||
|
}
|
||||||
|
|
||||||
public int openedWindows() {
|
private void initPlayer() {
|
||||||
return this.menu.openedWindows();
|
this.player = new Player(context.createCharacter(A.charsets.luna.uid));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initHUD() {
|
private void resetPlayer() {
|
||||||
hud = context.newGUI();
|
this.player.changeCharacterSet(A.charsets.luna.uid);
|
||||||
hud.hide();
|
this.player.setScale(1f);
|
||||||
var hudComponent = hud.inflateComponent(A.widgets.hud.uid);
|
this.player.setSpeed(4f);
|
||||||
hud.setRoot(hudComponent);
|
this.player.setAnimationSpeed(1f);
|
||||||
}
|
this.player.setBlocking(true);
|
||||||
|
this.player.setWeapon(new RangedWeapon("wooden_bow"));
|
||||||
|
}
|
||||||
|
|
||||||
private void initPlayer() {
|
public void newGame() {
|
||||||
this.player = new Player(context.createCharacter(A.charsets.luna.uid));
|
menu.closeAll();
|
||||||
}
|
menu.enableGameMenu();
|
||||||
|
resetPlayer();
|
||||||
|
context.openMap(A.maps.forrest.uid);
|
||||||
|
context.getMap().getObjectLayer(A.maps.forrest.layers.main).addEntity(this.player);
|
||||||
|
player.setCoordinates(5, 36);
|
||||||
|
context.resume();
|
||||||
|
hud.show();
|
||||||
|
}
|
||||||
|
|
||||||
private void resetPlayer() {
|
public void returnToStartMenu() {
|
||||||
this.player.changeCharacterSet(A.charsets.luna.uid);
|
menu.closeAll();
|
||||||
this.player.setScale(1f);
|
hud.hide();
|
||||||
this.player.setSpeed(4f);
|
context.pause();
|
||||||
this.player.setAnimationSpeed(1f);
|
context.closeMap();
|
||||||
this.player.setBlocking(true);
|
menu.disableGameMenu();
|
||||||
this.player.setWeapon(new RangedWeapon("wooden_bow"));
|
menu.showStartMenu();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void newGame() {
|
public void exit() {
|
||||||
menu.closeAll();
|
context.close();
|
||||||
menu.enableGameMenu();
|
}
|
||||||
resetPlayer();
|
|
||||||
context.openMap(A.maps.forrest.uid);
|
|
||||||
context.getMap().getObjectLayer(A.maps.forrest.layers.main).addEntity(this.player);
|
|
||||||
player.setCoordinates(5, 36);
|
|
||||||
context.resume();
|
|
||||||
hud.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void returnToStartMenu() {
|
public double instantFPS() {
|
||||||
menu.closeAll();
|
return fpsProfiler.getInstantFPS();
|
||||||
hud.hide();
|
}
|
||||||
context.pause();
|
|
||||||
context.closeMap();
|
|
||||||
menu.disableGameMenu();
|
|
||||||
menu.showStartMenu();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void exit() {
|
@Override
|
||||||
context.close();
|
public void update(float dt) {
|
||||||
}
|
fpsProfiler.update(dt);
|
||||||
|
}
|
||||||
|
|
||||||
public double instantFPS() {
|
@Override
|
||||||
return fpsProfiler.getInstantFPS();
|
public void dispose() {
|
||||||
}
|
// Do something after game loop is end
|
||||||
|
}
|
||||||
@Override
|
|
||||||
public void update(float dt) {
|
|
||||||
fpsProfiler.update(dt);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void dispose() {
|
|
||||||
// Do something after game loop is end
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,20 +1,21 @@
|
|||||||
package com.bartlomiejpluta.demo.util;
|
package com.bartlomiejpluta.demo.util;
|
||||||
|
|
||||||
import lombok.*;
|
import lombok.AllArgsConstructor;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class LimitedQueue<E> extends LinkedList<E> {
|
public class LimitedQueue<E> extends LinkedList<E> {
|
||||||
private int limit;
|
private int limit;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean add(E o) {
|
public boolean add(E o) {
|
||||||
super.add(o);
|
super.add(o);
|
||||||
|
|
||||||
while (size() > limit) {
|
while (size() > limit) {
|
||||||
super.remove();
|
super.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,62 +1,62 @@
|
|||||||
package com.bartlomiejpluta.demo.world.weapon;
|
package com.bartlomiejpluta.demo.world.weapon;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.context.*;
|
import com.bartlomiejpluta.base.api.context.Context;
|
||||||
|
import com.bartlomiejpluta.base.api.context.ContextHolder;
|
||||||
import com.bartlomiejpluta.base.api.icon.Icon;
|
import com.bartlomiejpluta.base.api.icon.Icon;
|
||||||
import com.bartlomiejpluta.base.lib.animation.*;
|
import com.bartlomiejpluta.base.lib.animation.AnimationRunner;
|
||||||
|
import com.bartlomiejpluta.base.lib.animation.RandomAnimationsRunner;
|
||||||
import com.bartlomiejpluta.base.util.random.DiceRoller;
|
import com.bartlomiejpluta.base.util.random.DiceRoller;
|
||||||
import com.bartlomiejpluta.demo.entity.Creature;
|
import com.bartlomiejpluta.demo.entity.Creature;
|
||||||
import com.bartlomiejpluta.demo.event.HitEvent;
|
import com.bartlomiejpluta.demo.event.HitEvent;
|
||||||
import lombok.*;
|
import lombok.Getter;
|
||||||
|
import lombok.NonNull;
|
||||||
import org.joml.Vector2i;
|
import org.joml.Vector2i;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class MeleeWeapon implements Weapon {
|
public class MeleeWeapon implements Weapon {
|
||||||
private final Random random = new Random();
|
private final Random random = new Random();
|
||||||
private final Context context;
|
private final Context context;
|
||||||
private final DiceRoller roller;
|
private final DiceRoller roller;
|
||||||
private final AnimationRunner animation;
|
private final AnimationRunner animation;
|
||||||
private final String sound;
|
private final String sound;
|
||||||
|
@Getter
|
||||||
|
private final Icon icon;
|
||||||
|
@Getter
|
||||||
|
private final String name;
|
||||||
|
@Getter
|
||||||
|
private final int cooldown;
|
||||||
|
|
||||||
@Getter
|
public MeleeWeapon(@NonNull String id) {
|
||||||
private String name;
|
this(DB.dao.melee_weapon.find(id));
|
||||||
|
}
|
||||||
|
|
||||||
@Getter
|
public MeleeWeapon(@NonNull DB.model.MeleeWeaponModel template) {
|
||||||
private final Icon icon;
|
this.context = ContextHolder.INSTANCE.getContext();
|
||||||
|
this.name = template.getName();
|
||||||
|
this.roller = DiceRoller.of(template.getDamage());
|
||||||
|
this.cooldown = template.getCooldown();
|
||||||
|
this.animation = new RandomAnimationsRunner(2).nRange(0, 2f).nScale(0.2f, 0.15f).uAnimationSpeed(0.5f, 1f).nRotation(0, 10).offset(0, -10).uDelay(250, 500).with(A.animations.get(template.getAnimation()).uid);
|
||||||
|
this.sound = A.sounds.get(template.getSound()).uid;
|
||||||
|
var icons = template.getIcon().split(",");
|
||||||
|
this.icon = context.createIcon(A.iconsets.get(icons[0]).uid, Integer.valueOf(icons[1]), Integer.valueOf(icons[2]));
|
||||||
|
}
|
||||||
|
|
||||||
@Getter
|
@Override
|
||||||
private int cooldown;
|
public boolean attack(Creature attacker) {
|
||||||
|
var facingNeighbour = attacker.getCoordinates().add(attacker.getFaceDirection().vector, new Vector2i());
|
||||||
|
for (var entity : attacker.getLayer().getEntities()) {
|
||||||
|
if (entity.getCoordinates().equals(facingNeighbour) && entity.isBlocking() && entity instanceof Creature) {
|
||||||
|
var character = (Creature) entity;
|
||||||
|
var damage = roller.roll();
|
||||||
|
character.hit(attacker, damage);
|
||||||
|
animation.run(context, character.getLayer(), character);
|
||||||
|
context.playSound(sound);
|
||||||
|
context.fireEvent(new HitEvent(attacker, character, damage));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public MeleeWeapon(@NonNull String id) {
|
return false;
|
||||||
this(DB.dao.melee_weapon.find(id));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public MeleeWeapon(@NonNull DB.model.MeleeWeaponModel template) {
|
|
||||||
this.context = ContextHolder.INSTANCE.getContext();
|
|
||||||
this.name = template.getName();
|
|
||||||
this.roller = DiceRoller.of(template.getDamage());
|
|
||||||
this.cooldown = template.getCooldown();
|
|
||||||
this.animation = new RandomAnimationsRunner(2).nRange(0, 2f).nScale(0.2f, 0.15f).uAnimationSpeed(0.5f, 1f).nRotation(0, 10).offset(0, -10).uDelay(250, 500).with(A.animations.get(template.getAnimation()).uid);
|
|
||||||
this.sound = A.sounds.get(template.getSound()).uid;
|
|
||||||
var icons = template.getIcon().split(",");
|
|
||||||
this.icon = context.createIcon(A.iconsets.get(icons[0]).uid, Integer.valueOf(icons[1]), Integer.valueOf(icons[2]));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean attack(Creature attacker) {
|
|
||||||
var facingNeighbour = attacker.getCoordinates().add(attacker.getFaceDirection().vector, new Vector2i());
|
|
||||||
for (var entity : attacker.getLayer().getEntities()) {
|
|
||||||
if (entity.getCoordinates().equals(facingNeighbour) && entity.isBlocking() && entity instanceof Creature) {
|
|
||||||
var character = (Creature) entity;
|
|
||||||
var damage = roller.roll();
|
|
||||||
character.hit(attacker, damage);
|
|
||||||
animation.run(context, character.getLayer(), character);
|
|
||||||
context.playSound(sound);
|
|
||||||
context.fireEvent(new HitEvent(attacker, character, damage));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,81 +1,82 @@
|
|||||||
package com.bartlomiejpluta.demo.world.weapon;
|
package com.bartlomiejpluta.demo.world.weapon;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.animation.Animation;
|
import com.bartlomiejpluta.base.api.animation.Animation;
|
||||||
import com.bartlomiejpluta.base.api.context.*;
|
import com.bartlomiejpluta.base.api.context.Context;
|
||||||
|
import com.bartlomiejpluta.base.api.context.ContextHolder;
|
||||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||||
import com.bartlomiejpluta.base.api.icon.Icon;
|
import com.bartlomiejpluta.base.api.icon.Icon;
|
||||||
import com.bartlomiejpluta.base.api.move.*;
|
import com.bartlomiejpluta.base.api.move.Movable;
|
||||||
import com.bartlomiejpluta.base.lib.animation.*;
|
import com.bartlomiejpluta.base.lib.animation.AnimationRunner;
|
||||||
|
import com.bartlomiejpluta.base.lib.animation.BulletAnimationRunner;
|
||||||
|
import com.bartlomiejpluta.base.lib.animation.SimpleAnimationRunner;
|
||||||
import com.bartlomiejpluta.base.util.random.DiceRoller;
|
import com.bartlomiejpluta.base.util.random.DiceRoller;
|
||||||
import com.bartlomiejpluta.demo.entity.Creature;
|
import com.bartlomiejpluta.demo.entity.Creature;
|
||||||
import com.bartlomiejpluta.demo.event.HitEvent;
|
import com.bartlomiejpluta.demo.event.HitEvent;
|
||||||
import lombok.*;
|
import lombok.Getter;
|
||||||
|
import lombok.NonNull;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class RangedWeapon implements Weapon {
|
public class RangedWeapon implements Weapon {
|
||||||
private final Random random = new Random();
|
private final Random random = new Random();
|
||||||
private final Context context;
|
private final Context context;
|
||||||
private final DiceRoller dmgRoller;
|
private final DiceRoller dmgRoller;
|
||||||
private final DiceRoller rangeRoller;
|
private final DiceRoller rangeRoller;
|
||||||
private final BulletAnimationRunner animation;
|
private final BulletAnimationRunner animation;
|
||||||
private final String sound;
|
private final String sound;
|
||||||
private final AnimationRunner punchAnimation;
|
private final AnimationRunner punchAnimation;
|
||||||
private final String punchSound;
|
private final String punchSound;
|
||||||
private final AnimationRunner missAnimation;
|
private final AnimationRunner missAnimation;
|
||||||
private final String missSound;
|
private final String missSound;
|
||||||
|
@Getter
|
||||||
|
private final Icon icon;
|
||||||
|
@Getter
|
||||||
|
private final String name;
|
||||||
|
@Getter
|
||||||
|
private final int cooldown;
|
||||||
|
|
||||||
@Getter
|
public RangedWeapon(@NonNull String id) {
|
||||||
private String name;
|
this(DB.dao.ranged_weapon.find(id));
|
||||||
|
}
|
||||||
|
|
||||||
@Getter
|
public RangedWeapon(@NonNull DB.model.RangedWeaponModel template) {
|
||||||
private final Icon icon;
|
this.context = ContextHolder.INSTANCE.getContext();
|
||||||
|
this.name = template.getName();
|
||||||
|
this.dmgRoller = DiceRoller.of(template.getDamage());
|
||||||
|
this.rangeRoller = DiceRoller.of(template.getRange());
|
||||||
|
this.cooldown = template.getCooldown();
|
||||||
|
this.animation = new BulletAnimationRunner(A.animations.get(template.getAnimation()).uid).infinite().offset(0, -15).onHit(this::onHit).onMiss(this::onMiss).speed(7f).animationSpeed(4f).scale(0.6f);
|
||||||
|
this.sound = A.sounds.get(template.getSound()).uid;
|
||||||
|
this.punchAnimation = new SimpleAnimationRunner(A.animations.get(template.getPunchAnimation()).uid);
|
||||||
|
this.punchSound = A.sounds.get(template.getPunchSound()).uid;
|
||||||
|
this.missAnimation = new SimpleAnimationRunner(A.animations.get(template.getMissAnimation()).uid).scale(0.4f);
|
||||||
|
this.missSound = A.sounds.get(template.getMissSound()).uid;
|
||||||
|
var icons = template.getIcon().split(",");
|
||||||
|
this.icon = context.createIcon(A.iconsets.get(icons[0]).uid, Integer.valueOf(icons[1]), Integer.valueOf(icons[2]));
|
||||||
|
}
|
||||||
|
|
||||||
@Getter
|
private void onHit(Movable attacker, Entity target) {
|
||||||
private int cooldown;
|
if (target.isBlocking() && target instanceof Creature) {
|
||||||
|
var namedAttacker = (Creature) attacker;
|
||||||
|
var character = (Creature) target;
|
||||||
|
var damage = dmgRoller.roll();
|
||||||
|
character.hit(namedAttacker, damage);
|
||||||
|
punchAnimation.run(context, character.getLayer(), character);
|
||||||
|
context.playSound(punchSound);
|
||||||
|
context.fireEvent(new HitEvent(namedAttacker, character, damage));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public RangedWeapon(@NonNull String id) {
|
private void onMiss(Movable attacker, Animation animation) {
|
||||||
this(DB.dao.ranged_weapon.find(id));
|
missAnimation.run(context, ((Creature) attacker).getLayer(), animation.getPosition());
|
||||||
}
|
context.playSound(missSound);
|
||||||
|
}
|
||||||
|
|
||||||
public RangedWeapon(@NonNull DB.model.RangedWeaponModel template) {
|
@Override
|
||||||
this.context = ContextHolder.INSTANCE.getContext();
|
public boolean attack(Creature attacker) {
|
||||||
this.name = template.getName();
|
var direction = attacker.getFaceDirection();
|
||||||
this.dmgRoller = DiceRoller.of(template.getDamage());
|
context.playSound(sound);
|
||||||
this.rangeRoller = DiceRoller.of(template.getRange());
|
animation.range(rangeRoller.roll()).direction(direction).rotation(direction.xAngle - 180).run(context, attacker.getLayer(), attacker);
|
||||||
this.cooldown = template.getCooldown();
|
return true;
|
||||||
this.animation = new BulletAnimationRunner(A.animations.get(template.getAnimation()).uid).infinite().offset(0, -15).onHit(this::onHit).onMiss(this::onMiss).speed(7f).animationSpeed(4f).scale(0.6f);
|
}
|
||||||
this.sound = A.sounds.get(template.getSound()).uid;
|
|
||||||
this.punchAnimation = new SimpleAnimationRunner(A.animations.get(template.getPunchAnimation()).uid);
|
|
||||||
this.punchSound = A.sounds.get(template.getPunchSound()).uid;
|
|
||||||
this.missAnimation = new SimpleAnimationRunner(A.animations.get(template.getMissAnimation()).uid).scale(0.4f);
|
|
||||||
this.missSound = A.sounds.get(template.getMissSound()).uid;
|
|
||||||
var icons = template.getIcon().split(",");
|
|
||||||
this.icon = context.createIcon(A.iconsets.get(icons[0]).uid, Integer.valueOf(icons[1]), Integer.valueOf(icons[2]));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onHit(Movable attacker, Entity target) {
|
|
||||||
if (target.isBlocking() && target instanceof Creature) {
|
|
||||||
var namedAttacker = (Creature) attacker;
|
|
||||||
var character = (Creature) target;
|
|
||||||
var damage = dmgRoller.roll();
|
|
||||||
character.hit(namedAttacker, damage);
|
|
||||||
punchAnimation.run(context, character.getLayer(), character);
|
|
||||||
context.playSound(punchSound);
|
|
||||||
context.fireEvent(new HitEvent(namedAttacker, character, damage));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onMiss(Movable attacker, Animation animation) {
|
|
||||||
missAnimation.run(context, ((Creature) attacker).getLayer(), animation.getPosition());
|
|
||||||
context.playSound(missSound);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean attack(Creature attacker) {
|
|
||||||
var direction = attacker.getFaceDirection();
|
|
||||||
context.playSound(sound);
|
|
||||||
animation.range(rangeRoller.roll()).direction(direction).rotation(direction.xAngle - 180).run(context, attacker.getLayer(), attacker);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -4,11 +4,11 @@ import com.bartlomiejpluta.base.api.icon.Icon;
|
|||||||
import com.bartlomiejpluta.demo.entity.Creature;
|
import com.bartlomiejpluta.demo.entity.Creature;
|
||||||
|
|
||||||
public interface Weapon {
|
public interface Weapon {
|
||||||
String getName();
|
String getName();
|
||||||
|
|
||||||
Icon getIcon();
|
Icon getIcon();
|
||||||
|
|
||||||
int getCooldown();
|
int getCooldown();
|
||||||
|
|
||||||
boolean attack(Creature attacker);
|
boolean attack(Creature attacker);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user