-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 🕹️ Added Game and GameManger * 🕹️ canJoin gets multiple Players * 🕹️ added Game Registering * 🕹️ added joinGame * 🕹️ fixed games array, added createGame and simplified joinGame * 🕹️ cleanup and joining works :D * 🕹️ added abstract onJoin * 🕹️ added instances, uuid and eventNode * 🌎 Made Game only have one instance * 🕹️ made onLeave and playerGame Map * 🕹️ added unregistering for player in GameManger * 🕹️ there was a typo whole time?
- Loading branch information
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package cc.lixou.stracciatella.game | ||
|
||
import net.minestom.server.entity.Player | ||
import net.minestom.server.event.EventFilter | ||
import net.minestom.server.event.EventNode | ||
import net.minestom.server.event.trait.PlayerEvent | ||
import net.minestom.server.instance.Instance | ||
import world.cepi.kstom.Manager | ||
import java.util.* | ||
|
||
abstract class Game { | ||
|
||
protected val players = ArrayList<Player>() | ||
protected lateinit var instance: Instance | ||
protected val uuid: UUID = UUID.randomUUID() | ||
protected val eventNode = | ||
EventNode.type("${this.javaClass.simpleName}-${uuid}", EventFilter.INSTANCE) { event, instance -> | ||
if (event is PlayerEvent) { | ||
return@type players.contains(event.player) | ||
} else { | ||
return@type this.instance.uniqueId.equals(instance) | ||
} | ||
} | ||
|
||
init { | ||
Manager.globalEvent.addChild(eventNode) | ||
} | ||
|
||
/** | ||
* @param players the players that should join | ||
* @return false if game is full, then creates new instance | ||
*/ | ||
abstract fun canJoin(players: Array<Player>): Boolean | ||
|
||
abstract fun onJoin(player: Player) | ||
abstract fun onLeave(player: Player) | ||
|
||
fun addPlayers(players: Array<Player>) { | ||
this.players.addAll(players) | ||
players.forEach { onJoin(it) } | ||
} | ||
|
||
fun removePlayer(player: Player) { | ||
this.players.remove(player) | ||
onLeave(player) | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
src/main/kotlin/cc/lixou/stracciatella/game/GameManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package cc.lixou.stracciatella.game | ||
|
||
import net.minestom.server.entity.Player | ||
import java.util.concurrent.ConcurrentHashMap | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.full.primaryConstructor | ||
|
||
object GameManager { | ||
|
||
private val playerGame: ConcurrentHashMap<Player, Game> = ConcurrentHashMap() | ||
private val games: ConcurrentHashMap<KClass<out Game>, ArrayList<Game>> = ConcurrentHashMap() | ||
|
||
fun unregisterPlayer(player: Player) { | ||
playerGame[player]?.removePlayer(player) | ||
playerGame.remove(player) | ||
} | ||
|
||
inline fun <reified T : Game> joinGame(players: Array<Player>) = joinGame(T::class, players) | ||
|
||
fun <T : Game> joinGame(clazz: KClass<out T>, players: Array<Player>) { | ||
val game: Game = games[clazz]?.find { it.canJoin(players) } | ||
?: createGame(clazz) | ||
players.forEach { | ||
playerGame[it]?.removePlayer(it) | ||
playerGame[it] = game | ||
} | ||
game.addPlayers(players) | ||
} | ||
|
||
private fun <T : Game> createGame(clazz: KClass<out T>): Game { | ||
val game: T = clazz.primaryConstructor?.call() | ||
?: throw java.lang.IllegalArgumentException("Game ${clazz.simpleName} has wrong constructor as a Game") | ||
games[clazz]?.add(game) | ||
return game | ||
} | ||
|
||
inline fun <reified T : Game> registerGame() = registerGame(T::class) | ||
|
||
fun <T : Game> registerGame(clazz: KClass<out T>) { | ||
if (games.containsKey(clazz)) return | ||
games[clazz] = ArrayList() | ||
} | ||
|
||
} |