movie-quote/shark.gd

121 lines
3.3 KiB
GDScript3
Raw Permalink Normal View History

2026-03-29 18:52:33 +00:00
#@tool
#@icon(icon_path: String)
#class_name MyNode
2026-03-30 04:16:49 +00:00
extends RigidBody2D
2026-03-29 18:52:33 +00:00
## Documentation comments
#signal
2026-03-30 04:16:49 +00:00
enum State {IDLE, SWIM, BITE}
const WATER_LEVEL: float = 0.0
2026-03-29 18:52:33 +00:00
@export_range(0, 360, 1.0, "radians_as_degrees") var rotation_speed: float = 0.0 ## degrees per second
2026-03-30 04:16:49 +00:00
@export var swim_power: float = 100.0
2026-03-29 18:52:33 +00:00
2026-03-30 04:16:49 +00:00
var altitude: float = 0.0
var current_state: State = State.IDLE: set = set_state
2026-03-29 18:52:33 +00:00
var input_vector: Vector2
2026-03-29 21:58:57 +00:00
var facing_right: bool = true: set = set_facing_right
var tilt: float = 0.0
2026-03-29 18:52:33 +00:00
@onready var shark_sprite: AnimatedSprite2D = $SharkSprite
2026-03-29 21:58:57 +00:00
@onready var shark_collider: CollisionShape2D = $SharkCollider
2026-03-30 04:16:49 +00:00
#@onready var shark_cam: Camera2D = $SharkCam
2026-03-29 18:52:33 +00:00
## OVERRIDES
func _ready() -> void:
2026-03-30 04:16:49 +00:00
_connect_signals()
2026-03-29 18:52:33 +00:00
func _process(_delta: float) -> void:
2026-03-29 21:58:57 +00:00
if facing_right:
tilt = clamp(rotation, -1.0, 1.0)
else:
tilt = -clamp(rotation, -1.0, 1.0)
2026-03-29 18:52:33 +00:00
func _physics_process(delta: float) -> void:
2026-03-30 04:16:49 +00:00
_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:
2026-03-29 21:58:57 +00:00
# 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)
2026-03-29 18:52:33 +00:00
## RECEIVERS
2026-03-30 04:16:49 +00:00
func on_animation_finished() -> void:
current_state = State.IDLE
2026-03-29 18:52:33 +00:00
## SETTERS/GETTERS
2026-03-29 21:58:57 +00:00
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
2026-03-30 04:16:49 +00:00
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")