diff --git a/lib/classes/movement_state_receiver.gd b/lib/classes/movement_state_receiver.gd index 90b8479..5e218ae 100644 --- a/lib/classes/movement_state_receiver.gd +++ b/lib/classes/movement_state_receiver.gd @@ -145,6 +145,11 @@ func _on_modifiers_updated(_modifier_type :int, _modifier_action :int, _merged_m modifier = _merged_modifier +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 @@ -225,10 +230,14 @@ func new_move_actor_as_desired(_delta :float, ## 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) ## Reset the impulse when back in range # may also want to reset when hspeed is static - if (is_out_of_range(calc_inertia.x, h_speed.x, h_speed.y) == false and + if (h_range_placement == RANGE_PLACEMENT.WITHIN_RANGE and impulse_applied == true ): print("resetting impulse") impulse_applied = false @@ -253,57 +262,37 @@ func new_move_actor_as_desired(_delta :float, # #calc_friction.x = calc_acceleration.x * -1 # calc_friction.x = abs(calc_acceleration.x) # calc_acceleration.x = 0.0 - - + #h_speed != debug_speed_tracker ## 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: - - ## Start speed really shouldn't be less than zero (modifiers can do this) - ## If we have mismatched signs we'll get really bad behavior - if (h_speed.x != 0 and h_speed.y != 0 ) and sign(h_speed.y) != sign(h_speed.x): - if h_speed.x < h_speed.y: - h_speed.x = clamp(h_speed.x, 0.0, h_speed.y) - else: - h_speed.y = clamp(h_speed.y, 0.0, h_speed.x) - - ##TODO: Find a better way to apply impulse on target velocity changes. - ## Determine whether we should apply impulse - ## If there is currently no inertia apply the base h_speed - ## If the direction of our inertia is different to our intended speed direction - # and the inertia is not already in range of the movement speed, apply impulse. - # range part is working but we need to also apply it when we have zero - # velocity, hopefully we're at full stop at this point - ## - if sign(calc_inertia.x) != sign(h_speed.x): - ## Only if inertia is not already in range. -# if calc_inertia.x <= h_speed.x or calc_inertia.x >= h_speed.y: - if is_out_of_range(calc_inertia.x, h_speed.x, h_speed.y) or h_speed != debug_speed_tracker: - ## ? But we only want to do this once though. + 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 impulse_applied == false: calc_inertia.x += h_speed.x impulse_applied = true impulse_applied_dir = move_direction.x - print(impulse_applied_dir, " Movement Impulse applied: ", h_speed.x) - debug_speed_tracker = h_speed -# elif calc_inertia.x != 0.0: -# ##WIP: attempts to apply start speed only once -# if h_speed.x < h_speed.y: -# if calc_inertia.x < h_speed.x and sign(move_direction.x) == calc_inertial_dir.x: -# calc_inertia.x = h_speed.x -# elif h_speed.x > h_speed.y: -# if calc_inertia.x > h_speed.x and sign(move_direction.x) == calc_inertial_dir.x: -# calc_inertia.x = h_speed.x - - if modifier and modifier.is_active: - var foo = 2+2 # breakpoint check - - ## Apply acceleration - calc_inertia.x = move_toward(calc_inertia.x, h_speed.y, calc_acceleration.x * _delta) - ## Apply friction - #calc_inertia.x = move_toward(calc_inertia.x, 0, abs(calc_friction.x * _delta)) + 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 impulse_applied == false and calc_inertia.x == 0.0: + calc_inertia.x += h_speed.x + impulse_applied = true + impulse_applied_dir = move_direction.x + 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: @@ -363,12 +352,13 @@ func new_move_actor_as_desired(_delta :float, 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}) ) @@ -380,12 +370,6 @@ func is_out_of_range(value: float, a: float, b: float) -> bool: var upper = max(a, b) return value <= lower or value >= upper -enum RANGE_PLACEMENT { - BEFORE_RANGE, - WITHIN_RANGE, - PAST_RANGE -} - ## 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.