2026-05-14 14:04:56 +00:00
|
|
|
extends CharacterBody3D
|
|
|
|
|
|
|
|
|
|
const SPEED = 2.5
|
|
|
|
|
const JUMP_VELOCITY = 3.5
|
|
|
|
|
|
|
|
|
|
var walk_animation_time = 0
|
2026-05-14 15:57:50 +00:00
|
|
|
var face_rotation = 0
|
2026-05-14 14:04:56 +00:00
|
|
|
var face_left = false
|
|
|
|
|
|
|
|
|
|
var combatable = true
|
|
|
|
|
|
2026-05-14 14:41:51 +00:00
|
|
|
var iframes := 0.0
|
|
|
|
|
var knockback_time := 0.0
|
|
|
|
|
var health := 100.0
|
2026-05-14 14:04:56 +00:00
|
|
|
|
|
|
|
|
|
2026-05-14 14:41:51 +00:00
|
|
|
var current_weapon := "test_sword"
|
2026-05-14 14:04:56 +00:00
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
STATE_IDLE,
|
|
|
|
|
STATE_APPROACH_ENEMY,
|
|
|
|
|
STATE_CHARGE_ATTACK,
|
2026-05-14 14:41:51 +00:00
|
|
|
STATE_ATTACK,
|
|
|
|
|
STATE_HIT,
|
|
|
|
|
STATE_DEAD,
|
2026-05-14 14:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
2026-05-14 14:41:51 +00:00
|
|
|
var state = STATE_APPROACH_ENEMY
|
|
|
|
|
var state_timer := 0.0
|
|
|
|
|
|
2026-05-14 14:04:56 +00:00
|
|
|
@onready var game = get_parent()
|
|
|
|
|
@onready var player = game.get_node("Player")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@onready var navigation_agent: NavigationAgent3D = $Navigator
|
|
|
|
|
|
|
|
|
|
func hit_me():
|
|
|
|
|
iframes = 0.5
|
|
|
|
|
health -= 10
|
|
|
|
|
|
|
|
|
|
func get_my_weapon_info(value: String):
|
|
|
|
|
return get_weapon_info(current_weapon, value)
|
|
|
|
|
|
|
|
|
|
func get_weapon_info(weapon: String, value: String):
|
|
|
|
|
if value in Global.weapons[weapon]:
|
|
|
|
|
return Global.weapons[weapon][value]
|
|
|
|
|
else: return null
|
|
|
|
|
|
2026-05-14 14:41:51 +00:00
|
|
|
func on_knockback(hitter: Node3D = null) -> void:
|
|
|
|
|
state = STATE_HIT
|
|
|
|
|
state_timer = 0.5
|
2026-05-14 14:04:56 +00:00
|
|
|
|
|
|
|
|
func _ready() -> void:
|
2026-05-14 14:41:51 +00:00
|
|
|
$DreamerBody/Animator.set("parameters/hold_weapon/blend_amount", 1.0)
|
2026-05-14 14:04:56 +00:00
|
|
|
|
|
|
|
|
func _process(delta: float) -> void:
|
2026-05-14 15:57:50 +00:00
|
|
|
$DreamerBody.rotation.x = 0
|
|
|
|
|
face_rotation = player.face_rotation
|
|
|
|
|
|
2026-05-14 14:04:56 +00:00
|
|
|
if iframes > 0:
|
|
|
|
|
iframes -= delta
|
|
|
|
|
$DreamerBody/Body.modulate.a = 0.5
|
|
|
|
|
else:
|
|
|
|
|
$DreamerBody/Body.modulate.a = 1
|
|
|
|
|
|
|
|
|
|
if knockback_time > 0:
|
|
|
|
|
knockback_time -= delta
|
|
|
|
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
|
|
|
if not is_on_floor():
|
|
|
|
|
velocity += get_gravity() * delta
|
|
|
|
|
|
|
|
|
|
var viewport_half = (get_viewport().get_visible_rect().size / 2.0)
|
|
|
|
|
var player_position = Vector2.ZERO #camera.unproject_position(global_transform.origin)
|
|
|
|
|
|
|
|
|
|
if face_left:
|
|
|
|
|
$DreamerBody.rotation.y = face_rotation + deg_to_rad(180)
|
|
|
|
|
$DreamerBody/Animator.set("parameters/layering/blend_amount", 1.0)
|
|
|
|
|
else:
|
|
|
|
|
$DreamerBody.rotation.y = face_rotation
|
|
|
|
|
$DreamerBody/Animator.set("parameters/layering/blend_amount", 0.0)
|
|
|
|
|
|
|
|
|
|
var mouse_point_angle = player_position.angle_to_point(get_viewport().get_mouse_position())
|
|
|
|
|
|
|
|
|
|
if combatable:
|
|
|
|
|
#$DreamerBody/Body/ArmPivot.rotation.z = mouse_point_angle
|
|
|
|
|
|
|
|
|
|
if abs(rad_to_deg(mouse_point_angle) - (-90)) < 50:
|
|
|
|
|
$HitCollision/Shape.shape.size.x = 1.5
|
|
|
|
|
else:
|
|
|
|
|
$HitCollision/Shape.shape.size.x = 1.2
|
|
|
|
|
|
|
|
|
|
if (rad_to_deg(mouse_point_angle) < -90) or (rad_to_deg(mouse_point_angle) > 90):
|
2026-05-15 16:21:50 +00:00
|
|
|
#face_left = true
|
2026-05-14 14:04:56 +00:00
|
|
|
|
|
|
|
|
var mouse_pos = get_viewport().get_mouse_position()
|
|
|
|
|
mouse_pos.y = (viewport_half.y) + ((viewport_half.y) - mouse_pos.y)
|
|
|
|
|
|
|
|
|
|
#$DreamerBody/Body/ArmPivot.rotation.z = viewport_half.angle_to_point(mouse_pos)
|
|
|
|
|
else:
|
2026-05-15 16:21:50 +00:00
|
|
|
#face_left = false
|
2026-05-14 14:04:56 +00:00
|
|
|
$DreamerBody/Body/ArmPivot.scale.y = 1
|
|
|
|
|
$DreamerBody/Body/ArmPivot.scale.x = 1
|
|
|
|
|
else:
|
|
|
|
|
$DreamerBody/Body/ArmPivot.global_rotation.y = face_rotation
|
|
|
|
|
$DreamerBody/Body/ArmPivot.scale.y = 1
|
|
|
|
|
$DreamerBody/Body/ArmPivot.scale.x = 1
|
|
|
|
|
$DreamerBody/Body/ArmPivot.rotation.z = 0
|
|
|
|
|
|
|
|
|
|
if false:
|
|
|
|
|
velocity.y = JUMP_VELOCITY
|
|
|
|
|
$DreamerBody/Animator.set("parameters/jump/request", AnimationNodeOneShot.ONE_SHOT_REQUEST_FIRE)
|
|
|
|
|
|
2026-05-14 16:40:50 +00:00
|
|
|
var direction = Vector3.ZERO
|
2026-05-14 14:41:51 +00:00
|
|
|
|
|
|
|
|
state_timer -= delta
|
|
|
|
|
|
|
|
|
|
if state == STATE_APPROACH_ENEMY:
|
|
|
|
|
navigation_agent.set_target_position(player.global_position)
|
|
|
|
|
|
|
|
|
|
var next_path_position: Vector3 = navigation_agent.get_next_path_position()
|
|
|
|
|
direction = global_position.direction_to(next_path_position)
|
|
|
|
|
|
|
|
|
|
if (player.global_position - global_position).length() < 1:
|
|
|
|
|
state = STATE_CHARGE_ATTACK
|
|
|
|
|
state_timer = 1
|
|
|
|
|
|
|
|
|
|
$DreamerBody/Animator.set("parameters/charge_weapon/request", AnimationNodeOneShot.ONE_SHOT_REQUEST_FIRE)
|
|
|
|
|
elif state == STATE_CHARGE_ATTACK:
|
|
|
|
|
$HitCollision.look_at(player.global_position)
|
|
|
|
|
$HitCollision.rotation.x = 0
|
|
|
|
|
|
|
|
|
|
if state_timer <= 0:
|
|
|
|
|
state = STATE_ATTACK
|
|
|
|
|
$DreamerBody/Animator.set("parameters/swing_weapon/request", AnimationNodeOneShot.ONE_SHOT_REQUEST_FIRE)
|
|
|
|
|
|
|
|
|
|
var slash = load("res://scenes/effects/slash.tscn").instantiate()
|
|
|
|
|
game.add_child(slash)
|
|
|
|
|
slash.global_position = global_position
|
|
|
|
|
slash.global_rotation.y = $HitCollision.global_rotation.y + deg_to_rad(90)
|
|
|
|
|
slash.play_effect()
|
2026-05-14 14:04:56 +00:00
|
|
|
|
2026-05-14 14:41:51 +00:00
|
|
|
if player in $HitCollision.get_overlapping_bodies():
|
|
|
|
|
if player.iframes <= 0:
|
|
|
|
|
player.health -= 20
|
|
|
|
|
player.on_hit(self)
|
|
|
|
|
|
|
|
|
|
state_timer = 0.5
|
|
|
|
|
elif state == STATE_ATTACK:
|
|
|
|
|
if state_timer <= 0:
|
|
|
|
|
state = STATE_APPROACH_ENEMY
|
|
|
|
|
elif state == STATE_HIT:
|
|
|
|
|
$DreamerBody/Animator.set("parameters/charge_weapon/request", AnimationNodeOneShot.ONE_SHOT_REQUEST_ABORT)
|
2026-05-15 16:21:50 +00:00
|
|
|
$DreamerBody/Animator.set("parameters/hit/blend_amount", 1.0)
|
|
|
|
|
$DreamerBody/Animator.set("parameters/dead/blend_amount", 0.0)
|
|
|
|
|
|
|
|
|
|
if (health <= 0) and ((state_timer <= 0) or is_on_floor()):
|
|
|
|
|
state = STATE_DEAD
|
2026-05-14 14:04:56 +00:00
|
|
|
|
2026-05-14 14:41:51 +00:00
|
|
|
if state_timer <= 0:
|
2026-05-15 16:21:50 +00:00
|
|
|
$DreamerBody/Animator.set("parameters/hit/blend_amount", 0.0)
|
|
|
|
|
|
2026-05-14 14:41:51 +00:00
|
|
|
state = STATE_APPROACH_ENEMY
|
2026-05-15 16:21:50 +00:00
|
|
|
elif state == STATE_DEAD:
|
|
|
|
|
if is_on_floor():
|
|
|
|
|
$DreamerBody/Animator.set("parameters/dead/blend_amount", 1.0)
|
|
|
|
|
else:
|
|
|
|
|
$DreamerBody/Animator.set("parameters/dead/blend_amount", 0.0)
|
2026-05-14 16:40:50 +00:00
|
|
|
|
|
|
|
|
var camera_right = game.get_node("PlayerCamera").global_transform.basis.x
|
2026-05-14 14:04:56 +00:00
|
|
|
|
2026-05-14 16:40:50 +00:00
|
|
|
if typeof(camera_right) == TYPE_VECTOR3:
|
|
|
|
|
var dot_direction = direction.dot(camera_right)
|
|
|
|
|
|
|
|
|
|
if dot_direction > 0:
|
|
|
|
|
face_left = false
|
|
|
|
|
if dot_direction < 0:
|
|
|
|
|
face_left = true
|
2026-05-14 14:04:56 +00:00
|
|
|
|
|
|
|
|
if direction and (knockback_time <= 0):
|
|
|
|
|
velocity.x = direction.x * SPEED
|
|
|
|
|
velocity.z = direction.z * SPEED
|
|
|
|
|
|
|
|
|
|
$DreamerBody/Animator.set("parameters/walk/blend_amount", abs(direction.length()))
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
|
|
velocity.x = move_toward(velocity.x, 0, delta * 4)
|
|
|
|
|
velocity.z = move_toward(velocity.z, 0, delta * 4)
|
|
|
|
|
|
|
|
|
|
$DreamerBody/Animator.set("parameters/walk/blend_amount", 0)
|
|
|
|
|
|
|
|
|
|
if direction and is_on_floor():
|
|
|
|
|
walk_animation_time += delta * 25
|
|
|
|
|
|
|
|
|
|
$DreamerBody.position.y = sin(walk_animation_time + 1) / 16
|
|
|
|
|
$DreamerBody.rotation.z = sin(walk_animation_time / 2) / 16
|
|
|
|
|
else:
|
|
|
|
|
walk_animation_time = 0
|
|
|
|
|
|
|
|
|
|
$DreamerBody.rotation.z /= 1 + (delta * 2.5)
|
|
|
|
|
|
|
|
|
|
$DreamerBody.position.y /= 1 + (delta * 12)
|
|
|
|
|
|
|
|
|
|
if is_on_floor():
|
|
|
|
|
$DreamerBody/Animator.set("parameters/fall/blend_amount", 0)
|
|
|
|
|
$DreamerBody/Animator.set("parameters/leap/blend_amount", 0)
|
|
|
|
|
else:
|
|
|
|
|
if velocity.y < 0:
|
|
|
|
|
$DreamerBody/Animator.set("parameters/fall/blend_amount", 1)
|
|
|
|
|
$DreamerBody/Animator.set("parameters/leap/blend_amount", 0)
|
|
|
|
|
else:
|
|
|
|
|
$DreamerBody/Animator.set("parameters/fall/blend_amount", 0)
|
|
|
|
|
$DreamerBody/Animator.set("parameters/leap/blend_amount", 1)
|
|
|
|
|
|
|
|
|
|
move_and_slide()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$Shadow.global_position.y = $Floorcast.get_collision_point().y + 0.01
|
|
|
|
|
|