Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Sprite
- var last_position = Vector2()
- var target_position = Vector2()
- var direction = Vector2()
- export(float) var speed = 5
- # References const TILE_SIZE from the Game Node2D
- onready var TILE_SIZE = get_parent().get_parent().TILE_SIZE
- onready var ray = $RayCast2D
- onready var tween = $Tween
- func _ready():
- speed = 1 / speed
- position = position.snapped(Vector2(TILE_SIZE / 2, TILE_SIZE / 2))
- target_position = position
- func _process(delta):
- _get_direction()
- # COLLISION == MOVE PLAYER BACK TO LAST POSITION
- if(ray.is_colliding()):
- target_position = last_position
- _move_player()
- # NO COLLISION AND PLAYER IS NOT MOVING == MOVE PLAYER TO WHERE THEY WANT
- elif(target_position == position):
- target_position = position + direction * TILE_SIZE
- last_position = position
- _move_player()
- func _move_player():
- tween.interpolate_property(self, "position", position, target_position, speed, Tween.TRANS_LINEAR, Tween.EASE_IN)
- tween.start()
- func _get_direction():
- var right = Input.is_action_pressed("ui_right")
- var left = Input.is_action_pressed("ui_left")
- var up = Input.is_action_pressed("ui_up")
- var down = Input.is_action_pressed("ui_down")
- direction.x = int(right) - int(left)
- direction.y = int(down) - int(up)
- # PLAYER TRIES TO MOVE DIAGONALLY
- if(direction.x != 0 && direction.y != 0):
- direction = Vector2.ZERO
- # PLAYER TRIES TO MOVE AT ALL
- if(direction != Vector2.ZERO):
- ray.cast_to = direction * TILE_SIZE / 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement