Apply BASE API improvements
This commit is contained in:
@@ -18,9 +18,7 @@ public class Chest extends MapObject {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void interact(Creature creature) {
|
||||
super.interact(creature);
|
||||
|
||||
protected void interact() {
|
||||
runner.openChestWindow(this);
|
||||
}
|
||||
|
||||
|
||||
36
src/main/java/com/bartlomiejpluta/demo/entity/Door.java
Normal file
36
src/main/java/com/bartlomiejpluta/demo/entity/Door.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package com.bartlomiejpluta.demo.entity;
|
||||
|
||||
import A.maps;
|
||||
import com.bartlomiejpluta.base.api.context.ContextHolder;
|
||||
import com.bartlomiejpluta.demo.runner.DemoRunner;
|
||||
import lombok.NonNull;
|
||||
|
||||
public class Door extends MapObject {
|
||||
private final String mapUid;
|
||||
private final int targetX;
|
||||
private final int targetY;
|
||||
private final int layerId;
|
||||
private final Player player;
|
||||
|
||||
public Door(@NonNull String mapName, @NonNull String layerName, int targetX, int targetY, @NonNull String id) {
|
||||
super(id);
|
||||
this.mapUid = maps.get(mapName).uid;
|
||||
this.targetX = targetX;
|
||||
this.targetY = targetY;
|
||||
this.layerId = maps.getLayer(mapName, layerName);
|
||||
player = ContextHolder.INSTANCE.getContext().getGlobal("player", Player.class);
|
||||
setPositionOffset(0, 16);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void interact() {
|
||||
context.openMap(mapUid);
|
||||
context.getMap().getObjectLayer(layerId).addEntity(player);
|
||||
player.setCoordinates(targetX, targetY);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldGoFurther(MapObject object) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -157,7 +157,7 @@ public class Enemy extends Creature implements NPC {
|
||||
}
|
||||
|
||||
public Enemy defaultAI() {
|
||||
var ai = new WeaponBasedAI(this, runner.getPlayer());
|
||||
var ai = new WeaponBasedAI(this, context.getGlobal("player", Player.class));
|
||||
|
||||
addEventListener(MoveEvent.TYPE, ai::recomputePath);
|
||||
addEventListener(EnemyDiedEvent.TYPE, e -> ai.recomputePath());
|
||||
|
||||
@@ -45,8 +45,10 @@ public abstract class MapObject extends NamedCharacter {
|
||||
.turn(Direction.RIGHT, frame)
|
||||
.wait(0.05f)
|
||||
.turn(Direction.UP, frame)
|
||||
.wait(0.5f)
|
||||
.wait(0.25f)
|
||||
.run(this::interact)
|
||||
.suspend(this::shouldGoFurther)
|
||||
.wait(0.25f)
|
||||
.turn(Direction.RIGHT, frame)
|
||||
.wait(0.05f)
|
||||
.turn(Direction.LEFT, frame)
|
||||
@@ -62,10 +64,12 @@ public abstract class MapObject extends NamedCharacter {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void interact(Creature creature) {
|
||||
public void triggerInteraction() {
|
||||
interacting = true;
|
||||
}
|
||||
|
||||
protected abstract void interact();
|
||||
|
||||
protected void startInteraction() {
|
||||
if (interactSound != null) {
|
||||
context.playSound(interactSound);
|
||||
|
||||
@@ -7,8 +7,6 @@ import com.bartlomiejpluta.demo.world.weapon.Weapon;
|
||||
import lombok.NonNull;
|
||||
import org.joml.Vector2i;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Player extends Creature {
|
||||
private final Item[] equipment = new Item[4 * 4];
|
||||
private int interactionCooldown = 0;
|
||||
@@ -31,7 +29,7 @@ public class Player extends Creature {
|
||||
|
||||
// Use some map object which player is looking at
|
||||
if (entity.getCoordinates().equals(coords) && entity instanceof MapObject object) {
|
||||
object.interact(this);
|
||||
object.triggerInteraction();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,6 @@ import static java.lang.String.format;
|
||||
|
||||
|
||||
public class EquipmentWindow extends DecoratedWindow {
|
||||
private final DemoRunner runner;
|
||||
private final Player player;
|
||||
|
||||
private final Window eqItemMenuWindow;
|
||||
@@ -45,8 +44,7 @@ public class EquipmentWindow extends DecoratedWindow {
|
||||
|
||||
public EquipmentWindow(Context context, GUI gui, Map<String, Component> refs) {
|
||||
super(context, gui, refs);
|
||||
this.runner = (DemoRunner) context.getGameRunner();
|
||||
this.player = runner.getPlayer();
|
||||
this.player = context.getGlobal("player", Player.class);
|
||||
this.eqItemMenuWindow = gui.inflateWindow(A.widgets.eq_item_menu.uid);
|
||||
this.eqItemMenu = eqItemMenuWindow.reference("menu", VOptionChoice.class);
|
||||
this.useBtn = eqItemMenuWindow.reference("use", Button.class);
|
||||
|
||||
@@ -8,10 +8,10 @@ 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.base.util.profiler.FPSProfiler;
|
||||
import com.bartlomiejpluta.demo.entity.Player;
|
||||
import com.bartlomiejpluta.demo.event.EnemyDiedEvent;
|
||||
import com.bartlomiejpluta.demo.event.HitEvent;
|
||||
import com.bartlomiejpluta.demo.runner.DemoRunner;
|
||||
import com.bartlomiejpluta.demo.util.LimitedQueue;
|
||||
import com.bartlomiejpluta.demo.world.weapon.Weapon;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -24,11 +24,12 @@ 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 final LimitedQueue<String> logger = new LimitedQueue<>(MAX_LOG_SIZE);
|
||||
|
||||
private final FPSProfiler fpsProfiler;
|
||||
|
||||
private float logVisibilityDuration = 0f;
|
||||
|
||||
private Weapon currentWeapon;
|
||||
@@ -47,8 +48,8 @@ public class HUD extends BorderLayout {
|
||||
|
||||
public HUD(Context context, GUI gui, Map<String, Component> refs) {
|
||||
super(context, gui, refs);
|
||||
this.runner = (DemoRunner) context.getGameRunner();
|
||||
this.player = runner.getPlayer();
|
||||
this.player = context.getGlobal("player", Player.class);
|
||||
this.fpsProfiler = context.getGlobal("fps-profiler", FPSProfiler.class);
|
||||
this.runtime = Runtime.getRuntime();
|
||||
context.addEventListener(HitEvent.TYPE, this::logHitEvent);
|
||||
context.addEventListener(EnemyDiedEvent.TYPE, this::logEnemyDiedEvent);
|
||||
@@ -89,7 +90,7 @@ public class HUD extends BorderLayout {
|
||||
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));
|
||||
debugLbl.setText(String.format("FPS: %.2f\n" + "Mem: %.2f / %.2f [MB]\n" + "Coords: %d : %d\n" + "Pos: %.2f : %.2f\n" + "Entities: %d", fpsProfiler.getInstantFPS(), 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));
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ public class LootWindow extends DecoratedWindow implements Inflatable {
|
||||
|
||||
public LootWindow(Context context, GUI gui, Map<String, Component> refs) {
|
||||
super(context, gui, refs);
|
||||
this.player = ((DemoRunner) context.getGameRunner()).getPlayer();
|
||||
this.player = context.getGlobal("player", Player.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -15,6 +15,7 @@ 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.Chest;
|
||||
import com.bartlomiejpluta.demo.entity.Door;
|
||||
import com.bartlomiejpluta.demo.entity.Enemy;
|
||||
import com.bartlomiejpluta.demo.entity.Player;
|
||||
import com.bartlomiejpluta.demo.event.EnemyDiedEvent;
|
||||
@@ -37,7 +38,7 @@ public abstract class BaseMapHandler implements MapHandler {
|
||||
this.runner = (DemoRunner) context.getGameRunner();
|
||||
this.camera = context.getCamera();
|
||||
this.map = map;
|
||||
this.player = runner.getPlayer();
|
||||
this.player = context.getGlobal("player", Player.class);
|
||||
this.cameraController = FollowingCameraController
|
||||
.on(screen, camera, map)
|
||||
.follow(player.getPosition());
|
||||
@@ -107,6 +108,13 @@ public abstract class BaseMapHandler implements MapHandler {
|
||||
return chest;
|
||||
}
|
||||
|
||||
public Door door(ObjectLayer layer, int x, int y, @NonNull String mapName, @NonNull String layerName, int targetX, int targetY, @NonNull String id) {
|
||||
var door = new Door(mapName, layerName, targetX, targetY, id);
|
||||
door.setCoordinates(x, y);
|
||||
layer.addEntity(door);
|
||||
return door;
|
||||
}
|
||||
|
||||
public CharacterSpawner spawner(ObjectLayer layer, int x, int y) {
|
||||
var spawner = new CharacterSpawner().trackEntities(EnemyDiedEvent.TYPE);
|
||||
spawner.setCoordinates(x, y);
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.bartlomiejpluta.demo.map;
|
||||
|
||||
public class HomeHandler extends BaseMapHandler {
|
||||
|
||||
}
|
||||
@@ -9,8 +9,6 @@ import com.bartlomiejpluta.demo.entity.Chest;
|
||||
import com.bartlomiejpluta.demo.entity.Enemy;
|
||||
import com.bartlomiejpluta.demo.entity.Player;
|
||||
import com.bartlomiejpluta.demo.menu.MenuManager;
|
||||
import com.bartlomiejpluta.demo.world.weapon.RangedWeapon;
|
||||
import lombok.Getter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -22,7 +20,6 @@ public class DemoRunner implements GameRunner {
|
||||
private Context context;
|
||||
private MenuManager menu;
|
||||
private GUI hud;
|
||||
@Getter
|
||||
private Player player;
|
||||
|
||||
@Override
|
||||
@@ -30,6 +27,8 @@ public class DemoRunner implements GameRunner {
|
||||
this.context = context;
|
||||
this.screen = context.getScreen();
|
||||
|
||||
context.putGlobal("fps-profiler", fpsProfiler);
|
||||
|
||||
configureScreen();
|
||||
configureCamera();
|
||||
initPlayer();
|
||||
@@ -68,6 +67,7 @@ public class DemoRunner implements GameRunner {
|
||||
|
||||
private void initPlayer() {
|
||||
this.player = new Player(context.createCharacter(A.charsets.luna.uid));
|
||||
context.putGlobal("player", player);
|
||||
}
|
||||
|
||||
private void resetPlayer() {
|
||||
@@ -82,9 +82,9 @@ public class DemoRunner implements GameRunner {
|
||||
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.openMap(A.maps.home.uid);
|
||||
// context.getMap().getObjectLayer(A.maps.home.layers.main).addEntity(this.player);
|
||||
player.setCoordinates(17, 9);
|
||||
context.resume();
|
||||
hud.show();
|
||||
}
|
||||
@@ -110,10 +110,6 @@ public class DemoRunner implements GameRunner {
|
||||
context.close();
|
||||
}
|
||||
|
||||
public double instantFPS() {
|
||||
return fpsProfiler.getInstantFPS();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
fpsProfiler.update(dt);
|
||||
|
||||
Reference in New Issue
Block a user