runtime

package
v0.3.67 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2024 License: LGPL-2.1 Imports: 18 Imported by: 40

Documentation

Overview

Package runtime 为运行时环境提供的一些接口与函数。

  • 运行时上下文(Context)
  • 实体管理器(EntityManager)
  • 实体树(EntityTree)
  • 帧(Frame)

Index

Constants

This section is empty.

Variables

View Source
var (
	EventEntityManagerAddEntityId                 = _entityManagerEventTabId + 0
	EventEntityManagerRemoveEntityId              = _entityManagerEventTabId + 1
	EventEntityManagerEntityAddComponentsId       = _entityManagerEventTabId + 2
	EventEntityManagerEntityRemoveComponentId     = _entityManagerEventTabId + 3
	EventEntityManagerEntityFirstTouchComponentId = _entityManagerEventTabId + 4
)
View Source
var (
	EventEntityTreeAddNodeId    = _entityTreeEventTabId + 0
	EventEntityTreeRemoveNodeId = _entityTreeEventTabId + 1
)
View Source
var (
	ErrContext       = fmt.Errorf("%w: runtime-context", exception.ErrCore) // 运行时上下文错误
	ErrEntityTree    = fmt.Errorf("%w: entity-tree", ErrContext)            // 实体树错误
	ErrEntityManager = fmt.Errorf("%w: entity-manager", ErrContext)         // 实体管理器错误
	ErrFrame         = fmt.Errorf("%w: frame", ErrContext)                  // 帧错误
)
View Source
var With _Option

Functions

func BindEventEntityManagerAddEntity added in v0.3.66

func BindEventEntityManagerAddEntity(auto iAutoEventEntityManagerAddEntity, subscriber EventEntityManagerAddEntity, priority ...int32) event.Hook

func BindEventEntityManagerEntityAddComponents added in v0.3.66

func BindEventEntityManagerEntityAddComponents(auto iAutoEventEntityManagerEntityAddComponents, subscriber EventEntityManagerEntityAddComponents, priority ...int32) event.Hook

func BindEventEntityManagerEntityFirstTouchComponent added in v0.3.66

func BindEventEntityManagerEntityFirstTouchComponent(auto iAutoEventEntityManagerEntityFirstTouchComponent, subscriber EventEntityManagerEntityFirstTouchComponent, priority ...int32) event.Hook

func BindEventEntityManagerEntityRemoveComponent added in v0.3.66

func BindEventEntityManagerEntityRemoveComponent(auto iAutoEventEntityManagerEntityRemoveComponent, subscriber EventEntityManagerEntityRemoveComponent, priority ...int32) event.Hook

func BindEventEntityManagerRemoveEntity added in v0.3.66

func BindEventEntityManagerRemoveEntity(auto iAutoEventEntityManagerRemoveEntity, subscriber EventEntityManagerRemoveEntity, priority ...int32) event.Hook

func BindEventEntityTreeAddNode added in v0.2.58

func BindEventEntityTreeAddNode(auto iAutoEventEntityTreeAddNode, subscriber EventEntityTreeAddNode, priority ...int32) event.Hook

func BindEventEntityTreeRemoveNode added in v0.2.58

func BindEventEntityTreeRemoveNode(auto iAutoEventEntityTreeRemoveNode, subscriber EventEntityTreeRemoveNode, priority ...int32) event.Hook

func UnsafeConcurrentContext deprecated added in v0.3.21

func UnsafeConcurrentContext(context ConcurrentContext) _UnsafeConcurrentContext

Deprecated: UnsafeConcurrentContext 访问多线程安全的上下文接口内部函数

func UnsafeContext deprecated

func UnsafeContext(ctx Context) _UnsafeContext

Deprecated: UnsafeContext 访问运行时上下文内部方法

func UnsafeFrame deprecated

func UnsafeFrame(frame Frame) _UnsafeFrame

Deprecated: UnsafeFrame 访问帧内部方法

Types

type ConcurrentContext

type ConcurrentContext interface {
	ictx.Context
	ictx.ConcurrentContextProvider
	async.Caller
	fmt.Stringer

	// GetName 获取名称
	GetName() string
	// GetId 获取运行时Id
	GetId() uid.Id
	// contains filtered or unexported methods
}

ConcurrentContext 多线程安全的运行时上下文接口

func Concurrent

Concurrent 获取多线程安全的运行时上下文

type ConcurrentContextProvider

type ConcurrentContextProvider = ictx.ConcurrentContextProvider

ConcurrentContextProvider 多线程安全的上下文提供者

type Context

type Context interface {
	ictx.Context
	ictx.CurrentContextProvider
	reinterpret.InstanceProvider
	extension.AddInProvider
	async.Caller
	GCCollector
	fmt.Stringer

	// GetName 获取名称
	GetName() string
	// GetId 获取运行时Id
	GetId() uid.Id
	// GetReflected 获取反射值
	GetReflected() reflect.Value
	// GetFrame 获取帧
	GetFrame() Frame
	// GetEntityManager 获取实体管理器
	GetEntityManager() EntityManager
	// GetEntityTree 获取实体树
	GetEntityTree() EntityTree
	// ActivateEvent 启用事件
	ActivateEvent(event event.IEventCtrl, recursion event.EventRecursion)
	// ManagedHooks 托管hook,在运行时停止时自动解绑定
	ManagedHooks(hooks ...event.Hook)
	// contains filtered or unexported methods
}

Context 运行时上下文接口

func Current

func Current(provider ictx.CurrentContextProvider) Context

Current 获取当前运行时上下文

func NewContext

func NewContext(svcCtx service.Context, settings ...option.Setting[ContextOptions]) Context

NewContext 创建运行时上下文

func UnsafeNewContext deprecated

func UnsafeNewContext(svcCtx service.Context, options ContextOptions) Context

Deprecated: UnsafeNewContext 内部创建运行时上下文

type ContextBehavior

type ContextBehavior struct {
	ictx.ContextBehavior
	// contains filtered or unexported fields
}

ContextBehavior 运行时上下文行为,在扩展运行时上下文能力时,匿名嵌入至运行时上下文结构体中

func (*ContextBehavior) ActivateEvent

func (ctx *ContextBehavior) ActivateEvent(event event.IEventCtrl, recursion event.EventRecursion)

ActivateEvent 启用事件

func (*ContextBehavior) Call

func (ctx *ContextBehavior) Call(fun generic.FuncVar0[any, async.Ret], args ...any) async.AsyncRet

Call 异步调用函数,有返回值。不会阻塞当前线程,会返回AsyncRet。

注意:
- 代码片段中的线程安全问题,如临界区访问、线程死锁等。
- 调用过程中的panic信息,均会转换为error返回。

func (*ContextBehavior) CallDelegate

func (ctx *ContextBehavior) CallDelegate(fun generic.DelegateVar0[any, async.Ret], args ...any) async.AsyncRet

CallDelegate 异步调用委托,有返回值。不会阻塞当前线程,会返回AsyncRet。

注意:
- 代码片段中的线程安全问题,如临界区访问、线程死锁等。
- 调用过程中的panic信息,均会转换为error返回。

func (*ContextBehavior) CallDelegateVoid added in v0.3.66

func (ctx *ContextBehavior) CallDelegateVoid(fun generic.DelegateVoidVar0[any], args ...any) async.AsyncRet

CallDelegateVoid 异步调用委托,无返回值。在运行时中。不会阻塞当前线程,会返回AsyncRet。

注意:
- 代码片段中的线程安全问题,如临界区访问、线程死锁等。
- 调用过程中的panic信息,均会转换为error返回。

func (*ContextBehavior) CallVoid

func (ctx *ContextBehavior) CallVoid(fun generic.ActionVar0[any], args ...any) async.AsyncRet

CallVoid 异步调用函数,无返回值。在运行时中。不会阻塞当前线程,会返回AsyncRet。

注意:
- 代码片段中的线程安全问题,如临界区访问、线程死锁等。
- 调用过程中的panic信息,均会转换为error返回。

func (*ContextBehavior) CollectGC

