PirateTreasureHunt/scenes/pirate.gd

42 lines
1006 B
GDScript
Raw Normal View History

2021-06-17 23:03:45 +02:00
extends KinematicBody2D
export (int) var speed = 200
var velocity = Vector2()
var target = Vector2()
2021-07-11 16:09:04 +02:00
var prev_tile = null # Hex coords of previous tile
var cur_tile = null # Hex coords of current tile
2021-06-17 23:03:45 +02:00
func get_input():
velocity = Vector2()
if Input.is_action_pressed("walk_right"):
velocity.x += 1
if Input.is_action_pressed("walk_left"):
velocity.x -= 1
if Input.is_action_pressed("walk_down"):
velocity.y += 1
if Input.is_action_pressed("walk_up"):
velocity.y -= 1
velocity = velocity.normalized() * speed
2021-07-11 16:09:04 +02:00
func _physics_process(_delta):
2021-06-17 23:03:45 +02:00
velocity = position.direction_to(target) * speed
if position.distance_to(target) > 5:
velocity = move_and_slide(velocity)
2021-07-11 16:09:04 +02:00
var tile = Globals.WorldToHex(position)
if tile != cur_tile:
prev_tile = cur_tile
cur_tile = tile
2021-06-17 23:03:45 +02:00
# Called when the node enters the scene tree for the first time.
func _ready():
position = Vector2(0,0)
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass