feat: Give item on dialog
This commit is contained in:
@@ -6,6 +6,7 @@ import lombok.Getter;
|
|||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -16,6 +17,13 @@ public class RoomCords implements Cloneable, Serializable {
|
|||||||
private int x;
|
private int x;
|
||||||
private int y;
|
private int y;
|
||||||
|
|
||||||
|
public RoomCords calculateCenter(BufferedImage texture) {
|
||||||
|
return new RoomCords(
|
||||||
|
x + texture.getWidth() / 2,
|
||||||
|
y + texture.getHeight() / 2
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@JsonCreator
|
@JsonCreator
|
||||||
public RoomCords(
|
public RoomCords(
|
||||||
@JsonProperty("x") int x,
|
@JsonProperty("x") int x,
|
||||||
|
|||||||
@@ -251,11 +251,11 @@ public class DialogEventHandler extends AbstractEventHandler<Dialog> {
|
|||||||
clear(start, size);
|
clear(start, size);
|
||||||
eventManager.emitEvent(nextDialog);
|
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);
|
dependencyManager.inject(runnable);
|
||||||
runnable.run();
|
runnable.run();
|
||||||
next(end, start, size);
|
next(runCode.getOnEnd(), start, size);
|
||||||
|
|
||||||
} else if (onEnd instanceof OnEnd.End) {
|
} else if (onEnd instanceof OnEnd.End) {
|
||||||
clear(start, size);
|
clear(start, size);
|
||||||
dialogState.setCurrentDialog(null);
|
dialogState.setCurrentDialog(null);
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ public class Player implements GamePlayer {
|
|||||||
private final int id;
|
private final int id;
|
||||||
public static final int MAX_STAMINA = 20;
|
public static final int MAX_STAMINA = 20;
|
||||||
public static final int MAX_HEALTH = 30;
|
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 RoomCords playerCords;
|
||||||
private final RoomPart collider;
|
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.JsonProperty;
|
||||||
import com.fasterxml.jackson.annotation.JsonSubTypes;
|
import com.fasterxml.jackson.annotation.JsonSubTypes;
|
||||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
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.GameState;
|
||||||
|
import cz.jzitnik.client.game.Player;
|
||||||
import cz.jzitnik.client.game.Requirement;
|
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.Arrays;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@@ -18,13 +28,56 @@ import java.util.Optional;
|
|||||||
@JsonSubTypes.Type(value = OnEnd.Continue.class, name = "continue"),
|
@JsonSubTypes.Type(value = OnEnd.Continue.class, name = "continue"),
|
||||||
@JsonSubTypes.Type(value = OnEnd.AskQuestion.class, name = "ask_question"),
|
@JsonSubTypes.Type(value = OnEnd.AskQuestion.class, name = "ask_question"),
|
||||||
@JsonSubTypes.Type(value = OnEnd.End.class, name = "end"),
|
@JsonSubTypes.Type(value = OnEnd.End.class, name = "end"),
|
||||||
|
@JsonSubTypes.Type(value = OnEnd.GiveItem.class, name = "give_item")
|
||||||
})
|
})
|
||||||
public interface OnEnd {
|
public interface OnEnd {
|
||||||
record End() implements OnEnd {
|
record End() implements OnEnd {
|
||||||
}
|
}
|
||||||
|
|
||||||
record RunCode(Runnable runnable, OnEnd onEnd) implements OnEnd {
|
class GiveItem extends RunCode {
|
||||||
} // TODO: Serialize
|
@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 {
|
record Continue(Dialog nextDialog) implements OnEnd {
|
||||||
@JsonCreator
|
@JsonCreator
|
||||||
@@ -57,15 +110,13 @@ public interface OnEnd {
|
|||||||
if (requirement.isPresent()) {
|
if (requirement.isPresent()) {
|
||||||
Requirement requirement = requirement().get();
|
Requirement requirement = requirement().get();
|
||||||
if (requirement.itemType() != null) {
|
if (requirement.itemType() != null) {
|
||||||
if (Arrays.stream(gameState.getPlayer().getInventory()).noneMatch(item -> {
|
return Arrays.stream(gameState.getPlayer().getInventory()).anyMatch(item -> {
|
||||||
if (item == null) {
|
if (item == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return item.getType().getItemType().getSimpleName().equals(requirement.itemType());
|
return item.getType().getItemType().getSimpleName().equals(requirement.itemType());
|
||||||
})) {
|
});
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -55,10 +55,9 @@ public class HittableMobDrops extends HittableMob {
|
|||||||
Player player = gameState.getPlayer();
|
Player player = gameState.getPlayer();
|
||||||
RoomCords enemyCords = getCords();
|
RoomCords enemyCords = getCords();
|
||||||
BufferedImage enemyTexture = getTexture();
|
BufferedImage enemyTexture = getTexture();
|
||||||
GameRoom currentRoom = gameState.getCurrentRoom();
|
|
||||||
|
|
||||||
int roomX = enemyCords.getX() + enemyTexture.getWidth() / 2;
|
int roomX = enemyCords.getX() + enemyTexture.getWidth() / 2;
|
||||||
int roomY = enemyCords.getY() + enemyTexture.getHeight() / 2;
|
int roomY = enemyCords.getY() + enemyTexture.getHeight() / 2;
|
||||||
|
GameRoom currentRoom = gameState.getCurrentRoom();
|
||||||
|
|
||||||
List<Event> events = new ArrayList<>();
|
List<Event> events = new ArrayList<>();
|
||||||
|
|
||||||
@@ -71,17 +70,21 @@ public class HittableMobDrops extends HittableMob {
|
|||||||
events.add(new InventoryRerender());
|
events.add(new InventoryRerender());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
double angle = ThreadLocalRandom.current().nextDouble(0, Math.PI * 2);
|
events.add(dropItem(roomX, roomY, currentRoom, item));
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
eventManager.emitEvent(events, this::afterKill);
|
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) {
|
public EventManager(Reflections reflections, DependencyManager dependencyManager) {
|
||||||
this.dependencyManager = dependencyManager;
|
this.dependencyManager = dependencyManager;
|
||||||
setDaemon(true);
|
setDaemon(true);
|
||||||
|
|||||||
@@ -78,12 +78,14 @@
|
|||||||
item: "quest_item_boss_skin"
|
item: "quest_item_boss_skin"
|
||||||
dialog:
|
dialog:
|
||||||
text: "Well done. Here is the key."
|
text: "Well done. Here is the key."
|
||||||
giveItem:
|
onEnd:
|
||||||
id: 300
|
type: "give_item"
|
||||||
name: "Cave Exit Key"
|
item:
|
||||||
type: { name: "quest_item_final_key" }
|
id: 800
|
||||||
texture: "KEY"
|
name: "Something"
|
||||||
onEnd: { type: end }
|
type: { name: "junk" }
|
||||||
|
texture: "APPLE"
|
||||||
|
then: { type: end }
|
||||||
- answer: "Not yet"
|
- answer: "Not yet"
|
||||||
dialog:
|
dialog:
|
||||||
text: "Then go back before it finds you."
|
text: "Then go back before it finds you."
|
||||||
|
|||||||
Reference in New Issue
Block a user