Musicgun47
You can move (hide) calculation of movement to other class, which will extends "abstract" class. Example:
file movement_type.gd
(autoload asMovementType
):
extends Node
const CLIMBING = "movement-climbing"
const WALKING = "movement-walking"
file abstract_movement.gd
:
extends Node
class_name AbstractMovement # let's pretend it is abstract class :)
var movement_type: String
func _init(movement_type: String):
self.movement_type = movement_type
func calc_movement(delta: float, velocity: Vector3):
assert(false, "Could not call abstract function")
file climbing.gd
(autoload as Climbing
):
extends AbstractMovement
# autoload
func _init().(MovementType.CLIMBING):
pass
func calc_movement(delta: float, velocity: Vector3):
var new_velocity := Vector3.ZERO
# do some magic and return new velocity
return new_velocity
file walking.gd
(autoload as Walking
):
extends AbstractMovement
# autoload
func _init().(MovementType.WALKING):
pass
func calc_movement(delta: float, velocity: Vector3):
var new_velocity := Vector3.ZERO
# do some magic and return new velocity
return new_velocity
file Player.gd
(see logic behind movement calculation is hidden, you just call movement.calc_movement
function):
extends KinematicBody
var movement: AbstractMovement = Walking
func _ready():
pass
func _process(delta):
if Input.is_action_just_pressed("ui_home"):
print("switching movement to climbing")
movement = Climbing
elif Input.is_action_just_pressed("ui_end"):
print("switching movement to walking")
movement = Walking
# or if player bump into ladder, etc...
# ...
if movement.movement_type == MovementType.CLIMBING:
print("climbing mode active")
if movement.movement_type == MovementType.CLIMBING:
print("walking mode active")
# ...
var velocity = Vector3.ZERO
# ...
var new_velocity = movement.calc_movement(delta, velocity)
move_and_slide(new_velocity)
🙂