Replace Context's globals with DemoRunner field references

This commit is contained in:
2022-08-31 13:15:33 +02:00
parent bf19c7bcf4
commit 9bf75a8458
11 changed files with 47 additions and 38 deletions

Binary file not shown.

View File

@@ -14,12 +14,12 @@ public class Chest extends MapObject {
@Override
protected void interact() {
guiManager.openChestWindow(this);
runner.getGuiManager().openChestWindow(this);
}
@Override
protected boolean shouldGoFurther(MapObject object) {
return guiManager.openedWindows() == 0;
return runner.getGuiManager().openedWindows() == 0;
}
public Chest addItem(Item item) {

View File

@@ -1,7 +1,6 @@
package com.bartlomiejpluta.demo.entity;
import A.maps;
import com.bartlomiejpluta.base.api.context.ContextHolder;
import com.bartlomiejpluta.demo.runner.DemoRunner;
import lombok.NonNull;
@@ -18,7 +17,7 @@ public class Door extends MapObject {
this.targetX = targetX;
this.targetY = targetY;
this.layerId = maps.getLayer(mapName, layerName);
player = ContextHolder.INSTANCE.getContext().getGlobal("player", Player.class);
player = DemoRunner.instance().getPlayer();
setPositionOffset(0, 16);
}

View File

@@ -13,6 +13,7 @@ import com.bartlomiejpluta.base.lib.db.Relop;
import com.bartlomiejpluta.base.util.random.DiceRoller;
import com.bartlomiejpluta.demo.ai.*;
import com.bartlomiejpluta.demo.event.EnemyDiedEvent;
import com.bartlomiejpluta.demo.runner.DemoRunner;
import com.bartlomiejpluta.demo.world.item.Item;
import com.bartlomiejpluta.demo.world.junk.Junk;
import com.bartlomiejpluta.demo.world.weapon.Ammunition;
@@ -162,7 +163,7 @@ public class Enemy extends Creature implements NPC {
}
public Enemy defaultAI() {
var ai = new WeaponBasedAI(this, context.getGlobal("player", Player.class));
var ai = new WeaponBasedAI(this, DemoRunner.instance().getPlayer());
addEventListener(MoveEvent.TYPE, ai::recomputePath);
addEventListener(EnemyDiedEvent.TYPE, e -> ai.recomputePath());

View File

@@ -4,19 +4,16 @@ 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.menu.GuiManager;
import com.bartlomiejpluta.demo.runner.DemoRunner;
public abstract class NamedCharacter extends CharacterDelegate {
protected final Context context;
protected final DemoRunner runner;
protected final GuiManager guiManager;
public NamedCharacter(Character character) {
super(character);
this.context = ContextHolder.INSTANCE.getContext();
this.runner = (DemoRunner) context.getGameRunner();
this.guiManager = context.getGlobal("gui", GuiManager.class);
this.runner = DemoRunner.instance();
}
public abstract String getName();

View File

@@ -47,7 +47,7 @@ public class Player extends Creature {
// Search the enemy corpse
if (entity instanceof Enemy enemy && !enemy.isAlive()) {
guiManager.openLootWindow(enemy);
runner.getGuiManager().openLootWindow(enemy);
interactionCooldown = INTERACTION_COOLDOWN;
return;
}
@@ -57,7 +57,7 @@ public class Player extends Creature {
public boolean pushItemToEquipment(@NonNull Item item) {
if (item instanceof Ammunition ammo) {
if (ammo.getId().equals(getAmmunition().getId())) {
if (getAmmunition() != null && ammo.getId().equals(getAmmunition().getId())) {
getAmmunition().increase(ammo.getCount());
return true;
}

View File

@@ -10,6 +10,7 @@ import com.bartlomiejpluta.base.lib.gui.Label;
import com.bartlomiejpluta.base.lib.gui.VGridOptionChoice;
import com.bartlomiejpluta.base.lib.gui.VOptionChoice;
import com.bartlomiejpluta.demo.entity.Player;
import com.bartlomiejpluta.demo.runner.DemoRunner;
import com.bartlomiejpluta.demo.world.item.Item;
import com.bartlomiejpluta.demo.world.item.Useable;
import com.bartlomiejpluta.demo.world.potion.Medicament;
@@ -55,7 +56,7 @@ public class EquipmentWindow extends DecoratedWindow {
public EquipmentWindow(Context context, GUI gui, Map<String, Component> refs) {
super(context, gui, refs);
this.player = context.getGlobal("player", Player.class);
this.player = DemoRunner.instance().getPlayer();
this.popupMenuWindow = gui.inflateWindow(A.widgets.eq_item_menu.uid);
this.popupMenu = popupMenuWindow.reference("menu", VOptionChoice.class);
this.useBtn = popupMenuWindow.reference("use", Button.class);

View File

@@ -12,6 +12,7 @@ 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;
@@ -48,8 +49,8 @@ public class HUD extends BorderLayout {
public HUD(Context context, GUI gui, Map<String, Component> refs) {
super(context, gui, refs);
this.player = context.getGlobal("player", Player.class);
this.fpsProfiler = context.getGlobal("fps-profiler", FPSProfiler.class);
this.player = DemoRunner.instance().getPlayer();
this.fpsProfiler = DemoRunner.instance().getFpsProfiler();
this.runtime = Runtime.getRuntime();
context.addEventListener(HitEvent.TYPE, this::logHitEvent);
context.addEventListener(EnemyDiedEvent.TYPE, this::logEnemyDiedEvent);

View File

@@ -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 = context.getGlobal("player", Player.class);
this.player = DemoRunner.instance().getPlayer();
}
@Override

View File

@@ -42,11 +42,11 @@ public abstract class BaseMapHandler implements MapHandler {
public void onCreate(Context context, GameMap map) {
this.context = context;
this.screen = context.getScreen();
this.runner = (DemoRunner) context.getGameRunner();
this.guiManager = context.getGlobal("gui", GuiManager.class);
this.runner = DemoRunner.instance();
this.guiManager = runner.getGuiManager();
this.camera = context.getCamera();
this.map = map;
this.player = context.getGlobal("player", Player.class);
this.player = runner.getPlayer();
this.cameraController = FollowingCameraController
.on(screen, camera, map)
.follow(player.getPosition());

View File

@@ -7,35 +7,29 @@ import com.bartlomiejpluta.base.api.screen.Screen;
import com.bartlomiejpluta.base.util.profiler.FPSProfiler;
import com.bartlomiejpluta.demo.entity.Player;
import com.bartlomiejpluta.demo.menu.GuiManager;
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 final FPSProfiler fpsProfiler = FPSProfiler.create(20);
private static DemoRunner INSTANCE;
private Screen screen;
private Context context;
private GuiManager guiManager;
private GUI hud;
@Getter
private final FPSProfiler fpsProfiler = FPSProfiler.create(20);
@Getter
private GuiManager guiManager;
@Getter
private Player player;
@Override
public void init(Context context) {
this.context = context;
this.screen = context.getScreen();
context.putGlobal("fps-profiler", fpsProfiler);
configureScreen();
configureCamera();
initPlayer();
initHUD();
initMenu();
guiManager.showStartMenu();
screen.show();
public static DemoRunner instance() {
return INSTANCE;
}
private void configureScreen() {
@@ -50,7 +44,6 @@ public class DemoRunner implements GameRunner {
private void initMenu() {
this.guiManager = new GuiManager(this, context);
context.putGlobal("gui", guiManager);
}
private void initHUD() {
@@ -62,7 +55,6 @@ 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() {
@@ -106,4 +98,22 @@ public class DemoRunner implements GameRunner {
public void dispose() {
// Do something after game loop is end
}
@Override
public void init(Context context) {
DemoRunner.INSTANCE = this;
this.context = context;
this.screen = context.getScreen();
configureScreen();
configureCamera();
initPlayer();
initHUD();
initMenu();
guiManager.showStartMenu();
screen.show();
}
}