func (ctx *ContextBehavior) CollectGC(gc GC)

CollectGC 收集GC

func (*ContextBehavior) GetAddInManager added in v0.3.66

func (ctx *ContextBehavior) GetAddInManager() extension.AddInManager

GetAddInManager 获取插件管理器

func (*ContextBehavior) GetConcurrentContext

func (ctx *ContextBehavior) GetConcurrentContext() iface.Cache

GetConcurrentContext 获取多线程安全的上下文

func (*ContextBehavior) GetCurrentContext

func (ctx *ContextBehavior) GetCurrentContext() iface.Cache

GetCurrentContext 获取当前上下文

func (*ContextBehavior) GetEntityManager added in v0.3.66

func (ctx *ContextBehavior) GetEntityManager() EntityManager

GetEntityManager 获取实体管理器

func (*ContextBehavior) GetEntityTree added in v0.2.58

func (ctx *ContextBehavior) GetEntityTree() EntityTree

GetEntityTree 获取主实体树

func (*ContextBehavior) GetFrame

func (ctx *ContextBehavior) GetFrame() Frame

GetFrame 获取帧

func (*ContextBehavior) GetId

func (ctx *ContextBehavior) GetId() uid.Id

GetId 获取运行时Id

func (*ContextBehavior) GetInstanceFaceCache added in v0.3.34

func (ctx *ContextBehavior) GetInstanceFaceCache() iface.Cache

GetInstanceFaceCache 支持重新解释类型

func (*ContextBehavior) GetName

func (ctx *ContextBehavior) GetName() string

GetName 获取名称

func (*ContextBehavior) GetReflected added in v0.2.58

func (ctx *ContextBehavior) GetReflected() reflect.Value

GetReflected 获取反射值

func (*ContextBehavior) ManagedHooks added in v0.2.28

func (ctx *ContextBehavior) ManagedHooks(hooks ...event.Hook)

ManagedHooks 托管hook,在运行时停止时自动解绑定

func (*ContextBehavior) String

func (ctx *ContextBehavior) String() string

String implements fmt.Stringer

type ContextOptions

type ContextOptions struct {
	InstanceFace   iface.Face[Context]    // 实例,用于扩展运行时上下文能力
	Context        context.Context        // 父Context
	AutoRecover    bool                   // 是否开启panic时自动恢复
	ReportError    chan error             // panic时错误写入的error channel
	Name           string                 // 运行时名称
	PersistId      uid.Id                 // 运行时持久化Id
	AddInManager   extension.AddInManager // 插件管理器
	RunningHandler RunningHandler         // 运行状态变化处理器
}

ContextOptions 创建运行时上下文的所有选项

type CurrentContextProvider

type CurrentContextProvider = ictx.CurrentContextProvider

CurrentContextProvider 当前上下文提供者

type EntityManager added in v0.3.66

type EntityManager interface {
	ictx.CurrentContextProvider

	// AddEntity 添加实体
	AddEntity(entity ec.Entity) error
	// RemoveEntity 删除实体
	RemoveEntity(id uid.Id)
	// GetEntity 查询实体
	GetEntity(id uid.Id) (ec.Entity, bool)
	// ContainsEntity 实体是否存在
	ContainsEntity(id uid.Id) bool
	// RangeEntities 遍历所有实体
	RangeEntities(fun generic.Func1[ec.Entity, bool])
	// ReversedRangeEntities 反向遍历所有实体
	ReversedRangeEntities(fun generic.Func1[ec.Entity, bool])
	// FilterEntities 过滤并获取实体
	FilterEntities(fun generic.Func1[ec.Entity, bool]) []ec.Entity
	// GetEntities 获取所有实体
	GetEntities() []ec.Entity
	// CountEntities 获取实体数量
	CountEntities() int

	IEntityManagerEventTab
}

EntityManager 实体管理器接口

type EntityTree added in v0.2.58

type EntityTree interface {
	ictx.CurrentContextProvider

	// AddNode 新增实体节点,会向实体管理器添加实体
	AddNode(entity ec.Entity, parentId uid.Id) error
	// PruningNode 实体树节点剪枝
	PruningNode(entityId uid.Id)
	// RangeChildren 遍历子实体
	RangeChildren(entityId uid.Id, fun generic.Func1[ec.Entity, bool])
	// ReversedRangeChildren 反向遍历子实体
	ReversedRangeChildren(entityId uid.Id, fun generic.Func1[ec.Entity, bool])
	// FilterChildren 过滤并获取子实体
	FilterChildren(entityId uid.Id, fun generic.Func1[ec.Entity, bool]) []ec.Entity
	// GetChildren 获取所有子实体
	GetChildren(entityId uid.Id) []ec.Entity
	// CountChildren 获取子实体数量
	CountChildren(entityId uid.Id) int
	// IsTop 是否是顶层节点
	IsTop(entityId uid.Id) bool
	// ChangeParent 修改父实体
	ChangeParent(entityId, parentId uid.Id) error
	// GetParent 获取父实体
	GetParent(entityId uid.Id) (ec.Entity, bool)

	IEntityTreeEventTab
}

EntityTree 实体树接口

type EventEntityManagerAddEntity added in v0.3.66

type EventEntityManagerAddEntity interface {
	OnEntityManagerAddEntity(entityManager EntityManager, entity ec.Entity)
}

EventEntityManagerAddEntity 事件:实体管理器添加实体 +event-gen:export=0

type EventEntityManagerAddEntityHandler added in v0.3.66

type EventEntityManagerAddEntityHandler func(entityManager EntityManager, entity ec.Entity)

func HandleEventEntityManagerAddEntity added in v0.3.66

func HandleEventEntityManagerAddEntity(fun func(entityManager EntityManager, entity ec.Entity)) EventEntityManagerAddEntityHandler

func (EventEntityManagerAddEntityHandler) OnEntityManagerAddEntity added in v0.3.66

func (h EventEntityManagerAddEntityHandler) OnEntityManagerAddEntity(entityManager EntityManager, entity ec.Entity)

type EventEntityManagerEntityAddComponents added in v0.3.66

type EventEntityManagerEntityAddComponents interface {
	OnEntityManagerEntityAddComponents(entityManager EntityManager, entity ec.Entity, components []ec.Component)
}

EventEntityManagerEntityAddComponents 事件:实体管理器中的实体添加组件 +event-gen:export=0

type EventEntityManagerEntityAddComponentsHandler added in v0.3.66

type EventEntityManagerEntityAddComponentsHandler func(entityManager EntityManager, entity ec.Entity, components []ec.Component)

func HandleEventEntityManagerEntityAddComponents added in v0.3.66

func HandleEventEntityManagerEntityAddComponents(fun func(entityManager EntityManager, entity ec.Entity, components []ec.Component)) EventEntityManagerEntityAddComponentsHandler

func (EventEntityManagerEntityAddComponentsHandler) OnEntityManagerEntityAddComponents added in v0.3.66

func (h EventEntityManagerEntityAddComponentsHandler) OnEntityManagerEntityAddComponents(entityManager EntityManager, entity ec.Entity, components []ec.Component)

type EventEntityManagerEntityFirstTouchComponent added in v0.3.66

type EventEntityManagerEntityFirstTouchComponent interface {
	OnEntityManagerEntityFirstTouchComponent(entityManager EntityManager, entity ec.Entity, component ec.Component)
}

EventEntityManagerEntityFirstTouchComponent 事件:实体管理器中的实体首次访问组件 +event-gen:export=0

type EventEntityManagerEntityFirstTouchComponentHandler added in v0.3.66

type EventEntityManagerEntityFirstTouchComponentHandler func(entityManager EntityManager, entity ec.Entity, component ec.Component)

func HandleEventEntityManagerEntityFirstTouchComponent added in v0.3.66

func HandleEventEntityManagerEntityFirstTouchComponent(fun func(entityManager EntityManager, entity ec.Entity, component ec.Component)) EventEntityManagerEntityFirstTouchComponentHandler

func (EventEntityManagerEntityFirstTouchComponentHandler) OnEntityManagerEntityFirstTouchComponent added in v0.3.66

