-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
66 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
### class for input handling. Returns 4 button states | ||
var input_name | ||
var prev_state | ||
var current_state | ||
var input | ||
|
||
|
||
var output_state | ||
var state_old | ||
|
||
### Get the input name and store it | ||
func _init(var input_name): | ||
self.input_name = input_name | ||
|
||
### check the input and compare it with previous states | ||
func check(): | ||
input = Input.is_action_pressed(self.input_name) | ||
prev_state = current_state | ||
current_state = input | ||
|
||
state_old = output_state | ||
|
||
if not prev_state and not current_state: | ||
output_state = 0 ### Released | ||
if not prev_state and current_state: | ||
output_state = 1 ### Just Pressed | ||
if prev_state and current_state: | ||
output_state = 2 ### Pressed | ||
if prev_state and not current_state: | ||
output_state = 3 ### Just Released | ||
|
||
return output_state |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,50 @@ | ||
|
||
extends RigidBody2D | ||
|
||
var input_states = preload("res://scripts/input_states.gd") | ||
|
||
export var player_speed = 200 | ||
export var jumpforce = 2000 | ||
export var acceleration = 5 | ||
export var extra_gravity = 400 | ||
|
||
var raycast_down = null | ||
|
||
var btn_right = Input.is_action_pressed("btn_right") | ||
var btn_left = Input.is_action_pressed("btn_left") | ||
var current_speed = Vector2(0,0) | ||
|
||
var btn_right = input_states.new("btn_right") | ||
var btn_left = input_states.new("btn_left") | ||
var btn_jump = input_states.new("btn_jump") | ||
|
||
func move(speed, acc, delta): | ||
current_speed.x = lerp(current_speed.x , speed, acc * delta) | ||
set_linear_velocity(Vector2(current_speed.x,get_linear_velocity().y)) | ||
|
||
func is_on_ground(): | ||
if raycast_down.is_colliding(): | ||
return true | ||
else: | ||
return false | ||
|
||
func _ready(): | ||
raycast_down = get_node("RayCast2D") | ||
raycast_down.add_exception(self) | ||
|
||
# Initalization here | ||
set_fixed_process(true) | ||
set_applied_force(Vector2(0,extra_gravity)) | ||
|
||
func _fixed_process(delta): | ||
btn_right = Input.is_action_pressed("btn_right") | ||
btn_left = Input.is_action_pressed("btn_left") | ||
|
||
if btn_left: | ||
set_linear_velocity(Vector2(-player_speed,get_linear_velocity().y)) | ||
elif btn_right: | ||
set_linear_velocity(Vector2(player_speed,get_linear_velocity().y)) | ||
if btn_left.check() == 2: | ||
move(-player_speed, acceleration, delta) | ||
elif btn_right.check() == 2: | ||
move(player_speed, acceleration, delta) | ||
else: | ||
set_linear_velocity(Vector2(0,get_linear_velocity().y)) | ||
|
||
move(0, acceleration, delta) | ||
|
||
if is_on_ground(): | ||
if btn_jump.check() == 1: | ||
set_axis_velocity(Vector2(0,-jumpforce)) | ||
|
||
|