Skip to content

Commit

Permalink
Commit for Tutorial 03
Browse files Browse the repository at this point in the history
  • Loading branch information
ndee85 committed Jan 24, 2015
1 parent 50cc935 commit 5a1e265
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
32 changes: 32 additions & 0 deletions project/scripts/input_states.gd
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
44 changes: 34 additions & 10 deletions project/scripts/player.gd
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))


0 comments on commit 5a1e265

Please sign in to comment.