Add ship movement and shooting mechanics; update flying ship scene with new components
This commit is contained in:
26
scripts/components/ship_movement.gd
Normal file
26
scripts/components/ship_movement.gd
Normal file
@@ -0,0 +1,26 @@
|
||||
class_name ShipMovement
|
||||
extends Node
|
||||
|
||||
@export var max_speed: float = 200.0
|
||||
@export var acceleration: float = 100.0
|
||||
@export var friction: float = 50.0
|
||||
@export var body: CharacterBody2D
|
||||
|
||||
var velocity: Vector2 = Vector2.ZERO
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
var input_vector := Vector2(
|
||||
Input.get_action_strength("right") - Input.get_action_strength("left"),
|
||||
Input.get_action_strength("down") - Input.get_action_strength("up")
|
||||
).normalized()
|
||||
|
||||
if input_vector != Vector2.ZERO:
|
||||
velocity = velocity.move_toward(input_vector * max_speed, acceleration * delta)
|
||||
else:
|
||||
velocity = velocity.move_toward(Vector2.ZERO, friction * delta)
|
||||
|
||||
velocity = velocity.limit_length(max_speed)
|
||||
body.velocity = velocity
|
||||
body.move_and_slide()
|
||||
|
1
scripts/components/ship_movement.gd.uid
Normal file
1
scripts/components/ship_movement.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://b3mrdvre1y567
|
33
scripts/components/ship_shooter.gd
Normal file
33
scripts/components/ship_shooter.gd
Normal file
@@ -0,0 +1,33 @@
|
||||
class_name ShipShooter
|
||||
extends Node
|
||||
|
||||
@export var bullet_scene: PackedScene
|
||||
@export var fire_rate: float = 0.2
|
||||
@export var bullet_spawn: Marker2D
|
||||
@export var shoot_sfx: AudioStreamPlayer2D
|
||||
|
||||
var can_shoot: bool = true
|
||||
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
if Input.is_action_just_pressed("attack") and can_shoot:
|
||||
shoot()
|
||||
|
||||
|
||||
func shoot() -> void:
|
||||
if not can_shoot:
|
||||
return
|
||||
|
||||
var bullet: Node2D = bullet_scene.instantiate()
|
||||
var init := bullet.get_node_or_null("ProjectileInitComponent") as ProjectileInitComponent
|
||||
if init:
|
||||
init.initialize({
|
||||
"position": bullet_spawn.global_position,
|
||||
})
|
||||
get_tree().current_scene.add_child(bullet)
|
||||
if shoot_sfx:
|
||||
shoot_sfx.play()
|
||||
|
||||
can_shoot = false
|
||||
await get_tree().create_timer(fire_rate).timeout
|
||||
can_shoot = true
|
1
scripts/components/ship_shooter.gd.uid
Normal file
1
scripts/components/ship_shooter.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://d1ctdx52gskv1
|
Reference in New Issue
Block a user