Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Actor
- var direction = Vector2.ZERO
- var hookOut = false
- var hookPosition = Vector2.ZERO
- var ropeLength = Vector2.ZERO
- var ropeAngleVelocity = 0
- var ropeAngle = 0
- var ropeEndPoint = Vector2.ZERO
- signal animate
- func _physics_process(delta: float) -> void:
- # check for short jump
- var is_jump_interrupted = Input.is_action_just_released("ButtonA") and velocity.y < 0
- # get where the player wants to go.
- direction = get_direction()
- operate_hook_shot()
- if hookOut:
- hook_swing()
- velocity.y = gravity
- velocity = calculate_platforming_velocity(velocity, direction, speed, is_jump_interrupted)
- velocity = move_and_slide(velocity, FLOOR_NORMAL)
- animate(velocity)
- func get_direction() -> Vector2:
- return Vector2(
- Input.get_action_strength("Right") - Input.get_action_strength("Left"),
- -Input.get_action_strength("ButtonA") if can_jump() and Input.is_action_just_pressed("ButtonA") else 0.0
- )
- func can_jump() -> bool:
- # TODO checks if player can jump based on being on the floor, having coyote time or swinging
- return is_on_floor()
- func calculate_platforming_velocity(
- linear_velocity: Vector2,
- direction: Vector2,
- speed: Vector2,
- is_jump_interrupted: bool
- ) -> Vector2:
- # Horizontal Velocity
- var new_velocity: = linear_velocity
- new_velocity.x = speed.x * direction.x
- # Vertical Velocity
- if direction.y != 0.0:
- new_velocity.y = speed.y * direction.y
- if is_jump_interrupted:
- new_velocity.y = 0.0
- return new_velocity
- func animate(velocity):
- emit_signal("animate", velocity)
- func operate_hook_shot():
- if Input.is_action_just_released("ButtonB"):
- hookOut = false
- velocity.y -= 150
- elif Input.is_action_just_pressed("ButtonB"):
- hookPosition = get_global_mouse_position()
- ropeLength = get_position().distance_to(hookPosition)
- ropeAngleVelocity = 0
- ropeAngle = get_position().angle_to_point(hookPosition)
- hookOut = true
- func hook_swing():
- var playerDelta = hookPosition - get_position()
- var currentDistance = playerDelta.length()
- if currentDistance > ropeLength:
- var correctionAmount = currentDistance - ropeLength
- var hookDir = playerDelta / currentDistance
- var correctionVector = (hookDir * correctionAmount)
- print(correctionVector)
- move_and_collide(correctionVector)
- velocity += (correctionVector * 0.1) / get_physics_process_delta_time()
- #func hook_swing():
- # var left_pressed = Input.get_action_strength("Left")
- # var right_pressed = Input.get_action_strength("Right")
- # var _ropeAcceleration = sin(ropeAngle - deg2rad(90)) * -0.08 / ropeLength
- #
- # _ropeAcceleration -= (right_pressed - left_pressed) * 0.1 / ropeLength
- # ropeAngleVelocity += _ropeAcceleration
- # ropeAngle += ropeAngleVelocity
- # ropeAngleVelocity *= 0.99
- # ropeEndPoint = hookPosition + (Vector2(cos(ropeAngle),sin(ropeAngle)) * ropeLength)
- #
- # var swingMotion = (ropeEndPoint - get_position())
- ## swingMotion.x = clamp(swingMotion.x, -5,5)
- ## swingMotion.y = clamp(swingMotion.y, -5,5)
- ## if swingMotion.x < 0.2 and swingMotion.x > -0.2:
- ## swingMotion.x = 0
- ## ropeAngleVelocity = 0
- ## ropeAngle = get_position().angle_to_point(hookPosition)
- ## print(swingMotion.x)
- # var colision = move_and_collide(swingMotion)
- # if colision:
- # ropeAngle = get_position().angle_to_point(hookPosition)
- # ropeAngleVelocity *= -0.5
- # velocity = swingMotion / get_physics_process_delta_time()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement