diff --git a/lib/classes/movement_state_receiver.gd b/lib/classes/movement_state_receiver.gd index 4f0f4b2..b048ae4 100644 --- a/lib/classes/movement_state_receiver.gd +++ b/lib/classes/movement_state_receiver.gd @@ -24,6 +24,7 @@ onready var desired_movement_vector: Vector2 = Vector2(0,0) # I'm thinking about moving the velocity tracker in here instead of player. var velocity = Vector2(0,0) var momentum = Vector2(0,0) +var inertia = Vector2(0,0) var acceleration = Vector2(0,0) #Removing Probably not used @@ -135,7 +136,161 @@ func _on_state_change(old_state_name:String, new_state :State): push_warning("Received non animated Actor state.") #current_state = new_state +## Side effects for these variables +# velocity +# momentum +# acceleration +# accepts the state to determine movement, the deltatime, and an optional direction +# Update: Actually should just return movement in PPS +## +var calc_inertia = velocity.abs() +func new_move_actor_as_desired(_delta :float, + _state :StateAnimatedActor, + _movement_override_normal := Vector2(0,0)) -> Vector2: + ## Calculated movement speed. + var movement_params :Dictionary = { + "_base_h_move_speed" : _state.horizontal_speed, + "_base_h_move_acceleration" : _state.horizontal_acceleration, + "_base_h_move_speed_modifier" : _state.horizontal_speed_offset, + "_base_h_move_modifier_move_acceleration" : _state.horizontal_speed_offset_acceleration, + "_jerk_factor" : _state.jerk_factor, + "_gravity" : _state.gravity + } + var calc_velocity = Vector2.ZERO + var calc_acceleration = Vector2.ZERO + #var calc_inertia = Vector2.ZERO + #var calc_inertia = inertia + + ## 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. + ## + var h_speed = resolve_h_speed(movement_params) + #if h_speed != Vector2.ZERO: + # pass + ## 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(inertia, _movement_override_normal) + else: + move_direction = resolve_move_direction(inertia, desired_movement_vector) + + calc_acceleration.x = resolve_h_acceleration(movement_params, h_speed, move_direction.x) + if calc_acceleration.x != 0: + acceleration.x = calc_acceleration.x + + ## if we have a difference of speed + if h_speed.x != h_speed.y: + if calc_inertia.x == 0.0: + #calc_inertia.x = lerp( h_speed.x , h_speed.y, calc_acceleration.x * _delta) + calc_inertia.x = clamp( calc_inertia.x + (calc_acceleration.x * _delta), + h_speed.x , h_speed.y) + else: + #calc_inertia.x = lerp( calc_inertia.x, h_speed.y, calc_acceleration.x * _delta) + calc_inertia.x = clamp( calc_inertia.x + ( calc_acceleration.x * _delta), + calc_inertia.x, h_speed.y) + elif calc_inertia.x != 0.0: + ## We still have inertia + #calc_inertia.x = lerp(calc_inertia.x, h_speed.x, _delta) + calc_inertia.x = clamp( calc_inertia.x + ( acceleration.x * _delta), + calc_inertia.x, h_speed.x) +# if (current_state.name == 'idle'): +# print(acceleration) + else: + ## inertia is just base speed + calc_inertia.x = h_speed.x + acceleration = Vector2.ZERO + + #calc_acceleration.x = resolve_h_acceleration(movement_params, h_speed, move_direction.x) + + ## One idea, pass all the calculations independently +# var movement = { +# "velocity_PPS" : calc_velocity, +# "acceleration_PPS" : calc_acceleration, +# "inertia_PPS" : calc_inertia +# } + + ## Another idea, just return the calculated velocity in PPS + ## calc x component of felicty + + ## For now, y component of velocity is just gravity + calc_velocity.y = movement_params["_gravity"] * Vector2.DOWN.y + + calc_velocity.x = calc_inertia.x * move_direction.x + + UiManager.debug_text = ( #"H_Speed x Dir: " + str(h_speed) + str('duh') + + "H_Speed: {0}, {1}".format({"0":"%5.2f" % h_speed.x, "1":"%5.2f" % h_speed.y}) + + "\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}) + + "\nAccel: {0}, {1}".format({"0":"%5.2f" % acceleration.x, "1":"%5.2f" % acceleration.y}) + + "\nAccelCalc : {0}, {1}".format({"0":"%5.2f" % calc_acceleration.x, "1":"%5.2f" % calc_acceleration.y}) + + + #"\nLength: " + str(velocity.length()) + + "\nMoveDir: {0}, {1}".format({"0":"%4.1f" % move_direction.x, "1":"%4.1f" % move_direction.y}) + ) + + return calc_velocity + +## Passed in acceleration, vel, etc is applied to whatever +## the parent is. If Kinematic then move_and_slide, otherwise manually adjusted +func apply_movement_to_parent( _velocity :Vector2) -> void: + var snap = Vector2.DOWN * 8 + if parent is KinematicBody2D: + 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 :Dictionary) -> Vector2: + var base_speed :float = _params["_base_h_move_speed"] + ## if a speed difference applies + if _params["_base_h_move_speed_modifier"] != 0: + var speed_differance = base_speed + _params["_base_h_move_speed_modifier"] + + ## We start at a slower base speed and move upward + if speed_differance > base_speed: + return(Vector2(base_speed, speed_differance)) + else: + ## We start at a higher speed and move downward to base + return(Vector2(speed_differance, base_speed )) +# if inertia: +# return Vector2(0,inertia) + return Vector2(base_speed,base_speed) + +func resolve_h_acceleration(_params :Dictionary, _speed :Vector2, _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 + if _params["_base_h_move_speed_modifier"] != 0: + _acceleration = _params["_base_h_move_acceleration"] + _params["_base_h_move_modifier_move_acceleration"] +# if sign(_acceleration) == sign(_move_direction): +# return _acceleration +# elif _move_direction == 0: +# return _acceleration * -1 + if _speed.x < _speed.y: + return _acceleration + else: + return _acceleration * -1 + 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) + + return Vector2(horizontal_movement_direction,0) + + +func adjust_base_movement(): + pass + +## 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 diff --git a/src/actors/players/playerE/PlayerE-Movement_StateReceiver.gd b/src/actors/players/playerE/PlayerE-Movement_StateReceiver.gd index 8095202..8a93874 100644 --- a/src/actors/players/playerE/PlayerE-Movement_StateReceiver.gd +++ b/src/actors/players/playerE/PlayerE-Movement_StateReceiver.gd @@ -159,6 +159,9 @@ func _state_process_physics_idle(): if $"%LadderDetector".is_colliding() and _wants_climb == true: request_state_change.call_func('climb') + var _v :Vector2 = new_move_actor_as_desired( physics_delta , current_state ) + apply_movement_to_parent(_v) + func attack_primary(): ##Another thing I wish I could avoid the funcref or string call # if get_parent().has_method("use_primary_item"): @@ -181,6 +184,8 @@ func attack_secondary(): func _state_process_physics_attack_shoot(): + ##TODO: Shoot should probably be more like how stun works + # I'd like to not have to have an instance to the pew in this code. if pew_machine.timer.time_left == 0: pew_machine.shoot() @@ -314,7 +319,9 @@ func _state_process_physics_move(): if !parent.is_on_floor(): request_state_change.call_func('fall') - move_actor_as_desired() + #move_actor_as_desired() + var _v :Vector2 = new_move_actor_as_desired( physics_delta , current_state ) + apply_movement_to_parent(_v) func get_climb_shape_location() -> Vector2: if $"%LadderDetector".is_colliding(): diff --git a/src/actors/players/playerE/states/move.tres b/src/actors/players/playerE/states/move.tres index 8ed120c..83c1033 100644 --- a/src/actors/players/playerE/states/move.tres +++ b/src/actors/players/playerE/states/move.tres @@ -10,8 +10,8 @@ debug_state = false timeout_seconds = 0.0 name = "move" horizontal_speed = 90.0 -horizontal_acceleration = 0.0 -horizontal_speed_offset = 0.0 +horizontal_acceleration = 10.0 +horizontal_speed_offset = -90.0 horizontal_speed_offset_acceleration = 0.0 jerk_factor = 0.0 animation_sequence = [ "run" ]