object.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package aoiTower
  2. import (
  3. "f1-game/internal/enum"
  4. "f1-game/internal/pb"
  5. "fmt"
  6. )
  7. var (
  8. _nilStruct = struct{}{}
  9. )
  10. type (
  11. Point struct {
  12. X int32
  13. Y int32
  14. }
  15. Object struct {
  16. ObjectID int64 // 对象唯一id
  17. ObjectType enum.ObjectType // 对象类型
  18. PrevPoint Point // 上一个坐标点
  19. Point Point // 当前坐标点
  20. isWatcher bool // 是否为观察者,当前对象可以观察到哪些人 (tower有变化会收到消息)
  21. isMarker bool // 是否为被观察者
  22. ownerID int64 // 拥有者ID
  23. delayLeaveTowers map[int32]int64 // 延迟离开的灯塔: key=towerID, value=expireTime
  24. }
  25. BaseObject struct {
  26. OwnerID int64 `json:"ownerID"` // 拥有者ID(玩家id)
  27. ObjectID int64 `json:"id"` // 对象唯一id
  28. ObjectType enum.ObjectType `json:"objectType"` // 对象类型
  29. X int32 `json:"x"` // 坐标x
  30. Y int32 `json:"y"` // 坐标y
  31. Status int32 `json:"status"` // 状态
  32. }
  33. )
  34. func NewWatcher(id int64, typ enum.ObjectType, x, y int32, ownerID int64) Object {
  35. return NewObject(id, typ, x, y, true, false, ownerID)
  36. }
  37. func NewMarker(id int64, typ enum.ObjectType, x, y int32) Object {
  38. return NewObject(id, typ, x, y, false, true, 0)
  39. }
  40. func FromProto(object *pb.AOIObject) Object {
  41. return NewObject(
  42. object.ObjectID,
  43. enum.ObjectType(object.ObjectType),
  44. object.X,
  45. object.Y,
  46. object.IsWatcher,
  47. object.IsMarker,
  48. object.OnwerID,
  49. )
  50. }
  51. func NewObject(id int64, typ enum.ObjectType, x, y int32, isWatcher, isMarker bool, ownerID int64) Object {
  52. object := Object{
  53. ObjectID: id,
  54. ObjectType: typ,
  55. isWatcher: isWatcher,
  56. isMarker: isMarker,
  57. ownerID: ownerID,
  58. }
  59. object.PrevPoint.X = x
  60. object.PrevPoint.Y = y
  61. object.Point.X = x
  62. object.Point.Y = y
  63. if isWatcher {
  64. object.delayLeaveTowers = make(map[int32]int64)
  65. }
  66. return object
  67. }
  68. func (p *Object) SetPoint(x, y int32) {
  69. p.PrevPoint.X = p.Point.X
  70. p.PrevPoint.Y = p.Point.Y
  71. p.Point.X = x
  72. p.Point.Y = y
  73. }
  74. func (p *Point) Equal(p2 *Point) bool {
  75. return p.X == p2.X && p.Y == p2.Y
  76. }
  77. func (p *Object) String() string {
  78. if p.isMarker {
  79. return fmt.Sprintf("marker objectID = %d, point(%d,%d -> %d,%d)", p.ObjectID, p.PrevPoint.X, p.PrevPoint.Y, p.Point.X, p.Point.Y)
  80. }
  81. if p.isWatcher {
  82. return fmt.Sprintf("watcher objectID = %d, point(%d,%d -> %d,%d)", p.ObjectID, p.PrevPoint.X, p.PrevPoint.Y, p.Point.X, p.Point.Y)
  83. }
  84. return fmt.Sprintf("objectID = %d, point(%d,%d -> %d,%d)", p.ObjectID, p.PrevPoint.X, p.PrevPoint.Y, p.Point.X, p.Point.Y)
  85. }
  86. func (p *Object) Info() string {
  87. return fmt.Sprintf("id = %d, type = %v, point(%d,%d), isMarker = %v, isWatcher = %v",
  88. p.ObjectID,
  89. p.ObjectType,
  90. p.Point.X,
  91. p.Point.Y,
  92. p.isMarker,
  93. p.isWatcher,
  94. )
  95. }
  96. func (p *Object) IsWatcher() bool {
  97. return p.isWatcher
  98. }
  99. func (p *Object) IsMarker() bool {
  100. return p.isMarker
  101. }
  102. func (p *Object) OwnerID() int64 {
  103. return p.ownerID
  104. }
  105. func (p *Object) Clean() {
  106. if p.IsWatcher() {
  107. p.delayLeaveTowers = make(map[int32]int64)
  108. }
  109. p.ObjectID = 0
  110. p.PrevPoint.X = 0
  111. p.PrevPoint.Y = 0
  112. p.Point.X = 0
  113. p.Point.Y = 0
  114. p.isWatcher = false
  115. p.isMarker = false
  116. p.ownerID = 0
  117. }
  118. func NewBaseObject(ownerID, objectID int64, objectType enum.ObjectType, x, y, status int32) *BaseObject {
  119. return &BaseObject{
  120. OwnerID: ownerID,
  121. ObjectID: objectID,
  122. ObjectType: objectType,
  123. X: x,
  124. Y: y,
  125. Status: status,
  126. }
  127. }
  128. func (p *BaseObject) String() string {
  129. return fmt.Sprintf("objectID = %d, ObjectType = %s, X = %d, y = %d",
  130. p.ObjectID,
  131. enum.GetObjectTypeName(p.ObjectType),
  132. p.X,
  133. p.Y,
  134. )
  135. }
  136. // AddDelayLeaveTower 添加延迟离开的灯塔
  137. func (p *Object) AddDelayLeaveTower(towerID int32, expireTime int64) {
  138. p.delayLeaveTowers[towerID] = expireTime
  139. }
  140. // RemoveDelayLeaveTower 移除延迟离开的灯塔
  141. func (p *Object) RemoveDelayLeaveTower(towerID int32) {
  142. delete(p.delayLeaveTowers, towerID)
  143. }
  144. // GetDelayLeaveTowers 获取所有延迟离开的灯塔
  145. func (p *Object) GetDelayLeaveTowers() map[int32]int64 {
  146. return p.delayLeaveTowers
  147. }