movie-quote/shark.gd

76 lines
2.2 KiB
GDScript3
Raw Normal View History

2026-03-29 18:52:33 +00:00
#@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
2026-03-29 21:58:57 +00:00
var facing_right: bool = true: set = set_facing_right
var tilt: float = 0.0
var tilt_power: 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-29 18:52:33 +00:00
@onready var shark_cam: Camera2D = $SharkCam
## OVERRIDES
func _ready() -> void:
pass
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-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
## CORE
## PRIVATE/HELPER
## RECEIVERS
## 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