fight

package
v0.5.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 3 Imported by: 0

README

Fight

Go doc

暂无介绍...

目录导航

列出了该 package 下所有的函数及类型定义,可通过目录导航进行快捷跳转 ❤️

展开 / 折叠目录导航

包级函数定义

函数名称 描述
NewTurnBased 创建一个新的回合制

类型定义

类型 名称 描述
STRUCT TurnBased 回合制
INTERFACE TurnBasedControllerInfo 暂无描述...
INTERFACE TurnBasedControllerAction 暂无描述...
STRUCT TurnBasedController 回合制控制器
STRUCT TurnBasedEntitySwitchEventHandler 暂无描述...

详情信息

func NewTurnBased[CampID comparable, EntityID comparable, Camp generic.IdR[CampID], Entity generic.IdR[EntityID]](calcNextTurnDuration func ( Camp, Entity) time.Duration) *TurnBased[CampID, EntityID, Camp, Entity]

创建一个新的回合制

  • calcNextTurnDuration 将返回下一次行动时间间隔,适用于按照速度计算下一次行动时间间隔的情况。当返回 0 时,将使用默认的行动超时时间

TurnBased STRUCT

回合制

type TurnBased[CampID comparable, EntityID comparable, Camp generic.IdR[CampID], Entity generic.IdR[EntityID]] struct {
	*turnBasedEvents[CampID, EntityID, Camp, Entity]
	controller           *TurnBasedController[CampID, EntityID, Camp, Entity]
	ticker               *time.Ticker
	actionWaitTicker     *time.Ticker
	actioning            bool
	actionMutex          sync.RWMutex
	entities             []Entity
	campRel              map[EntityID]Camp
	calcNextTurnDuration func(Camp, Entity) time.Duration
	actionTimeoutHandler func(Camp, Entity) time.Duration
	signal               chan signal
	round                int
	currCamp             Camp
	currEntity           Entity
	currActionTimeout    time.Duration
	currStart            time.Time
	closeMutex           sync.RWMutex
	closed               bool
}

func (*TurnBased) Close()

关闭回合制


func (*TurnBased) AddCamp(camp Camp, entity Entity, entities ...Entity)

添加阵营


func (*TurnBased) SetActionTimeout(actionTimeoutHandler func ( Camp, Entity) time.Duration)

设置行动超时时间处理函数

  • 默认情况下行动超时时间函数将始终返回 0

func (*TurnBased) Run()

运行

查看 / 收起单元测试

func TestTurnBased_Run(t *testing.T) {
	tbi := fight.NewTurnBased[string, string, *Camp, *Entity](func(camp *Camp, entity *Entity) time.Duration {
		return time.Duration(float64(time.Second) / entity.speed)
	})
	tbi.SetActionTimeout(func(camp *Camp, entity *Entity) time.Duration {
		return time.Second * 5
	})
	tbi.RegTurnBasedEntityActionTimeoutEvent(func(controller fight.TurnBasedControllerInfo[string, string, *Camp, *Entity]) {
		t.Log("时间", time.Now().Unix(), "回合", controller.GetRound(), "阵营", controller.GetCamp().GetId(), "实体", controller.GetEntity().GetId(), "超时")
	})
	tbi.RegTurnBasedRoundChangeEvent(func(controller fight.TurnBasedControllerInfo[string, string, *Camp, *Entity]) {
		t.Log("时间", time.Now().Unix(), "回合", controller.GetRound(), "回合切换")
	})
	tbi.RegTurnBasedEntitySwitchEvent(func(controller fight.TurnBasedControllerAction[string, string, *Camp, *Entity]) {
		switch controller.GetEntity().GetId() {
		case "1":
			go func() {
				time.Sleep(time.Second * 2)
				controller.Finish()
			}()
		case "2":
			controller.Refresh(time.Second)
		case "4":
			controller.Stop()
		}
		t.Log("时间", time.Now().Unix(), "回合", controller.GetRound(), "阵营", controller.GetCamp().GetId(), "实体", controller.GetEntity().GetId(), "开始行动")
	})
	tbi.AddCamp(&Camp{id: "1"}, &Entity{id: "1", speed: 1}, &Entity{id: "2", speed: 1})
	tbi.AddCamp(&Camp{id: "2"}, &Entity{id: "3", speed: 1}, &Entity{id: "4", speed: 1})
	tbi.Run()
}


