gfolf2/src/player/shot_setup/ball_point.gd

43 lines
1.2 KiB
GDScript3
Raw Normal View History

class_name BallPoint extends Node3D
## Ball spawn point & origin. I.E. the "tee".
## Emitted when a new ball is placed.
signal ball_changed(ball: GameBall)
## Scenes for each type of ball.
const SCENE_MAP: Dictionary = {
2024-11-20 19:22:11 -07:00
GameBall.Type.BASIC: preload("res://src/equipment/balls/physics_ball/physics_ball.tscn"),
GameBall.Type.PLASMA: preload("res://src/equipment/balls/plasma_ball/plasma_ball.tscn"),
}
@export var ball: GameBall:
set(value):
ball = value
ball_changed.emit(ball)
## Get a new instance of a ball of the given type.
## Returns null if the type can't be instantiated (e.g. NONE type)
2024-11-20 19:22:11 -07:00
func get_instance(type: GameBall.Type) -> GameBall:
if type in SCENE_MAP:
var scene: PackedScene = SCENE_MAP.get(type)
return scene.instantiate() as GameBall
return null
## Clear any existing ball, instantiate a new one of the given type, and place it at the ball point.
2024-11-20 19:22:11 -07:00
func spawn_ball(type: GameBall.Type) -> void:
# Clear existing ball
if is_instance_valid(ball):
ball.queue_free()
ball = get_instance(type)
if is_instance_valid(ball):
add_child(ball)
snap()
## Snap the ball back to the ball point.
func snap() -> void:
ball.global_transform = global_transform