Player movement tweaks

This commit is contained in:
2020-05-29 01:39:44 -04:00
parent dee457a561
commit 3a6df205d2
4 changed files with 127 additions and 109 deletions

View File

@ -15,12 +15,15 @@ var jumped = false
var moveMotion : float = 0 # Player Input ( <- & -> )
var motion : Vector2 = Vector2(0,0) # Player's current velocity
var gui
var gui # Node representing GUI object
var interactables = []
var items = []
var equiped = null
var interactables = [] # Objects in range to interact with
var items = [] # Items in player inventory
var equiped = null # Currently equiped item
#==================
# Inventory System
#==================
func add_item(item):
items.append(item)
equip_item(item)
@ -33,63 +36,52 @@ func equip_item(item):
else:
print("Tried to equip: " + item + " but item was missing!")
#==============
# Interactions
#==============
func add_interactable(interactable):
interactables.append(interactable)
func remove_interactable(interactable):
var loc = interactables.find(interactable)
if loc >= 0:
interactables.remove(loc)
func interact():
interactables[0].interact()
#==========
# Game Loop
#===========
func _physics_process(delta):
jumped = false
# Gravity
motion.y += baseGravity
if is_on_floor():
motion.y = 0
if not gui:
gui = get_node("/root/World/GUI")
elif not gui.is_in_dialog():
user_input()
if jumped:
motion.y = jumpVelocity
else:
moveMotion = 0
# Apply velocity limits
moveMotion = clamp(moveMotion, -maxMoveVelocity, maxMoveVelocity)
# Apply velocity to frame
motion.x = moveMotion
animation_manager(moveMotion)
move_and_slide(motion, Vector2(0,-1))
func user_input():
if Input.is_action_just_pressed("interact") and len(interactables) > 0 and not gui.is_in_dialog():
interactables[0].interact()
if is_on_floor() and Input.is_action_just_pressed("ui_up") and Input.is_action_pressed("ui_down"):
var test_pos = Vector2(position.x,position.y+5)
if not test_move(Transform2D(0,test_pos), Vector2(0,8)):
position.y = position.y + 8
return
if(Input.is_action_pressed("ui_left")):
moveMotion = -moveAcceleration
if(Input.is_action_pressed("ui_right")):
moveMotion = moveAcceleration
if(is_on_floor() and Input.is_action_just_pressed("ui_up")):
motion.y = jumpVelocity
jumped = true
if is_on_floor() and (!Input.is_action_pressed("ui_left") and !Input.is_action_pressed("ui_right")):
if moveMotion > 0:
moveMotion = clamp(moveMotion - moveFriction, 0, moveMotion)
elif moveMotion < 0:
moveMotion = clamp(moveMotion + moveFriction, moveMotion, 0)
#===================
# Animation Manager
#===================
#
# Changes animation basd on current
# conditions
func animation_manager(motion : float):
if not is_on_floor():
@ -108,3 +100,33 @@ func animation_manager(motion : float):
else:
$AnimationPlayer.playback_speed = 1
$AnimationPlayer.play("Idle")
#============
# User Input
#============
func user_input():
# Interactions
if Input.is_action_just_pressed("interact") and len(interactables) > 0 and not gui.is_in_dialog():
interact()
# Move down platforms
if is_on_floor() and Input.is_action_just_pressed("ui_up") and Input.is_action_pressed("ui_down"):
var test_pos = Vector2(position.x,position.y+5)
if not test_move(Transform2D(0,test_pos), Vector2(0,1)):
position.y = position.y + 1
return
# Jumping
if(is_on_floor() and Input.is_action_just_pressed("ui_up")):
jumped = true
# Move left and right <- & ->
if(Input.is_action_pressed("ui_left")):
moveMotion = -moveAcceleration
if(Input.is_action_pressed("ui_right")):
moveMotion = moveAcceleration
if is_on_floor() and (!Input.is_action_pressed("ui_left") and !Input.is_action_pressed("ui_right")):
if moveMotion > 0:
moveMotion = clamp(moveMotion - moveFriction, 0, moveMotion)
elif moveMotion < 0:
moveMotion = clamp(moveMotion + moveFriction, moveMotion, 0)