This repository has been archived on 2025-09-28. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
rectangular/core/player/player.gd

55 lines
1.7 KiB
GDScript3
Raw Permalink Normal View History

2024-09-25 21:18:58 +02:00
extends CharacterBody2D
@onready var camera = $Camera2D
# die
func die():
2024-10-16 20:23:53 +02:00
LevelsCore.load_entrypoint(Gamestate.last_entrypoint)
# movement and stuff
2024-09-25 21:18:58 +02:00
@export var movement_speed = 300.0
@export var jump_velocity = -350.0
2024-10-16 20:23:53 +02:00
@export var air_friction = 0.02
@export var floor_friction = 0.1
2024-09-25 21:18:58 +02:00
@export var max_jumps: int = 2
@export var rigidbody_impulse_mult: float = 0.04
2024-09-25 21:18:58 +02:00
2024-10-16 20:23:53 +02:00
var locked: bool = false
var jumps: int = 0
2024-09-25 21:18:58 +02:00
func _physics_process(delta: float) -> void:
2024-10-16 20:23:53 +02:00
var gravity = get_gravity()
2024-09-25 21:18:58 +02:00
var dir = Input.get_axis("player_left", "player_right")
var on_floor = is_on_floor()
var on_wall = is_on_wall()
# left right movement
2024-10-16 20:23:53 +02:00
if dir and not locked:
velocity.x = move_toward(velocity.x, dir * movement_speed, movement_speed*floor_friction)
elif on_floor:# or on_ceiling:
velocity.x = move_toward(velocity.x, 0, movement_speed*floor_friction)
elif not locked:
velocity.x = move_toward(velocity.x, 0, movement_speed*air_friction)
2024-09-25 21:18:58 +02:00
# gravity
2024-10-16 20:23:53 +02:00
if not on_floor:
velocity += gravity * delta
2024-09-25 21:18:58 +02:00
# reset number of jumps
2024-10-16 20:23:53 +02:00
if on_floor or on_wall:
2024-09-25 21:18:58 +02:00
jumps = 0
# jumping / dropping from ceiling
2024-10-16 20:23:53 +02:00
if Input.is_action_just_pressed("player_jump") and not locked:
if jumps < max_jumps: # (allows air jumps)
2024-09-25 21:18:58 +02:00
velocity.y = jump_velocity
jumps += 1
move_and_slide()
# affect rigid bodies
# adapted solution from
# https://kidscancode.org/godot_recipes/4.x/physics/character_vs_rigid/index.html
for i in get_slide_collision_count():
var collision = get_slide_collision(i)
var collider = collision.get_collider()
if collider is RigidBody2D:
var impulse = -collision.get_normal() * (velocity.length() / collider.mass) * rigidbody_impulse_mult
collider.apply_central_impulse(impulse)