PirateTreasureHunt/scenes/pirate.gd

84 lines
1.6 KiB
GDScript

extends Node2D
export (int) var speed = 200
enum State {
Sailing,
Walking,
Digging
}
signal dig_stopped
onready var Shovel = get_node("Shovel")
onready var DigTimer = get_node("DigTimer")
var velocity = Vector2()
var target = Vector2()
var prev_tile = null # Hex coords of previous tile
var cur_tile = null # Hex coords of current tile
var state = State.Sailing
func get_input():
velocity = Vector2()
if state == State.Digging:
return
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
func _physics_process(delta):
var r_to_target = target - position
var dist = r_to_target.length()
if delta > 0.0001 and dist < speed * delta:
position = target
velocity = Vector2.ZERO
else:
velocity = r_to_target.normalized() * speed
if state == State.Digging:
velocity = Vector2.ZERO
position = position + velocity * delta
var tile = Globals.WorldToHex(position)
if tile != cur_tile:
prev_tile = cur_tile
cur_tile = tile
Shovel.visible = state == State.Digging
# Called when the node enters the scene tree for the first time.
func _ready():
position = Vector2(0,0)
DigTimer.connect("timeout", self, "on_dig_stop")
func on_enter_island():
state = State.Walking
func on_leave_island():
state = State.Sailing
func on_dig_start():
state = State.Digging
DigTimer.start(2.0)
func on_dig_stop():
state = State.Walking
emit_signal ("dig_stopped")