|
1 | 1 |
|
2 | 2 | extends RigidBody2D |
3 | 3 |
|
| 4 | +var input_states = preload("res://scripts/input_states.gd") |
| 5 | + |
4 | 6 | 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 |
5 | 12 |
|
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) |
8 | 14 |
|
| 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") |
9 | 18 |
|
| 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 |
10 | 28 |
|
11 | 29 | func _ready(): |
| 30 | + raycast_down = get_node("RayCast2D") |
| 31 | + raycast_down.add_exception(self) |
| 32 | + |
12 | 33 | # Initalization here |
13 | 34 | set_fixed_process(true) |
| 35 | + set_applied_force(Vector2(0,extra_gravity)) |
14 | 36 |
|
15 | 37 | func _fixed_process(delta): |
16 | | - btn_right = Input.is_action_pressed("btn_right") |
17 | | - btn_left = Input.is_action_pressed("btn_left") |
18 | 38 |
|
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) |
23 | 43 | 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 | + |
26 | 50 |
|
0 commit comments