object.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package aoiGrid
  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. }
  24. BaseObject struct {
  25. OwnerID int64 `json:"ownerID"` // 拥有者ID(玩家id)
  26. ObjectID int64 `json:"id"` // 对象唯一id
  27. ObjectType enum.ObjectType `json:"objectType"` // 对象类型
  28. X int32 `json:"x"` // 坐标x
  29. Y int32 `json:"y"` // 坐标y
  30. Status int32 `json:"status"` // 状态
  31. }
  32. )
  33. func NewWatcher(id int64, typ enum.ObjectType, x, y int32, ownerID int64) Object {
  34. return NewObject(id, typ, x, y, true, false, ownerID)
  35. }
  36. func NewMarker(id int64, typ enum.ObjectType, x, y int32) Object {
  37. return NewObject(id, typ, x, y, false, true, 0)
  38. }
  39. func FromProto(object *pb.AOIObject) Object {
  40. return NewObject(
  41. object.ObjectID,
  42. enum.ObjectType(object.ObjectType),
  43. object.X,
  44. object.Y,
  45. object.IsWatcher,
  46. object.IsMarker,
  47. object.OnwerID,
  48. )
  49. }
  50. func NewObject(id int64, typ enum.ObjectType, x, y int32, isWatcher, isMarker bool, ownerID int64) Object {
  51. object := Object{
  52. ObjectID: id,
  53. ObjectType: typ,
  54. isWatcher: isWatcher,
  55. isMarker: isMarker,
  56. ownerID: ownerID,
  57. }
  58. object.PrevPoint.X = x
  59. object.PrevPoint.Y = y
  60. object.Point.X = x
  61. object.Point.Y = y
  62. return object
  63. }
  64. func (p *Object) SetPoint(x, y int32) {
  65. p.PrevPoint.X = p.Point.X
  66. p.PrevPoint.Y = p.Point.Y
  67. p.Point.X = x
  68. p.Point.Y = y
  69. }
  70. func (p *Point) Equal(p2 *Point) bool {
  71. return p.X == p2.X && p.Y == p2.Y
  72. }
  73. func (p *Object) String() string {
  74. if p.isMarker {
  75. 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)
  76. }
  77. if p.isWatcher {
  78. 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)
  79. }
  80. return fmt.Sprintf("objectID = %d, point(%d,%d -> %d,%d)", p.ObjectID, p.PrevPoint.X, p.PrevPoint.Y, p.Point.X, p.Point.Y)
  81. }
  82. func (p *Object) Info() string {
  83. return fmt.Sprintf("id = %d, type = %v, point(%d,%d), isMarker = %v, isWatcher = %v",
  84. p.ObjectID,
  85. p.ObjectType,
  86. p.Point.X,
  87. p.Point.Y,
  88. p.isMarker,
  89. p.isWatcher,
  90. )
  91. }
  92. func (p *Object) IsWatcher() bool {
  93. return p.isWatcher
  94. }
  95. func (p *Object) IsMarker() bool {
  96. return p.isMarker
  97. }
  98. func (p *Object) OwnerID() int64 {
  99. return p.ownerID
  100. }
  101. func (p *Object) Clean() {
  102. p.ObjectID = 0
  103. p.PrevPoint.X = 0
  104. p.PrevPoint.Y = 0
  105. p.Point.X = 0
  106. p.Point.Y = 0
  107. p.isWatcher = false
  108. p.isMarker = false
  109. p.ownerID = 0
  110. }
  111. func NewBaseObject(ownerID, objectID int64, objectType enum.ObjectType, x, y, status int32) *BaseObject {
  112. return &BaseObject{
  113. OwnerID: ownerID,
  114. ObjectID: objectID,
  115. ObjectType: objectType,
  116. X: x,
  117. Y: y,
  118. Status: status,
  119. }
  120. }
  121. func (p *BaseObject) String() string {
  122. return fmt.Sprintf("objectID = %d, ObjectType = %s, X = %d, y = %d",
  123. p.ObjectID,
  124. enum.GetObjectTypeName(p.ObjectType),
  125. p.X,
  126. p.Y,
  127. )
  128. }