The_Dark_Side_of_Earth/player.gd

82 lines
2.1 KiB
GDScript3
Raw Normal View History

2025-09-15 19:15:43 +02:00
extends Node2D
var body : CharacterBody2D;
2025-09-16 00:28:49 +02:00
var camera : Camera2D;
2025-09-15 19:15:43 +02:00
var earth_center = Vector2.ZERO;
2025-09-16 00:28:49 +02:00
var max_fall_speed = 700;
var jump_strength = 900;
var gravity = 100;
var hspeed = 150;
var angle = 0;
var bonus_velocity = Vector2.ZERO
var air_jumps_max = 1;
var air_jumps_current = 1;
var current_hp = 5;
var max_hp = 5;
var inv_time = 0;
var hit_invulnerability = 0.5
var knockback_strength = 1200
var damage_knockup = 200
2025-09-15 19:15:43 +02:00
func _ready() -> void:
body = get_parent().get_node("Player")
2025-09-16 00:28:49 +02:00
camera = get_node("Camera2D")
2025-09-15 19:15:43 +02:00
func _physics_process(delta: float) -> void:
2025-09-16 00:28:49 +02:00
if(inv_time > 0):
inv_time = max(0, inv_time-delta)
angle = -(self.position - earth_center).angle_to(Vector2.UP)
body.rotation = angle;
2025-09-15 19:15:43 +02:00
body.up_direction = (self.position - earth_center).normalized();
2025-09-16 00:28:49 +02:00
if(body.is_on_floor()):
air_jumps_current = air_jumps_max
manage_velocity()
body.move_and_slide()
func manage_velocity() -> void:
var old_local_velocity = local_from_global(body.velocity)
var local_velocity = Vector2(old_local_velocity.x/1.7, old_local_velocity.y);
2025-09-15 19:15:43 +02:00
local_velocity += Vector2(0, gravity)
2025-09-16 00:28:49 +02:00
if(Input.is_action_pressed("move_right")):
local_velocity += Vector2(hspeed, 0)
if(Input.is_action_pressed("move_left")):
local_velocity += Vector2(-hspeed, 0)
if(Input.is_action_just_pressed("jump") and (body.is_on_floor() or air_jumps_current > 0)):
if(not body.is_on_floor()):
air_jumps_current -= 1;
local_velocity.y = -jump_strength
if(local_velocity.y > max_fall_speed):
local_velocity.y = max_fall_speed
local_velocity += bonus_velocity
bonus_velocity = Vector2.ZERO
2025-09-15 19:15:43 +02:00
body.velocity = global_from_local(local_velocity)
2025-09-16 00:28:49 +02:00
2025-09-15 19:15:43 +02:00
func global_from_local (_velocity: Vector2) -> Vector2:
2025-09-16 00:28:49 +02:00
return _velocity.rotated(angle)
func local_from_global (_velocity: Vector2) -> Vector2:
return _velocity.rotated(-angle)
func hurt(dmg: int, dir: Vector2 = Vector2.ZERO):
if(inv_time <= 0):
current_hp -= dmg
if(current_hp <= 0):
die()
inv_time = hit_invulnerability
bonus_velocity = Vector2(-sign(local_from_global(dir).x)*knockback_strength, -damage_knockup)
func die():
print("You Died") # Placeholder