Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WaveFunctionCollapse procedural arena #34

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/main/java/net/minestom/arena/feature/CombatFeature.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
import java.util.function.ToLongFunction;

/**
* @param playerCombat Allow player combat
* @param damageFunction Uses the return value as damage to apply (in lambda arg 1 is attacker, arg 2 is victim)
* @param playerCombat Allow player combat
* @param damageFunction Uses the return value as damage to apply (in lambda arg 1 is attacker, arg 2 is victim)
* @param invulnerabilityFunction Uses the return value as time an entity is invulnerable after getting attacked (in lambda arg 1 is victim)
*/
record CombatFeature(boolean playerCombat, ToDoubleBiFunction<Entity, Entity> damageFunction, ToLongFunction<Entity> invulnerabilityFunction) implements Feature {
record CombatFeature(boolean playerCombat, ToDoubleBiFunction<Entity, Entity> damageFunction,
ToLongFunction<Entity> invulnerabilityFunction) implements Feature {
private static final Tag<Long> INVULNERABLE_UNTIL_TAG = Tag.Long("invulnerable_until").defaultValue(0L);

private void takeKnockback(Entity target, Entity source) {
Expand Down
58 changes: 40 additions & 18 deletions src/main/java/net/minestom/arena/game/ArenaCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minestom.arena.Lobby;
import net.minestom.arena.Messenger;
import net.minestom.arena.game.mob.MobArena;
import net.minestom.arena.game.procedural.ProceduralArena;
import net.minestom.arena.group.Group;
import net.minestom.arena.utils.CommandUtils;
import net.minestom.arena.utils.ItemUtils;
Expand All @@ -17,15 +18,34 @@
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
import net.minestom.server.tag.Tag;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Function;

public final class ArenaCommand extends Command {
private static final Map<String, Function<Group, Arena>> ARENAS = Map.of(
"mob", MobArena::new);

// In order to add a new arena, add it to this section:
public static final ArenaType MOB = new ArenaType("mob", ItemStack.of(Material.ZOMBIE_HEAD),
builder -> builder.displayName(Component.text("Mob Arena", NamedTextColor.GREEN)),
MobArena::new);
public static final ArenaType PROCEDURAL = new ArenaType("procedural", ItemStack.of(Material.BONE_BLOCK),
builder -> builder.displayName(Component.text("Procedural", NamedTextColor.RED)),
ProceduralArena::new);
public static final Map<String, ArenaType> ARENAS = Map.of(
"mob", MOB,
"procedural", PROCEDURAL
);

private record ArenaType(String name, ItemStack menuItem, Consumer<ItemStack.Builder> builderConsumer,
Function<Group, Arena> arenaCreator) {

public Arena startNew(Group group) {
return arenaCreator.apply(group);
}
}

public ArenaCommand() {
super("arena");
Expand All @@ -34,9 +54,10 @@ public ArenaCommand() {
setDefaultExecutor((sender, context) ->
((Player) sender).openInventory(new ArenaInventory()));

addSyntax((sender, context) ->
play((Player) sender, context.get("type")),
ArgumentType.Word("type").from(ARENAS.keySet().toArray(String[]::new)));
addSyntax(
(sender, context) -> play((Player) sender, context.get("type")),
ArgumentType.Word("type").from(ARENAS.keySet().toArray(String[]::new))
);
}

private static void play(Player player, String type) {
Expand All @@ -49,7 +70,7 @@ private static void play(Player player, String type) {
Messenger.warn(player, "You are not the leader of your group!");
return;
}
Arena arena = ARENAS.get(type).apply(group);
Arena arena = ARENAS.get(type).startNew(group);
arena.init().thenRun(() -> group.members().forEach(Player::refreshCommands));
}

Expand All @@ -59,23 +80,24 @@ private static class ArenaInventory extends Inventory {
.displayName(Component.text("Arena", NamedTextColor.RED))
.lore(Component.text("Select an arena to play in", NamedTextColor.GRAY))
.build());
private static final Map<ItemStack, String> ARENA_ITEM = Map.of(
ItemStack.builder(Material.ZOMBIE_HEAD)
.displayName(Component.text("Mob Arena", NamedTextColor.GREEN))
.build(), "mob");

public ArenaInventory() {
super(InventoryType.CHEST_4_ROW, Component.text("Arena"));

setItemStack(4, HEADER);
setItemStack(31, Items.CLOSE);

AtomicInteger i = new AtomicInteger(13 - ARENA_ITEM.size() / 2);
ARENA_ITEM.forEach((item, arena) ->
setItemStack(i.getAndIncrement(), ItemUtils.stripItalics(item.withLore(List.of(
Component.text("Click to play in the " + arena + " arena", NamedTextColor.GRAY))
).withTag(ARENA_TAG, arena)))
);
int i = 13 - ARENAS.size() / 2;
for (ArenaType type : ARENAS.values()) {
ItemStack item = type.menuItem();
setItemStack(
i++,
ItemUtils.stripItalics(
item.withLore(List.of(Component.text("Click to play in the " + type.name() + " arena", NamedTextColor.GRAY)))
.withTag(ARENA_TAG, type.name())
)
);
}

addInventoryCondition((player, slot, c, result) -> {
result.setCancel(true);
Expand All @@ -85,7 +107,7 @@ public ArenaInventory() {
return;
}

final String arena = result.getClickedItem().getTag(ARENA_TAG);
String arena = result.getClickedItem().getTag(ARENA_TAG);

if (arena != null) {
player.closeInventory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public interface SingleInstanceArena extends Arena {

CompletableFuture<?>[] futures =
group().members().stream()
.map(player -> player.setInstance(instance, spawnPosition(player)))
.toArray(CompletableFuture<?>[]::new);
.map(player -> player.setInstance(instance, spawnPosition(player)))
.toArray(CompletableFuture<?>[]::new);

return CompletableFuture.allOf(futures).thenRun(this::start);
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/minestom/arena/game/mob/ArenaMob.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public ArenaMob(@NotNull EntityType entityType, int stage) {
setCustomName(generateHealthBar(getMaxHealth(), getHealth()));
setCustomNameVisible(true);
eventNode().addListener(EntityDamageEvent.class, event ->
setCustomName(generateHealthBar(getMaxHealth(), getHealth())))
.addListener(EntityDeathEvent.class, event ->
setCustomName(generateHealthBar(getMaxHealth(), 0)));
setCustomName(generateHealthBar(getMaxHealth(), getHealth())))
.addListener(EntityDeathEvent.class, event ->
setCustomName(generateHealthBar(getMaxHealth(), 0)));
}

@Contract(pure = true)
Expand All @@ -44,7 +44,7 @@ public ArenaMob(@NotNull EntityType entityType, int stage) {
NamedTextColor.RED
)).append(Component.text(CHARACTERS.get((int) Math.round(
(charHealth - Math.floor(charHealth)) // number from 0-1
* (CHARACTERS.size() - 1) // indexes start at 0
* (CHARACTERS.size() - 1) // indexes start at 0
)), NamedTextColor.YELLOW))
.append(Component.text("]", NamedTextColor.DARK_GRAY))
.build();
Expand Down
Loading