游戏玩法游戏时长实现

This commit is contained in:
kercylan98 2023-04-27 10:49:30 +08:00
parent edb2c0e21f
commit 861dd7ecef
5 changed files with 168 additions and 1 deletions

View File

@ -0,0 +1,67 @@
package builtin
import (
"minotaur/game"
"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
}

View File

@ -0,0 +1,30 @@
package builtin
import (
"fmt"
"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)
}
}

View File

@ -0,0 +1,49 @@
package main
import (
"log"
"minotaur/game"
"minotaur/game/builtin"
"sync"
"time"
)
func NewGame() *Game {
gameplay := builtin.NewGameplay()
gameplayOver := builtin.NewGameplayOver()
return &Game{
GameplayTime: builtin.NewGameplayTime(gameplay, gameplayOver),
}
}
type Game struct {
game.GameplayTime
wait sync.WaitGroup
}
func (slf *Game) init() error {
slf.wait.Add(1)
return nil
}
func (slf *Game) onGameStart(startTime time.Time) {
log.Println("游戏开始")
slf.SetEndTime(startTime.Add(3 * time.Second))
}
func (slf *Game) onGameOver() {
log.Println("游戏结束")
slf.wait.Done()
}
func main() {
g := NewGame()
g.RegGameplayStartEvent(g.onGameStart)
g.RegGameplayOverEvent(g.onGameOver)
if err := g.GameStart(g.init); err != nil {
panic(err)
}
g.wait.Wait()
}

17
game/gameplay_time.go Normal file
View File

@ -0,0 +1,17 @@
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()
}

View File

@ -1,7 +1,11 @@
package time package offset
import "time" import "time"
func NewTime(offset time.Duration) *Time {
return &Time{offset: offset}
}
// Time 带有偏移量的时间 // Time 带有偏移量的时间
type Time struct { type Time struct {
offset time.Duration offset time.Duration