feat: Dialog running code and changeable tasks

This commit is contained in:
2026-01-22 14:13:41 +01:00
parent 46981937ce
commit f8d28acc33
8 changed files with 92 additions and 52 deletions

View File

@@ -15,6 +15,7 @@ import cz.jzitnik.states.TerminalState;
import cz.jzitnik.ui.pixels.AlphaPixel; import cz.jzitnik.ui.pixels.AlphaPixel;
import cz.jzitnik.ui.pixels.ColoredPixel; import cz.jzitnik.ui.pixels.ColoredPixel;
import cz.jzitnik.ui.pixels.Empty; import cz.jzitnik.ui.pixels.Empty;
import cz.jzitnik.utils.DependencyManager;
import cz.jzitnik.utils.TextRenderer; import cz.jzitnik.utils.TextRenderer;
import cz.jzitnik.utils.events.AbstractEventHandler; import cz.jzitnik.utils.events.AbstractEventHandler;
import cz.jzitnik.utils.events.EventManager; import cz.jzitnik.utils.events.EventManager;
@@ -43,6 +44,9 @@ public class DialogEventHandler extends AbstractEventHandler<Dialog> {
@InjectDependency @InjectDependency
private TextRenderer textRenderer; private TextRenderer textRenderer;
@InjectDependency
private DependencyManager dependencyManager;
private static final int WIDTH = 350; private static final int WIDTH = 350;
private static final int MARGIN_BOTTOM = 15; private static final int MARGIN_BOTTOM = 15;
public static final int PADDING = 7; public static final int PADDING = 7;
@@ -158,6 +162,14 @@ public class DialogEventHandler extends AbstractEventHandler<Dialog> {
dialogState.setRenderInProgress(false); dialogState.setRenderInProgress(false);
next(onEnd, start, size);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
private void next(OnEnd onEnd, TerminalPosition start, TerminalSize size) throws InterruptedException {
if (onEnd instanceof OnEnd.Continue(Dialog nextDialog)) { if (onEnd instanceof OnEnd.Continue(Dialog nextDialog)) {
Thread.sleep(1000); Thread.sleep(1000);
for (int y = start.getRow(); y < start.getRow() + size.getRows(); y++) { for (int y = start.getRow(); y < start.getRow() + size.getRows(); y++) {
@@ -179,9 +191,10 @@ public class DialogEventHandler extends AbstractEventHandler<Dialog> {
} else { } else {
eventManager.emitEvent(nextDialog); eventManager.emitEvent(nextDialog);
} }
} } else if (onEnd instanceof OnEnd.RunCode(Runnable runnable, OnEnd end)) {
} catch (InterruptedException e) { dependencyManager.inject(runnable);
throw new RuntimeException(e); runnable.run();
next(end, start, size);
} }
} }
} }

View File

@@ -30,6 +30,6 @@ public class ExitEventHandler extends AbstractEventHandler<ExitEvent> {
scheduledTaskManager.shutdown(); scheduledTaskManager.shutdown();
roomTaskScheduler.finalShutdown(); roomTaskScheduler.finalShutdown();
runningState.setRunning(false); runningState.setRunning(false);
System.exit(0); // Pls don't blame me //System.exit(0); // Pls don't blame me
} }
} }

View File

