81 lines
2.1 KiB
GDScript
81 lines
2.1 KiB
GDScript
extends Node2D
|
|
var body : CharacterBody2D;
|
|
var camera : Camera2D;
|
|
var earth_center = Vector2.ZERO;
|
|
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
|
|
|
|
func _ready() -> void:
|
|
body = get_parent().get_node("Player")
|
|
camera = get_node("Camera2D")
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if(inv_time > 0):
|
|
inv_time = max(0, inv_time-delta)
|
|
|
|
angle = -(self.position - earth_center).angle_to(Vector2.UP)
|
|
body.rotation = angle;
|
|
body.up_direction = (self.position - earth_center).normalized();
|
|
|
|
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);
|
|
local_velocity += Vector2(0, gravity)
|
|
|
|
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
|
|
body.velocity = global_from_local(local_velocity)
|
|
|
|
|
|
func global_from_local (_velocity: Vector2) -> Vector2:
|
|
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
|