Want to contribute? Fork me on Codeberg.org!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.7 KiB

extends RigidBody2D
@export var speed = 300.0
@export var jump_velocity = -400.0
@export var wall_jump_velocity = 600.0
@export var fox_time = 5
@export var limp_gravity_modifier = 2.0
@export var on_ground_force = 50.0
@export var in_air_force = 10.0
@export var stop_force = 10.0
var last_grounded = 0
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var gun: Node2D
func _ready():
$MultiplayerSynchronizer.set_multiplayer_authority(name.to_int())
var username = GameManager.players[name.to_int()].name
$Label.text = username
var username_hash = 0
for i in username.length():
username_hash += username.unicode_at(i)
$Sprite2D.modulate = Color.from_hsv((username_hash % 255) / 255.0, 0.5, 0.8)
gun = $Gun
func _physics_process(_delta):
if $MultiplayerSynchronizer.get_multiplayer_authority() != multiplayer.get_unique_id():
return
# Add the gravity.
var is_on_floor = len($FloorDetect.get_overlapping_bodies()) > 0
if is_on_floor:
last_grounded = Time.get_ticks_msec()
gravity_scale = limp_gravity_modifier if Input.is_action_pressed("limp") else 1.0
gun.look_at(get_global_mouse_position())
var grounded = Time.get_ticks_msec() - last_grounded < fox_time
var on_left_wall = len($WallDetectLeft.get_overlapping_bodies()) > 0
var on_right_wall = len($WallDetectRight.get_overlapping_bodies()) > 0
var on_wall = on_left_wall or on_right_wall
#velocity_offset = velocity_offset.lerp(Vector2.ZERO, (grounded_drag_lerp if is_on_floor() else ungrounded_drag_lerp) * delta)
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("move_left", "move_right")
if grounded:
apply_central_force(Vector2.RIGHT * direction * speed * on_ground_force)
if direction == 0:
if linear_velocity.x > 0.1:
apply_central_force(Vector2.LEFT * stop_force)
elif linear_velocity.x < 0.1:
apply_central_force(Vector2.RIGHT * stop_force)
elif (not on_wall) or (on_left_wall && direction > 0) or (on_right_wall and direction < 0):
apply_central_force(Vector2.RIGHT * direction * speed * in_air_force)
if linear_velocity.x > speed:
linear_velocity.x = speed
elif linear_velocity.x < -speed:
linear_velocity.x = -speed
# Handle Jump.
if Input.is_action_just_pressed("jump"):
if grounded:
apply_central_impulse(Vector2.UP * jump_velocity)
$JumpSound.play()
if not grounded and on_wall:
apply_central_impulse(Vector2.UP * jump_velocity)
if on_right_wall:
apply_central_impulse(Vector2.LEFT * wall_jump_velocity)
else:
apply_central_impulse(Vector2.RIGHT * wall_jump_velocity)
$JumpSound.play()