fix rule initialize

add dynamic rule when the wall falls
This commit is contained in:
tony_all 2023-06-07 15:47:36 +08:00
parent 45882c8cb6
commit ed6f40b462
3 changed files with 14 additions and 11 deletions

View File

@ -15,7 +15,7 @@ class Game(
val scoreboard: GameScoreboard = GameScoreboard(this) val scoreboard: GameScoreboard = GameScoreboard(this)
val timer: GameTimer = GameTimer(this) val timer: GameTimer = GameTimer(this)
val players = ArrayList<Player>() val players = ArrayList<Player>()
val rule = GamePlaceBreakRule(this) val placeBreakRule = GamePlaceBreakRule(this)
var state: GameState = GameState.WAITING var state: GameState = GameState.WAITING
private fun autoJoinTeam() { private fun autoJoinTeam() {
@ -48,6 +48,7 @@ class Game(
fun start() { fun start() {
debug("game ${map.name} started.") debug("game ${map.name} started.")
placeBreakRule.defaultRule()
state = GameState.START state = GameState.START
timer.startTimer() timer.startTimer()
@ -57,6 +58,9 @@ class Game(
map.wall.forBlocksInArea().forEach { map.wall.forBlocksInArea().forEach {
it.block.type = Material.AIR it.block.type = Material.AIR
} }
placeBreakRule.addRule { _, _, loc ->
map.wall.contains(loc)
}
} }
} }
autoJoinTeam() autoJoinTeam()

View File

@ -10,10 +10,10 @@ class GamePlaceBreakRule(val game: Game) {
BREAK BREAK
} }
private val rule = ArrayList<(Player, ActionType, Location) -> Boolean>() private val rules = ArrayList<(Player, ActionType, Location) -> Boolean>()
fun matchRule(player: Player, actionType: ActionType, location: Location): Boolean { fun matchRule(player: Player, actionType: ActionType, location: Location): Boolean {
// match rules // match rules
rule.forEach { rules.forEach {
if (it(player, actionType, location)) return true if (it(player, actionType, location)) return true
} }
@ -21,13 +21,13 @@ class GamePlaceBreakRule(val game: Game) {
return false return false
} }
fun addRule(rule: (Player, Location) -> Boolean) { fun addRule(rule: (player: Player, type: ActionType, loc: Location) -> Boolean) {
rules.add(rule)
} }
fun defaultRule(game: Game) { fun defaultRule() {
// allow mine // allow mine
rule.add { _, _, loc -> rules.add { _, _, loc ->
game.map.teams.flatMap { game.map.teams.flatMap {
it.sides + it.mine it.sides + it.mine
}.map { }.map {
@ -36,7 +36,7 @@ class GamePlaceBreakRule(val game: Game) {
} }
// allow enemy wall // allow enemy wall
rule.add { player, type, loc -> rules.add { player, type, loc ->
val team = player.team ?: return@add false val team = player.team ?: return@add false
val result = game.teams.filterNot { it == team } val result = game.teams.filterNot { it == team }
.map { team.teamInfo.wall.containsBlock(loc) } .map { team.teamInfo.wall.containsBlock(loc) }

View File

@ -1,7 +1,6 @@
package cc.maxmc.blastingcrisis.listener package cc.maxmc.blastingcrisis.listener
import cc.maxmc.blastingcrisis.game.GamePlaceBreakRule import cc.maxmc.blastingcrisis.game.GamePlaceBreakRule
import cc.maxmc.blastingcrisis.game.team
import cc.maxmc.blastingcrisis.misc.Area import cc.maxmc.blastingcrisis.misc.Area
import cc.maxmc.blastingcrisis.misc.GameManager import cc.maxmc.blastingcrisis.misc.GameManager
import cc.maxmc.blastingcrisis.misc.debug import cc.maxmc.blastingcrisis.misc.debug
@ -49,7 +48,7 @@ object GameListener {
fun onBreak(breakEvent: BlockBreakEvent) { fun onBreak(breakEvent: BlockBreakEvent) {
val loc = breakEvent.block.location ?: return val loc = breakEvent.block.location ?: return
val player = breakEvent.player ?: return val player = breakEvent.player ?: return
if (GameManager.currentGame.rule.matchRule(player, GamePlaceBreakRule.ActionType.BREAK, loc)) return if (GameManager.currentGame.placeBreakRule.matchRule(player, GamePlaceBreakRule.ActionType.BREAK, loc)) return
breakEvent.isCancelled = true breakEvent.isCancelled = true
player.sendLang("game_cant_break_block") player.sendLang("game_cant_break_block")
} }
@ -58,7 +57,7 @@ object GameListener {
fun onPlace(placeEvent: BlockPlaceEvent) { fun onPlace(placeEvent: BlockPlaceEvent) {
val loc = placeEvent.block.location ?: return val loc = placeEvent.block.location ?: return
val player = placeEvent.player ?: return val player = placeEvent.player ?: return
if (GameManager.currentGame.rule.matchRule(player, GamePlaceBreakRule.ActionType.PLACE, loc)) return if (GameManager.currentGame.placeBreakRule.matchRule(player, GamePlaceBreakRule.ActionType.PLACE, loc)) return
placeEvent.isCancelled = true placeEvent.isCancelled = true
player.sendLang("game_cant_place_block") player.sendLang("game_cant_place_block")
} }