-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCharacter3D.gd
83 lines (70 loc) · 2.22 KB
/
Character3D.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
extends KinematicBody
onready var smp = $StateMachinePlayer
export var speed = 1.0
export var damping = 0.1
var velocity = Vector3.ZERO
var walk = Vector3.ZERO
var _last_jump = 0
var _jump_count = 0
func _ready():
smp.connect("updated", self, "_on_StateMachinePlayer_updated")
func _physics_process(delta):
if Input.is_action_pressed("ui_left"):
walk += Vector3.LEFT
if Input.is_action_pressed("ui_right"):
walk += Vector3.RIGHT
if Input.is_action_pressed("ui_down"):
walk += Vector3.BACK
if Input.is_action_pressed("ui_up"):
walk += Vector3.FORWARD
walk = walk.normalized()
smp.set_param("on_floor", is_on_floor())
smp.set_param("walk", walk.abs().length())
# NOTE: It is more efficient to run directly in _physics_process, this demo is to showcase how to handle logic in updated signal
# velocity += Vector3.DOWN * 9.8 * delta
# match smp.get_current():
# "Idle":
# pass
# "Walk":
# velocity += walk * speed * delta
# walk = Vector3.ZERO
# "Jump":
# _jump_count = 0
# jump()
# smp.set_param("jump_count", _jump_count)
# "Jump(n)":
# jump()
# smp.set_param("jump_count", _jump_count)
# "Fall":
# smp.set_param("jump_elapsed", OS.get_system_time_msecs() - _last_jump)
# velocity = move_and_slide(velocity, Vector3.UP)
# velocity.x *= pow(1.0 - damping, delta)
# velocity.z *= pow(1.0 - damping, delta)
func _unhandled_key_input(event):
if Input.is_action_just_pressed("ui_accept"):
smp.set_trigger("space")
func jump():
velocity += Vector3.UP * 10
_last_jump = OS.get_system_time_msecs()
_jump_count += 1
func _on_StateMachinePlayer_updated(state, delta):
# NOTE: It is more efficient to run directly in _physics_process, this demo is to showcase how to handle logic in updated signal
velocity += Vector3.DOWN * 9.8 * delta
match state:
"Idle":
pass
"Walk":
velocity += walk * speed * delta
walk = Vector3.ZERO
"Jump":
_jump_count = 0
jump()
smp.set_param("jump_count", _jump_count)
"Jump(n)":
jump()
smp.set_param("jump_count", _jump_count)
"Fall":
smp.set_param("jump_elapsed", OS.get_system_time_msecs() - _last_jump)
velocity = move_and_slide(velocity, Vector3.UP)
velocity.x *= pow(1.0 - damping, delta)
velocity.z *= pow(1.0 - damping, delta)