node.go 762 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package astar
  2. import "fmt"
  3. // Node 节点结构
  4. type Node struct {
  5. x, y int32
  6. isWalkable bool // 是否可行走
  7. neighbors map[*Node]int32
  8. }
  9. // NewNode 创建新节点
  10. func NewNode(x, y int32, isWalkable bool) *Node {
  11. return &Node{
  12. x: x,
  13. y: y,
  14. isWalkable: isWalkable,
  15. neighbors: make(map[*Node]int32),
  16. }
  17. }
  18. func (p *Node) AddNeighbor(node *Node, distance int32) {
  19. if node != nil && node.isWalkable {
  20. p.neighbors[node] = distance
  21. }
  22. }
  23. func (p *Node) X() int32 {
  24. return p.x
  25. }
  26. func (p *Node) Y() int32 {
  27. return p.y
  28. }
  29. func (p *Node) IsOdd() bool {
  30. return (p.x & 0x1) == 0x1
  31. }
  32. func (p *Node) IsEven() bool {
  33. return (p.x & 0x1) == 0x0
  34. }
  35. func (p *Node) String() string {
  36. return fmt.Sprintf("{%d, %d}", p.x, p.y)
  37. }