func (h EventEntityManagerEntityFirstTouchComponentHandler) OnEntityManagerEntityFirstTouchComponent(entityManager EntityManager, entity ec.Entity, component ec.Component)

type EventEntityManagerEntityRemoveComponent added in v0.3.66

type EventEntityManagerEntityRemoveComponent interface {
	OnEntityManagerEntityRemoveComponent(entityManager EntityManager, entity ec.Entity, component ec.Component)
}

EventEntityManagerEntityRemoveComponent 事件:实体管理器中的实体删除组件 +event-gen:export=0

type EventEntityManagerEntityRemoveComponentHandler added in v0.3.66

type EventEntityManagerEntityRemoveComponentHandler func(entityManager EntityManager, entity ec.Entity, component ec.Component)

func HandleEventEntityManagerEntityRemoveComponent added in v0.3.66

func HandleEventEntityManagerEntityRemoveComponent(fun func(entityManager EntityManager, entity ec.Entity, component ec.Component)) EventEntityManagerEntityRemoveComponentHandler

func (EventEntityManagerEntityRemoveComponentHandler) OnEntityManagerEntityRemoveComponent added in v0.3.66

func (h EventEntityManagerEntityRemoveComponentHandler) OnEntityManagerEntityRemoveComponent(entityManager EntityManager, entity ec.Entity, component ec.Component)

type EventEntityManagerRemoveEntity added in v0.3.66

type EventEntityManagerRemoveEntity interface {
	OnEntityManagerRemoveEntity(entityManager EntityManager, entity ec.Entity)
}

EventEntityManagerRemoveEntity 事件:实体管理器删除实体 +event-gen:export=0

type EventEntityManagerRemoveEntityHandler added in v0.3.66

type EventEntityManagerRemoveEntityHandler func(entityManager EntityManager, entity ec.Entity)

func HandleEventEntityManagerRemoveEntity added in v0.3.66

func HandleEventEntityManagerRemoveEntity(fun func(entityManager EntityManager, entity ec.Entity)) EventEntityManagerRemoveEntityHandler

func (EventEntityManagerRemoveEntityHandler) OnEntityManagerRemoveEntity added in v0.3.66

func (h EventEntityManagerRemoveEntityHandler) OnEntityManagerRemoveEntity(entityManager EntityManager, entity ec.Entity)

type EventEntityTreeAddNode added in v0.2.58

type EventEntityTreeAddNode interface {
	OnEntityTreeAddNode(entityTree EntityTree, parent, child ec.Entity)
}

EventEntityTreeAddNode 事件:新增实体树节点 +event-gen:export=0

type EventEntityTreeAddNodeHandler added in v0.2.58

type EventEntityTreeAddNodeHandler func(entityTree EntityTree, parent, child ec.Entity)

func HandleEventEntityTreeAddNode added in v0.2.58

func HandleEventEntityTreeAddNode(fun func(entityTree EntityTree, parent, child ec.Entity)) EventEntityTreeAddNodeHandler

func (EventEntityTreeAddNodeHandler) OnEntityTreeAddNode added in v0.2.58

func (h EventEntityTreeAddNodeHandler) OnEntityTreeAddNode(entityTree EntityTree, parent, child ec.Entity)

type EventEntityTreeRemoveNode added in v0.2.58

type EventEntityTreeRemoveNode interface {
	OnEntityTreeRemoveNode(entityTree EntityTree, parent, child ec.Entity)
}

EventEntityTreeRemoveNode 事件:删除实体树节点 +event-gen:export=0

type EventEntityTreeRemoveNodeHandler added in v0.2.58

type EventEntityTreeRemoveNodeHandler func(entityTree EntityTree, parent, child ec.Entity)

func HandleEventEntityTreeRemoveNode added in v0.2.58

func HandleEventEntityTreeRemoveNode(fun func(entityTree EntityTree, parent, child ec.Entity)) EventEntityTreeRemoveNodeHandler

func (EventEntityTreeRemoveNodeHandler) OnEntityTreeRemoveNode added in v0.2.58

