75 lines
1.4 KiB
GDScript
75 lines
1.4 KiB
GDScript
extends KinematicBody2D
|
|
|
|
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):
|
|
velocity = position.direction_to(target) * speed
|
|
if position.distance_to(target) > 5:
|
|
velocity = move_and_slide(velocity)
|
|
|
|
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():
|
|
print ("Start digging")
|
|
state = State.Digging
|
|
DigTimer.start(1.0)
|
|
|
|
|
|
func on_dig_stop():
|
|
state = State.Walking
|
|
emit_signal ("dig_stopped")
|