ld52_olive_harvest/characters/player/Player.gd

122 lines
3.1 KiB
GDScript

extends KinematicBody2D
const SPEED_MULTIPLIER_WHEN_ATTACHED = 0.7
export var speed = 200 # How fast the player will move (pixels/sec).
var screen_size # Size of the game window.
var item_holders = []
var direction = Vector2.ZERO
# Called when the node enters the scene tree for the first time.
func _ready():
screen_size = get_viewport_rect().size
item_holders = [$ItemHolder1, $ItemHolder2]
var _is_h_flipped = false
func _set_h_flipped(flip: bool):
if flip == _is_h_flipped:
return
_is_h_flipped = flip
$AnimatedSprite.flip_h = flip
$ItemHolder1.position.x *= -1
$ItemHolder1.scale.x *= -1
$ItemHolder1.rotation *= -1
func _physics_process(_delta):
var velocity = Vector2.ZERO # The player's movement vector.
velocity.x += Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
velocity.y += Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
if velocity.length() > 0:
direction = velocity.normalized()
if $PlayerAttachmentManager.attached != null:
velocity = direction * speed * SPEED_MULTIPLIER_WHEN_ATTACHED
else:
velocity = direction * speed
$AnimatedSprite.play()
if $PlayerAttachmentManager.attached != null:
var _att_lin_vel = $PlayerAttachmentManager.attached.move_and_slide(velocity)
var _lin_vel = self.move_and_slide(velocity)
if abs(velocity.x) > 0:
self._set_h_flipped(velocity.x < 0)
$InteractionArea.rotation = direction.angle()
else:
$AnimatedSprite.stop()
func _process(_delta):
if Input.is_action_just_pressed("drop"):
_act_drop()
if Input.is_action_just_pressed("interact"):
print("interact")
if $PlayerAttachmentManager.attached != null:
print("detach")
$PlayerAttachmentManager.detach()
return
var bodies = $InteractionArea.get_overlapping_bodies()
print("found", len(bodies))
for body in bodies:
print(body)
if body.is_in_group("attachable"):
print("attachable!")
$PlayerAttachmentManager.attach(body)
break
if body.is_in_group("interactable"):
print("interactable!")
if body.interact(self):
return
func _act_drop():
if $ItemHolder1.get_child_count() < 1:
return
var child = $ItemHolder1.get_child(0)
child.drop(self, direction * 10)
var press_area_press = null
func on_press_area_entered(press: Node2D):
press_area_press = press
if press.crushing_needed():
self._crush_jump()
func on_press_area_exited(_press: Node2D):
press_area_press = null
var is_crush_jumping = false
func _crush_jump():
if is_crush_jumping:
return
is_crush_jumping = true
$Tween.interpolate_property($AnimatedSprite, "position:y", 0, -72, 0.3, Tween.TRANS_QUAD, Tween.EASE_OUT)
$Tween.interpolate_property($AnimatedSprite, "position:y", -72, 0, 0.3, Tween.TRANS_QUAD, Tween.EASE_IN, 0.3)
$Tween.start()
func has_free_item_slot():
for slot in [$ItemHolder1, $ItemHolder2]:
if slot.get_child_count() < 1:
return true
return false
func _on_Tween_tween_all_completed():
$Tween.remove_all()
if is_crush_jumping:
is_crush_jumping = false
if press_area_press != null:
press_area_press.crush()
if press_area_press.crushing_needed():
self._crush_jump()