actor.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package player
  2. import (
  3. nameActor "f1-game/internal/name/actor"
  4. cstring "github.com/cherry-game/cherry/extend/string"
  5. cfacade "github.com/cherry-game/cherry/facade"
  6. "github.com/cherry-game/cherry/net/parser/pomelo"
  7. )
  8. type (
  9. actor struct {
  10. pomelo.ActorBase
  11. workerSize int
  12. }
  13. )
  14. func NewActor(workerSize int) *actor {
  15. if workerSize < 1 {
  16. workerSize = 1
  17. }
  18. return &actor{
  19. workerSize: workerSize,
  20. }
  21. }
  22. func (p *actor) AliasID() string {
  23. return nameActor.CenterPlayer
  24. }
  25. func (p *actor) OnInit() {
  26. for i := range p.workerSize {
  27. childID := cstring.ToString(i)
  28. childActor := &actorUID{}
  29. p.Child().Create(childID, childActor)
  30. }
  31. }
  32. // OnRemoteReceived 转发到actorUID
  33. func (p *actor) OnRemoteReceived(m *cfacade.Message) (next bool, invoke bool) {
  34. if m.TargetPath().IsParent() {
  35. return true, false
  36. }
  37. hashID, ok := cstring.ToInt(m.TargetPath().ChildID, 0)
  38. if !ok || hashID < 1 {
  39. return true, false
  40. }
  41. childID := cstring.ToString(hashID % p.workerSize)
  42. if child, found := p.Child().Get(childID); found {
  43. child.PostRemote(m)
  44. }
  45. return false, false
  46. }