feat: Give item on dialog

This commit is contained in:
2026-02-18 10:59:51 +01:00
parent 8935349f92
commit f8f150cdf0
7 changed files with 90 additions and 27 deletions

View File

@@ -6,6 +6,7 @@ import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import java.awt.image.BufferedImage;
import java.io.Serializable;
import java.util.List;
@@ -16,6 +17,13 @@ public class RoomCords implements Cloneable, Serializable {
private int x;
private int y;
public RoomCords calculateCenter(BufferedImage texture) {
return new RoomCords(
x + texture.getWidth() / 2,
y + texture.getHeight() / 2
);
}
@JsonCreator
public RoomCords(
@JsonProperty("x") int x,

View File

@@ -251,11 +251,11 @@ public class DialogEventHandler extends AbstractEventHandler<Dialog> {
clear(start, size);
eventManager.emitEvent(nextDialog);
} else if (onEnd instanceof OnEnd.RunCode(Runnable runnable, OnEnd end)) {
} else if (onEnd instanceof OnEnd.RunCode runCode) {
Runnable runnable = runCode.getRunnable();
dependencyManager.inject(runnable);
runnable.run();
next(end, start, size);
next(runCode.getOnEnd(), start, size);
} else if (onEnd instanceof OnEnd.End) {
clear(start, size);
dialogState.setCurrentDialog(null);

View File

@@ -28,7 +28,7 @@ public class Player implements GamePlayer {
private final int id;
public static final int MAX_STAMINA = 20;
public static final int MAX_HEALTH = 30;
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private final RoomCords playerCords;
private final RoomPart collider;

View File

@@ -4,8 +4,18 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import cz.jzitnik.client.annotations.injectors.InjectDependency;
import cz.jzitnik.client.annotations.injectors.InjectState;
import cz.jzitnik.client.events.InventoryRerender;
import cz.jzitnik.client.game.GameState;
import cz.jzitnik.client.game.Player;
import cz.jzitnik.client.game.Requirement;
import cz.jzitnik.client.game.ResourceManager;
import cz.jzitnik.client.game.items.GameItem;
import cz.jzitnik.client.game.mobs.HittableMobDrops;
import cz.jzitnik.client.utils.events.EventManager;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import java.util.Arrays;
import java.util.Optional;
@@ -18,13 +28,56 @@ import java.util.Optional;
@JsonSubTypes.Type(value = OnEnd.Continue.class, name = "continue"),
@JsonSubTypes.Type(value = OnEnd.AskQuestion.class, name = "ask_question"),
@JsonSubTypes.Type(value = OnEnd.End.class, name = "end"),
@JsonSubTypes.Type(value = OnEnd.GiveItem.class, name = "give_item")
})
public interface OnEnd {
record End() implements OnEnd {
}
record RunCode(Runnable runnable, OnEnd onEnd) implements OnEnd {
} // TODO: Serialize
class GiveItem extends RunCode {
@JsonCreator
public GiveItem(
@JsonProperty("item") GameItem item,
@JsonProperty("then") OnEnd onEnd
) {
super(new Run(item), onEnd);
}
@RequiredArgsConstructor
private static class Run implements Runnable {
private final GameItem item;
@InjectState
private GameState gameState;
@InjectDependency
private EventManager eventManager;
@InjectDependency
private ResourceManager resourceManager;
@Override
public void run() {
Player player = gameState.getPlayer();
var playerCords = player.getPlayerCords().calculateCenter(player.getTexture(resourceManager));
boolean addedIntoInventory = player.addItem(item);
if (!addedIntoInventory) {
eventManager.emitEvent(HittableMobDrops.dropItem(playerCords.getX(), playerCords.getY(), gameState.getCurrentRoom(), item));
} else {
eventManager.emitEvent(new InventoryRerender());
}
}
}
}
@Getter
@RequiredArgsConstructor
class RunCode implements OnEnd {
private final Runnable runnable;
private final OnEnd onEnd;
}
record Continue(Dialog nextDialog) implements OnEnd {
@JsonCreator
@@ -57,15 +110,13 @@ public interface OnEnd {
if (requirement.isPresent()) {
Requirement requirement = requirement().get();
if (requirement.itemType() != null) {
if (Arrays.stream(gameState.getPlayer().getInventory()).noneMatch(item -> {
return Arrays.stream(gameState.getPlayer().getInventory()).anyMatch(item -> {
if (item == null) {
return false;
}
return item.getType().getItemType().getSimpleName().equals(requirement.itemType());
})) {
return false;
}
});
}
return true;

View File

@@ -55,10 +55,9 @@ public class HittableMobDrops extends HittableMob {
Player player = gameState.getPlayer();
RoomCords enemyCords = getCords();
BufferedImage enemyTexture = getTexture();
GameRoom currentRoom = gameState.getCurrentRoom();
int roomX = enemyCords.getX() + enemyTexture.getWidth() / 2;
int roomY = enemyCords.getY() + enemyTexture.getHeight() / 2;
GameRoom currentRoom = gameState.getCurrentRoom();
List<Event> events = new ArrayList<>();
@@ -71,17 +70,21 @@ public class HittableMobDrops extends HittableMob {
events.add(new InventoryRerender());
}
} else {
double angle = ThreadLocalRandom.current().nextDouble(0, Math.PI * 2);
double radius = ThreadLocalRandom.current().nextDouble(0, DROP_ITEM_ON_GROUND_RADIUS);
int randomX = roomX + (int) (Math.cos(angle) * radius);
int randomY = roomY + (int) (Math.sin(angle) * radius);
RoomCords itemCords = new RoomCords(randomX, randomY);
DroppedItem droppedItem = new DroppedItem(currentRoom, itemCords, item);
currentRoom.getDroppedItems().add(droppedItem);
events.add(new DroppedItemRerender(droppedItem));
events.add(dropItem(roomX, roomY, currentRoom, item));
}
}
eventManager.emitEvent(events, this::afterKill);
}
public static Event dropItem(int x, int y, GameRoom currentRoom, GameItem item) {
double angle = ThreadLocalRandom.current().nextDouble(0, Math.PI * 2);
double radius = ThreadLocalRandom.current().nextDouble(0, DROP_ITEM_ON_GROUND_RADIUS);
int randomX = x + (int) (Math.cos(angle) * radius);
int randomY = y + (int) (Math.sin(angle) * radius);
RoomCords itemCords = new RoomCords(randomX, randomY);
DroppedItem droppedItem = new DroppedItem(currentRoom, itemCords, item);
currentRoom.getDroppedItems().add(droppedItem);
return new DroppedItemRerender(droppedItem);
}
}

View File

@@ -64,7 +64,6 @@ public class EventManager extends Thread {
}
}
@SuppressWarnings("unchecked")
public EventManager(Reflections reflections, DependencyManager dependencyManager) {
this.dependencyManager = dependencyManager;
setDaemon(true);

View File

@@ -78,12 +78,14 @@
item: "quest_item_boss_skin"
dialog:
text: "Well done. Here is the key."
giveItem:
id: 300
name: "Cave Exit Key"
type: { name: "quest_item_final_key" }
texture: "KEY"
onEnd: { type: end }
onEnd:
type: "give_item"
item:
id: 800
name: "Something"
type: { name: "junk" }
texture: "APPLE"
then: { type: end }
- answer: "Not yet"
dialog:
text: "Then go back before it finds you."