extends CharacterBody2D # Child Nodes var camera : Camera2D; var anim_sprite : AnimatedSprite2D var earth_aligner : Node2D # Gravity var earth_center = Vector2.ZERO; var max_fall_speed = 700; var gravity = 100; # Movement var hspeed = 150; var jump_strength = 1100; var air_jumps_max = 1; var air_jumps_current = 1; # HP and Iframes signal health_changed(new_health : int) var current_hp = 5: set(new_hp): current_hp = new_hp health_changed.emit(current_hp) var max_hp = 5; var hit_invulnerability = 0.5 var inv_time = 0; # Received Knockback var reset_to_velocity = Vector2.ZERO var knockback_strength = 1500 var damage_knockup = 500 func _ready() -> void: camera = $Camera2D anim_sprite = $AnimatedSprite2D earth_aligner = $EarthAligner func _physics_process(delta: float) -> void: manage_iframes(delta) manage_movement_options() manage_animation() manage_velocity() move_and_slide() func manage_iframes(delta: float): if(inv_time > 0): inv_time = max(0, inv_time-delta) func manage_movement_options() -> void: if(is_on_floor()): air_jumps_current = air_jumps_max func manage_animation() -> void: var walk_dir = 0 if(Input.is_action_pressed("move_right")): walk_dir += 1 if(Input.is_action_pressed("move_left")): walk_dir -= 1 if(walk_dir != 0): anim_sprite.scale.x = - abs(anim_sprite.scale.x) * walk_dir if(is_on_floor()): anim_sprite.play("walk") else: anim_sprite.stop() # Later: Jump Animation? else : anim_sprite.stop() func manage_velocity() -> void: up_direction = (position - earth_center).normalized(); var old_local_velocity = local_from_global(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 (is_on_floor() or air_jumps_current > 0)): if(not 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 if(reset_to_velocity != Vector2.ZERO): local_velocity = reset_to_velocity reset_to_velocity = Vector2.ZERO velocity = global_from_local(local_velocity) func global_from_local (_velocity: Vector2) -> Vector2: return _velocity.rotated(earth_aligner.angle) func local_from_global (_velocity: Vector2) -> Vector2: return _velocity.rotated(-earth_aligner.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 reset_to_velocity = Vector2(-sign(local_from_global(dir).x)*knockback_strength, -damage_knockup) func die(): print("You Died") # Placeholder