feat: Give item on dialog
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,6 @@ public class EventManager extends Thread {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public EventManager(Reflections reflections, DependencyManager dependencyManager) {
|
||||
this.dependencyManager = dependencyManager;
|
||||
setDaemon(true);
|
||||
|
||||
@@ -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."
|
||||
|
||||
Reference in New Issue
Block a user