Compare commits

..

No commits in common. "d1d33f30878ae79521edb8eaa66213e54cd7025d" and "fc29c18552af66ed3dde2b921bfb9e5d19f537f4" have entirely different histories.

5 changed files with 512 additions and 374 deletions

View File

@ -22,7 +22,7 @@ var jerk: float = 0.0
func modify(_movement_parameters :MovementParameters): func modify(_movement_parameters :MovementParameters):
pass pass
func apply_state_modifier(_modifier :StateModifierMovement): func apply_state_modifier(_modifier :StateModifierMovement, _movement_direction :float):
match _modifier.modifier_type: match _modifier.modifier_type:
StateModifierMovement.SUB_TYPE.NEGATIVE_CONSTRAINT: StateModifierMovement.SUB_TYPE.NEGATIVE_CONSTRAINT:
var adjusted_move_speed = base_move_speed.x + _modifier.horizontal_speed var adjusted_move_speed = base_move_speed.x + _modifier.horizontal_speed

View File

@ -14,6 +14,7 @@ export var debug_component: bool = false
onready var current_state = StateAnimatedActor.new() onready var current_state = StateAnimatedActor.new()
onready var parent: KinematicBody2D = get_parent() onready var parent: KinematicBody2D = get_parent()
export var callable_state_machine :NodePath export var callable_state_machine :NodePath
var request_state_change: FuncRef var request_state_change: FuncRef
@ -21,8 +22,10 @@ onready var desired_movement_vector: Vector2 = Vector2(0,0)
# Since animactor state machine can actually view properties from this type # Since animactor state machine can actually view properties from this type
# I'm thinking about moving the velocity tracker in here instead of player. # I'm thinking about moving the velocity tracker in here instead of player.
#var velocity = Vector2(0,0) var velocity = Vector2(0,0)
var velocity :VelocityCalculations = VelocityCalculations.new() #var momentum = Vector2(0,0)
#var inertia = Vector2(0,0)
#var acceleration = Vector2(0,0)
#Removing Probably not used #Removing Probably not used
#var sim_velocity = Vector2(0,0) #var sim_velocity = Vector2(0,0)
@ -40,8 +43,6 @@ const LEFT = -1.0
const RIGHT = 1.0 const RIGHT = 1.0
func _ready(): func _ready():
#velocity.debug = true
## FuncRef to request state machine change state ## FuncRef to request state machine change state
request_state_change = funcref(get_node(callable_state_machine), 'change_to_known_state') request_state_change = funcref(get_node(callable_state_machine), 'change_to_known_state')
## Connect signal to get alerted of state change ## Connect signal to get alerted of state change
@ -143,13 +144,240 @@ func _on_modifiers_updated(_modifier_type :int, _modifier_action :int, _merged_m
print("Registered Modifier Movement: ", _merged_modifier.horizontal_speed) print("Registered Modifier Movement: ", _merged_modifier.horizontal_speed)
modifier = _merged_modifier modifier = _merged_modifier
func apply_state_modifier(movement_parameters :MovementParameters) -> MovementParameters:
if modifier and modifier.is_active:
if (current_state.is_grounded and modifier.only_grounded): # Should we skip
movement_parameters.apply_state_modifier(modifier)
return movement_parameters
enum RANGE_PLACEMENT {
BEFORE_RANGE = -1,
WITHIN_RANGE = 0,
PAST_RANGE = 1
}
## Side effects for these variables
# velocity - doesn't change but uses it to set base calculations
# accepts the state to determine movement, the deltatime, and an optional direction
# Update: Actually should just return movement in PPS
##
## Could these be datatypes or a class? Sure
var _h_impulse_applied :bool = false
var _h_impulse_speed_tracking :Vector2
var _v_impulse_applied :bool = false
var _v_impulse_speed_tracking :Vector2
func new_move_actor_as_desired(_delta :float,
_state :StateAnimatedActor,
_movement_override_normal := Vector2(0,0),
_velocity_override := Vector2(0,0)) -> Vector2:
var calc_velocity = Vector2.ZERO
if _velocity_override.x != 0.0:
calc_velocity.x = _velocity_override.x
else:
calc_velocity.x = velocity.x
if _velocity_override.y != 0.0:
calc_velocity.y = _velocity_override.y
else:
calc_velocity.y = velocity.y
var calc_acceleration = Vector2.ZERO
var calc_inertia :Vector2
#calc_inertia.x = abs(calc_velocity.x)
## We're now toing to preserve inertia direction
calc_inertia.x = calc_velocity.x
calc_inertia.y = calc_velocity.y
var calc_inertial_dir = Vector2(sign(calc_velocity.x),sign(calc_velocity.y))
#var calc_friction :Vector2 = Vector2.ZERO
## Determine movement direction
# If there is an inertia direction from existing velocity and
# no desired movement we'll continue to travel in that direction.
##
## If an override has been passed (we're ignoring input direction
var move_direction = Vector2.ZERO
if _movement_override_normal != Vector2.ZERO:
move_direction = resolve_move_direction(calc_inertia, _movement_override_normal)
else:
move_direction = resolve_move_direction(calc_inertia, desired_movement_vector)
var modifier_indicator := ''
var movement_parameters :MovementParameters = _state.get_movement_parameters()
if modifier and modifier.is_active:
if (_state.is_grounded and modifier.only_grounded): # Should we skip
modifier_indicator = '*'
if _state.name == 'jump':
var foo = 2+2 # break
movement_parameters.apply_state_modifier(modifier, move_direction.x)
## Inertia only applies if there is a difference between the
# base move speed and a derived move speed. This makes it so all you
# have to do is provide a move speed and that is the speed we will travel.
# but if a modifier applies, or an accelleration is given.
# an entirely differant process occurs.
# h_speed.x can be thought of as impulse speed. While h_speed.y
# is the destination speed.
# We move towards h_speed.y at the acceleration rate
##
var h_speed = resolve_h_speed(movement_parameters)
var v_speed = resolve_v_speed(movement_parameters)
## Speed will now be expected to move in the direction of travel
h_speed *= move_direction.x
v_speed *= move_direction.y
## Now determine placement of current velocity to speed range
var h_range_placement = placement_to_speed_range(calc_velocity.x, h_speed)
var v_range_placement = placement_to_speed_range(calc_velocity.y, v_speed)
## We don't want to be able to scoot our impulse speed to cheat the movement
# Any non zero speed that goes opposite to our inertial direction
# should only be allowed once.
## Reset the impulse when back in range
# may also want to reset when hspeed is static
if (h_range_placement == RANGE_PLACEMENT.WITHIN_RANGE and
_h_impulse_applied == true ):
print("resetting H impulse")
_h_impulse_applied = false
if (v_range_placement == RANGE_PLACEMENT.WITHIN_RANGE and
_v_impulse_applied == true ):
print("resetting V impulse")
_v_impulse_applied = false
## Acceleration is always postive because we use the
# move toward functions.
calc_acceleration.x = abs(resolve_h_acceleration(movement_parameters, move_direction.x))
## We are always moving from h_speed.x towards y at a given rate
## if we have a difference of speed and an acceleration
##TODO: The Min Max could be augmented to just be h_speed.x and prevent the sliding.
if h_speed.x != h_speed.y and calc_acceleration.x != 0.0:
var direction_accel :float = calc_acceleration.x * _delta #* move_direction.x
if h_speed.x > h_speed.y:
direction_accel *= -1
match h_range_placement:
RANGE_PLACEMENT.BEFORE_RANGE:
## Also apply impulse here
if _h_impulse_applied == false:
calc_inertia.x += h_speed.x
_h_impulse_applied = true
#impulse_applied_dir = move_direction.x
calc_inertia.x = clamp(calc_inertia.x + direction_accel,
min(calc_inertia.x, h_speed.y),
max(calc_inertia.x, h_speed.y))
RANGE_PLACEMENT.WITHIN_RANGE:
## If we're within the range but our speed has just changed
if _h_impulse_applied == false and _h_impulse_speed_tracking != h_speed:
## Set inertia to starting speed, we're already in range
calc_inertia.x = h_speed.x
_h_impulse_applied = true
calc_inertia.x = clamp(calc_inertia.x + direction_accel,
min(h_speed.x, h_speed.y),
max(h_speed.x, h_speed.y))
RANGE_PLACEMENT.PAST_RANGE:
calc_inertia.x = clamp(calc_inertia.x - direction_accel, # Friction
min(calc_inertia.x, h_speed.y),
max(calc_inertia.x, h_speed.y))
elif calc_inertia.x != 0.0 and calc_acceleration.x != 0.0: ## We still have inertia but no difference in movement
if h_speed.x < h_speed.y:
h_speed.x = clamp(h_speed.x,0.0,h_speed.y)
## Move back towards the base speed
calc_inertia.x = move_toward(calc_inertia.x, h_speed.x, calc_acceleration.x * _delta)
else:
## inertia is just base speed
calc_inertia.x = h_speed.x
calc_inertial_dir.x = 0.0 # sign(calc_inertia.x)
## Another idea, just return the calculated velocity in PPS
## For now, y component of velocity is just gravity
if v_speed.x != v_speed.y: ## For now gravity is just the default acceleration
var direction_accel :float = movement_parameters.gravity * _delta #* move_direction.x
if v_speed.x > v_speed.y:
direction_accel *= -1
match v_range_placement:
RANGE_PLACEMENT.BEFORE_RANGE:
## Also apply impulse here
if _v_impulse_applied == false:
calc_inertia.y += v_speed.x
_v_impulse_applied = true
#impulse_applied_dir = move_direction.x
calc_inertia.y = clamp(calc_inertia.y + direction_accel,
min(calc_inertia.x, v_speed.y),
max(calc_inertia.x, v_speed.y))
RANGE_PLACEMENT.WITHIN_RANGE:
## If we're within the range but our speed has just changed
if _v_impulse_applied == false and _v_impulse_speed_tracking != v_speed:
## Set inertia to starting speed, we're already in range
calc_inertia.y = v_speed.x
_v_impulse_applied = true
calc_inertia.y = clamp(calc_inertia.y + direction_accel,
min(v_speed.x, v_speed.y),
max(v_speed.x, v_speed.y))
RANGE_PLACEMENT.PAST_RANGE:
calc_inertia.y = clamp(calc_inertia.y - direction_accel, # Friction
min(calc_inertia.y, v_speed.y),
max(calc_inertia.y, v_speed.y))
else:
## The previous vertical movement methods
calc_inertia.y += movement_parameters.gravity * _delta
## Track or last speed for in range impulses
_h_impulse_speed_tracking = h_speed
_v_impulse_speed_tracking = v_speed
calc_velocity.y = calc_inertia.y
calc_velocity.x = calc_inertia.x
if debug_component:
UiManager.debug_text = ( #"H_Speed x Dir: " + str(h_speed) + str('duh') +
modifier_indicator + "H_Speed Fm:{0} To:{1}".format({"0":"%5.2f" % h_speed.x, "1":"%5.2f" % h_speed.y}) +
"\nV_Speed Fm:{0} To:{1}".format({"0":"%5.2f" % v_speed.x, "1":"%5.2f" % v_speed.y}) +
"\nSpeedRange : {0}, {1}".format({"0":"%5.2f" % h_range_placement, "1":"%5.2f" % v_range_placement}) +
"\nVelocity_Calc: {0}, {1}".format({"0":"%5.2f" % calc_velocity.x, "1":"%5.2f" % calc_velocity.y}) +
"\nInertia_Calc: {0}, {1}".format({"0":"%5.2f" % calc_inertia.x, "1":"%5.2f" % calc_inertia.y}) +
"\nVelocity_Real: {0}, {1}".format({"0":"%5.2f" % velocity.x, "1":"%5.2f" % velocity.y}) +
#"\nFriction: {0}, {1}".format({"0":"%5.2f" % calc_friction.x, "1":"%5.2f" % calc_friction.y}) +
"\nAccelCalc : {0}, {1}".format({"0":"%5.2f" % calc_acceleration.x, "1":"%5.2f" % calc_acceleration.y}) +
# h_range_placement
#"\nLength: " + str(velocity.length()) +
"\nMoveDir: {0}, {1}".format({"0":"%4.1f" % move_direction.x, "1":"%4.1f" % move_direction.y})
)
return calc_velocity
func is_out_of_range(value: float, a: float, b: float) -> bool:
var lower = min(a, b)
var upper = max(a, b)
return value <= lower or value >= upper
## Determine the trend of direction and where our current speed is
# in relation to it. This should help determine the direction of
# acceleration and whether we speed up or slow down.
##
func placement_to_speed_range(speed: float, speed_range: Vector2) -> int:
## Actually we don't care about mix or min
#var range_start :float = min(speed_range.x, speed_range.y)
#var range_end :float = max(speed_range.x, speed_range.y)
## We can be at the base range to ensure impulse can happen
## Direction <-- that way
if speed_range.x > speed_range.y:
if speed < speed_range.x and speed >= speed_range.y:
return RANGE_PLACEMENT.WITHIN_RANGE
elif speed < speed_range.y:
return RANGE_PLACEMENT.PAST_RANGE
else:
return RANGE_PLACEMENT.BEFORE_RANGE
else: ## Direction --> that way
if speed > speed_range.x and speed <= speed_range.y:
return RANGE_PLACEMENT.WITHIN_RANGE
elif speed > speed_range.y:
return RANGE_PLACEMENT.PAST_RANGE
else:
return RANGE_PLACEMENT.BEFORE_RANGE
return 0
## Passed in acceleration, vel, etc is applied to whatever ## Passed in acceleration, vel, etc is applied to whatever
## the parent is. If Kinematic then move_and_slide, otherwise manually adjusted ## the parent is. If Kinematic then move_and_slide, otherwise manually adjusted
@ -159,5 +387,264 @@ func apply_movement_to_parent( _velocity :Vector2) -> void:
if _velocity.y < -8: if _velocity.y < -8:
snap = Vector2.ZERO snap = Vector2.ZERO
if parent is KinematicBody2D: if parent is KinematicBody2D:
velocity.velocity = parent.move_and_slide_with_snap(_velocity, snap , Vector2.UP, true) velocity = parent.move_and_slide_with_snap(_velocity, snap , Vector2.UP, true)
## Returns an Vector where x is MIN_SPEED and y is MAX_SPEED
func resolve_h_speed(_params :MovementParameters) -> Vector2:
var base_speed :float = _params.base_move_speed.x
## if a speed difference applies
if _params.move_speed_modifier.x != 0:
var speed_differance = base_speed + _params.move_speed_modifier.x
return(Vector2(base_speed, speed_differance))
return Vector2(base_speed,base_speed)
func resolve_v_speed(_params :MovementParameters) -> Vector2:
var base_speed :float = _params.base_move_speed.y
## if a speed difference applies
if _params.move_speed_modifier.y != 0:
var speed_differance = base_speed + _params.move_speed_modifier.y
return(Vector2(base_speed, speed_differance))
return Vector2(base_speed,base_speed)
func resolve_h_acceleration(_params :MovementParameters, _move_direction :float) -> float:
## TODO: Adjust for jerk, determine if we're currently experiencing accel
## if a speed difference applies
var _acceleration :float = 0.0
var base_speed :float = _params.base_move_speed.x
if _params.move_speed_modifier.x != 0:
var speed_differance = base_speed + _params.move_speed_modifier.x
_acceleration = _params.base_move_acceleration.x + _params.move_speed_modifier_acceleration.x
##WIP: Add part where offset acceleration only applies until the inertia equals the offset or whatever
# if sign(_params["_base_h_move_modifier_move_acceleration"]) == -1 and abs(_inertia.x) >= speed_differance:
# #print("I should add the modifier acceleration maybe?")
# _acceleration += _params["_base_h_move_modifier_move_acceleration"]
# else:
# print ("not yet.")
else:
_acceleration = _params.base_move_acceleration.x
## This works but I want to try something differant.
# if _inertia.x != 0.0 and sign(_inertia.x) != sign(_move_direction):
# return _acceleration * -1
# elif sign(_move_direction) != 0:
# ## If no velocity or it matches just return as is.
# return _acceleration
if sign(_move_direction) != 0:
return _acceleration
# No accel returned unless intended
return 0.0
func resolve_move_direction(_momentum :Vector2,
_movement_direction :Vector2) -> Vector2:
var horizontal_movement_direction:float
if sign(_movement_direction.x): ## We're trying to move
horizontal_movement_direction = sign(_movement_direction.x)
elif sign(_momentum.x): ## We still have momentum but not trying to move
horizontal_movement_direction = sign(_momentum.x)
var vertical_movement_direction:float
if sign(_movement_direction.y):
vertical_movement_direction = sign(_movement_direction.y)
elif sign(_momentum.y):
vertical_movement_direction = sign(_momentum.y)
return Vector2(horizontal_movement_direction, vertical_movement_direction)
## About to DEPR this one for redesigned one above
#func move_actor_as_desired( x_move_direction_override: float = 0):
# var delta:float = physics_delta
# var _move_speed = current_state.horizontal_speed
# var _move_acceleration = current_state.horizontal_acceleration
# var _move_speed_modifier = current_state.horizontal_speed_offset
# var _move_modifier_move_acceleration = current_state.horizontal_speed_offset_acceleration
# var _jerk_factor = current_state.jerk_factor
# var _gravity = current_state.gravity
#
## if current_state.physics_modifier:
## #physics_modifier.modifier_properties.jerk_factor
## #print(physics_modifier.name)
## #UiManager.debug_text = physics_modifier.name + str(round(adjusted_move_speed))
## var mod_props :ModifierProperties = current_state.physics_modifier.get_modifier_properties()
## if mod_props.move_speed != 0 and mod_props.directional_modifier == false:
## _move_speed = mod_props.move_speed
## _move_speed_modifier += mod_props.move_speed_modifier
## _move_acceleration += mod_props.move_acceleration
## _move_modifier_move_acceleration += mod_props.move_modifier_move_acceleration
## _jerk_factor += mod_props.jerk_factor
#
# var help_me_not_be_dumb = ''
#
# # Allow us to bump out of halt.
# if _move_speed == 0 and desired_movement_vector.x != 0:
# _move_speed = abs(desired_movement_vector.x)
#
# # Determine or physics movement direction
# var current_x_velocity = velocity.x #move_component.velocity.x
# var move_direction = 0.0
# # Determine movement direction if we have momentum
# if momentum.x != 0:
# if x_move_direction_override == 0:
# move_direction = sign(current_x_velocity)
## if move_component.desired_movement_vector.x != 0:
## # set the move direction to the desired direction
## move_direction = move_component.desired_movement_vector.x
## else:
## # Set move direction to the transform direction
## move_direction = parent.transform.x.x
# else:
# move_direction = x_move_direction_override
# else: # No current momentum in place
# if x_move_direction_override == 0:
# move_direction = desired_movement_vector.x
# else:
# move_direction = x_move_direction_override
#
### TODO: I hate that I have to do this check again.
## if current_state.physics_modifier:
## var mod_props :ModifierProperties = current_state.physics_modifier.get_modifier_properties()
## if mod_props.move_speed != 0 and mod_props.directional_modifier == true:
## # Since move_direction is always positive
## if sign(move_direction) == sign(mod_props.move_speed):
## _move_speed += mod_props.move_speed
## else:
## _move_speed -= mod_props.move_speed
## if sign(move_direction) == 0:
## move_direction = sign(mod_props.move_speed) * -1
## #print("doing this? ")
#
#
# # Determine the maximum move speed
# var MAX_SPEED :float = 0
# var MIN_SPEED :float = 0
# if sign(_move_speed_modifier) == -1: # decreased speed modifier
# help_me_not_be_dumb += '-SpeedMod'
# # Move speed cannot go below zero with modifier applied
# MIN_SPEED = _move_speed + _move_speed_modifier
# # Poor man's clamp
# if MIN_SPEED < 0:
# MIN_SPEED = 0
# MAX_SPEED = _move_speed
# elif sign(_move_speed_modifier) == +1: # increased speed modifier
# help_me_not_be_dumb += '+SpeedMod'
# MIN_SPEED = _move_speed
# MAX_SPEED = _move_speed + _move_speed_modifier
# else: # physics won't apply here
# help_me_not_be_dumb += '_Speed' # Neutral Speed
# MIN_SPEED = _move_speed
# MAX_SPEED = _move_speed
#
# # get the latest aggregate acceleration
# # If we have any acceleration applying
# if (_move_modifier_move_acceleration + _move_acceleration) != 0:
# # The acceleration is differant
# # Perhaps it would be better to trigger this on a difference in modifier but they usually go together.
# if abs(acceleration.x) != abs(_move_modifier_move_acceleration + _move_acceleration):
# if move_direction:
# #print("Acceleration changed.", abs(acceleration.x), "-", (_move_modifier_move_acceleration + _move_acceleration))
# acceleration.x = _move_modifier_move_acceleration + _move_acceleration
#
# # If we're no longer tryingg to move int the direction of our movement and momentum.
# if (sign(current_x_velocity) != sign(desired_movement_vector.x) and momentum.x != 0):
# if sign(_move_speed_modifier) == -1 : # decreased speed modifier
# # Maybe we can compare the direction of the acceleration
# # The direction of the acceleration should usually be positive at this point.
# # when the modifier is negative.
# if sign(move_direction) == sign(current_x_velocity):
# if sign(acceleration.x) == sign(momentum.x):
# acceleration.x *= -1
# # print("Whoh Woah")
# # Flip the direction of the acceleration
# if (sign(desired_movement_vector.x) == -1 and
# sign(current_x_velocity) == 1) or (sign(desired_movement_vector.x) == -1 and
# sign(current_x_velocity) == -1):
# print_debug("be more opposite")
#
# elif (sign(desired_movement_vector.x) == sign(current_x_velocity) and momentum.x != 0):
# print('why')
# if (sign(acceleration.x) != sign(momentum.x) and x_move_direction_override == 0):
# #print("Step it up!")
# # Flip the direction of the acceleration
# acceleration.x *= -1
#
# # Apply momentum and acceleration if a modifer exists
# if momentum.x <= 0 and _move_speed_modifier !=0:
# #if we're trying to move but we have no momentum applied currently
# #if move_component.desired_movement_vector.x != 0:
# if move_direction:
# acceleration.x = _move_modifier_move_acceleration + _move_acceleration
# ##TODO: I don't know where I should actually set the momentum. :
# if sign(_move_speed_modifier) == -1: # decreased speed modifier
# momentum.x = 0
# elif sign(_move_speed_modifier) == +1: # increased speed modifier
# ##TODO: in most cases this should only be applied once. need to find a way to warn against this.
# momentum.x = abs(_move_speed_modifier)
# print("momentum applied!")
#
# # Reverse the accelerations if we carry over momentum but the modifier no longer applies.
# if momentum.x > 0 and _move_speed_modifier == 0:
# if sign(acceleration.x) == sign(momentum.x):
# print("Whoh Woah your done.")
# # Flip the direction of the acceleration
# acceleration.x *= -1
#
#
# # We're going to adjust our move speed only to the modifier
# momentum.x += acceleration.x * delta
# ##TODO: Apparently I disabled jerk some time ago for some reason.
# current_state.jerk += _jerk_factor * delta
#
#
# var new_move_speed :float = 0
# if sign(_move_speed_modifier) == -1: # decreased speed modifier
## MIN_SPEED = _move_speed + _move_speed_modifier
## MAX_SPEED = _move_speed
# momentum.x = clamp(momentum.x, 0, abs(_move_speed_modifier))
## new_move_speed = (_move_speed + _move_speed_modifier ) + (sign(_move_speed_modifier) * -1 * move_component.momentum.x) # + jerk
# new_move_speed = MIN_SPEED + (sign(_move_speed_modifier) * -1 * momentum.x) # + jerk
# elif sign(_move_speed_modifier) == +1: # increased speed modifier
## MIN_SPEED = _move_speed
## MAX_SPEED = _move_speed + _move_speed_modifier
# momentum.x = clamp(momentum.x, 0, _move_speed_modifier )
# new_move_speed = _move_speed + momentum.x # + jerk
# else: # physics won't apply here
## move_component.acceleration.x = 0
## move_component.momentum.x = 0
## new_move_speed = _move_speed
# momentum.x = clamp(momentum.x, 0, MIN_SPEED )
# new_move_speed = _move_speed + momentum.x # + jerk
#
# #new_move_speed = clamp(new_move_speed, MIN_SPEED, MAX_SPEED)
#
# var sim_move_speed = (MIN_SPEED + (sign(_move_speed_modifier) * -1 * momentum.x))
#
## if move_component.momentum.x != 0:
# if debug_component == true:
# UiManager.debug_text = ( "Mod(" + str(sign(_move_speed_modifier)) + ") Dir(" + str(move_direction) + ") Vel(" + str(sign(current_x_velocity)) + ") Mov(" + str(sign(desired_movement_vector.x)) +
# ")\nNS:" + str(round(sim_move_speed)) + #" Z:" + str(sign(_move_speed_modifier) * -1 * move_component.momentum.x) +
# "\nS:" + str(round(_move_speed + _move_speed_modifier)) + ",M" + str(round(momentum.x)) +
# ",A" + str(round(acceleration.x)) +
# "\nVel:" + str(round(velocity.x)) + "Min:" + str(round(MIN_SPEED)) + ",Max:" + str(round(MAX_SPEED))
# ) # str(round(jerk))
#
# #sim_velocity.x = move_direction * sim_move_speed
# #print(adjusted_move_speed, ",", new_move_speed, ",", jerk)
#
# #print(new_move_speed, " ", adjusted_move_speed)
#
# velocity.y += _gravity * delta
# #parent.velocity = parent.move_and_slide(parent.velocity, Vector2(0, -1))
# #print(speed_multiplier)
## move_component.velocity.x = move_component.desired_movement_vector.x * _move_speed
# velocity.x = move_direction * new_move_speed
# #move_component.velocity = parent.move_and_slide(move_component.velocity, Vector2.UP)
# var snap = Vector2.DOWN * 8
# if velocity.y < -8:
# snap = Vector2.ZERO
# velocity = parent.move_and_slide_with_snap(velocity, snap , Vector2.UP, true)

View File

@ -163,11 +163,6 @@ _global_script_classes=[ {
"class": "UIComponent", "class": "UIComponent",
"language": "GDScript", "language": "GDScript",
"path": "res://src/ui/HUD.gd" "path": "res://src/ui/HUD.gd"
}, {
"base": "Reference",
"class": "VelocityCalculations",
"language": "GDScript",
"path": "res://src/classes/velocity_calculations.gd"
} ] } ]
_global_script_class_icons={ _global_script_class_icons={
"Actor": "", "Actor": "",
@ -200,8 +195,7 @@ _global_script_class_icons={
"StateModifier": "", "StateModifier": "",
"StateModifierAnimatedActor": "", "StateModifierAnimatedActor": "",
"StateModifierMovement": "", "StateModifierMovement": "",
"UIComponent": "", "UIComponent": ""
"VelocityCalculations": ""
} }
[application] [application]

View File

@ -160,14 +160,8 @@ func _state_process_physics_idle():
if $"%LadderDetector".is_colliding() and _wants_climb == true: if $"%LadderDetector".is_colliding() and _wants_climb == true:
request_state_change.call_func('climb') request_state_change.call_func('climb')
# var _v :Vector2 = new_move_actor_as_desired( physics_delta , current_state ) var _v :Vector2 = new_move_actor_as_desired( physics_delta , current_state )
# apply_movement_to_parent(_v) apply_movement_to_parent(_v)
apply_movement_to_parent(
velocity.calculate_velocity(
physics_delta ,
apply_state_modifier(current_state.get_movement_parameters()),
desired_movement_vector )
)
func attack_primary(): func attack_primary():
##Another thing I wish I could avoid the funcref or string call ##Another thing I wish I could avoid the funcref or string call
@ -226,13 +220,8 @@ func _state_process_physics_roll():
# Force role in facing direction # Force role in facing direction
# instead of movement direction # instead of movement direction
#move_actor_as_desired(parent.transform.x.x) #move_actor_as_desired(parent.transform.x.x)
#apply_movement_to_parent(new_move_actor_as_desired( physics_delta , current_state, parent.transform.x )) apply_movement_to_parent(new_move_actor_as_desired( physics_delta , current_state, parent.transform.x ))
apply_movement_to_parent(
velocity.calculate_velocity(
physics_delta ,
apply_state_modifier(current_state.get_movement_parameters()),
parent.transform.x )
)
if !parent.is_on_floor(): if !parent.is_on_floor():
#return fall_state #return fall_state
request_state_change.call_func('fall') request_state_change.call_func('fall')
@ -262,12 +251,7 @@ func _state_process_physics_enter_right():
# Force role in facing direction # Force role in facing direction
# instead of movement direction # instead of movement direction
#move_actor_as_desired(parent.transform.x.x) #move_actor_as_desired(parent.transform.x.x)
apply_movement_to_parent( apply_movement_to_parent(new_move_actor_as_desired( physics_delta , current_state, parent.transform.x ))
velocity.calculate_velocity(
physics_delta ,
apply_state_modifier(current_state.get_movement_parameters()),
parent.transform.x )
)
func _state_process_physics_land(): func _state_process_physics_land():
$"%LedgeDetector".set_enabled(true) $"%LedgeDetector".set_enabled(true)
@ -278,12 +262,7 @@ func _state_process_physics_land():
#return fall_state #return fall_state
request_state_change.call_func('fall') request_state_change.call_func('fall')
#move_actor_as_desired() #move_actor_as_desired()
apply_movement_to_parent( apply_movement_to_parent(new_move_actor_as_desired( physics_delta , current_state ))
velocity.calculate_velocity(
physics_delta ,
apply_state_modifier(current_state.get_movement_parameters()),
desired_movement_vector )
)
func _state_process_physics_hurt(): func _state_process_physics_hurt():
if current_state.state_timeout.time_left == 0: if current_state.state_timeout.time_left == 0:
@ -299,7 +278,7 @@ func _state_process_physics_hurt():
# parent.velocity.x = 1 * move_speed # parent.velocity.x = 1 * move_speed
# else: # else:
# parent.velocity.x = -1 * move_speed # parent.velocity.x = -1 * move_speed
velocity.velocity = parent.move_and_slide(velocity.velocity, Vector2(0, -1)) velocity = parent.move_and_slide(velocity, Vector2(0, -1))
func _state_process_physics_fall(): func _state_process_physics_fall():
@ -317,12 +296,7 @@ func _state_process_physics_fall():
request_state_change.call_func('land') request_state_change.call_func('land')
#move_actor_as_desired() #move_actor_as_desired()
apply_movement_to_parent( apply_movement_to_parent(new_move_actor_as_desired( physics_delta , current_state ))
velocity.calculate_velocity(
physics_delta ,
apply_state_modifier(current_state.get_movement_parameters()),
desired_movement_vector )
)
func _state_process_physics_jump(): func _state_process_physics_jump():
@ -333,16 +307,11 @@ func _state_process_physics_jump():
# #modifier.reference() # #modifier.reference()
# #idle_state.modifier = landing_modifier # #idle_state.modifier = landing_modifier
# request_state_change.call_func('idle') # request_state_change.call_func('idle')
if velocity.velocity.y > 0: if velocity.y > 0:
request_state_change.call_func('fall') request_state_change.call_func('fall')
desired_movement_vector.y = UP desired_movement_vector.y = UP
#move_actor_as_desired() #move_actor_as_desired()
apply_movement_to_parent( apply_movement_to_parent(new_move_actor_as_desired( physics_delta , current_state ))
velocity.calculate_velocity(
physics_delta ,
apply_state_modifier(current_state.get_movement_parameters()),
desired_movement_vector )
)
func _state_process_physics_move(): func _state_process_physics_move():
# flip sprite in direction # flip sprite in direction
@ -364,13 +333,7 @@ func _state_process_physics_move():
#var _v :Vector2 = new_move_actor_as_desired( physics_delta , current_state ) #var _v :Vector2 = new_move_actor_as_desired( physics_delta , current_state )
#apply_movement_to_parent(_v) #apply_movement_to_parent(_v)
#apply_movement_to_parent(new_move_actor_as_desired( physics_delta , current_state )) apply_movement_to_parent(new_move_actor_as_desired( physics_delta , current_state ))
apply_movement_to_parent(
velocity.calculate_velocity(
physics_delta ,
apply_state_modifier(current_state.get_movement_parameters()),
desired_movement_vector )
)
func get_climb_shape_location() -> Vector2: func get_climb_shape_location() -> Vector2:
if $"%LadderDetector".is_colliding(): if $"%LadderDetector".is_colliding():
@ -481,12 +444,7 @@ func _state_process_physics_crouch_move():
request_state_change.call_func('move') request_state_change.call_func('move')
#move_actor_as_desired() #move_actor_as_desired()
apply_movement_to_parent( apply_movement_to_parent(new_move_actor_as_desired( physics_delta , current_state ))
velocity.calculate_velocity(
physics_delta ,
apply_state_modifier(current_state.get_movement_parameters()),
desired_movement_vector )
)
#func _on_state_entered_idle(): #func _on_state_entered_idle():
# print("Movement State Idle State Entered!") # print("Movement State Idle State Entered!")

View File

@ -1,301 +0,0 @@
class_name VelocityCalculations
var velocity :Vector2
var debug :bool = false
enum RANGE_PLACEMENT {
BEFORE_RANGE = -1,
WITHIN_RANGE = 0,
PAST_RANGE = 1
}
## Side effects for these variables
# velocity - doesn't change but uses it to set base calculations
# accepts the state to determine movement, the deltatime, and an optional direction
# Update: Actually should just return movement in PPS
##
## Could these be datatypes or a class? Sure
var _h_impulse_applied :bool = false
var _h_impulse_speed_tracking :Vector2
var _v_impulse_applied :bool = false
var _v_impulse_speed_tracking :Vector2
func calculate_velocity(_delta :float,
movement_parameters :MovementParameters,
_movement_direction := Vector2(0,0),
_velocity_override := Vector2(0,0)) -> Vector2:
var calc_velocity = Vector2.ZERO
if _velocity_override.x != 0.0:
calc_velocity.x = _velocity_override.x
else:
calc_velocity.x = velocity.x
if _velocity_override.y != 0.0:
calc_velocity.y = _velocity_override.y
else:
calc_velocity.y = velocity.y
var calc_acceleration = Vector2.ZERO
var calc_inertia :Vector2
#calc_inertia.x = abs(calc_velocity.x)
## We're now toing to preserve inertia direction
calc_inertia.x = calc_velocity.x
calc_inertia.y = calc_velocity.y
var calc_inertial_dir = Vector2(sign(calc_velocity.x),sign(calc_velocity.y))
#var calc_friction :Vector2 = Vector2.ZERO
## Determine movement direction
# If there is an inertia direction from existing velocity and
# no desired movement we'll continue to travel in that direction.
##
## If an override has been passed (we're ignoring input direction
var move_direction = Vector2.ZERO
move_direction = resolve_move_direction(calc_inertia, _movement_direction)
# else:
# move_direction = resolve_move_direction(calc_inertia, desired_movement_vector)
#var movement_parameters :MovementParameters = _state.get_movement_parameters()
# if modifier and modifier.is_active:
# if (_state.is_grounded and modifier.only_grounded): # Should we skip
# modifier_indicator = '*'
# if _state.name == 'jump':
# var foo = 2+2 # break
# movement_parameters.apply_state_modifier(modifier, move_direction.x)
## Inertia only applies if there is a difference between the
# base move speed and a derived move speed. This makes it so all you
# have to do is provide a move speed and that is the speed we will travel.
# but if a modifier applies, or an accelleration is given.
# an entirely differant process occurs.
# h_speed.x can be thought of as impulse speed. While h_speed.y
# is the destination speed.
# We move towards h_speed.y at the acceleration rate
##
var h_speed = resolve_h_speed(movement_parameters)
var v_speed = resolve_v_speed(movement_parameters)
## Speed will now be expected to move in the direction of travel
h_speed *= move_direction.x
v_speed *= move_direction.y
## Now determine placement of current velocity to speed range
var h_range_placement = placement_to_speed_range(calc_velocity.x, h_speed)
var v_range_placement = placement_to_speed_range(calc_velocity.y, v_speed)
## We don't want to be able to scoot our impulse speed to cheat the movement
# Any non zero speed that goes opposite to our inertial direction
# should only be allowed once.
## Reset the impulse when back in range
# may also want to reset when hspeed is static
if (h_range_placement == RANGE_PLACEMENT.WITHIN_RANGE and
_h_impulse_applied == true ):
print("resetting H impulse")
_h_impulse_applied = false
if (v_range_placement == RANGE_PLACEMENT.WITHIN_RANGE and
_v_impulse_applied == true ):
print("resetting V impulse")
_v_impulse_applied = false
## Acceleration is always postive because we use the
# move toward functions.
calc_acceleration.x = abs(resolve_h_acceleration(movement_parameters, move_direction.x))
## We are always moving from h_speed.x towards y at a given rate
## if we have a difference of speed and an acceleration
##TODO: The Min Max could be augmented to just be h_speed.x and prevent the sliding.
if h_speed.x != h_speed.y and calc_acceleration.x != 0.0:
var direction_accel :float = calc_acceleration.x * _delta #* move_direction.x
if h_speed.x > h_speed.y:
direction_accel *= -1
match h_range_placement:
RANGE_PLACEMENT.BEFORE_RANGE:
## Also apply impulse here
if _h_impulse_applied == false:
calc_inertia.x += h_speed.x
_h_impulse_applied = true
#impulse_applied_dir = move_direction.x
calc_inertia.x = clamp(calc_inertia.x + direction_accel,
min(calc_inertia.x, h_speed.y),
max(calc_inertia.x, h_speed.y))
RANGE_PLACEMENT.WITHIN_RANGE:
## If we're within the range but our speed has just changed
if _h_impulse_applied == false and _h_impulse_speed_tracking != h_speed:
## Set inertia to starting speed, we're already in range
calc_inertia.x = h_speed.x
_h_impulse_applied = true
calc_inertia.x = clamp(calc_inertia.x + direction_accel,
min(h_speed.x, h_speed.y),
max(h_speed.x, h_speed.y))
RANGE_PLACEMENT.PAST_RANGE:
calc_inertia.x = clamp(calc_inertia.x - direction_accel, # Friction
min(calc_inertia.x, h_speed.y),
max(calc_inertia.x, h_speed.y))
elif calc_inertia.x != 0.0 and calc_acceleration.x != 0.0: ## We still have inertia but no difference in movement
if h_speed.x < h_speed.y:
h_speed.x = clamp(h_speed.x,0.0,h_speed.y)
## Move back towards the base speed
calc_inertia.x = move_toward(calc_inertia.x, h_speed.x, calc_acceleration.x * _delta)
else:
## inertia is just base speed
calc_inertia.x = h_speed.x
calc_inertial_dir.x = 0.0 # sign(calc_inertia.x)
## Another idea, just return the calculated velocity in PPS
## For now, y component of velocity is just gravity
if v_speed.x != v_speed.y: ## For now gravity is just the default acceleration
var direction_accel :float = movement_parameters.gravity * _delta #* move_direction.x
if v_speed.x > v_speed.y:
direction_accel *= -1
match v_range_placement:
RANGE_PLACEMENT.BEFORE_RANGE:
## Also apply impulse here
if _v_impulse_applied == false:
calc_inertia.y += v_speed.x
_v_impulse_applied = true
#impulse_applied_dir = move_direction.x
calc_inertia.y = clamp(calc_inertia.y + direction_accel,
min(calc_inertia.x, v_speed.y),
max(calc_inertia.x, v_speed.y))
RANGE_PLACEMENT.WITHIN_RANGE:
## If we're within the range but our speed has just changed
if _v_impulse_applied == false and _v_impulse_speed_tracking != v_speed:
## Set inertia to starting speed, we're already in range
calc_inertia.y = v_speed.x
_v_impulse_applied = true
calc_inertia.y = clamp(calc_inertia.y + direction_accel,
min(v_speed.x, v_speed.y),
max(v_speed.x, v_speed.y))
RANGE_PLACEMENT.PAST_RANGE:
calc_inertia.y = clamp(calc_inertia.y - direction_accel, # Friction
min(calc_inertia.y, v_speed.y),
max(calc_inertia.y, v_speed.y))
else:
## The previous vertical movement methods
calc_inertia.y += movement_parameters.gravity * _delta
## Track or last speed for in range impulses
_h_impulse_speed_tracking = h_speed
_v_impulse_speed_tracking = v_speed
calc_velocity.y = calc_inertia.y
calc_velocity.x = calc_inertia.x
if debug:
UiManager.debug_text = ( #"H_Speed x Dir: " + str(h_speed) + str('duh') +
"H_Speed Fm:{0} To:{1}".format({"0":"%5.2f" % h_speed.x, "1":"%5.2f" % h_speed.y}) +
"\nV_Speed Fm:{0} To:{1}".format({"0":"%5.2f" % v_speed.x, "1":"%5.2f" % v_speed.y}) +
"\nSpeedRange : {0}, {1}".format({"0":"%5.2f" % h_range_placement, "1":"%5.2f" % v_range_placement}) +
"\nVelocity_Calc: {0}, {1}".format({"0":"%5.2f" % calc_velocity.x, "1":"%5.2f" % calc_velocity.y}) +
"\nInertia_Calc: {0}, {1}".format({"0":"%5.2f" % calc_inertia.x, "1":"%5.2f" % calc_inertia.y}) +
"\nVelocity_Real: {0}, {1}".format({"0":"%5.2f" % velocity.x, "1":"%5.2f" % velocity.y}) +
#"\nFriction: {0}, {1}".format({"0":"%5.2f" % calc_friction.x, "1":"%5.2f" % calc_friction.y}) +
"\nAccelCalc : {0}, {1}".format({"0":"%5.2f" % calc_acceleration.x, "1":"%5.2f" % calc_acceleration.y}) +
# h_range_placement
#"\nLength: " + str(velocity.length()) +
"\nMoveDir: {0}, {1}".format({"0":"%4.1f" % move_direction.x, "1":"%4.1f" % move_direction.y})
)
return calc_velocity
func is_out_of_range(value: float, a: float, b: float) -> bool:
var lower = min(a, b)
var upper = max(a, b)
return value <= lower or value >= upper
## Determine the trend of direction and where our current speed is
# in relation to it. This should help determine the direction of
# acceleration and whether we speed up or slow down.
##
func placement_to_speed_range(speed: float, speed_range: Vector2) -> int:
## Actually we don't care about mix or min
#var range_start :float = min(speed_range.x, speed_range.y)
#var range_end :float = max(speed_range.x, speed_range.y)
## We can be at the base range to ensure impulse can happen
## Direction <-- that way
if speed_range.x > speed_range.y:
if speed < speed_range.x and speed >= speed_range.y:
return RANGE_PLACEMENT.WITHIN_RANGE
elif speed < speed_range.y:
return RANGE_PLACEMENT.PAST_RANGE
else:
return RANGE_PLACEMENT.BEFORE_RANGE
else: ## Direction --> that way
if speed > speed_range.x and speed <= speed_range.y:
return RANGE_PLACEMENT.WITHIN_RANGE
elif speed > speed_range.y:
return RANGE_PLACEMENT.PAST_RANGE
else:
return RANGE_PLACEMENT.BEFORE_RANGE
return 0
## Returns an Vector where x is MIN_SPEED and y is MAX_SPEED
func resolve_h_speed(_params :MovementParameters) -> Vector2:
var base_speed :float = _params.base_move_speed.x
## if a speed difference applies
if _params.move_speed_modifier.x != 0:
var speed_differance = base_speed + _params.move_speed_modifier.x
return(Vector2(base_speed, speed_differance))
return Vector2(base_speed,base_speed)
func resolve_v_speed(_params :MovementParameters) -> Vector2:
var base_speed :float = _params.base_move_speed.y
## if a speed difference applies
if _params.move_speed_modifier.y != 0:
var speed_differance = base_speed + _params.move_speed_modifier.y
return(Vector2(base_speed, speed_differance))
return Vector2(base_speed,base_speed)
func resolve_h_acceleration(_params :MovementParameters, _move_direction :float) -> float:
## TODO: Adjust for jerk, determine if we're currently experiencing accel
## if a speed difference applies
var _acceleration :float = 0.0
var base_speed :float = _params.base_move_speed.x
if _params.move_speed_modifier.x != 0:
var speed_differance = base_speed + _params.move_speed_modifier.x
_acceleration = _params.base_move_acceleration.x + _params.move_speed_modifier_acceleration.x
##WIP: Add part where offset acceleration only applies until the inertia equals the offset or whatever
# if sign(_params["_base_h_move_modifier_move_acceleration"]) == -1 and abs(_inertia.x) >= speed_differance:
# #print("I should add the modifier acceleration maybe?")
# _acceleration += _params["_base_h_move_modifier_move_acceleration"]
# else:
# print ("not yet.")
else:
_acceleration = _params.base_move_acceleration.x
## This works but I want to try something differant.
# if _inertia.x != 0.0 and sign(_inertia.x) != sign(_move_direction):
# return _acceleration * -1
# elif sign(_move_direction) != 0:
# ## If no velocity or it matches just return as is.
# return _acceleration
if sign(_move_direction) != 0:
return _acceleration
# No accel returned unless intended
return 0.0
func resolve_move_direction(_momentum :Vector2,
_movement_direction :Vector2) -> Vector2:
var horizontal_movement_direction:float
if sign(_movement_direction.x): ## We're trying to move
horizontal_movement_direction = sign(_movement_direction.x)
elif sign(_momentum.x): ## We still have momentum but not trying to move
horizontal_movement_direction = sign(_momentum.x)
var vertical_movement_direction:float
if sign(_movement_direction.y):
vertical_movement_direction = sign(_movement_direction.y)
elif sign(_momentum.y):
vertical_movement_direction = sign(_momentum.y)
return Vector2(horizontal_movement_direction, vertical_movement_direction)