TurnBasedControllerInfo INTERFACE
type TurnBasedControllerInfo[CampID comparable, EntityID comparable, Camp generic.IdR[CampID], Entity generic.IdR[EntityID]] interface {
	GetRound() int
	GetCamp() Camp
	GetEntity() Entity
	GetActionTimeoutDuration() time.Duration
	GetActionStartTime() time.Time
	GetActionEndTime() time.Time
	Stop()
}

TurnBasedControllerAction INTERFACE
type TurnBasedControllerAction[CampID comparable, EntityID comparable, Camp generic.IdR[CampID], Entity generic.IdR[EntityID]] interface {
	TurnBasedControllerInfo[CampID, EntityID, Camp, Entity]
	Finish()
	Refresh(duration time.Duration) time.Time
}

TurnBasedController STRUCT

回合制控制器

type TurnBasedController[CampID comparable, EntityID comparable, Camp generic.IdR[CampID], Entity generic.IdR[EntityID]] struct {
	tb *TurnBased[CampID, EntityID, Camp, Entity]
}

func (*TurnBasedController) GetRound() int

获取当前回合数


func (*TurnBasedController) GetCamp() Camp

获取当前操作阵营


func (*TurnBasedController) GetEntity() Entity

获取当前操作实体


func (*TurnBasedController) GetActionTimeoutDuration() time.Duration

获取当前行动超时时长


func (*TurnBasedController) GetActionStartTime() time.Time

获取当前行动开始时间


func (*TurnBasedController) GetActionEndTime() time.Time

获取当前行动结束时间


func (*TurnBasedController) Finish()

结束当前操作,将立即切换到下一个操作实体


func (*TurnBasedController) Stop()

在当前回合执行完毕后停止回合进程


func (*TurnBasedController) Refresh(duration time.Duration) time.Time

刷新当前操作实体的行动超时时间

  • 当不在行动阶段时,将返回 time.Time 零值

TurnBasedEntitySwitchEventHandler STRUCT
type TurnBasedEntitySwitchEventHandler[CampID comparable, EntityID comparable, Camp generic.IdR[CampID], Entity generic.IdR[EntityID]] func(controller TurnBasedControllerAction[CampID, EntityID, Camp, Entity])

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TurnBased added in v0.2.7

type TurnBased[CampID, EntityID comparable, Camp generic.IdR[CampID], Entity generic.IdR[EntityID]] struct {
	// contains filtered or unexported fields
}

TurnBased 回合制

func NewTurnBased added in v0.2.7

func NewTurnBased[CampID, EntityID comparable, Camp generic.IdR[CampID], Entity generic.IdR[EntityID]](calcNextTurnDuration func(Camp, Entity) time.Duration) *TurnBased[CampID, EntityID, Camp, Entity]

NewTurnBased 创建一个新的回合制

  • calcNextTurnDuration 将返回下一次行动时间间隔,适用于按照速度计算下一次行动时间间隔的情况。当返回 0 时,将使用默认的行动超时时间

func (*TurnBased[CampID, EntityID, Camp, Entity]) AddCamp added in v0.2.7

func (slf *TurnBased[CampID, EntityID, Camp, Entity]) AddCamp(camp Camp, entity Entity, entities ...Entity)

AddCamp 添加阵营

func (*TurnBased[CampID, EntityID, Camp, Entity]) Close added in v0.2.7

func (slf *TurnBased[CampID, EntityID, Camp, Entity]) Close()

Close 关闭回合制

func (TurnBased) OnTurnBasedEntityActionFinishEvent added in v0.2.7

func (slf TurnBased) OnTurnBasedEntityActionFinishEvent(controller TurnBasedControllerInfo[CampID, EntityID, Camp, Entity])

OnTurnBasedEntityActionFinishEvent 触发回合制实体行动结束事件

func (TurnBased) OnTurnBasedEntityActionSubmitEvent added in v0.2.7

func (slf TurnBased) OnTurnBasedEntityActionSubmitEvent(controller TurnBasedControllerInfo[CampID, EntityID, Camp, Entity])

OnTurnBasedEntityActionSubmitEvent 触发回合制实体行动提交事件

func (TurnBased) OnTurnBasedEntityActionTimeoutEvent added in v0.2.7

func (slf TurnBased) OnTurnBasedEntityActionTimeoutEvent(controller TurnBasedControllerInfo[CampID, EntityID, Camp, Entity])

OnTurnBasedEntityActionTimeoutEvent 触发回合制实体行动超时事件

func (TurnBased) OnTurnBasedEntitySwitchEvent added in v0.2.7

func (slf TurnBased) OnTurnBasedEntitySwitchEvent(controller TurnBasedControllerAction[CampID, EntityID, Camp, Entity])

