Skip to content

Commit 4ffd155

Browse files
committed
new bounce on the X walls and new friction system and bug fixies
1 parent d553ca9 commit 4ffd155

7 files changed

Lines changed: 135 additions & 21 deletions

File tree

main.gd

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
extends Node2D
22

3-
var direction := Vector2.ZERO
4-
export var velocity := 120
3+
export var max_speed := 120
4+
export var acceleration := 50
55
export 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
713
var window_position_delta := Vector2.ZERO
814
var is_grabbed := false
915

16+
var last_positions := []
17+
var velocity := Vector2.ZERO
18+
19+
1020
func _ready():
21+
1122
window_position_delta = OS.window_position
1223
randomize()
1324
$StateMachine.init(self)
1425

1526
func _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

2035
func _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

5791
func 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

64101
func 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]

main.tscn

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[gd_scene load_steps=8 format=2]
1+
[gd_scene load_steps=9 format=2]
22

33
[ext_resource path="res://icon.png" type="Texture" id=1]
44
[ext_resource path="res://main.gd" type="Script" id=2]
@@ -7,16 +7,19 @@
77
[ext_resource path="res://pet/scripts/WALK.gd" type="Script" id=5]
88
[ext_resource path="res://pet/scripts/FALL.gd" type="Script" id=6]
99
[ext_resource path="res://pet/scripts/GRAB.gd" type="Script" id=7]
10+
[ext_resource path="res://pet/scripts/JUMP.gd" type="Script" id=8]
1011

1112
[node name="Node2D" type="Node2D"]
1213
script = ExtResource( 2 )
14+
acceleration = 100
1315

1416
[node name="Icon" type="Sprite" parent="."]
1517
position = Vector2( 32, 32 )
1618
texture = ExtResource( 1 )
1719

1820
[node name="Timer" type="Timer" parent="."]
19-
wait_time = 4.0
21+
wait_time = 0.025
22+
autostart = true
2023

2124
[node name="StateMachine" type="Node" parent="."]
2225
script = ExtResource( 3 )
@@ -39,3 +42,11 @@ script = ExtResource( 6 )
3942

4043
[node name="GRAB" type="Node" parent="StateMachine"]
4144
script = ExtResource( 7 )
45+
46+
[node name="JUMP" type="Node" parent="StateMachine"]
47+
script = ExtResource( 8 )
48+
49+
[node name="Timer" type="Timer" parent="StateMachine/JUMP"]
50+
one_shot = true
51+
52+
[connection signal="timeout" from="Timer" to="." method="_on_Timer_timeout"]

pet/scripts/FALL.gd

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,31 @@ extends State
22

33

44
func enter(_ls):
5+
parent.modulate = Color(0.3, 0.6, 0.9)
56
pass
67

7-
func _physics_process(_delta):
8+
func process_physics(_delta):
9+
if OS.window_position.x < 0:
10+
if parent.motion.x < 0:
11+
parent.motion.x *= -parent.bounce
12+
13+
parent.window_position_delta.x = 0
14+
parent.motion.y /= 2
15+
16+
if OS.window_position.x + OS.window_size.x > OS.get_screen_size().x:
17+
if parent.motion.x > 0:
18+
parent.motion.x *= -parent.bounce
19+
20+
parent.window_position_delta.x = OS.get_screen_size().x - OS.window_size.x
21+
parent.motion.y /= 2
22+
23+
if OS.window_position.y < 0:
24+
parent.motion.y = 0
25+
parent.window_position_delta.y = 0
26+
27+
parent.motion.x /= 2
828

9-
pass
10-
1129
func process_state():
1230
if parent.onFloor():
1331
return "IDLE"
32+

pet/scripts/GRAB.gd

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
extends State
22

33
func enter(_ls):
4+
parent.modulate = Color(0.7, 0.6, 0.2)
45
parent.direction = Vector2.ZERO
56

6-
func _physics_process(_delta):
7+
func physics_process(_delta):
78

89
pass
910

1011
func process_state():
1112
if not parent.is_grabbed:
12-
return "IDLE"
13+
return "FALL"
1314
pass
15+
16+
func exit():
17+
parent.motion = parent.velocity * 10

pet/scripts/IDLE.gd

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,35 @@ extends State
22

33
onready var time := $Timer
44

5+
var nextState := ["WALK", "JUMP", "WALK", "WALK", "WALK"]
6+
57
func enter(_ls):
8+
parent.modulate = Color(1, 0.3, 0.6)
69
parent.direction.x = 0
710
time.wait_time = rand_range(2, 5)
811
time.start()
912

13+
func process_physics(_delta):
14+
if OS.window_position.x < 0:
15+
if parent.motion.x < 0:
16+
parent.motion.x *= -parent.bounce
17+
18+
parent.window_position_delta.x = 0
19+
parent.motion.y /= 2
20+
21+
if OS.window_position.x + OS.window_size.x > OS.get_screen_size().x:
22+
if parent.motion.x > 0:
23+
parent.motion.x *= -parent.bounce
24+
25+
parent.window_position_delta.x = OS.get_screen_size().x - OS.window_size.x
26+
parent.motion.y /= 2
27+
1028
func process_state():
29+
if not parent.onFloor():
30+
return "FALL"
31+
1132
if time.is_stopped():
12-
return "WALK"
33+
nextState.shuffle()
34+
return nextState[0]
35+
1336

pet/scripts/JUMP.gd

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
extends State
2+
3+
4+
func enter(_ls):
5+
parent.modulate = Color(0.3, 1, 0.1)
6+
parent.direction.x = 1 - ((randi() % 2) * 2)
7+
8+
var movement : Vector2 = Vector2.DOWN * parent.jump_force * rand_range(0.3, 1.2)
9+
parent.motion.y = movement.y
10+
11+
$Timer.start()
12+
13+
func process_state():
14+
if parent.motion.y >= 0:
15+
return "FALL"

pet/scripts/WALK.gd

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ extends State
22

33
onready var time := $Timer
44

5+
var nextState := ["IDLE", "JUMP", "IDLE", "IDLE", "WALK", "WALK"]
6+
57
func enter(_ls):
8+
parent.modulate = Color(0.8, 0.6, 0.9)
69
parent.direction.x = 1 - ((randi() % 2) * 2)
710

8-
911
time.wait_time = rand_range(0.5, 12)
1012
time.start()
1113

12-
func _physics_process(_delta):
13-
14-
pass
15-
1614
func process_state():
1715
if time.is_stopped():
18-
return "IDLE"
16+
nextState.shuffle()
17+
return nextState[0]
1918

0 commit comments

Comments
 (0)