PirateTreasureHunt/Player.gd

83 lines
2.1 KiB
GDScript
Raw Normal View History

2021-06-12 17:48:58 +02:00
extends Node2D
enum PlayerState {
IDLE,
WALKING,
SHOVEL
}
signal DigSiteReached
signal DigStop
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var speed = 200
var isInteracting = false
var ShovelSprite
var shovel_offset = Vector2.ZERO
var NavTarget = Vector2.ZERO
var State = PlayerState.IDLE
var ShovelTimer = 0
# Called when the node enters the scene tree for the first time.
func _ready():
ShovelSprite = get_node("PirateWithShovelSprite")
var ShovelTargetOffset = get_node("ShovelTargetOffset")
shovel_offset = ShovelTargetOffset.transform.origin - self.transform.origin
func start_dig():
self.State = PlayerState.SHOVEL
self.ShovelTimer = Globals.SHOVEL_DURATION
func update_state(delta):
if State == PlayerState.SHOVEL:
ShovelTimer = ShovelTimer - delta
if ShovelTimer < 0:
emit_signal ("DigStop")
ShovelTimer = 0
State = PlayerState.IDLE
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
update_state(delta)
Globals.DebugLabelAdd(str("Player.NavTarget: ", NavTarget))
var direction = Vector2.ZERO
if Input.is_action_pressed("walk_left"):
direction.x = direction.x - 1
if Input.is_action_pressed("walk_right"):
direction.x = direction.x + 1
if Input.is_action_pressed("walk_up"):
direction.y = direction.y - 1
if Input.is_action_pressed("walk_down"):
direction.y = direction.y + 1
direction = direction.normalized()
var movement_target = NavTarget - shovel_offset
var vec_to_target = movement_target - self.transform.origin
var dist_to_target = vec_to_target.length()
Globals.DebugLabelAdd(str("Direction: ", direction))
if dist_to_target > speed * delta:
direction = (vec_to_target).normalized()
var velocity = direction * speed
self.transform.origin = self.transform.origin + velocity * delta
self.State = PlayerState.WALKING
else:
direction = Vector2.ZERO
self.transform.origin = movement_target
if self.State == PlayerState.WALKING:
emit_signal ("DigSiteReached", Globals.WorldToGridCenter(NavTarget))
ShovelSprite.visible = State == PlayerState.SHOVEL