revert: 移除 gameplay,设计不合理
This commit is contained in:
parent
87f26dd394
commit
41ea022261
|
@ -1,87 +0,0 @@
|
|||
package builtin
|
||||
|
||||
import (
|
||||
"github.com/kercylan98/minotaur/game"
|
||||
"github.com/kercylan98/minotaur/utils/offset"
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewGameplay() *Gameplay {
|
||||
return &Gameplay{
|
||||
Time: offset.NewTime(0),
|
||||
}
|
||||
}
|
||||
|
||||
type Gameplay struct {
|
||||
*offset.Time
|
||||
startTime time.Time
|
||||
|
||||
gameplayStartEventHandles []game.GameplayStartEventHandle
|
||||
gameplayTimeChangeEventHandles []game.GameplayTimeChangeEventHandle
|
||||
gameplayReleaseEventHandles []game.GameplayReleaseEventHandle
|
||||
}
|
||||
|
||||
func (slf *Gameplay) GameStart(handle func() error) error {
|
||||
if handle != nil {
|
||||
if err := handle(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
slf.startTime = slf.Time.Now()
|
||||
slf.OnGameplayStartEvent()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *Gameplay) GetTime() *offset.Time {
|
||||
return slf.Time
|
||||
}
|
||||
|
||||
func (slf *Gameplay) GetCurrentTime() time.Time {
|
||||
return slf.Time.Now()
|
||||
}
|
||||
|
||||
func (slf *Gameplay) SetTimeOffset(offset time.Duration) {
|
||||
slf.Time.SetOffset(offset)
|
||||
slf.OnGameplayTimeChangeEvent()
|
||||
}
|
||||
|
||||
func (slf *Gameplay) Release() {
|
||||
slf.OnGameplayReleaseEvent()
|
||||
slf.gameplayStartEventHandles = nil
|
||||
slf.gameplayTimeChangeEventHandles = nil
|
||||
slf.gameplayReleaseEventHandles = nil
|
||||
}
|
||||
|
||||
func (slf *Gameplay) RegGameplayStartEvent(handle game.GameplayStartEventHandle) {
|
||||
slf.gameplayStartEventHandles = append(slf.gameplayStartEventHandles, handle)
|
||||
}
|
||||
|
||||
func (slf *Gameplay) OnGameplayStartEvent() {
|
||||
for _, handle := range slf.gameplayStartEventHandles {
|
||||
handle(slf.startTime)
|
||||
}
|
||||
}
|
||||
|
||||
func (slf *Gameplay) RegGameplayTimeChangeEvent(handle game.GameplayTimeChangeEventHandle) {
|
||||
slf.gameplayTimeChangeEventHandles = append(slf.gameplayTimeChangeEventHandles, handle)
|
||||
}
|
||||
|
||||
func (slf *Gameplay) OnGameplayTimeChangeEvent() {
|
||||
if len(slf.gameplayTimeChangeEventHandles) == 0 {
|
||||
return
|
||||
}
|
||||
current := slf.Time.Now()
|
||||
for _, handle := range slf.gameplayTimeChangeEventHandles {
|
||||
handle(current)
|
||||
}
|
||||
}
|
||||
|
||||
func (slf *Gameplay) RegGameplayReleaseEvent(handle game.GameplayReleaseEventHandle) {
|
||||
slf.gameplayReleaseEventHandles = append(slf.gameplayReleaseEventHandles, handle)
|
||||
}
|
||||
|
||||
func (slf *Gameplay) OnGameplayReleaseEvent() {
|
||||
for _, handle := range slf.gameplayReleaseEventHandles {
|
||||
handle()
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
package builtin
|
||||
|
||||
import (
|
||||
"github.com/kercylan98/minotaur/game"
|
||||
)
|
||||
|
||||
func NewGameplayOver() *GameplayOver {
|
||||
return &GameplayOver{}
|
||||
}
|
||||
|
||||
type GameplayOver struct {
|
||||
gameplayOverEventHandles []game.GameplayOverEventHandle
|
||||
}
|
||||
|
||||
func (slf *GameplayOver) GameOver() {
|
||||
slf.OnGameplayOverEvent()
|
||||
}
|
||||
|
||||
func (slf *GameplayOver) RegGameplayOverEvent(handle game.GameplayOverEventHandle) {
|
||||
slf.gameplayOverEventHandles = append(slf.gameplayOverEventHandles, handle)
|
||||
}
|
||||
|
||||
func (slf *GameplayOver) OnGameplayOverEvent() {
|
||||
for _, handle := range slf.gameplayOverEventHandles {
|
||||
handle()
|
||||
}
|
||||
}
|
|
@ -1,67 +0,0 @@
|
|||
package builtin
|
||||
|
||||
import (
|
||||
"github.com/kercylan98/minotaur/game"
|
||||
"github.com/kercylan98/minotaur/utils/timer"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
gameplayTimeTickerEndTime = "GameplayTimeTickerEndTime"
|
||||
)
|
||||
|
||||
func NewGameplayTime(gameplay game.Gameplay, gameplayOver game.GameplayOver, options ...GameplayTimeOption) *GameplayTime {
|
||||
gameplayTime := &GameplayTime{
|
||||
Gameplay: gameplay,
|
||||
GameplayOver: gameplayOver,
|
||||
}
|
||||
for _, option := range options {
|
||||
option(gameplayTime)
|
||||
}
|
||||
if gameplayTime.ticker == nil {
|
||||
gameplayTime.afterName = gameplayTimeTickerEndTime
|
||||
gameplayTime.ticker = timer.GetTicker(10)
|
||||
}
|
||||
return gameplayTime
|
||||
}
|
||||
|
||||
type GameplayTime struct {
|
||||
game.Gameplay
|
||||
game.GameplayOver
|
||||
id int64
|
||||
afterName string
|
||||
endTime time.Time
|
||||
ticker *timer.Ticker
|
||||
}
|
||||
|
||||
func (slf *GameplayTime) GetEndTime() time.Time {
|
||||
return slf.endTime
|
||||
}
|
||||
|
||||
func (slf *GameplayTime) SetEndTime(t time.Time) {
|
||||
compare := t.Compare(slf.endTime)
|
||||
if compare == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
slf.ticker.StopTimer(slf.afterName)
|
||||
current := slf.GetCurrentTime()
|
||||
if compare < 0 && t.Compare(current) < 0 {
|
||||
slf.GameplayOver.GameOver()
|
||||
return
|
||||
}
|
||||
|
||||
slf.endTime = t
|
||||
slf.ticker.After(slf.afterName, slf.endTime.Sub(current), func() {
|
||||
slf.GameplayOver.GameOver()
|
||||
})
|
||||
}
|
||||
|
||||
func (slf *GameplayTime) ChangeEndTime(d time.Duration) {
|
||||
slf.SetEndTime(slf.endTime.Add(d))
|
||||
}
|
||||
|
||||
func (slf *GameplayTime) Release() {
|
||||
slf.ticker.Release()
|
||||
slf.ticker = nil
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
package builtin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/kercylan98/minotaur/utils/timer"
|
||||
)
|
||||
|
||||
type GameplayTimeOption func(time *GameplayTime)
|
||||
|
||||
func WithGameplayTimeWheelSize(size int) GameplayTimeOption {
|
||||
return func(time *GameplayTime) {
|
||||
if time.ticker != nil {
|
||||
time.ticker.Release()
|
||||
time.id = 0
|
||||
}
|
||||
time.ticker = timer.GetTicker(size)
|
||||
time.afterName = gameplayTimeTickerEndTime
|
||||
}
|
||||
}
|
||||
|
||||
func WithGameplayTimeTicker(id int64, ticker *timer.Ticker) GameplayTimeOption {
|
||||
return func(time *GameplayTime) {
|
||||
if time.ticker != nil {
|
||||
time.ticker.Release()
|
||||
}
|
||||
time.id = id
|
||||
time.ticker = ticker
|
||||
time.afterName = fmt.Sprintf("%s_%d", gameplayTimeTickerEndTime, id)
|
||||
}
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
package game
|
||||
|
||||
import (
|
||||
"github.com/kercylan98/minotaur/utils/offset"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Gameplay 游戏玩法
|
||||
type Gameplay interface {
|
||||
// GameStart 游戏玩法开始
|
||||
GameStart(handle func() error) error
|
||||
// GetTime 获取游戏玩法的时间偏移
|
||||
GetTime() *offset.Time
|
||||
// GetCurrentTime 获取玩法当前存在偏移的时间
|
||||
GetCurrentTime() time.Time
|
||||
// SetTimeOffset 设置玩法时间偏移
|
||||
SetTimeOffset(offset time.Duration)
|
||||
// Release 释放游戏玩法资源
|
||||
Release()
|
||||
|
||||
// RegGameplayStartEvent 在游戏玩法开始时将立即执行被注册的事件处理函数
|
||||
RegGameplayStartEvent(handle GameplayStartEventHandle)
|
||||
OnGameplayStartEvent()
|
||||
// RegGameplayTimeChangeEvent 游戏玩法的时间被改变(非自然流逝)时将立刻执行被注册的事件处理函数
|
||||
RegGameplayTimeChangeEvent(handle GameplayTimeChangeEventHandle)
|
||||
OnGameplayTimeChangeEvent()
|
||||
// RegGameplayReleaseEvent 游戏玩法资源被释放前将立即执行被注册的事件处理函数
|
||||
RegGameplayReleaseEvent(handle GameplayReleaseEventHandle)
|
||||
OnGameplayReleaseEvent()
|
||||
}
|
||||
|
||||
type (
|
||||
GameplayStartEventHandle func(startTime time.Time)
|
||||
GameplayTimeChangeEventHandle func(current time.Time)
|
||||
GameplayReleaseEventHandle func()
|
||||
)
|
|
@ -1,14 +0,0 @@
|
|||
package game
|
||||
|
||||
type GameplayOver interface {
|
||||
// GameOver 游戏玩法结束
|
||||
GameOver()
|
||||
|
||||
// RegGameplayOverEvent 游戏玩法结束时将立即调用被注册的事件处理函数
|
||||
RegGameplayOverEvent(handle GameplayOverEventHandle)
|
||||
OnGameplayOverEvent()
|
||||
}
|
||||
|
||||
type (
|
||||
GameplayOverEventHandle func()
|
||||
)
|
|
@ -1,17 +0,0 @@
|
|||
package game
|
||||
|
||||
import "time"
|
||||
|
||||
// GameplayTime 为游戏玩法添加游戏时长的特性
|
||||
type GameplayTime interface {
|
||||
Gameplay
|
||||
GameplayOver
|
||||
// GetEndTime 获取游戏结束时间
|
||||
GetEndTime() time.Time
|
||||
// SetEndTime 设置游戏结束时间
|
||||
SetEndTime(t time.Time)
|
||||
// ChangeEndTime 通过相对时间的方式改变游戏结束时间
|
||||
ChangeEndTime(d time.Duration)
|
||||
// Release 释放资源
|
||||
Release()
|
||||
}
|
Loading…
Reference in New Issue