108 lines
3.3 KiB
GDScript
108 lines
3.3 KiB
GDScript
extends CharacterBody2D
|
|
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
|
|
var current_xp = 0
|
|
var max_xp = 5
|
|
var level = 1
|
|
var speed = 60
|
|
var strength = 3
|
|
var attacks = false
|
|
var fruit_queue: Array = []
|
|
var orbit_angle: float = 0.0
|
|
const ORBIT_RADIUS := 20.0
|
|
const ORBIT_SPEED := 2.5
|
|
|
|
func _physics_process(delta):
|
|
var direction = Input.get_vector("move_left", "move_right", "move_up", "move_down")
|
|
velocity = direction * speed
|
|
move_and_slide()
|
|
orbit_angle += ORBIT_SPEED * delta
|
|
for i in fruit_queue.size():
|
|
var fruit = fruit_queue[i]
|
|
if is_instance_valid(fruit):
|
|
var angle = orbit_angle + (TAU / fruit_queue.size()) * i
|
|
var target_pos = global_position + Vector2.from_angle(angle) * ORBIT_RADIUS
|
|
fruit.global_position = fruit.global_position.lerp(target_pos, 1.0 - exp(-12.0 * delta))
|
|
if attacks == true:
|
|
return
|
|
if direction == Vector2.ZERO:
|
|
animated_sprite_2d.speed_scale = 1.0
|
|
animated_sprite_2d.play("idle")
|
|
elif abs(direction.x) >= abs(direction.y):
|
|
animated_sprite_2d.speed_scale = speed / 60.0
|
|
if direction.x < 0:
|
|
animated_sprite_2d.play("walk_left")
|
|
else:
|
|
animated_sprite_2d.play("walk_right")
|
|
else:
|
|
animated_sprite_2d.speed_scale = speed / 60.0
|
|
if direction.y < 0:
|
|
animated_sprite_2d.play("walk_up")
|
|
else:
|
|
animated_sprite_2d.play("walk_down")
|
|
|
|
|
|
|
|
|
|
func attack():
|
|
if attacks:
|
|
return
|
|
for body in $MeleeArea.get_overlapping_bodies():
|
|
if body.is_in_group("enemies"):
|
|
attacks = true
|
|
var dir = global_position.direction_to(body.global_position)
|
|
if dir == Vector2.ZERO:
|
|
animated_sprite_2d.play("idle")
|
|
elif abs(dir.x) >= abs(dir.y):
|
|
if dir.x < 0:
|
|
animated_sprite_2d.play("attack_left")
|
|
else:
|
|
animated_sprite_2d.play("attack_right")
|
|
else:
|
|
if dir.y < 0:
|
|
animated_sprite_2d.play("attack_up")
|
|
else:
|
|
animated_sprite_2d.play("attack_down")
|
|
animated_sprite_2d.speed_scale = 0.5 / $AttackSpeed.wait_time * 1.4
|
|
var wait_time = $AttackSpeed.wait_time
|
|
await get_tree().create_timer(wait_time / 2).timeout
|
|
body.take_damage(strength)
|
|
var knockback_dir = global_position.direction_to(body.global_position)
|
|
var enemy_tween = create_tween()
|
|
enemy_tween.tween_property(body, "global_position", body.global_position + knockback_dir * 20, 0.1).set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_QUAD)
|
|
var player_tween = create_tween()
|
|
player_tween.tween_property(self, "global_position", global_position - knockback_dir * 10, 0.1).set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_QUAD)
|
|
await animated_sprite_2d.animation_finished
|
|
animated_sprite_2d.speed_scale = 1
|
|
attacks = false
|
|
break
|
|
|
|
|
|
|
|
func _on_attack_speed_timeout() -> void:
|
|
if $MeleeArea.get_overlapping_bodies().any(func(b): return b.is_in_group("enemies")):
|
|
attack()
|
|
else:
|
|
$AttackSpeed.call_deferred("stop")
|
|
pass # Replace with function body.
|
|
|
|
|
|
|
|
func add_to_queue(fruit) -> void:
|
|
fruit.in_orbit = true
|
|
fruit_queue.append(fruit)
|
|
|
|
func flush_queue() -> void:
|
|
var to_flush = fruit_queue.duplicate()
|
|
fruit_queue.clear()
|
|
for fruit in to_flush:
|
|
if is_instance_valid(fruit):
|
|
fruit.in_orbit = false
|
|
fruit.collect()
|
|
await get_tree().create_timer(0.15).timeout
|
|
|
|
func _on_melee_area_body_entered(body: Node2D) -> void:
|
|
if body.is_in_group("enemies"):
|
|
if $AttackSpeed.is_stopped():
|
|
attack()
|
|
$AttackSpeed.start()
|