11extends Node2D
22
3- var direction := Vector2 . ZERO
4- export var velocity := 120
3+ export var max_speed := 120
4+ export var acceleration := 50
55export var have_gravity := true
6- export var gravity_force := 400
6+ export var gravity_force := 200
7+ export var jump_force := - 400
8+ export var friction_force := 3.8
9+ export var bounce := 0.5
10+
11+ var motion := Vector2 .ZERO
12+ var direction := Vector2 .ZERO
713var window_position_delta := Vector2 .ZERO
814var is_grabbed := false
915
16+ var last_positions := []
17+ var velocity := Vector2 .ZERO
18+
19+
1020func _ready ():
21+
1122 window_position_delta = OS .window_position
1223 randomize ()
1324 $ StateMachine .init (self )
1425
1526func _physics_process (delta ):
1627 $ StateMachine .processMachine (delta )
1728 if not is_grabbed :
29+ moveMotion (delta )
1830 move (delta )
31+ if OS .window_position .y < 0 :
32+ motion .y = 0
33+
1934
2035func _input (event ):
2136 if event is InputEventMouseButton :
@@ -35,12 +50,14 @@ func move(delta):
3550 if window_position_delta .x <= 0 and direction .x < 0 :
3651 window_position_delta .x = 0
3752 direction .x = 1
53+ motion .x *= - 1
3854
3955 if window_position_delta .x >= OS .get_screen_size ().x - (OS .window_size .x ) and direction .x > 0 :
4056 window_position_delta .x = OS .get_screen_size ().x - (OS .window_size .x )
4157 direction .x = - 1
58+ motion .x *= - 1
4259
43- var movement : Vector2 = direction * delta * velocity
60+ var movement : Vector2 = motion * delta
4461
4562 window_position_delta += movement
4663
@@ -52,14 +69,40 @@ func move(delta):
5269
5370 OS .window_position = window_position_delta
5471
72+ func moveMotion (delta ):
73+
74+ if direction .x > 0 and motion .x < max_speed :
75+ motion .x += acceleration * delta
76+ if motion .x > max_speed :
77+ motion .x = max_speed
78+
79+ if direction .x < 0 and motion .x > - max_speed :
80+ motion .x -= acceleration * delta
81+ if motion .x < - max_speed :
82+ motion .x = - max_speed
83+
84+ if not direction .x and onFloor ():
85+ var lastDirection := sign (motion .x )
86+ motion .x -= friction_force * motion .x * delta
87+ if lastDirection != sign (motion .x ):
88+ motion .x = 0
5589
5690
5791func gravity (delta ):
58- if not onFloor (): return
92+ if onFloor ():
93+ if motion .y > 0 :
94+ motion .y = 0
95+ return
5996
6097 var movement : Vector2 = Vector2 .DOWN * gravity_force * delta
6198
62- window_position_delta += movement
99+ motion += movement
63100
64101func onFloor ():
65- return OS .window_position .y < OS .get_screen_size ().y - (OS .window_size .y )
102+ return OS .window_position .y >= OS .get_screen_size ().y - (OS .window_size .y )
103+
104+ func _on_Timer_timeout ():
105+ last_positions .append (OS .window_position )
106+ if last_positions .size () > 2 :
107+ last_positions .pop_front ()
108+ velocity = last_positions [1 ] - last_positions [0 ]
0 commit comments