Pacman.gd 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. class_name Pacman
  2. extends AnimatedSprite2D
  3. @export var tileset: PacTiles
  4. const UP := Vector2(0,-1)
  5. const DOWN := Vector2(0,1)
  6. const LEFT := Vector2(-1,0)
  7. const RIGHT := Vector2(1,0)
  8. #the direction that the player has chosen
  9. var direction: Vector2 = LEFT
  10. # the direction pacman is traveling in
  11. var currentDirection: Vector2 = LEFT
  12. var currentTile: Vector2
  13. var speed: float = 88
  14. # Called when the node enters the scene tree for the first time.
  15. func _ready():
  16. autoplay = "default"
  17. # Called every frame. 'delta' is the elapsed time since the previous frame.
  18. func _process(delta):
  19. currentTile.x = int(position.x / 8)
  20. currentTile.y = int(position.y / 8)
  21. #print(getCurrentTile().isPacmanIntersection)
  22. # tile trail for debugging
  23. #getCurrentTile().texture = load("res://white.png")
  24. var currentTileRef: Tile = getCurrentTile()
  25. if currentTileRef.hasBite:
  26. currentTileRef.hasBite = false
  27. eatBite()
  28. if currentTileRef.hasPower:
  29. currentTileRef.hasPower = false
  30. eatPower()
  31. if currentDirection.x == direction.x:
  32. currentDirection = direction
  33. if currentDirection.y == direction.y:
  34. currentDirection = direction
  35. var nextTile: Tile = getTileByVector(currentTile+currentDirection)
  36. var chosenNextTile: Tile = getTileByVector(currentTile+direction)
  37. if !chosenNextTile.isWall && isTouching(nextTile):
  38. currentDirection = direction
  39. match currentDirection:
  40. UP:
  41. rotation = deg_to_rad(270)
  42. DOWN:
  43. rotation = deg_to_rad(90)
  44. LEFT:
  45. rotation = deg_to_rad(180)
  46. RIGHT:
  47. rotation = deg_to_rad(0)
  48. print(rotation)
  49. if nextTile.isWall && isTouching(nextTile):
  50. position=position
  51. pause()
  52. else:
  53. position += currentDirection * speed * delta
  54. play()
  55. func _unhandled_key_input(event):
  56. if event.is_action_pressed("ui_up"):
  57. direction = UP
  58. if event.is_action_pressed("ui_down"):
  59. direction = DOWN
  60. if event.is_action_pressed("ui_left"):
  61. direction = LEFT
  62. if event.is_action_pressed("ui_right"):
  63. direction = RIGHT
  64. func getCurrentTile() -> Tile:
  65. return tileset.tiles[currentTile.y][currentTile.x]
  66. func getTile(x: int,y: int) -> Tile:
  67. return tileset.tiles[y][x]
  68. func getTileByVector(vector: Vector2) -> Tile:
  69. return tileset.tiles[vector.y][vector.x]
  70. func isTouching(tile: Tile):
  71. var xDifference = abs(tile.global_position.x - position.x)
  72. var yDifference = abs(tile.global_position.y - position.y)
  73. if xDifference >= 8 && xDifference <= 9:
  74. return true
  75. if yDifference >= 8 && yDifference <= 9:
  76. return true
  77. return false
  78. func eatBite():
  79. pass
  80. func eatPower():
  81. pass