2021-06-17 23:03:45 +02:00
|
|
|
extends KinematicBody2D
|
|
|
|
|
|
|
|
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-07 22:22:07 +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-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():
|
|
|
|
print ("Start digging")
|
|
|
|
state = State.Digging
|
|
|
|
DigTimer.start(1.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")
|