游戏世界接口优化
This commit is contained in:
parent
f084613aa9
commit
f8c9014baf
|
@ -37,9 +37,9 @@ type World[PlayerID comparable, Player game.Player[PlayerID]] struct {
|
||||||
|
|
||||||
playerJoinWorldEventHandles []game.PlayerJoinWorldEventHandle[PlayerID, Player]
|
playerJoinWorldEventHandles []game.PlayerJoinWorldEventHandle[PlayerID, Player]
|
||||||
playerLeaveWorldEventHandles []game.PlayerLeaveWorldEventHandle[PlayerID, Player]
|
playerLeaveWorldEventHandles []game.PlayerLeaveWorldEventHandle[PlayerID, Player]
|
||||||
actorGeneratedEventHandles []game.ActorGeneratedEventHandle
|
actorGeneratedEventHandles []game.ActorGeneratedEventHandle[PlayerID, Player]
|
||||||
actorAnnihilationEventHandles []game.ActorAnnihilationEventHandle
|
actorAnnihilationEventHandles []game.ActorAnnihilationEventHandle[PlayerID, Player]
|
||||||
actorOwnerChangeEventHandles []game.ActorOwnerChangeEventHandle[PlayerID]
|
actorOwnerChangeEventHandles []game.ActorOwnerChangeEventHandle[PlayerID, Player]
|
||||||
worldResetEventHandles []game.WorldResetEventHandle[PlayerID, Player]
|
worldResetEventHandles []game.WorldResetEventHandle[PlayerID, Player]
|
||||||
worldReleasedEventHandles []game.WorldReleaseEventHandle[PlayerID, Player]
|
worldReleasedEventHandles []game.WorldReleaseEventHandle[PlayerID, Player]
|
||||||
|
|
||||||
|
@ -62,16 +62,16 @@ func (slf *World[PlayerID, Player]) GetPlayer(id PlayerID) Player {
|
||||||
return slf.players.Get(id)
|
return slf.players.Get(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *World[PlayerID, Player]) GetPlayers() map[PlayerID]Player {
|
func (slf *World[PlayerID, Player]) GetPlayers() synchronization.MapReadonly[PlayerID, Player] {
|
||||||
return slf.players.Map()
|
return slf.players
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *World[PlayerID, Player]) GetActor(guid int64) game.Actor {
|
func (slf *World[PlayerID, Player]) GetActor(guid int64) game.Actor {
|
||||||
return slf.actors.Get(guid)
|
return slf.actors.Get(guid)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *World[PlayerID, Player]) GetActors() map[int64]game.Actor {
|
func (slf *World[PlayerID, Player]) GetActors() synchronization.MapReadonly[int64, game.Actor] {
|
||||||
return slf.actors.Map()
|
return slf.actors
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *World[PlayerID, Player]) GetPlayerActor(id PlayerID, guid int64) game.Actor {
|
func (slf *World[PlayerID, Player]) GetPlayerActor(id PlayerID, guid int64) game.Actor {
|
||||||
|
@ -81,8 +81,8 @@ func (slf *World[PlayerID, Player]) GetPlayerActor(id PlayerID, guid int64) game
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *World[PlayerID, Player]) GetPlayerActors(id PlayerID) map[int64]game.Actor {
|
func (slf *World[PlayerID, Player]) GetPlayerActors(id PlayerID) synchronization.MapReadonly[int64, game.Actor] {
|
||||||
return slf.playerActors.Get(id).Map()
|
return slf.playerActors.Get(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *World[PlayerID, Player]) IsExistPlayer(id PlayerID) bool {
|
func (slf *World[PlayerID, Player]) IsExistPlayer(id PlayerID) bool {
|
||||||
|
@ -236,7 +236,7 @@ func (slf *World[PlayerID, Player]) RegPlayerJoinWorldEvent(handle game.PlayerJo
|
||||||
|
|
||||||
func (slf *World[PlayerID, Player]) OnPlayerJoinWorldEvent(player Player) {
|
func (slf *World[PlayerID, Player]) OnPlayerJoinWorldEvent(player Player) {
|
||||||
for _, handle := range slf.playerJoinWorldEventHandles {
|
for _, handle := range slf.playerJoinWorldEventHandles {
|
||||||
handle(player)
|
handle(slf, player)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -246,36 +246,36 @@ func (slf *World[PlayerID, Player]) RegPlayerLeaveWorldEvent(handle game.PlayerL
|
||||||
|
|
||||||
func (slf *World[PlayerID, Player]) OnPlayerLeaveWorldEvent(player Player) {
|
func (slf *World[PlayerID, Player]) OnPlayerLeaveWorldEvent(player Player) {
|
||||||
for _, handle := range slf.playerLeaveWorldEventHandles {
|
for _, handle := range slf.playerLeaveWorldEventHandles {
|
||||||
handle(player)
|
handle(slf, player)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *World[PlayerID, Player]) RegActorGeneratedEvent(handle game.ActorGeneratedEventHandle) {
|
func (slf *World[PlayerID, Player]) RegActorGeneratedEvent(handle game.ActorGeneratedEventHandle[PlayerID, Player]) {
|
||||||
slf.actorGeneratedEventHandles = append(slf.actorGeneratedEventHandles, handle)
|
slf.actorGeneratedEventHandles = append(slf.actorGeneratedEventHandles, handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *World[PlayerID, Player]) OnActorGeneratedEvent(actor game.Actor) {
|
func (slf *World[PlayerID, Player]) OnActorGeneratedEvent(actor game.Actor) {
|
||||||
for _, handle := range slf.actorGeneratedEventHandles {
|
for _, handle := range slf.actorGeneratedEventHandles {
|
||||||
handle(actor)
|
handle(slf, actor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *World[PlayerID, Player]) RegActorAnnihilationEvent(handle game.ActorAnnihilationEventHandle) {
|
func (slf *World[PlayerID, Player]) RegActorAnnihilationEvent(handle game.ActorAnnihilationEventHandle[PlayerID, Player]) {
|
||||||
slf.actorAnnihilationEventHandles = append(slf.actorAnnihilationEventHandles, handle)
|
slf.actorAnnihilationEventHandles = append(slf.actorAnnihilationEventHandles, handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *World[PlayerID, Player]) OnActorAnnihilationEvent(actor game.Actor) {
|
func (slf *World[PlayerID, Player]) OnActorAnnihilationEvent(actor game.Actor) {
|
||||||
for _, handle := range slf.actorAnnihilationEventHandles {
|
for _, handle := range slf.actorAnnihilationEventHandles {
|
||||||
handle(actor)
|
handle(slf, actor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *World[PlayerID, Player]) RegActorOwnerChangeEvent(handle game.ActorOwnerChangeEventHandle[PlayerID]) {
|
func (slf *World[PlayerID, Player]) RegActorOwnerChangeEvent(handle game.ActorOwnerChangeEventHandle[PlayerID, Player]) {
|
||||||
slf.actorOwnerChangeEventHandles = append(slf.actorOwnerChangeEventHandles, handle)
|
slf.actorOwnerChangeEventHandles = append(slf.actorOwnerChangeEventHandles, handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *World[PlayerID, Player]) OnActorOwnerChangeEvent(actor game.Actor, old, new PlayerID, isolated bool) {
|
func (slf *World[PlayerID, Player]) OnActorOwnerChangeEvent(actor game.Actor, old, new PlayerID, isolated bool) {
|
||||||
for _, handle := range slf.actorOwnerChangeEventHandles {
|
for _, handle := range slf.actorOwnerChangeEventHandles {
|
||||||
handle(actor, old, new, isolated)
|
handle(slf, actor, old, new, isolated)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package game
|
package game
|
||||||
|
|
||||||
|
import "minotaur/utils/synchronization"
|
||||||
|
|
||||||
// World 游戏世界接口定义
|
// World 游戏世界接口定义
|
||||||
type World[PlayerID comparable, P Player[PlayerID]] interface {
|
type World[PlayerID comparable, P Player[PlayerID]] interface {
|
||||||
// GetGuid 获取世界的唯一标识符
|
// GetGuid 获取世界的唯一标识符
|
||||||
|
@ -11,15 +13,15 @@ type World[PlayerID comparable, P Player[PlayerID]] interface {
|
||||||
// GetPlayer 根据玩家id获取玩家
|
// GetPlayer 根据玩家id获取玩家
|
||||||
GetPlayer(id PlayerID) P
|
GetPlayer(id PlayerID) P
|
||||||
// GetPlayers 获取世界中的所有玩家
|
// GetPlayers 获取世界中的所有玩家
|
||||||
GetPlayers() map[PlayerID]P
|
GetPlayers() synchronization.MapReadonly[PlayerID, P]
|
||||||
// GetActor 根据唯一标识符获取世界中的游戏对象
|
// GetActor 根据唯一标识符获取世界中的游戏对象
|
||||||
GetActor(guid int64) Actor
|
GetActor(guid int64) Actor
|
||||||
// GetActors 获取世界中的所有游戏对象
|
// GetActors 获取世界中的所有游戏对象
|
||||||
GetActors() map[int64]Actor
|
GetActors() synchronization.MapReadonly[int64, Actor]
|
||||||
// GetPlayerActor 获取游戏世界中归属特定玩家的特定游戏对象
|
// GetPlayerActor 获取游戏世界中归属特定玩家的特定游戏对象
|
||||||
GetPlayerActor(id PlayerID, guid int64) Actor
|
GetPlayerActor(id PlayerID, guid int64) Actor
|
||||||
// GetPlayerActors 获取游戏世界中归属特定玩家的所有游戏对象
|
// GetPlayerActors 获取游戏世界中归属特定玩家的所有游戏对象
|
||||||
GetPlayerActors(id PlayerID) map[int64]Actor
|
GetPlayerActors(id PlayerID) synchronization.MapReadonly[int64, Actor]
|
||||||
// IsExistPlayer 检查游戏世界中是否存在特定玩家
|
// IsExistPlayer 检查游戏世界中是否存在特定玩家
|
||||||
IsExistPlayer(id PlayerID) bool
|
IsExistPlayer(id PlayerID) bool
|
||||||
// IsExistActor 检查游戏世界中是否存在特定游戏对象
|
// IsExistActor 检查游戏世界中是否存在特定游戏对象
|
||||||
|
@ -59,22 +61,22 @@ type World[PlayerID comparable, P Player[PlayerID]] interface {
|
||||||
RegPlayerLeaveWorldEvent(handle PlayerLeaveWorldEventHandle[PlayerID, P])
|
RegPlayerLeaveWorldEvent(handle PlayerLeaveWorldEventHandle[PlayerID, P])
|
||||||
OnPlayerLeaveWorldEvent(player P)
|
OnPlayerLeaveWorldEvent(player P)
|
||||||
// RegActorGeneratedEvent 游戏世界中的游戏对象生成完成时将立即执行被注册的事件处理函数
|
// RegActorGeneratedEvent 游戏世界中的游戏对象生成完成时将立即执行被注册的事件处理函数
|
||||||
RegActorGeneratedEvent(handle ActorGeneratedEventHandle)
|
RegActorGeneratedEvent(handle ActorGeneratedEventHandle[PlayerID, P])
|
||||||
OnActorGeneratedEvent(actor Actor)
|
OnActorGeneratedEvent(actor Actor)
|
||||||
// RegActorAnnihilationEvent 游戏世界中的游戏对象被移除前执行被注册的事件处理函数
|
// RegActorAnnihilationEvent 游戏世界中的游戏对象被移除前执行被注册的事件处理函数
|
||||||
RegActorAnnihilationEvent(handle ActorAnnihilationEventHandle)
|
RegActorAnnihilationEvent(handle ActorAnnihilationEventHandle[PlayerID, P])
|
||||||
OnActorAnnihilationEvent(actor Actor)
|
OnActorAnnihilationEvent(actor Actor)
|
||||||
// RegActorOwnerChangeEvent 游戏对象的归属被改变时立刻执行被注册的事件处理函数
|
// RegActorOwnerChangeEvent 游戏对象的归属被改变时立刻执行被注册的事件处理函数
|
||||||
RegActorOwnerChangeEvent(handle ActorOwnerChangeEventHandle[PlayerID])
|
RegActorOwnerChangeEvent(handle ActorOwnerChangeEventHandle[PlayerID, P])
|
||||||
OnActorOwnerChangeEvent(actor Actor, old, new PlayerID, isolated bool)
|
OnActorOwnerChangeEvent(actor Actor, old, new PlayerID, isolated bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
type (
|
type (
|
||||||
WorldResetEventHandle[ID comparable, P Player[ID]] func(world World[ID, P])
|
WorldResetEventHandle[ID comparable, P Player[ID]] func(world World[ID, P])
|
||||||
WorldReleaseEventHandle[ID comparable, P Player[ID]] func(world World[ID, P])
|
WorldReleaseEventHandle[ID comparable, P Player[ID]] func(world World[ID, P])
|
||||||
PlayerJoinWorldEventHandle[ID comparable, P Player[ID]] func(player P)
|
PlayerJoinWorldEventHandle[ID comparable, P Player[ID]] func(world World[ID, P], player P)
|
||||||
PlayerLeaveWorldEventHandle[ID comparable, P Player[ID]] func(player P)
|
PlayerLeaveWorldEventHandle[ID comparable, P Player[ID]] func(world World[ID, P], player P)
|
||||||
ActorGeneratedEventHandle func(actor Actor)
|
ActorGeneratedEventHandle[ID comparable, P Player[ID]] func(world World[ID, P], actor Actor)
|
||||||
ActorAnnihilationEventHandle func(actor Actor)
|
ActorAnnihilationEventHandle[ID comparable, P Player[ID]] func(world World[ID, P], actor Actor)
|
||||||
ActorOwnerChangeEventHandle[ID comparable] func(actor Actor, old, new ID, isolated bool)
|
ActorOwnerChangeEventHandle[ID comparable, P Player[ID]] func(world World[ID, P], actor Actor, old, new ID, isolated bool)
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue