Skip to content

Commit 24bb55d

Browse files
committed
Commit for Tutorial 05
1 parent 5a1e265 commit 24bb55d

3 files changed

Lines changed: 139 additions & 21 deletions

File tree

project/engine.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ icon="icon.png"
88

99
btn_right=[key(Right)]
1010
btn_left=[key(Left)]
11+
btn_jump=[key(Space)]

project/level_01.xml

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<resource_file type="PackedScene" subresource_count="5" version="1.0" version_name="Godot Engine v1.0.stable.custom_build">
2+
<resource_file type="PackedScene" subresource_count="6" version="1.0" version_name="Godot Engine v1.0.stable.custom_build">
33
<ext_resource path="res://textures/blue_square.png" type="Texture"></ext_resource>
44
<ext_resource path="res://textures/red_square.png" type="Texture"></ext_resource>
5+
<ext_resource path="res://scripts/player.gd" type="Script"></ext_resource>
56
<resource type="RectangleShape2D" path="local://1">
67
<real name="custom_solver_bias"> 0 </real>
78
<vector2 name="extents"> 32, 32 </vector2>
@@ -15,7 +16,7 @@
1516
<main_resource>
1617
<dictionary name="_bundled" shared="false">
1718
<string> "names" </string>
18-
<string_array len="56">
19+
<string_array len="83">
1920
<string> "world" </string>
2021
<string> "Node2D" </string>
2122
<string> "_import_path" </string>
@@ -50,12 +51,41 @@
5051
<string> "velocity/angular" </string>
5152
<string> "damp_override/linear" </string>
5253
<string> "damp_override/angular" </string>
54+
<string> "script/script" </string>
55+
<string> "player_speed" </string>
56+
<string> "jumpforce" </string>
57+
<string> "acceleration" </string>
58+
<string> "air_acceleration" </string>
59+
<string> "extra_gravity" </string>
60+
<string> "CollisionShape2D" </string>
61+
<string> "shape" </string>
62+
<string> "trigger" </string>
63+
<string> "Camera2D" </string>
64+
<string> "offset" </string>
65+
<string> "centered" </string>
66+
<string> "rotating" </string>
67+
<string> "current" </string>
68+
<string> "smoothing" </string>
69+
<string> "zoom" </string>
70+
<string> "limit/left" </string>
71+
<string> "limit/top" </string>
72+
<string> "limit/right" </string>
73+
<string> "limit/bottom" </string>
74+
<string> "drag_margin/h_enabled" </string>
75+
<string> "drag_margin/v_enabled" </string>
76+
<string> "drag_margin/left" </string>
77+
<string> "drag_margin/top" </string>
78+
<string> "drag_margin/right" </string>
79+
<string> "drag_margin/bottom" </string>
80+
<string> "RayCast2D" </string>
81+
<string> "enabled" </string>
82+
<string> "cast_to" </string>
83+
<string> "layer_mask" </string>
84+
<string> "rotate" </string>
5385
<string> "player_sprite" </string>
5486
<string> "Sprite" </string>
5587
<string> "shader/use_parent" </string>
5688
<string> "texture" </string>
57-
<string> "centered" </string>
58-
<string> "offset" </string>
5989
<string> "flip_h" </string>
6090
<string> "flip_v" </string>
6191
<string> "vframes" </string>
@@ -64,9 +94,7 @@
6494
<string> "modulate" </string>
6595
<string> "region" </string>
6696
<string> "region_rect" </string>
67-
<string> "CollisionShape2D" </string>
68-
<string> "shape" </string>
69-
<string> "trigger" </string>
97+
<string> "player_sprite1" </string>
7098
<string> "ground" </string>
7199
<string> "StaticBody2D" </string>
72100
<string> "constant_linear_velocity" </string>
@@ -78,9 +106,9 @@
78106
<string> "conn_count" </string>
79107
<int> 0 </int>
80108
<string> "node_count" </string>
81-
<int> 7 </int>
109+
<int> 11 </int>
82110
<string> "variants" </string>
83-
<array len="26" shared="false">
111+
<array len="39" shared="false">
84112
<node_path> "" </node_path>
85113
<bool> True </bool>
86114
<real> 1 </real>
@@ -91,6 +119,16 @@
91119
<dictionary shared="false">
92120
<string> "__editor_plugin_states__" </string>
93121
<dictionary shared="false">
122+
<string> "Script" </string>
123+
<dictionary shared="false">
124+
<string> "current" </string>
125+
<int> 0 </int>
126+
<string> "sources" </string>
127+
<array len="2" shared="false">
128+
<string> "res://scripts/player.gd" </string>
129+
<string> "res://scripts/input_states.gd" </string>
130+
</array>
131+
</dictionary>
94132
<string> "2D" </string>
95133
<dictionary shared="false">
96134
<string> "pixel_snap" </string>
@@ -205,7 +243,7 @@
205243
<int> 0 </int>
206244
</dictionary>
207245
<string> "__editor_plugin_screen__" </string>
208-
<string> "2D" </string>
246+
<string> "Script" </string>
209247
</dictionary>
210248
<vector2> 297, 95 </vector2>
211249
<int> 1 </int>
@@ -214,26 +252,36 @@
214252
<bool> False </bool>
215253
<int> 2 </int>
216254
<real> -1 </real>
217-
<dictionary shared="false">
218-
<string> "_editor_collapsed" </string>
219-
<bool> True </bool>
220-
</dictionary>
255+
<resource resource_type="Script" path="res://scripts/player.gd"> </resource>
256+
<int> 400 </int>
257+
<int> 8 </int>
258+
<int> 300 </int>
259+
<vector2> 4.41074e-06, -0.270176 </vector2>
260+
<vector2> 0, -100 </vector2>
261+
<real> 7 </real>
262+
<vector2> 2, 2 </vector2>
263+
<int> -10000000 </int>
264+
<int> 10000000 </int>
265+
<real> 0.2 </real>
266+
<vector2> 0, 60 </vector2>
221267
<resource resource_type="Texture" path="res://textures/blue_square.png"> </resource>
222268
<color> 1, 1, 1, 1 </color>
223269
<rect2> 0, 0, 0, 0 </rect2>
224-
<vector2> 4.41074e-06, -0.270176 </vector2>
270+
<vector2> 35.1975, 0.902496 </vector2>
271+
<vector2> 0.2, 0.2 </vector2>
225272
<vector2> 399, 487 </vector2>
226273
<resource resource_type="Shape2D" path="local://2"> </resource>
227274
<matrix32> 1, -0, 0, 1, 0, 0 </matrix32>
228275
<dictionary shared="false">
229276
<string> "_editor_collapsed" </string>
230277
<bool> True </bool>
231278
</dictionary>
232-
<vector2> 11.8125, 1 </vector2>
279+
<vector2> 0.451187, 0 </vector2>
280+
<vector2> 12.4753, 1 </vector2>
233281
<resource resource_type="Texture" path="res://textures/red_square.png"> </resource>
234282
</array>
235283
<string> "nodes" </string>
236-
<int_array len="293"> -1, -1, 1, 0, -1, 10, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 7, 4, 8, 5, 9, 6, 10, 1, 11, 7, 0, 0, 0, 13, 12, -1, 30, 2, 0, 3, 1, 4, 2, 5, 2, 6, 8, 7, 4, 8, 5, 9, 6, 10, 1, 14, 9, 15, 10, 16, 11, 17, 12, 18, 9, 19, 13, 20, 2, 21, 4, 22, 4, 23, 2, 24, 12, 25, 6, 26, 6, 27, 12, 28, 12, 29, 1, 30, 3, 31, 4, 32, 14, 33, 14, 11, 15, 0, 1, 0, 35, 34, -1, 21, 2, 0, 3, 1, 4, 2, 5, 2, 36, 1, 6, 3, 7, 4, 8, 5, 9, 6, 10, 1, 37, 16, 38, 1, 39, 3, 40, 12, 41, 12, 42, 9, 43, 9, 44, 6, 45, 17, 46, 12, 47, 18, 0, 1, 0, 48, 48, -1, 11, 2, 0, 3, 1, 4, 2, 5, 2, 6, 19, 7, 4, 8, 5, 9, 6, 10, 1, 49, 10, 50, 12, 0, 0, 0, 52, 51, -1, 19, 2, 0, 3, 1, 4, 2, 5, 2, 6, 20, 7, 4, 8, 5, 9, 6, 10, 1, 14, 9, 15, 21, 16, 22, 17, 12, 18, 9, 53, 3, 54, 4, 21, 2, 22, 4, 11, 23, 0, 4, 0, 35, 55, -1, 20, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 7, 4, 8, 24, 9, 6, 10, 1, 37, 25, 38, 1, 39, 3, 40, 12, 41, 12, 42, 9, 43, 9, 44, 6, 45, 17, 46, 12, 47, 18, 0, 4, 0, 48, 48, -1, 11, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 7, 4, 8, 5, 9, 6, 10, 1, 49, 21, 50, 12, 0 </int_array>
284+
<int_array len="465"> -1, -1, 1, 0, -1, 10, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 7, 4, 8, 5, 9, 6, 10, 1, 11, 7, 0, 0, 0, 13, 12, -1, 35, 2, 0, 3, 1, 4, 2, 5, 2, 6, 8, 7, 4, 8, 5, 9, 6, 10, 1, 14, 9, 15, 10, 16, 11, 17, 12, 18, 9, 19, 13, 20, 2, 21, 4, 22, 4, 23, 2, 24, 12, 25, 6, 26, 6, 27, 12, 28, 12, 29, 1, 30, 3, 31, 4, 32, 14, 33, 14, 34, 15, 35, 16, 36, 16, 37, 17, 38, 9, 39, 18, 0, 1, 0, 40, 40, -1, 11, 2, 0, 3, 1, 4, 2, 5, 2, 6, 19, 7, 4, 8, 5, 9, 6, 10, 1, 41, 10, 42, 12, 0, 1, 0, 43, 43, -1, 25, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 7, 4, 8, 5, 9, 6, 10, 1, 44, 20, 45, 1, 46, 12, 47, 1, 48, 21, 49, 22, 50, 23, 51, 23, 52, 24, 53, 24, 54, 1, 55, 1, 56, 25, 57, 25, 58, 25, 59, 25, 0, 1, 0, 60, 60, -1, 12, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 7, 4, 8, 5, 9, 6, 10, 1, 61, 1, 62, 26, 63, 9, 0, 1, 0, 1, 64, -1, 9, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 7, 4, 8, 5, 9, 6, 10, 1, 0, 5, 0, 66, 65, -1, 21, 2, 0, 3, 1, 4, 2, 5, 2, 67, 1, 6, 3, 7, 4, 8, 5, 9, 6, 10, 1, 68, 27, 45, 1, 44, 3, 69, 12, 70, 12, 71, 9, 72, 9, 73, 6, 74, 28, 75, 12, 76, 29, 0, 5, 0, 66, 77, -1, 21, 2, 0, 3, 1, 4, 2, 5, 2, 67, 1, 6, 30, 7, 4, 8, 31, 9, 6, 10, 1, 68, 27, 45, 1, 44, 3, 69, 12, 70, 12, 71, 9, 72, 9, 73, 6, 74, 28, 75, 12, 76, 29, 0, 0, 0, 79, 78, -1, 19, 2, 0, 3, 1, 4, 2, 5, 2, 6, 32, 7, 4, 8, 5, 9, 6, 10, 1, 14, 9, 15, 33, 16, 34, 17, 12, 18, 9, 80, 3, 81, 4, 21, 2, 22, 4, 11, 35, 0, 8, 0, 66, 82, -1, 20, 2, 0, 3, 1, 4, 2, 5, 2, 6, 36, 7, 4, 8, 37, 9, 6, 10, 1, 68, 38, 45, 1, 44, 3, 69, 12, 70, 12, 71, 9, 72, 9, 73, 6, 74, 28, 75, 12, 76, 29, 0, 8, 0, 40, 40, -1, 11, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 7, 4, 8, 5, 9, 6, 10, 1, 41, 33, 42, 12, 0 </int_array>
237285
<string> "conns" </string>
238286
<int_array len="0"> </int_array>
239287
</dictionary>

