wasd movement
parent
25e72421c3
commit
83faf15858
@ -0,0 +1,66 @@
|
||||
extends Node
|
||||
|
||||
# Player AI
|
||||
var ignore_input = false
|
||||
var velocity_sum = Vector2(0.0,0.0)
|
||||
|
||||
func set_ignore_input(new_bool):
|
||||
ignore_input = new_bool
|
||||
func get_ignore_input():
|
||||
return ignore_input
|
||||
func get_user():
|
||||
return get_parent()
|
||||
|
||||
# Get Player Input
|
||||
func get_input():
|
||||
if not ignore_input:
|
||||
get_movement_input()
|
||||
|
||||
# Get Movement Key Input
|
||||
func get_movement_input():
|
||||
if Input.is_action_pressed('ui_right'):
|
||||
movement_key_pressed(1)
|
||||
if Input.is_action_pressed('ui_left'):
|
||||
movement_key_pressed(2)
|
||||
if Input.is_action_pressed('ui_up'):
|
||||
movement_key_pressed(3)
|
||||
if Input.is_action_pressed('ui_down'):
|
||||
movement_key_pressed(4)
|
||||
if Input.is_action_just_released("ui_right"):
|
||||
movement_key_released(1)
|
||||
if Input.is_action_just_released("ui_left"):
|
||||
movement_key_released(2)
|
||||
if Input.is_action_just_released("ui_up"):
|
||||
movement_key_released(3)
|
||||
if Input.is_action_just_released("ui_down"):
|
||||
movement_key_released(4)
|
||||
|
||||
# WASD Movement
|
||||
func movement_key_pressed(direction):
|
||||
get_user().set_internal_velocity(Vector2(0.0,0.0))
|
||||
if direction == 1: # right
|
||||
velocity_sum.x = 0
|
||||
velocity_sum.x += 1
|
||||
elif direction == 2: # left
|
||||
velocity_sum.x = 0
|
||||
velocity_sum.x -= 1
|
||||
elif direction == 3: # up
|
||||
velocity_sum.y = 0
|
||||
velocity_sum.y -= 1
|
||||
elif direction == 4: # down
|
||||
velocity_sum.y = 0
|
||||
velocity_sum.y += 1
|
||||
# Velocity cannot be faster with more vectors
|
||||
get_user().set_internal_velocity(velocity_sum)
|
||||
|
||||
func movement_key_released(direction):
|
||||
if direction == 1 or direction == 2: # left/right
|
||||
velocity_sum.x = 0
|
||||
elif direction == 3 or direction == 4: # up/down
|
||||
velocity_sum.y = 0
|
||||
get_user().set_internal_velocity(velocity_sum)
|
||||
|
||||
# Physics Process
|
||||
func _physics_process(delta):
|
||||
## Keyboard Movement
|
||||
get_input()
|
@ -1,3 +1,12 @@
|
||||
extends Node
|
||||
|
||||
# Variables
|
||||
## Nodes
|
||||
func get_body_node():
|
||||
return $KinematicBody2D
|
||||
## Parameters
|
||||
func set_gpos(new_pos):
|
||||
return get_body_node().set_gpos(new_pos)
|
||||
func get_gpos():
|
||||
return get_body_node().get_gpos()
|
||||
func set_internal_velocity(new_vel):
|
||||
get_body_node().set_internal_velocity(new_vel)
|
||||
|
Loading…
Reference in New Issue