func (h EventEntityTreeRemoveNodeHandler) OnEntityTreeRemoveNode(entityTree EntityTree, parent, child ec.Entity)

type Frame

type Frame interface {

	// GetTargetFPS 获取目标FPS
	GetTargetFPS() float32
	// GetCurFPS 获取当前FPS
	GetCurFPS() float32
	// GetTotalFrames 获取运行帧数上限
	GetTotalFrames() int64
	// GetCurFrames 获取当前帧数
	GetCurFrames() int64
	// GetRunningBeginTime 获取运行开始时间
	GetRunningBeginTime() time.Time
	// GetRunningElapseTime 获取运行持续时间
	GetRunningElapseTime() time.Duration
	// GetLoopBeginTime 获取当前帧循环开始时间(包含异步调用)
	GetLoopBeginTime() time.Time
	// GetLastLoopElapseTime 获取上一帧循环耗时(包含异步调用)
	GetLastLoopElapseTime() time.Duration
	// GetUpdateBeginTime 获取当前帧更新开始时间
	GetUpdateBeginTime() time.Time
	// GetLastUpdateElapseTime 获取上一次帧更新耗时
	GetLastUpdateElapseTime() time.Duration
	// contains filtered or unexported methods
}

Frame 帧,在运行时初始化时可以设置帧,用于设置运行时帧更新方式,在逻辑运行过程中可以在运行时上下文中获取帧信息

func NewFrame

func NewFrame(settings ...option.Setting[FrameOptions]) Frame

NewFrame 创建帧,在运行时初始化时可以设置帧,用于设置运行时帧更新方式,在逻辑运行过程中可以在运行时上下文中获取帧信息。

type FrameOptions

type FrameOptions struct {
	TargetFPS   float32 // 目标FPS
	TotalFrames int64   // 运行帧数上限
}

FrameOptions 帧的所有选项

type GC added in v0.2.53

type GC interface {
	// GC GC
	GC()
	// NeedGC 是否需要GC
	NeedGC() bool
}

GC GC接口

type GCCollector added in v0.2.53

type GCCollector interface {
	// CollectGC 收集GC
	CollectGC(gc GC)
}

GCCollector GC收集器接口

type IEntityManagerEventTab added in v0.3.66

type IEntityManagerEventTab interface {
	EventEntityManagerAddEntity() event.IEvent
	EventEntityManagerRemoveEntity() event.IEvent
	EventEntityManagerEntityAddComponents() event.IEvent
	EventEntityManagerEntityRemoveComponent() event.IEvent
	EventEntityManagerEntityFirstTouchComponent() event.IEvent
}

type IEntityTreeEventTab added in v0.3.48

type IEntityTreeEventTab interface {
	EventEntityTreeAddNode() event.IEvent
	EventEntityTreeRemoveNode() event.IEvent
}

type RunningHandler

type RunningHandler = generic.DelegateVoidVar2[Context, RunningState, any] // 运行状态变化处理器

type RunningState

type RunningState int32

RunningState 运行状态

const (
	RunningState_Birth             RunningState = iota // 出生
	RunningState_Starting                              // 开始启动
	RunningState_Started                               // 已启动
	RunningState_FrameLoopBegin                        // 帧循环开始
	RunningState_FrameUpdateBegin                      // 帧更新开始
	RunningState_FrameUpdateEnd                        // 帧更新结束
	RunningState_FrameLoopEnd                          // 帧循环结束
	RunningState_RunCallBegin                          // Call开始执行
	RunningState_RunCallEnd                            // Call结束执行
	RunningState_RunGCBegin                            // GC开始执行
	RunningState_RunGCEnd                              // GC结束执行
	RunningState_Terminating                           // 开始停止
	RunningState_Terminated                            // 已停止
	RunningState_AddInActivating                       // 开始激活插件
	RunningState_AddInActivated                        // 插件已激活
	RunningState_AddInDeactivating                     // 开始去激活插件
	RunningState_AddInDeactivated                      // 插件已去激活
)

func (RunningState) String

func (i RunningState) String() string

Jump to

Keyboard shortcuts

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