project/scripts/player.gd

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
1+
### todo
2+
#setup Statemachine
3+
#left/right rotation
4+
#doublejump
5+
6+
17

28
extends RigidBody2D
39

410
var input_states = preload("res://scripts/input_states.gd")
511

612
export var player_speed = 200
713
export var jumpforce = 2000
8-
export var acceleration = 5
14+
export var acceleration = 7
15+
export var air_acceleration = 1
916
export var extra_gravity = 400
1017

18+
var PLAYERSTATE_PREV = ""
19+
var PLAYERSTATE = ""
20+
var PLAYERSTATE_NEXT = "ground"
21+
22+
var ORIENTATION_PREV = ""
23+
var ORIENTATION = ""
24+
var ORIENTATION_NEXT = "right"
25+
1126
var raycast_down = null
1227

1328
var current_speed = Vector2(0,0)
1429

30+
var rotate = null
31+
var jumping = 0
32+
1533
var btn_right = input_states.new("btn_right")
1634
var btn_left = input_states.new("btn_left")
1735
var btn_jump = input_states.new("btn_jump")
@@ -29,22 +47,73 @@ func is_on_ground():
2947
func _ready():
3048
raycast_down = get_node("RayCast2D")
3149
raycast_down.add_exception(self)
32-
50+
rotate = get_node("rotate")
51+
3352
# Initalization here
3453
set_fixed_process(true)
3554
set_applied_force(Vector2(0,extra_gravity))
36-
55+
56+
57+
func rotate_behavior():
58+
if ORIENTATION == "right" and ORIENTATION_NEXT == "left":
59+
rotate.set_scale(rotate.get_scale() * Vector2(-1,1))
60+
elif ORIENTATION == "left" and ORIENTATION_NEXT == "right":
61+
rotate.set_scale(rotate.get_scale() * Vector2(-1,1))
62+
63+
64+
3765
func _fixed_process(delta):
3866

67+
PLAYERSTATE_PREV = PLAYERSTATE
68+
PLAYERSTATE = PLAYERSTATE_NEXT
69+
70+
ORIENTATION_PREV = ORIENTATION
71+
ORIENTATION = ORIENTATION_NEXT
72+
73+
74+
if PLAYERSTATE == "ground":
75+
ground_state(delta)
76+
elif PLAYERSTATE == "air":
77+
air_state(delta)
78+
79+
80+
func ground_state(delta):
81+
3982
if btn_left.check() == 2:
4083
move(-player_speed, acceleration, delta)
84+
ORIENTATION_NEXT = "left"
4185
elif btn_right.check() == 2:
4286
move(player_speed, acceleration, delta)
87+
ORIENTATION_NEXT = "right"
4388
else:
4489
move(0, acceleration, delta)
4590

91+
rotate_behavior()
92+
4693
if is_on_ground():
4794
if btn_jump.check() == 1:
4895
set_axis_velocity(Vector2(0,-jumpforce))
49-
96+
jumping = 1
97+
else:
98+
PLAYERSTATE_NEXT = "air"
99+
50100

101+
func air_state(delta):
102+
103+
104+
if btn_left.check() == 2:
105+
move(-player_speed, air_acceleration, delta)
106+
ORIENTATION_NEXT = "left"
107+
elif btn_right.check() == 2:
108+
move(player_speed, air_acceleration, delta)
109+
ORIENTATION_NEXT = "right"
110+
else:
111+
move(0, air_acceleration, delta)
112+
113+
if btn_jump.check() == 1 and jumping == 1:
114+
set_axis_velocity(Vector2(0,-jumpforce))
115+
jumping += 1
116+
117+
rotate_behavior()
118+
if is_on_ground():
119+
PLAYERSTATE_NEXT = "ground"

0 commit comments

Comments
 (0)