This repository has been archived by the owner on Aug 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummoner.gd
96 lines (78 loc) · 2.73 KB
/
summoner.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
class_name Summoner
extends CharacterBody3D
@export var speed: int = 10
@export var stuck_speed: int = 10
@export var fall_acceleration: int = 75
@export var jump_impulse: int = 20
@export var mass: float = 1.0
var force_accumulator: Vector3 = Vector3.ZERO
var target_velocity: Vector3 = Vector3.ZERO
var fixed_z: float = 0
var stuck: bool = false
func _ready():
fixed_z = self.position.z
func _stuck_movement(delta: float):
var l: int = Input.is_action_pressed("move_left")
var r: int = Input.is_action_pressed("move_right")
var u: int = Input.is_action_pressed("move_up")
var d: int = Input.is_action_pressed("move_down")
var direction: Vector3 = Vector3(r - l, u - d, 0)
target_velocity = direction*stuck_speed
if direction.x == 0 and direction.y == 0:
$AnimationPlayer.set_current_animation("idle")
else:
$AnimationPlayer.set_current_animation("walk")
# facing
if direction.x < 0:
$Pivot.rotation.y = - PI / 6
elif direction.x > 0:
$Pivot.rotation.y = + PI / 6
# if Input.is_action_just_pressed("jump"):
# target_velocity.y = jump_impulse
# stuck = false
var impulse: Vector3 = (force_accumulator / mass) * delta
target_velocity += impulse
velocity = target_velocity
func _normal_movement(delta: float):
var l: int = Input.is_action_pressed("move_left")
var r: int = Input.is_action_pressed("move_right")
var direction: Vector3 = Vector3(r - l, 0, 0)
target_velocity.x = direction.x * speed
target_velocity.z = direction.z * speed
if is_on_floor():
if direction.x == 0 and direction.y == 0:
$AnimationPlayer.set_current_animation("idle")
else:
$AnimationPlayer.set_current_animation("walk")
# facing
if direction.x < 0:
$Pivot.rotation.y = - PI / 6
elif direction.x > 0:
$Pivot.rotation.y = + PI / 6
# falling
if not is_on_floor():
if is_on_ceiling():
target_velocity.y = 0
target_velocity.y -= fall_acceleration * delta
# jumping
elif is_on_floor() and Input.is_action_just_pressed("jump"):
# print("jumping")
target_velocity.y = jump_impulse
var impulse: Vector3 = (force_accumulator / mass) * delta
target_velocity += impulse
velocity = target_velocity
func play_ufo(p_forward: bool = true):
if p_forward:
%UfoAnimationPlayer.play("spawn")
%AudioStreamPlayer.play()
else:
%UfoAnimationPlayer.play("despawn")
await %UfoAnimationPlayer.animation_finished
func _physics_process(delta):
self.position.z = fixed_z
if stuck:
_stuck_movement(delta)
else:
_normal_movement(delta)
move_and_slide()
force_accumulator = Vector3.ZERO