| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- class_name Pacman
- extends AnimatedSprite2D
- @export var tileset: PacTiles
- const UP := Vector2(0,-1)
- const DOWN := Vector2(0,1)
- const LEFT := Vector2(-1,0)
- const RIGHT := Vector2(1,0)
- #the direction that the player has chosen
- var direction: Vector2 = LEFT
- # the direction pacman is traveling in
- var currentDirection: Vector2 = LEFT
- var currentTile: Vector2
- var speed: float = 88
- # Called when the node enters the scene tree for the first time.
- func _ready():
- autoplay = "default"
- # Called every frame. 'delta' is the elapsed time since the previous frame.
- func _process(delta):
- currentTile.x = int(position.x / 8)
- currentTile.y = int(position.y / 8)
-
- #print(getCurrentTile().isPacmanIntersection)
-
- # tile trail for debugging
- #getCurrentTile().texture = load("res://white.png")
-
- var currentTileRef: Tile = getCurrentTile()
-
- if currentTileRef.hasBite:
- currentTileRef.hasBite = false
- eatBite()
-
- if currentTileRef.hasPower:
- currentTileRef.hasPower = false
- eatPower()
-
- if currentDirection.x == direction.x:
- currentDirection = direction
- if currentDirection.y == direction.y:
- currentDirection = direction
-
- var nextTile: Tile = getTileByVector(currentTile+currentDirection)
- var chosenNextTile: Tile = getTileByVector(currentTile+direction)
-
- if !chosenNextTile.isWall && isTouching(nextTile):
- currentDirection = direction
-
- match currentDirection:
- UP:
- rotation = deg_to_rad(270)
- DOWN:
- rotation = deg_to_rad(90)
- LEFT:
- rotation = deg_to_rad(180)
- RIGHT:
- rotation = deg_to_rad(0)
- print(rotation)
-
- if nextTile.isWall && isTouching(nextTile):
- position=position
- pause()
- else:
- position += currentDirection * speed * delta
- play()
- func _unhandled_key_input(event):
- if event.is_action_pressed("ui_up"):
- direction = UP
- if event.is_action_pressed("ui_down"):
- direction = DOWN
- if event.is_action_pressed("ui_left"):
- direction = LEFT
- if event.is_action_pressed("ui_right"):
- direction = RIGHT
- func getCurrentTile() -> Tile:
- return tileset.tiles[currentTile.y][currentTile.x]
- func getTile(x: int,y: int) -> Tile:
- return tileset.tiles[y][x]
- func getTileByVector(vector: Vector2) -> Tile:
- return tileset.tiles[vector.y][vector.x]
- func isTouching(tile: Tile):
- var xDifference = abs(tile.global_position.x - position.x)
- var yDifference = abs(tile.global_position.y - position.y)
-
- if xDifference >= 8 && xDifference <= 9:
- return true
- if yDifference >= 8 && yDifference <= 9:
- return true
-
- return false
- func eatBite():
- pass
- func eatPower():
- pass
|