Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node2D
- var speed = 256
- var tile_size = 32
- var last_position = Vector2()
- var target_position = Vector2()
- var movedir = Vector2()
- onready var ray = $Sprite/RayCast2D
- func _ready():
- position = position.snapped(Vector2(tile_size, tile_size))
- last_position = position
- target_position = position
- func _process(delta):
- #MOVEMENT
- #collision
- if ray.is_colliding():
- position = last_position
- target_position = last_position
- else:
- #Movement
- position += speed * movedir * delta
- if position.distance_to(last_position) >= tile_size - speed * delta:
- position = target_position
- #IDLE
- if position == target_position:
- get_movedir()
- last_position = position
- target_position += movedir * tile_size
- func get_movedir():
- var Left = Input.is_action_just_pressed("left")
- var Right = Input.is_action_just_pressed("right")
- var Down = Input.is_action_just_pressed("down")
- var Up = Input.is_action_just_pressed("up")
- movedir.x = -int(Left) + int(Right)
- movedir.y = - int(Up) + int(Down)
- if movedir.x != 0 && movedir.y != 0:
- movedir = Vector2.ZERO
- #collsion
- if movedir != Vector2.ZERO:
- ray.cast_to = movedir * tile_size
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement