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.ColoredPixel;
import cz.jzitnik.ui.pixels.Empty;
import cz.jzitnik.utils.DependencyManager;
import cz.jzitnik.utils.TextRenderer;
import cz.jzitnik.utils.events.AbstractEventHandler;
import cz.jzitnik.utils.events.EventManager;
@@ -43,6 +44,9 @@ public class DialogEventHandler extends AbstractEventHandler<Dialog> {
@InjectDependency
private TextRenderer textRenderer;
@InjectDependency
private DependencyManager dependencyManager;
private static final int WIDTH = 350;
private static final int MARGIN_BOTTOM = 15;
public static final int PADDING = 7;
@@ -158,30 +162,39 @@ public class DialogEventHandler extends AbstractEventHandler<Dialog> {
dialogState.setRenderInProgress(false);
if (onEnd instanceof OnEnd.Continue(Dialog nextDialog)) {
Thread.sleep(1000);
for (int y = start.getRow(); y < start.getRow() + size.getRows(); y++) {
for (int x = start.getColumn(); x < start.getColumn() + size.getColumns(); x++) {
screenBuffer.getGlobalOverrideBuffer()[y][x] = new Empty();
}
}
next(onEnd, start, size);
if (nextDialog == null) {
dialogState.setCurrentDialog(null);
eventManager.emitEvent(
new RerenderScreen(
new RerenderScreen.ScreenPart(
start,
new TerminalPosition(start.getColumn() + size.getColumns(), start.getRow() + size.getRows())
)
)
);
} else {
eventManager.emitEvent(nextDialog);
}
}
} 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)) {
Thread.sleep(1000);
for (int y = start.getRow(); y < start.getRow() + size.getRows(); y++) {
for (int x = start.getColumn(); x < start.getColumn() + size.getColumns(); x++) {
screenBuffer.getGlobalOverrideBuffer()[y][x] = new Empty();
}
}
if (nextDialog == null) {
dialogState.setCurrentDialog(null);
eventManager.emitEvent(
new RerenderScreen(
new RerenderScreen.ScreenPart(
start,
new TerminalPosition(start.getColumn() + size.getColumns(), start.getRow() + size.getRows())
)
)
);
} else {
eventManager.emitEvent(nextDialog);
}
} else if (onEnd instanceof OnEnd.RunCode(Runnable runnable, OnEnd end)) {
dependencyManager.inject(runnable);
runnable.run();
next(end, start, size);
}
}
}

View File

@@ -30,6 +30,6 @@ public class ExitEventHandler extends AbstractEventHandler<ExitEvent> {
scheduledTaskManager.shutdown();
roomTaskScheduler.finalShutdown();
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;
public interface OnEnd {
record RunCode(Runnable runnable, OnEnd onEnd) 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.utils.events.EventManager;
import cz.jzitnik.utils.roomtasks.RoomTask;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import java.awt.image.BufferedImage;
@Slf4j
public abstract class DialogMob extends Mob {
private final Dialog dialog;
protected Dialog dialog;
public DialogMob(BufferedImage texture, RoomTask[] tasks, RoomCords cords, Dialog dialog) {
super(texture, tasks, cords);

View File

@@ -1,9 +1,11 @@
package cz.jzitnik.game.mobs;
import cz.jzitnik.annotations.injectors.InjectDependency;
import cz.jzitnik.game.utils.Renderable;
import cz.jzitnik.game.utils.RoomCords;
import cz.jzitnik.game.utils.Selectable;
import cz.jzitnik.utils.roomtasks.RoomTask;
import cz.jzitnik.utils.roomtasks.RoomTaskScheduler;
import lombok.Getter;
import lombok.Setter;
@@ -13,10 +15,19 @@ import java.awt.image.BufferedImage;
public abstract class Mob implements Renderable, Selectable {
protected final BufferedImage texture;
@Setter
protected RoomTask[] tasks;
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) {
this.texture = texture;
this.tasks = new RoomTask[] {task};

View File

@@ -4,31 +4,34 @@ import cz.jzitnik.game.ResourceManager;
import cz.jzitnik.game.dialog.Dialog;
import cz.jzitnik.game.dialog.OnEnd;
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.utils.roomtasks.RoomTask;
public class Pepa extends DialogMob {
public Pepa(ResourceManager resourceManager, RoomCords cords) {
super(resourceManager.getResource(ResourceManager.Resource.PLAYER_FRONT), new RoomTask[]{}, cords,
new Dialog(
"Pepa: Never gonna give you up",
new OnEnd.Continue(new Dialog(
"Pepa: Never gonna let you down",
new OnEnd.Continue(new Dialog(
"Pepa: Never gonna run around",
new OnEnd.Continue(new Dialog(
"Pepa: How it continues?", new OnEnd.AskQuestion(
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("You are a dessert", new Dialog("Pepa: WRONG!", new OnEnd.Continue(null)))
}
)))
))
))
)
super(resourceManager.getResource(ResourceManager.Resource.PLAYER_FRONT), new RoomTask[]{}, cords, null
);
setTasks(new RoomTask[]{
});
dialog = new Dialog(
"Pepa: Never gonna give you up",
new OnEnd.Continue(new Dialog(
"Pepa: Never gonna let you down",
new OnEnd.Continue(new Dialog(
"Pepa: Never gonna run around",
new OnEnd.Continue(new Dialog(
"Pepa: How it continues?", new OnEnd.AskQuestion(
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("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)))
}
)))
))
))
);
}
}

View File

@@ -13,9 +13,9 @@ import lombok.extern.slf4j.Slf4j;
public class Zombie extends HittableMobDrops {
public Zombie(ResourceManager resourceManager, RoomCords cords) {
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 EnemyPlayerHittingTask(this, 500, 15, () -> 5)
});
};
}
}

View File

@@ -77,15 +77,25 @@ public class RoomTaskScheduler {
}
for (Mob mob : currentRoom.getMobs()) {
RoomTask[] mobTasks = mob.getTasks();
for (RoomTask task : mobTasks) {
dependencyManager.inject(task.getTask());
ScheduledFuture<?> future = scheduler.scheduleAtFixedRate(task.getTask(), 0, task.getRate(), task.getRateUnit());
tasks.put(task, future);
}
registerNewTasks(mob.getTasks());
}
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());
ScheduledFuture<?> future = scheduler.scheduleAtFixedRate(task.getTask(), 0, task.getRate(), task.getRateUnit());
tasks.put(task, future);
}
}
}