From d96ed58548ed87ec0e2730ed90aa32b11a2c3394 Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Wed, 2 Aug 2023 11:58:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20fight.Round=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E5=88=B7=E6=96=B0=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/fight/round.go | 35 ++++++++++++++++++++++++++++++----- game/fight/round_options.go | 8 ++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/game/fight/round.go b/game/fight/round.go index 0b6e9e9..4baa9f7 100644 --- a/game/fight/round.go +++ b/game/fight/round.go @@ -29,6 +29,7 @@ func NewRound[Data RoundData](data Data, camps []*RoundCamp, roundGameOverVerify round.camps[camp.campId] = camp.entities round.campOrder = append(round.campOrder, camp.campId) } + round.campOrderSource = slice.Copy(round.campOrder) for _, option := range options { option(round) } @@ -45,6 +46,7 @@ type Round[Data RoundData] struct { ticker *timer.Ticker // 计时器 camps map[int][]int // 阵营 campOrder []int // 阵营顺序 + campOrderSource []int // 阵营顺序源 actionTimeout time.Duration // 行动超时时间 actionTimeoutTickerName string // 行动超时计时器名称 actionTimeoutTime int64 // 行动超时时间 @@ -52,7 +54,6 @@ type Round[Data RoundData] struct { roundCount int // 回合计数 currentCamp int // 当前行动阵营 currentEntity int // 当前行动的阵营实体索引 - currentEndTime int64 // 当前行动结束时间 shareAction bool // 是否共享行动(同阵营共享行动时间) roundGameOverVerifyHandle RoundGameOverVerifyHandle[Data] // 游戏结束验证函数 campCounterclockwise bool // 是否阵营逆时针 @@ -63,6 +64,7 @@ type Round[Data RoundData] struct { gameOverEventHandles []RoundGameOverEvent[Data] // 游戏结束事件 changeEventHandles []RoundChangeEvent[Data] // 游戏回合变更事件 actionTimeoutEventHandles []RoundActionTimeoutEvent[Data] // 行动超时事件 + actionRefreshEventHandles []RoundActionRefreshEvent[Data] // 行动刷新事件 } // GetData 获取游戏数据 @@ -70,15 +72,32 @@ func (slf *Round[Data]) GetData() Data { return slf.data } -// Run 运行游戏 +// Start 开始回合 // - 将通过传入的 Camp 进行初始化,Camp 为一个二维数组,每个数组内的元素都是一个行动标识 -func (slf *Round[Data]) Run() { +func (slf *Round[Data]) Start() { slf.currentEntity = -1 slf.round = 1 slf.loop(false) } -// Release 释放游戏 +// Stop 停止回合 +// - 停止回合时仅会停止行动超时计时器,其他所有数据均会保留 +// - 若要重置回合,需要调用 Reset 方法 +func (slf *Round[Data]) Stop() { + slf.ticker.StopTimer(slf.actionTimeoutTickerName) +} + +// Reset 重置回合 +func (slf *Round[Data]) Reset() { + slf.ticker.StopTimer(slf.actionTimeoutTickerName) + slf.currentCamp = 0 + slf.currentEntity = 0 + slf.round = 0 + slf.roundCount = 0 + slf.campOrder = slice.Copy(slf.campOrderSource) +} + +// Release 释放资源 func (slf *Round[Data]) Release() { if slf.mark == slf.ticker.Mark() { slf.ticker.Release() @@ -137,7 +156,6 @@ func (slf *Round[Data]) SkipCamp() { // ActionRefresh 刷新行动超时时间 func (slf *Round[Data]) ActionRefresh() { - slf.currentEndTime = time.Now().Unix() slf.actionTimeoutTime = time.Now().Add(slf.actionTimeout).Unix() slf.ticker.After(slf.actionTimeoutTickerName, slf.actionTimeout, slf.loop, true) } @@ -224,3 +242,10 @@ func (slf *Round[Data]) OnActionTimeoutEvent() { handle(slf, slf.currentCamp, slf.camps[slf.currentCamp][slf.currentEntity]) } } + +// OnActionRefreshEvent 触发行动刷新事件 +func (slf *Round[Data]) OnActionRefreshEvent() { + for _, handle := range slf.actionRefreshEventHandles { + handle(slf, slf.currentCamp, slf.camps[slf.currentCamp][slf.currentEntity]) + } +} diff --git a/game/fight/round_options.go b/game/fight/round_options.go index 97b9683..03d010d 100644 --- a/game/fight/round_options.go +++ b/game/fight/round_options.go @@ -14,6 +14,7 @@ type ( RoundGameOverEvent[Data RoundData] func(round *Round[Data]) RoundChangeEvent[Data RoundData] func(round *Round[Data]) RoundActionTimeoutEvent[Data RoundData] func(round *Round[Data], campId, entity int) + RoundActionRefreshEvent[Data RoundData] func(round *Round[Data], campId, entity int) ) // WithRoundTicker 设置游戏的计时器 @@ -88,3 +89,10 @@ func WithRoundEntityCounterclockwise[Data RoundData]() RoundOption[Data] { round.entityCounterclockwise = true } } + +// WithRoundActionRefreshEvent 设置游戏的行动刷新事件 +func WithRoundActionRefreshEvent[Data RoundData](refreshEventHandle RoundActionRefreshEvent[Data]) RoundOption[Data] { + return func(round *Round[Data]) { + round.actionRefreshEventHandles = append(round.actionRefreshEventHandles, refreshEventHandle) + } +}