#@tool #@icon(icon_path: String) #class_name MyNode extends RigidBody2D ## Documentation comments #signal enum State {IDLE, SWIM, BITE} const WATER_LEVEL: float = 0.0 @export_range(0, 360, 1.0, "radians_as_degrees") var rotation_speed: float = 0.0 ## degrees per second @export var swim_power: float = 100.0 var altitude: float = 0.0 var current_state: State = State.IDLE: set = set_state var input_vector: Vector2 var facing_right: bool = true: set = set_facing_right var tilt: 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: _connect_signals() 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_movement(delta) if Input.is_action_just_pressed("swim"): _swim() if Input.is_action_just_pressed("bite"): _bite() if global_position.y < WATER_LEVEL: _apply_gravity() else: altitude = 0.0 gravity_scale = 0.01 ## CORE func _bite() -> void: current_state = State.BITE _apply_swimpulse() func _swim() -> void: current_state = State.SWIM _apply_swimpulse() ## PRIVATE/HELPER func _apply_gravity() -> void: altitude = -global_position.y gravity_scale = 0.01 * altitude func _apply_swimpulse() -> void: if facing_right: apply_central_impulse(Vector2(1.0, tilt).normalized() * swim_power) else: apply_central_impulse(Vector2(-1.0, tilt).normalized() * swim_power) func _connect_signals() -> void: shark_sprite.connect("animation_finished", on_animation_finished) func _handle_movement(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) ## RECEIVERS func on_animation_finished() -> void: current_state = State.IDLE ## 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 func set_state(new_state: State) -> void: match new_state: State.IDLE: shark_sprite.play("idle") State.SWIM: shark_sprite.play("swim") State.BITE: shark_sprite.play("bite")