Skip to content

Commit 5a1e265

Browse files
committed
Commit for Tutorial 03
1 parent 50cc935 commit 5a1e265

File tree

2 files changed

+66
-10
lines changed

2 files changed

+66
-10
lines changed

project/scripts/input_states.gd

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
### class for input handling. Returns 4 button states
2+
var input_name
3+
var prev_state
4+
var current_state
5+
var input
6+
7+
8+
var output_state
9+
var state_old
10+
11+
### Get the input name and store it
12+
func _init(var input_name):
13+
self.input_name = input_name
14+
15+
### check the input and compare it with previous states
16+
func check():
17+
input = Input.is_action_pressed(self.input_name)
18+
prev_state = current_state
19+
current_state = input
20+
21+
state_old = output_state
22+
23+
if not prev_state and not current_state:
24+
output_state = 0 ### Released
25+
if not prev_state and current_state:
26+
output_state = 1 ### Just Pressed
27+
if prev_state and current_state:
28+
output_state = 2 ### Pressed
29+
if prev_state and not current_state:
30+
output_state = 3 ### Just Released
31+
32+
return output_state

project/scripts/player.gd

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,50 @@
11

22
extends RigidBody2D
33

4+
var input_states = preload("res://scripts/input_states.gd")
5+
46
export var player_speed = 200
7+
export var jumpforce = 2000
8+
export var acceleration = 5
9+
export var extra_gravity = 400
10+
11+
var raycast_down = null
512

6-
var btn_right = Input.is_action_pressed("btn_right")
7-
var btn_left = Input.is_action_pressed("btn_left")
13+
var current_speed = Vector2(0,0)
814

15+
var btn_right = input_states.new("btn_right")
16+
var btn_left = input_states.new("btn_left")
17+
var btn_jump = input_states.new("btn_jump")
918

19+
func move(speed, acc, delta):
20+
current_speed.x = lerp(current_speed.x , speed, acc * delta)
21+
set_linear_velocity(Vector2(current_speed.x,get_linear_velocity().y))
22+
23+
func is_on_ground():
24+
if raycast_down.is_colliding():
25+
return true
26+
else:
27+
return false
1028

1129
func _ready():
30+
raycast_down = get_node("RayCast2D")
31+
raycast_down.add_exception(self)
32+
1233
# Initalization here
1334
set_fixed_process(true)
35+
set_applied_force(Vector2(0,extra_gravity))
1436

1537
func _fixed_process(delta):
16-
btn_right = Input.is_action_pressed("btn_right")
17-
btn_left = Input.is_action_pressed("btn_left")
1838

19-
if btn_left:
20-
set_linear_velocity(Vector2(-player_speed,get_linear_velocity().y))
21-
elif btn_right:
22-
set_linear_velocity(Vector2(player_speed,get_linear_velocity().y))
39+
if btn_left.check() == 2:
40+
move(-player_speed, acceleration, delta)
41+
elif btn_right.check() == 2:
42+
move(player_speed, acceleration, delta)
2343
else:
24-
set_linear_velocity(Vector2(0,get_linear_velocity().y))
25-
44+
move(0, acceleration, delta)
45+
46+
if is_on_ground():
47+
if btn_jump.check() == 1:
48+
set_axis_velocity(Vector2(0,-jumpforce))
49+
2650

0 commit comments

Comments
 (0)