游戏玩法游戏时长实现
This commit is contained in:
parent
edb2c0e21f
commit
861dd7ecef
|
@ -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
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
}
|
|
@ -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()
|
||||
}
|
|
@ -1,7 +1,11 @@
|
|||
package time
|
||||
package offset
|
||||
|
||||
import "time"
|
||||
|
||||
func NewTime(offset time.Duration) *Time {
|
||||
return &Time{offset: offset}
|
||||
}
|
||||
|
||||
// Time 带有偏移量的时间
|
||||
type Time struct {
|
||||
offset time.Duration
|
Loading…
Reference in New Issue