154 lines
4.7 KiB
GDScript
154 lines
4.7 KiB
GDScript
extends CharacterBody2D
|
|
class_name Player
|
|
|
|
|
|
@export var WALK_SPEED : float = 300.0
|
|
@export var SLOW_SPEED_MULTIPLIER : float = 3
|
|
@export var JUMP_VELOCITY : float = -500
|
|
@export var DASH_DISTANCE : float = 5
|
|
@export var DASH_TIME_LENGTH : float = 1
|
|
|
|
@export var ROPE_MAX_DISTANCE : float = 10000
|
|
@export var SWING_SWAY : float = 200.0
|
|
@export var DAMPENING : float = 0.8
|
|
|
|
@export var MID_AIR_MOVEMENT_SPEED : float = 300.0
|
|
|
|
const MOVEMENT_MARGIN : float = 0.01
|
|
|
|
|
|
var DASH_SPEED = DASH_DISTANCE/DASH_TIME_LENGTH
|
|
|
|
var MK : bool = true # mouse and keyboard or controller mode
|
|
|
|
|
|
var connection_point : Vector2 = Vector2.ZERO
|
|
var rope_length : float = -1
|
|
var on_rope : bool = false
|
|
@onready var rope : Rope = %Rope
|
|
|
|
@onready var sprite : AnimatedSprite2D = $AnimatedSprite2D
|
|
|
|
func _process(delta: float) -> void:
|
|
if Input.is_action_just_pressed("restart"):
|
|
Universe.switch_scene(2)
|
|
|
|
|
|
func add_gravity(delta: float) -> void:
|
|
velocity += get_gravity() * delta
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
|
|
# Add the gravity.
|
|
if not is_on_floor():
|
|
add_gravity(delta)
|
|
|
|
if on_rope: # handle the stuff here with if it's hanging at the time
|
|
#print("point: ", connection_points[-1], " ", position)
|
|
var conn_dir : Vector2 = connection_point - global_position
|
|
|
|
var angle : float = (-self.up_direction).angle_to(conn_dir)
|
|
|
|
#general tangential
|
|
var tangential_direction : Vector2 = Vector2(-conn_dir.y, conn_dir.x) # clockwise tangential
|
|
|
|
velocity = velocity.project(tangential_direction) # takes the part of the velocity that is tangent the the swing arc
|
|
|
|
if velocity.dot(up_direction) > 0:
|
|
velocity *= pow(DAMPENING, delta)
|
|
|
|
var direction := Input.get_axis("move_left", "move_right") ## walking left and right
|
|
if direction:
|
|
#print("swaying")
|
|
velocity += delta*SWING_SWAY*(tangential_direction* (1 if direction > 0 else -1)).normalized()
|
|
|
|
move_and_slide()
|
|
|
|
if global_position.distance_to(connection_point) != rope_length: # correct the player position to onto the rope length position of the pendulum
|
|
var diff :float = rope_length - global_position.distance_to(connection_point) # negative if farther
|
|
global_position -= conn_dir.normalized()*diff
|
|
|
|
sprite.play("swing")
|
|
$CollisionShape2D.shape = load("res://assets/swing_shape.tres")
|
|
if velocity.x > MOVEMENT_MARGIN:
|
|
sprite.flip_h = true
|
|
elif velocity.x < -MOVEMENT_MARGIN:
|
|
sprite.flip_h = false
|
|
|
|
sprite.rotation = angle + PI
|
|
var temp : Transform2D = Transform2D(0,Vector2(-3,18))
|
|
#temp.rotated(angle + PI)
|
|
$CollisionShape2D.transform = temp.rotated(angle + PI)
|
|
#$CollisionShape2D.rotate(angle + PI)
|
|
#print(position.distance_to(connection_point), " ", rope_length)
|
|
pass
|
|
|
|
|
|
else:
|
|
|
|
#reset sprite orientation here
|
|
sprite.rotation = 0
|
|
if sprite.animation == "swing":
|
|
sprite.play("mid_air")
|
|
if velocity.x < 0:
|
|
sprite.flip_h = true
|
|
elif velocity.x > 0:
|
|
sprite.flip_h = false
|
|
#print("hi")
|
|
$CollisionShape2D.shape = load("res://assets/mid_air_shape.tres")
|
|
$CollisionShape2D.transform = Transform2D(0,Vector2(-2,16))
|
|
elif sprite.animation != "mid_air" or is_on_floor():
|
|
if velocity.x < 0:
|
|
sprite.flip_h = true
|
|
elif velocity.x > 0:
|
|
sprite.flip_h = false
|
|
if abs(velocity.x) > 0:
|
|
sprite.play("run")
|
|
$CollisionShape2D.shape = load("res://assets/normal_shape.tres")
|
|
$CollisionShape2D.transform = Transform2D(0,Vector2(-1.5,-0.5))
|
|
# Handle jump.
|
|
if Input.is_action_just_pressed("jump") and is_on_floor():
|
|
%PlayerAudio.play_sound("jump")
|
|
velocity.y = JUMP_VELOCITY
|
|
|
|
var direction := Input.get_axis("move_left", "move_right") ## walking left and right
|
|
if is_on_floor():
|
|
if direction:
|
|
var acting_speed : float = WALK_SPEED
|
|
if direction * velocity.x < 0:
|
|
acting_speed *= SLOW_SPEED_MULTIPLIER
|
|
|
|
velocity.x = clamp(velocity.x + (direction*delta*acting_speed), -WALK_SPEED, WALK_SPEED)
|
|
|
|
if direction > MOVEMENT_MARGIN:
|
|
sprite.play("run")
|
|
sprite.flip_h = false
|
|
elif direction < -MOVEMENT_MARGIN:
|
|
sprite.play("run")
|
|
sprite.flip_h = true
|
|
else:
|
|
velocity.x = move_toward(velocity.x, 0, SLOW_SPEED_MULTIPLIER*WALK_SPEED*delta)
|
|
|
|
if abs(velocity.x) > 0:
|
|
#print("hi")
|
|
if not %PlayerAudio.is_playing("run"):
|
|
%PlayerAudio.play_sound("run")
|
|
else:
|
|
%PlayerAudio.stop_sound("run")
|
|
else:
|
|
if direction:
|
|
var acting_speed : float = MID_AIR_MOVEMENT_SPEED
|
|
if direction * velocity.x < 0:
|
|
acting_speed *= SLOW_SPEED_MULTIPLIER
|
|
if abs(velocity.x + (direction * delta * acting_speed)) < MID_AIR_MOVEMENT_SPEED:
|
|
velocity.x = clamp(velocity.x + (direction*delta*acting_speed), -MID_AIR_MOVEMENT_SPEED, MID_AIR_MOVEMENT_SPEED)
|
|
|
|
|
|
|
|
|
|
move_and_slide()
|
|
if (velocity == Vector2.ZERO):
|
|
sprite.play("idle")
|
|
#print("v3: ", velocity)
|