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

👕 Teams #8

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
53 changes: 38 additions & 15 deletions src/main/kotlin/cc/lixou/bedwarsextension/game/BedWarsGame.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,39 @@ package cc.lixou.bedwarsextension.game

import cc.lixou.bedwarsextension.game.generator.ResourceGenerator
import cc.lixou.bedwarsextension.game.generator.fabric.Generators
import cc.lixou.bedwarsextension.game.team.BedWarsTeam
import cc.lixou.bedwarsextension.game.team.TeamTypes
import cc.lixou.bedwarsextension.game.team.getBedWarsTeam
import cc.lixou.bedwarsextension.inventory.ShopInventory
import cc.lixou.stracciatella.game.Game
import net.minestom.server.entity.ItemEntity
import net.minestom.server.entity.Player
import net.minestom.server.event.entity.EntityAttackEvent
import net.minestom.server.event.item.ItemDropEvent
import net.minestom.server.event.item.PickupItemEvent
import net.minestom.server.event.player.PlayerChatEvent
import net.minestom.server.item.ItemStack
import net.minestom.server.item.Material
import net.minestom.server.network.packet.server.play.EntityAnimationPacket
import net.minestom.server.utils.time.TimeUnit
import world.cepi.kstom.Manager
import world.cepi.kstom.event.listenOnly
import kotlin.math.cos
import kotlin.math.sin

class BedWarsGame : Game() {

companion object {
val MAX_PLAYERS = 8
}
var currentState: BedWarsGameState = BedWarsGameState.WAITING

private var state = BedWarsGameState.WAITING
private val TEAM_AMOUNT = 8
private val PLAYER_PER_TEAM = 2

val teams = mutableListOf<BedWarsTeam>()

init {
instance = Manager.instance.instances.first()
// DEBUG
eventNode.listenOnly<PlayerChatEvent> {
if (message.lowercase() == "play") {
state = BedWarsGameState.INGAME
currentState = BedWarsGameState.INGAME
} else if (message.lowercase() == "shop") {
player.openInventory(ShopInventory())
} else if (message.lowercase() == "generator") {
Expand All @@ -37,7 +43,7 @@ class BedWarsGame : Game() {
}
// ENDDEBUG
eventNode.listenOnly<ItemDropEvent> {
when(state) {
when (currentState) {
BedWarsGameState.INGAME -> {
val itemEntity = ItemEntity(itemStack)
itemEntity.setPickupDelay(40, TimeUnit.SERVER_TICK)
Expand All @@ -49,7 +55,7 @@ class BedWarsGame : Game() {
}
}
eventNode.listenOnly<PickupItemEvent> {
when(state) {
when (currentState) {
BedWarsGameState.INGAME -> {
if (entity is Player) {
ResourceGenerator.mayPickup(itemEntity)
Expand All @@ -61,24 +67,41 @@ class BedWarsGame : Game() {
else -> isCancelled = true
}
}
eventNode.listenOnly<EntityAttackEvent> {
if (currentState != BedWarsGameState.INGAME) return@listenOnly
val attacker = entity
if (attacker !is Player) return@listenOnly
if (attacker.getBedWarsTeam()!!.players.contains(target as Player)) return@listenOnly
target.takeKnockback(
0.4f,
sin(attacker.position.yaw * (Math.PI / 180)),
-cos(attacker.position.yaw * (Math.PI / 180))
)
(target as Player).sendPacketToViewersAndSelf(
EntityAnimationPacket(
target.entityId,
EntityAnimationPacket.Animation.TAKE_DAMAGE
)
)
}
}

var currentState: BedWarsGameState = BedWarsGameState.WAITING

override fun canJoin(newPlayers: Array<Player>): Boolean = (this.players.size + newPlayers.size) >= MAX_PLAYERS && currentState == BedWarsGameState.WAITING
override fun canJoin(newPlayers: Array<Player>): Boolean =
(this.players.size + newPlayers.size) <= TEAM_AMOUNT * PLAYER_PER_TEAM && currentState == BedWarsGameState.WAITING

override fun onJoin(joiningPlayer: Player) {
joiningPlayer.inventory.setItemStack(0, ItemStack.of(Material.WOODEN_SWORD))
teams.find { it.players.size < PLAYER_PER_TEAM }?.addPlayer(joiningPlayer)
?: teams.add(TeamTypes.createTeam(teams.size, uuid).also { it.addPlayer(joiningPlayer) })
}

override fun onLeave(leavingPlayer: Player) {
leavingPlayer.sendMessage("bye bye")
leavingPlayer.getBedWarsTeam()?.removePlayer(leavingPlayer)
}

override fun shouldClose(): Boolean = this.players.size == 0

override fun onClose() {

println("closing..")
}

}
30 changes: 30 additions & 0 deletions src/main/kotlin/cc/lixou/bedwarsextension/game/team/BedWarsTeam.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cc.lixou.bedwarsextension.game.team

import cc.lixou.bedwarsextension.game.BedWarsGame
import cc.lixou.stracciatella.game.GameManager.playingGame
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.NamedTextColor
import net.minestom.server.color.DyeColor
import net.minestom.server.entity.Player
import world.cepi.kstom.Manager

class BedWarsTeam(name: String, prefix: Component, color: NamedTextColor, private val bedColor: DyeColor) {

val players = mutableListOf<Player>()
val team = Manager.team.createBuilder(name).prefix(prefix).teamColor(color).build()

fun addPlayer(player: Player) {
players.add(player)
player.team = team
}

fun removePlayer(player: Player) {
players.remove(player)
player.team = null
}

}

/* Extensions */
fun Player.getBedWarsTeam(): BedWarsTeam? = playingGame()?.takeIf { it is BedWarsGame }
?.run { return (this as BedWarsGame).teams.find { it.players.contains(this@getBedWarsTeam) } }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cc.lixou.bedwarsextension.game.team

import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.NamedTextColor
import net.minestom.server.color.DyeColor

data class TeamType(val baseName: String, val prefix: Component, val color: NamedTextColor, val bedColor: DyeColor)
33 changes: 33 additions & 0 deletions src/main/kotlin/cc/lixou/bedwarsextension/game/team/TeamTypes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cc.lixou.bedwarsextension.game.team

import net.kyori.adventure.text.format.NamedTextColor
import net.minestom.server.color.DyeColor
import world.cepi.kstom.adventure.asMini
import java.util.UUID

object TeamTypes {

val RED = TeamType("red", "<red><bold>R <reset>".asMini(), NamedTextColor.RED, DyeColor.RED)

val BLUE = TeamType("blue", "<blue><bold>B <reset>".asMini(), NamedTextColor.BLUE, DyeColor.BLUE)

val GREEN = TeamType("green", "<green><bold>G <reset>".asMini(), NamedTextColor.GREEN, DyeColor.GREEN)

val YELLOW = TeamType("yellow", "<yellow><bold>Y <reset>".asMini(), NamedTextColor.YELLOW, DyeColor.YELLOW)

val AQUA = TeamType("aqua", "<aqua><bold>A <reset>".asMini(), NamedTextColor.AQUA, DyeColor.CYAN)

val WHITE = TeamType("white", "<white><bold>W <reset>".asMini(), NamedTextColor.WHITE, DyeColor.WHITE)

val PINK = TeamType("pink", "<light_purple><bold>P <reset>".asMini(), NamedTextColor.LIGHT_PURPLE, DyeColor.PINK)

val GRAY = TeamType("gray", "<gray><bold>S <reset>".asMini(), NamedTextColor.GRAY, DyeColor.GRAY)

private val entries = listOf(RED, BLUE, GREEN, YELLOW, AQUA, WHITE, PINK, GRAY)

fun createTeam(nextFree: Int, uuid: UUID): BedWarsTeam {
val next: TeamType = entries[nextFree]
return BedWarsTeam(nextFree.toString() + next.baseName + uuid, next.prefix, next.color, next.bedColor)
}

}