@@ -1,6 +1,8 @@
package cz.jzitnik.game.dialog; package cz.jzitnik.game.dialog;
public interface OnEnd { public interface OnEnd {
record RunCode(Runnable runnable, OnEnd onEnd) implements OnEnd {}
record Continue(Dialog nextDialog) implements OnEnd { record Continue(Dialog nextDialog) implements OnEnd {
} }

View File

@@ -7,13 +7,14 @@ import cz.jzitnik.game.utils.RoomCords;
import cz.jzitnik.states.DialogState; import cz.jzitnik.states.DialogState;
import cz.jzitnik.utils.events.EventManager; import cz.jzitnik.utils.events.EventManager;
import cz.jzitnik.utils.roomtasks.RoomTask; import cz.jzitnik.utils.roomtasks.RoomTask;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
@Slf4j @Slf4j
public abstract class DialogMob extends Mob { public abstract class DialogMob extends Mob {
private final Dialog dialog; protected Dialog dialog;
public DialogMob(BufferedImage texture, RoomTask[] tasks, RoomCords cords, Dialog dialog) { public DialogMob(BufferedImage texture, RoomTask[] tasks, RoomCords cords, Dialog dialog) {
super(texture, tasks, cords); super(texture, tasks, cords);

View File

@@ -1,9 +1,11 @@
package cz.jzitnik.game.mobs; package cz.jzitnik.game.mobs;
import cz.jzitnik.annotations.injectors.InjectDependency;
import cz.jzitnik.game.utils.Renderable; import cz.jzitnik.game.utils.Renderable;
import cz.jzitnik.game.utils.RoomCords; import cz.jzitnik.game.utils.RoomCords;
import cz.jzitnik.game.utils.Selectable; import cz.jzitnik.game.utils.Selectable;
import cz.jzitnik.utils.roomtasks.RoomTask; import cz.jzitnik.utils.roomtasks.RoomTask;
import cz.jzitnik.utils.roomtasks.RoomTaskScheduler;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@@ -13,10 +15,19 @@ import java.awt.image.BufferedImage;
public abstract class Mob implements Renderable, Selectable { public abstract class Mob implements Renderable, Selectable {
protected final BufferedImage texture; protected final BufferedImage texture;
@Setter
protected RoomTask[] tasks; protected RoomTask[] tasks;
protected final RoomCords cords; protected final RoomCords cords;
@InjectDependency
private RoomTaskScheduler roomTaskScheduler;
protected void updateTasks(RoomTask[] tasks) {
var oldTasks = this.tasks;
this.tasks = tasks;
roomTaskScheduler.registerNewMob(this, oldTasks);
}
public Mob(BufferedImage texture, RoomTask task, RoomCords cords) { public Mob(BufferedImage texture, RoomTask task, RoomCords cords) {
this.texture = texture; this.texture = texture;
this.tasks = new RoomTask[] {task}; this.tasks = new RoomTask[] {task};

View File

@@ -4,13 +4,17 @@ import cz.jzitnik.game.ResourceManager;
import cz.jzitnik.game.dialog.Dialog; import cz.jzitnik.game.dialog.Dialog;
import cz.jzitnik.game.dialog.OnEnd; import cz.jzitnik.game.dialog.OnEnd;
import cz.jzitnik.game.mobs.DialogMob; import cz.jzitnik.game.mobs.DialogMob;
import cz.jzitnik.game.mobs.tasks.EnemyPlayerHittingTask;
import cz.jzitnik.game.mobs.tasks.MobFollowingPlayerTask;
import cz.jzitnik.game.utils.RoomCords; import cz.jzitnik.game.utils.RoomCords;
import cz.jzitnik.utils.roomtasks.RoomTask; import cz.jzitnik.utils.roomtasks.RoomTask;
public class Pepa extends DialogMob { public class Pepa extends DialogMob {
public Pepa(ResourceManager resourceManager, RoomCords cords) { public Pepa(ResourceManager resourceManager, RoomCords cords) {
super(resourceManager.getResource(ResourceManager.Resource.PLAYER_FRONT), new RoomTask[]{}, cords, super(resourceManager.getResource(ResourceManager.Resource.PLAYER_FRONT), new RoomTask[]{}, cords, null
new Dialog( );
dialog = new Dialog(
"Pepa: Never gonna give you up", "Pepa: Never gonna give you up",
new OnEnd.Continue(new Dialog( new OnEnd.Continue(new Dialog(
"Pepa: Never gonna let you down", "Pepa: Never gonna let you down",
@@ -20,15 +24,14 @@ public class Pepa extends DialogMob {
"Pepa: How it continues?", new OnEnd.AskQuestion( "Pepa: How it continues?", new OnEnd.AskQuestion(
new OnEnd.AskQuestion.Answer[]{ new OnEnd.AskQuestion.Answer[]{
new OnEnd.AskQuestion.Answer("And desert you", new Dialog("Pepa: You are god damn right!", new OnEnd.Continue(null))), new OnEnd.AskQuestion.Answer("And desert you", new Dialog("Pepa: You are god damn right!", new OnEnd.Continue(null))),
new OnEnd.AskQuestion.Answer("You are a dessert", new Dialog("Pepa: WRONG!", new OnEnd.Continue(null))) new OnEnd.AskQuestion.Answer("You are a dessert", new Dialog("Pepa: WRONG!", new OnEnd.RunCode(() -> updateTasks(new RoomTask[]{
new MobFollowingPlayerTask(this, 1, 100),
new EnemyPlayerHittingTask(this, 500, 15, () -> 5)
}), null)))
} }
))) )))
)) ))
)) ))
)
); );
setTasks(new RoomTask[]{
});
} }
} }

View File

@@ -13,9 +13,9 @@ import lombok.extern.slf4j.Slf4j;
public class Zombie extends HittableMobDrops { public class Zombie extends HittableMobDrops {
public Zombie(ResourceManager resourceManager, RoomCords cords) { public Zombie(ResourceManager resourceManager, RoomCords cords) {
super(() -> new Apple(resourceManager), resourceManager.getResource(ResourceManager.Resource.PLAYER_FRONT), null, cords, 10); super(() -> new Apple(resourceManager), resourceManager.getResource(ResourceManager.Resource.PLAYER_FRONT), null, cords, 10);
setTasks(new RoomTask[]{ tasks = new RoomTask[]{
new MobFollowingPlayerTask(this, 1, 100), new MobFollowingPlayerTask(this, 1, 100),
new EnemyPlayerHittingTask(this, 500, 15, () -> 5) new EnemyPlayerHittingTask(this, 500, 15, () -> 5)
}); };
} }
} }

View File

@@ -77,15 +77,25 @@ public class RoomTaskScheduler {
} }
for (Mob mob : currentRoom.getMobs()) { for (Mob mob : currentRoom.getMobs()) {
RoomTask[] mobTasks = mob.getTasks(); registerNewTasks(mob.getTasks());
}
for (RoomTask task : mobTasks) { firstRun = false;
}
public void registerNewMob(Mob mob, RoomTask[] oldTasks) {
for (RoomTask oldTask : oldTasks) {
stopTask(oldTask);
}
registerNewTasks(mob.getTasks());
}
private void registerNewTasks(RoomTask[] registerTasks) {
for (RoomTask task : registerTasks) {
dependencyManager.inject(task.getTask()); dependencyManager.inject(task.getTask());
ScheduledFuture<?> future = scheduler.scheduleAtFixedRate(task.getTask(), 0, task.getRate(), task.getRateUnit()); ScheduledFuture<?> future = scheduler.scheduleAtFixedRate(task.getTask(), 0, task.getRate(), task.getRateUnit());
tasks.put(task, future); tasks.put(task, future);
} }
} }
firstRun = false;
}
} }