Updated existing shot code with constants and hard-coded variables. No

magic numbers!
This commit is contained in:
Henry 2026-04-05 12:02:13 +01:00
parent 1794abe50a
commit 0ccdc03bf8
3 changed files with 10 additions and 5 deletions

View file

@ -104,7 +104,7 @@ autoplay = "fwd"
[node name="MuzzleFlash" type="AnimatedSprite2D" parent="Ship" unique_id=1584132038] [node name="MuzzleFlash" type="AnimatedSprite2D" parent="Ship" unique_id=1584132038]
unique_name_in_owner = true unique_name_in_owner = true
position = Vector2(0, -16) position = Vector2(0.5, -16)
sprite_frames = SubResource("SpriteFrames_3v2ag") sprite_frames = SubResource("SpriteFrames_3v2ag")
animation = &"stock" animation = &"stock"
autoplay = "stock" autoplay = "stock"

View file

@ -24,6 +24,7 @@ var weapon_spacing: float = 0.1
@onready var screensize = get_viewport_rect().size @onready var screensize = get_viewport_rect().size
@onready var muzzle_damage = %MuzzleBox @onready var muzzle_damage = %MuzzleBox
func _process(delta): func _process(delta):
var input = Input.get_vector("left", "right", "up","down") var input = Input.get_vector("left", "right", "up","down")
ship_displacement = position.y - previous_position.y ship_displacement = position.y - previous_position.y
@ -63,7 +64,6 @@ func _process(delta):
is_shooting = false is_shooting = false
if is_shooting == true: if is_shooting == true:
print("Spacebar pressed!")
%MuzzleFlash.show() %MuzzleFlash.show()
%Ship/MuzzleFlash.animation = "stock" %Ship/MuzzleFlash.animation = "stock"
@ -87,13 +87,15 @@ func _on_weapon_cooldown_timeout() -> void:
func shoot(): func shoot():
var projectiles: int = 2 var projectiles: int = 3
const PROJECTILE_SPACING: float = 10
const MUZZLE_HEIGHT: int = -23
for b in projectiles: for b in projectiles:
# Instantiate the bullets # Instantiate the bullets
var bullet = weapon_current.instantiate() var bullet = weapon_current.instantiate()
get_tree().root.add_child(bullet) get_tree().root.add_child(bullet)
bullet.position = position + Vector2((b * 10) - (projectiles * projectiles),-23) bullet.position = position + Vector2((b * PROJECTILE_SPACING) - (projectiles * projectiles), MUZZLE_HEIGHT)
# Get weapon speed and spacing for equidistant calculations # Get weapon speed and spacing for equidistant calculations
weapon_rate = bullet.shot_data.rate weapon_rate = bullet.shot_data.rate

View file

@ -1,11 +1,14 @@
class_name PlayerShot class_name PlayerShot
extends Resource extends Resource
@export_category("Info")
@export var shot_name: String @export var shot_name: String
@export var sprite: Texture2D = preload("res://graphics/shot.png")
@export_category("Shot Data")
@export var damage: int = 1 @export var damage: int = 1
@export var speed: int = 135 @export var speed: int = 135
@export var projectiles: int = 2 @export var projectiles: int = 2
@export var rate: float = 0.1 @export var rate: float = 0.1
@export var cooldown: float = 0.25 @export var cooldown: float = 0.25
@export var sprite: Texture2D = preload("res://graphics/shot.png")
@export var spacing: float = 35 @export var spacing: float = 35