package aoiTower import ( "f1-game/internal/enum" "f1-game/internal/pb" "fmt" ) var ( _nilStruct = struct{}{} ) type ( Point struct { X int32 Y int32 } Object struct { ObjectID int64 // 对象唯一id ObjectType enum.ObjectType // 对象类型 PrevPoint Point // 上一个坐标点 Point Point // 当前坐标点 isWatcher bool // 是否为观察者,当前对象可以观察到哪些人 (tower有变化会收到消息) isMarker bool // 是否为被观察者 ownerID int64 // 拥有者ID delayLeaveTowers map[int32]int64 // 延迟离开的灯塔: key=towerID, value=expireTime } BaseObject struct { OwnerID int64 `json:"ownerID"` // 拥有者ID(玩家id) ObjectID int64 `json:"id"` // 对象唯一id ObjectType enum.ObjectType `json:"objectType"` // 对象类型 X int32 `json:"x"` // 坐标x Y int32 `json:"y"` // 坐标y Status int32 `json:"status"` // 状态 } ) func NewWatcher(id int64, typ enum.ObjectType, x, y int32, ownerID int64) Object { return NewObject(id, typ, x, y, true, false, ownerID) } func NewMarker(id int64, typ enum.ObjectType, x, y int32) Object { return NewObject(id, typ, x, y, false, true, 0) } func FromProto(object *pb.AOIObject) Object { return NewObject( object.ObjectID, enum.ObjectType(object.ObjectType), object.X, object.Y, object.IsWatcher, object.IsMarker, object.OnwerID, ) } func NewObject(id int64, typ enum.ObjectType, x, y int32, isWatcher, isMarker bool, ownerID int64) Object { object := Object{ ObjectID: id, ObjectType: typ, isWatcher: isWatcher, isMarker: isMarker, ownerID: ownerID, } object.PrevPoint.X = x object.PrevPoint.Y = y object.Point.X = x object.Point.Y = y if isWatcher { object.delayLeaveTowers = make(map[int32]int64) } return object } func (p *Object) SetPoint(x, y int32) { p.PrevPoint.X = p.Point.X p.PrevPoint.Y = p.Point.Y p.Point.X = x p.Point.Y = y } func (p *Point) Equal(p2 *Point) bool { return p.X == p2.X && p.Y == p2.Y } func (p *Object) String() string { if p.isMarker { 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) } if p.isWatcher { 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) } return fmt.Sprintf("objectID = %d, point(%d,%d -> %d,%d)", p.ObjectID, p.PrevPoint.X, p.PrevPoint.Y, p.Point.X, p.Point.Y) } func (p *Object) Info() string { return fmt.Sprintf("id = %d, type = %v, point(%d,%d), isMarker = %v, isWatcher = %v", p.ObjectID, p.ObjectType, p.Point.X, p.Point.Y, p.isMarker, p.isWatcher, ) } func (p *Object) IsWatcher() bool { return p.isWatcher } func (p *Object) IsMarker() bool { return p.isMarker } func (p *Object) OwnerID() int64 { return p.ownerID } func (p *Object) Clean() { if p.IsWatcher() { p.delayLeaveTowers = make(map[int32]int64) } p.ObjectID = 0 p.PrevPoint.X = 0 p.PrevPoint.Y = 0 p.Point.X = 0 p.Point.Y = 0 p.isWatcher = false p.isMarker = false p.ownerID = 0 } func NewBaseObject(ownerID, objectID int64, objectType enum.ObjectType, x, y, status int32) *BaseObject { return &BaseObject{ OwnerID: ownerID, ObjectID: objectID, ObjectType: objectType, X: x, Y: y, Status: status, } } func (p *BaseObject) String() string { return fmt.Sprintf("objectID = %d, ObjectType = %s, X = %d, y = %d", p.ObjectID, enum.GetObjectTypeName(p.ObjectType), p.X, p.Y, ) } // AddDelayLeaveTower 添加延迟离开的灯塔 func (p *Object) AddDelayLeaveTower(towerID int32, expireTime int64) { p.delayLeaveTowers[towerID] = expireTime } // RemoveDelayLeaveTower 移除延迟离开的灯塔 func (p *Object) RemoveDelayLeaveTower(towerID int32) { delete(p.delayLeaveTowers, towerID) } // GetDelayLeaveTowers 获取所有延迟离开的灯塔 func (p *Object) GetDelayLeaveTowers() map[int32]int64 { return p.delayLeaveTowers }