OnTurnBasedEntitySwitchEvent 触发回合制实体切换事件

func (TurnBased) OnTurnBasedRoundChangeEvent added in v0.2.7

func (slf TurnBased) OnTurnBasedRoundChangeEvent(controller TurnBasedControllerInfo[CampID, EntityID, Camp, Entity])

OnTurnBasedRoundChangeEvent 触发回合制回合变更事件

func (TurnBased) RegTurnBasedEntityActionFinishEvent added in v0.2.7

func (slf TurnBased) RegTurnBasedEntityActionFinishEvent(handler TurnBasedEntityActionFinishEventHandler[CampID, EntityID, Camp, Entity])

RegTurnBasedEntityActionFinishEvent 注册回合制实体行动结束事件处理函数,该处理函数将在实体行动结束时触发

func (TurnBased) RegTurnBasedEntityActionSubmitEvent added in v0.2.7

func (slf TurnBased) RegTurnBasedEntityActionSubmitEvent(handler TurnBasedEntityActionSubmitEventHandler[CampID, EntityID, Camp, Entity])

RegTurnBasedEntityActionSubmitEvent 注册回合制实体行动提交事件处理函数,该处理函数将在实体行动提交时触发

  • 该事件将在实体以任意方式结束行动时触发,包括正常结束、超时结束等
  • 该事件会在原本的行动结束事件之后触发

func (TurnBased) RegTurnBasedEntityActionTimeoutEvent added in v0.2.7

func (slf TurnBased) RegTurnBasedEntityActionTimeoutEvent(handler TurnBasedEntityActionTimeoutEventHandler[CampID, EntityID, Camp, Entity])

RegTurnBasedEntityActionTimeoutEvent 注册回合制实体行动超时事件处理函数,该处理函数将在实体行动超时时触发

func (TurnBased) RegTurnBasedEntitySwitchEvent added in v0.2.7

func (slf TurnBased) RegTurnBasedEntitySwitchEvent(handler TurnBasedEntitySwitchEventHandler[CampID, EntityID, Camp, Entity])

RegTurnBasedEntitySwitchEvent 注册回合制实体切换事件处理函数,该处理函数将在切换到实体切换为操作时机时触发

  • 刚函数通常仅用于告知当前操作实体已经完成切换,适合做一些前置校验,但不应该在该函数中执行长时间阻塞操作
  • 操作计时将在该函数执行完毕后开始

场景:

  • 回合开始,如果该实体被标记为已死亡,则跳过该实体

func (TurnBased) RegTurnBasedRoundChangeEvent added in v0.2.7

func (slf TurnBased) RegTurnBasedRoundChangeEvent(handler TurnBasedRoundChangeEventHandler[CampID, EntityID, Camp, Entity])

RegTurnBasedRoundChangeEvent 注册回合制回合变更事件处理函数,该处理函数将在回合变更时触发

func (*TurnBased[CampID, EntityID, Camp, Entity]) Run added in v0.2.7

func (slf *TurnBased[CampID, EntityID, Camp, Entity]) Run()

Run 运行

func (*TurnBased[CampID, EntityID, Camp, Entity]) SetActionTimeout added in v0.2.7

func (slf *TurnBased[CampID, EntityID, Camp, Entity]) SetActionTimeout(actionTimeoutHandler func(Camp, Entity) time.Duration)

SetActionTimeout 设置行动超时时间处理函数

  • 默认情况下行动超时时间函数将始终返回 0

type TurnBasedController added in v0.2.7

type TurnBasedController[CampID, EntityID comparable, Camp generic.IdR[CampID], Entity generic.IdR[EntityID]] struct {
	// contains filtered or unexported fields
}

TurnBasedController 回合制控制器

func (*TurnBasedController[CampID, EntityID, Camp, Entity]) Finish added in v0.2.7

func (slf *TurnBasedController[CampID, EntityID, Camp, Entity]) Finish()

Finish 结束当前操作,将立即切换到下一个操作实体

func (*TurnBasedController[CampID, EntityID, Camp, Entity]) GetActionEndTime added in v0.2.7

func (slf *TurnBasedController[CampID, EntityID, Camp, Entity]) GetActionEndTime() time.Time

GetActionEndTime 获取当前行动结束时间

func (*TurnBasedController[CampID, EntityID, Camp, Entity]) GetActionStartTime added in v0.2.7

func (slf *TurnBasedController[CampID, EntityID, Camp, Entity]) GetActionStartTime() time.Time

GetActionStartTime 获取当前行动开始时间

