#@tool #@icon(icon_path: String) #class_name MyNode extends CharacterBody2D ## Documentation comments #signal enum FaceDirection {LEFT, RIGHT} #const @export_range(0, 360, 1.0, "radians_as_degrees") var rotation_speed: float = 0.0 ## degrees per second var input_vector: Vector2 var facing_right: bool = true: set = set_facing_right var tilt: float = 0.0 var tilt_power: float = 0.0 @onready var shark_sprite: AnimatedSprite2D = $SharkSprite @onready var shark_collider: CollisionShape2D = $SharkCollider @onready var shark_cam: Camera2D = $SharkCam ## OVERRIDES func _ready() -> void: pass func _process(_delta: float) -> void: if facing_right: tilt = clamp(rotation, -1.0, 1.0) else: tilt = -clamp(rotation, -1.0, 1.0) func _physics_process(delta: float) -> void: # handle left-right movement input_vector.x = Input.get_axis("left", "right") # flip sprite if needed if abs(input_vector.x) > 0: # moving horizontally if input_vector.x > 0: # moving right facing_right = true elif input_vector.x < 0: # moving left facing_right = false # handle up/down tilt input_vector.y = Input.get_axis("up", "down") if abs(input_vector.y) > 0: # attempting to tilt if input_vector.y < 0: # attempting to tilt up (up is negative) if tilt > -1.0: # able to tilt further up #tilt = rotate_toward(tilt, (-PI/2), rotation_speed * delta) if facing_right: # tilting up means negative rotation rotate(-rotation_speed * delta) else: # tilting up means positive rotation rotate(rotation_speed * delta) elif input_vector.y > 0: # attempting to tilt down (up is negative) if tilt < 1.0: # able to tilt further down #tilt = rotate_toward(tilt, (PI/2), rotation_speed * delta) if facing_right: # tilting down means positive rotation rotate(rotation_speed * delta) else: # tilting down means negative rotation rotate(-rotation_speed * delta) ## CORE ## PRIVATE/HELPER ## RECEIVERS ## SETTERS/GETTERS func set_facing_right(is_facing_right: bool) -> void: if facing_right != is_facing_right: rotation = -rotation if is_facing_right == true: shark_sprite.flip_h = true else: shark_sprite.flip_h = false facing_right = is_facing_right