forked from geegaz/Multiple-Windows-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacter.gd
More file actions
150 lines (125 loc) · 3.92 KB
/
Character.gd
File metadata and controls
150 lines (125 loc) · 3.92 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
extends CharacterBody2D
# From A Key(s) Path
# Movement exports
@export_group("Movement")
@export var max_speed: float = 120.0 # Player max speed in px/s
@export var jump_height: float = 40.0 # px
#@export var gravity: float = 310.0 # px/s/s
#@export var gravity_strong: float = 650.0 # px/s/s
@export var gravity: float = 0.0 # px/s/s
@export var gravity_strong: float = 0.0 # px/s/s
@export var acceleration: float = 512.0 # px/s/s
@export var deceleration: float = 1024.0 # px/s/s
# Buffers
@export_group("Buffers")
@export var air_buffer = 0.1
@export var jump_buffer = 0.07
@onready var _Sprite = $Sprite
@onready var _Collision = $Collider
@onready var _AnimationTree: AnimationTree = $AnimationTree
@onready var _StateMachine: AnimationNodeStateMachinePlayback = _AnimationTree["parameters/playback"]
# Provided values
var dir: float = 0.0
var jump: bool = false
var target_speed: float = 0.0
var target_accel: float = 0.0
var target_gravity: float = gravity_strong
var air_time = air_buffer
var jump_time = jump_buffer
var on_ground: bool = false
# New variable to control manual movement
var manual_control: bool = true
func _process(_delta):
#var mouse_position = get_global_mouse_position()
#var pointerKazem = (mouse_position - position).normalized()
#
#position += pointerKazem * 200 * _delta
#if pointerKazem.x > 0:
#target_speed = 1.0
#elif pointerKazem.x < 0:
#target_speed = -1.0
#elif pointerKazem.x == 0:
#target_speed = 0
if manual_control:
handle_manual_control(_delta)
else:
# Only update the animation if manual control is off
if target_speed < 0.0:
_Sprite.flip_h = true
elif target_speed > 0.0:
_Sprite.flip_h = false
# Animation states
if on_ground:
if target_speed != 0.0:
_StateMachine.travel("run")
else:
_StateMachine.travel("idle")
else:
if velocity.y >= 0.0:
_StateMachine.travel("fall")
func _physics_process(delta):
if manual_control:
handle_manual_physics(delta)
return # Skip the default behavior if manual control is enabled
# Get horizontal movement direction
# Direction is provided by a Movement Provider -> either a Player or IA
# Vertical movement
if velocity.y > 0 or (not jump and jump_time < jump_buffer):
target_gravity = gravity_strong
velocity.y += target_gravity * delta
# Horizontal movement
target_speed = dir * max_speed
target_accel = acceleration if dir and sign(dir) == sign(velocity.x) else deceleration
velocity.x = move_toward(velocity.x, target_speed, target_accel * delta)
# Apply velocity
var collision = move_and_slide()
var landed = is_on_floor() and not on_ground
# Update buffers
if jump:
jump_time = 0.0 # Reset jump time
on_ground = is_on_floor()
if on_ground:
air_time = 0.0 # Reset air time
else:
air_time = min(air_time + delta, air_buffer)
jump_time = min(jump_time + delta, jump_buffer)
# Apply jump / landing
if jump_time < jump_buffer and air_time < air_buffer:
do_jump()
elif landed:
do_land()
func handle_manual_control(_delta):
var input_dir = Vector2.ZERO
if Input.is_action_pressed("ui_right"):
input_dir.x += 1
if Input.is_action_pressed("ui_left"):
input_dir.x -= 1
if Input.is_action_pressed("ui_down"):
input_dir.y += 1
if Input.is_action_pressed("ui_up"):
input_dir.y -= 1
position += input_dir.normalized() * max_speed * _delta
func handle_manual_physics(delta):
# Handle gravity for manual control
if Input.is_action_just_pressed("ui_up") and on_ground:
velocity.y = -sqrt(jump_height * 2.0 * gravity)
on_ground = false
if not on_ground:
velocity.y += gravity * delta
position.y += velocity.y * delta
# Reset on_ground if landed
if is_on_floor():
on_ground = true
velocity.y = 0
func do_jump() -> void:
# Movement variables
velocity.y = -sqrt(jump_height * 2.0 * gravity)
target_gravity = gravity
# Status variables
jump_time = jump_buffer
air_time = air_buffer
on_ground = false
# Animation and effects
_StateMachine.travel("jump")
func do_land() -> void:
pass