PirateTreasureHunt/scenes/pirate.gd

84 lines
1.6 KiB
GDScript
Raw Normal View History

2021-07-20 22:44:13 +02:00
extends Node2D
2021-06-17 23:03:45 +02:00
export (int) var speed = 200
2021-07-16 12:07:11 +02:00
enum State {
Sailing,
Walking,
Digging
}
signal dig_stopped
onready var Shovel = get_node("Shovel")
onready var DigTimer = get_node("DigTimer")
2021-06-17 23:03:45 +02:00
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-07-16 12:07:11 +02:00
var state = State.Sailing
2021-06-17 23:03:45 +02:00
func get_input():
velocity = Vector2()
2021-07-16 12:07:11 +02:00
if state == State.Digging:
return
2021-06-17 23:03:45 +02:00
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
2021-07-20 22:44:13 +02:00
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
2021-07-21 22:46:26 +02:00
if state == State.Digging:
velocity = Vector2.ZERO
2021-07-20 22:44:13 +02:00
position = position + velocity * delta
2021-06-17 23:03:45 +02:00
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-07-16 12:07:11 +02:00
Shovel.visible = state == State.Digging
2021-07-11 16:09:04 +02:00
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)
2021-07-16 12:07:11 +02:00
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
2021-07-20 22:51:38 +02:00
DigTimer.start(2.0)
2021-06-17 23:03:45 +02:00
2021-07-16 12:07:11 +02:00
func on_dig_stop():
state = State.Walking
emit_signal ("dig_stopped")