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