object.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package aoi
  2. import (
  3. "f1-game/internal/enum"
  4. "fmt"
  5. )
  6. type (
  7. Point struct {
  8. X int32
  9. Y int32
  10. }
  11. Object struct {
  12. ObjectID int64 // 对象唯一id
  13. ObjectType enum.ObjectType // 对象类型
  14. PrevPoint Point // 上一个坐标点
  15. Point Point // 当前坐标点
  16. isWatcher bool // 是否为观察者,当前对象可以观察到哪些人 (tower有变化会收到消息)
  17. isMarker bool // 是否为被观察者
  18. ownerID int64 // 拥有者ID
  19. }
  20. )
  21. func NewWatcher(id int64, _type enum.ObjectType, x, y int32, ownerID int64) *Object {
  22. return NewObject(id, _type, x, y, true, false, ownerID)
  23. }
  24. func NewMarker(id int64, _type enum.ObjectType, x, y int32) *Object {
  25. return NewObject(id, _type, x, y, false, true, 0)
  26. }
  27. func NewObject(id int64, _type enum.ObjectType, x, y int32, isWatcher, isMarker bool, ownerID int64) *Object {
  28. object := &Object{
  29. ObjectID: id,
  30. ObjectType: _type,
  31. isWatcher: isWatcher,
  32. isMarker: isMarker,
  33. ownerID: ownerID,
  34. }
  35. object.PrevPoint.X = x
  36. object.PrevPoint.Y = y
  37. object.Point.X = x
  38. object.Point.Y = y
  39. return object
  40. }
  41. func (p *Object) SetPoint(x, y int32) {
  42. p.PrevPoint.X = p.Point.X
  43. p.PrevPoint.Y = p.Point.Y
  44. p.Point.X = x
  45. p.Point.Y = y
  46. }
  47. func (p *Point) Equal(p2 *Point) bool {
  48. return p.X == p2.X && p.Y == p2.Y
  49. }
  50. func (p *Object) String() string {
  51. if p.isMarker {
  52. 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)
  53. }
  54. if p.isWatcher {
  55. 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)
  56. }
  57. return fmt.Sprintf("objectID = %d, point(%d,%d -> %d,%d)", p.ObjectID, p.PrevPoint.X, p.PrevPoint.Y, p.Point.X, p.Point.Y)
  58. }
  59. func (p *Object) Info() string {
  60. return fmt.Sprintf("id = %d, type = %v, point(%d,%d), isMarker = %v, isWatcher = %v, ownerID = %d",
  61. p.ObjectID,
  62. enum.GetObjectTypeName(p.ObjectType),
  63. p.Point.X,
  64. p.Point.Y,
  65. p.isMarker,
  66. p.isWatcher,
  67. p.ownerID,
  68. )
  69. }
  70. func (p *Object) IsWatcher() bool {
  71. return p.isWatcher
  72. }
  73. func (p *Object) IsMarker() bool {
  74. return p.isMarker
  75. }
  76. func (p *Object) OwnerID() int64 {
  77. return p.ownerID
  78. }
  79. func (p *Object) Clean() {
  80. p.ObjectID = 0
  81. p.PrevPoint.X = 0
  82. p.PrevPoint.Y = 0
  83. p.Point.X = 0
  84. p.Point.Y = 0
  85. p.isWatcher = false
  86. p.isMarker = false
  87. p.ownerID = 0
  88. }