func (*TurnBasedController[CampID, EntityID, Camp, Entity]) GetActionTimeoutDuration added in v0.2.7

func (slf *TurnBasedController[CampID, EntityID, Camp, Entity]) GetActionTimeoutDuration() time.Duration

GetActionTimeoutDuration 获取当前行动超时时长

func (*TurnBasedController[CampID, EntityID, Camp, Entity]) GetCamp added in v0.2.7

func (slf *TurnBasedController[CampID, EntityID, Camp, Entity]) GetCamp() Camp

GetCamp 获取当前操作阵营

func (*TurnBasedController[CampID, EntityID, Camp, Entity]) GetEntity added in v0.2.7

func (slf *TurnBasedController[CampID, EntityID, Camp, Entity]) GetEntity() Entity

GetEntity 获取当前操作实体

func (*TurnBasedController[CampID, EntityID, Camp, Entity]) GetRound added in v0.2.7

func (slf *TurnBasedController[CampID, EntityID, Camp, Entity]) GetRound() int

GetRound 获取当前回合数

func (*TurnBasedController[CampID, EntityID, Camp, Entity]) Refresh added in v0.2.7

func (slf *TurnBasedController[CampID, EntityID, Camp, Entity]) Refresh(duration time.Duration) time.Time

Refresh 刷新当前操作实体的行动超时时间

  • 当不在行动阶段时,将返回 time.Time 零值

func (*TurnBasedController[CampID, EntityID, Camp, Entity]) Stop added in v0.2.7

func (slf *TurnBasedController[CampID, EntityID, Camp, Entity]) Stop()

Stop 在当前回合执行完毕后停止回合进程

type TurnBasedControllerAction added in v0.2.7

type TurnBasedControllerAction[CampID, EntityID comparable, Camp generic.IdR[CampID], Entity generic.IdR[EntityID]] interface {
	TurnBasedControllerInfo[CampID, EntityID, Camp, Entity]
	// Finish 结束当前操作,将立即切换到下一个操作实体
	Finish()
	// Refresh 刷新当前操作实体的行动超时时间并返回新的行动超时时间
	Refresh(duration time.Duration) time.Time
}

type TurnBasedControllerInfo added in v0.2.7

type TurnBasedControllerInfo[CampID, EntityID comparable, Camp generic.IdR[CampID], Entity generic.IdR[EntityID]] interface {
	// GetRound 获取当前回合数
	GetRound() int
	// GetCamp 获取当前操作阵营
	GetCamp() Camp
	// GetEntity 获取当前操作实体
	GetEntity() Entity
	// GetActionTimeoutDuration 获取当前行动超时时长
	GetActionTimeoutDuration() time.Duration
	// GetActionStartTime 获取当前行动开始时间
	GetActionStartTime() time.Time
	// GetActionEndTime 获取当前行动结束时间
	GetActionEndTime() time.Time
	// Stop 在当前回合执行完毕后停止回合进程
	Stop()
}

type TurnBasedEntityActionFinishEventHandler added in v0.2.7

type TurnBasedEntityActionFinishEventHandler[CampID, EntityID comparable, Camp generic.IdR[CampID], Entity generic.IdR[EntityID]] func(controller TurnBasedControllerInfo[CampID, EntityID, Camp, Entity])

type TurnBasedEntityActionSubmitEventHandler added in v0.2.7

type TurnBasedEntityActionSubmitEventHandler[CampID, EntityID comparable, Camp generic.IdR[CampID], Entity generic.IdR[EntityID]] func(controller TurnBasedControllerInfo[CampID, EntityID, Camp, Entity])

type TurnBasedEntityActionTimeoutEventHandler added in v0.2.7

type TurnBasedEntityActionTimeoutEventHandler[CampID, EntityID comparable, Camp generic.IdR[CampID], Entity generic.IdR[EntityID]] func(controller TurnBasedControllerInfo[CampID, EntityID, Camp, Entity])

type TurnBasedEntitySwitchEventHandler added in v0.2.7

type TurnBasedEntitySwitchEventHandler[CampID, EntityID comparable, Camp generic.IdR[CampID], Entity generic.IdR[EntityID]] func(controller TurnBasedControllerAction[CampID, EntityID, Camp, Entity])

type TurnBasedRoundChangeEventHandler added in v0.2.7

type TurnBasedRoundChangeEventHandler[CampID, EntityID comparable, Camp generic.IdR[CampID], Entity generic.IdR[EntityID]] func(controller TurnBasedControllerInfo[CampID, EntityID, Camp, Entity])

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL