facsimile-wing/scenes/player_refactor.gd
Henry db57a0234e Equidistent code working in component form! Also cleaned up spacing for
increased projectiles in stock weapon by rounding remainders from
division.
2026-04-07 13:54:30 +01:00

115 lines
3.3 KiB
GDScript

extends Area2D
@export var speed: int = 150
@export var shield_max: int = 10
var shield: int = 0: set = shield_set
var can_shoot: bool = true
var is_shooting: bool = false
var previous_position: Vector2
var ship_displacement: float
var travel: float = 0
var bullet_speed: int = 150
var weapon_spacing: float = 0.1
@export var weapon_current : PackedScene = preload("res://scenes/stock_weapon.tscn")
#var weapon_current = load("res://scenes/stock_weapon.tscn")
#var weapon_cooldown = Timer
#var weapon_rate: float = 0.1
#var weapon_timer: float = 0.0
@onready var screensize = get_viewport_rect().size
@onready var muzzle_damage = %MuzzleBox
func _process(delta):
var input = Input.get_vector("left", "right", "up","down")
# Calculate position and travel for equidistant bullet calculations
ship_displacement = abs(position.y - previous_position.y)
travel += bullet_speed * delta - ship_displacement
travel = clamp(travel, 0, 1.9 * weapon_spacing)
# Sprite feedback based one player input
if input.x > 0:
%Ship/Thrusters.position.x = $Ship.position.x+1
%Ship.frame = 2
%Ship/Thrusters.flip_h = true
%Ship/Thrusters.animation = "banked"
elif input.x < 0:
%Ship/Thrusters.position.x = $Ship.position.x-1
%Ship.frame = 1
%Ship/Thrusters.flip_h = false
%Ship/Thrusters.animation = "banked"
else:
%Ship.frame = 0
%Ship/Thrusters.position.x = $Ship.position.x
%Ship/Thrusters.flip_h = false
%Ship/Thrusters.animation = "fwd"
# Get previous positon for equidistant bullet calculation
previous_position = position
# Move the ship based on input within the screen bounds
position += input * speed * delta
position = position.clamp(Vector2(12,12), screensize - Vector2(12,12))
# Enable muzzle damage hitbox upon firing
if Input.is_action_pressed("shoot"):
is_shooting = true
muzzle_damage.set("disabled", false)
else:
is_shooting = false
if is_shooting == true:
%MuzzleFlash.show()
%Ship/MuzzleFlash.animation = "stock"
# Adjust bullet spacing before firing
if travel > weapon_spacing:
print("Travel is greater!")
shoot()
travel -= weapon_spacing
if is_shooting == false:
%MuzzleFlash.hide()
muzzle_damage.set("disabled", true)
func shield_set(_value: int):
return
func _on_weapon_cooldown_timeout() -> void:
#print("You can shoot again!")
#can_shoot = true
return
func shoot():
var projectiles: int = 5
const PROJECTILE_SPACING: float = 20
const MUZZLE_HEIGHT: int = -23
var bullet_resource: PlayerShot = load("res://resources/player_weapons/shot_stock.tres")
for b in range(projectiles):
# Instantiate the bullets
var bullet = weapon_current.instantiate()
get_tree().root.add_child(bullet)
prints(bullet_resource.speed,bullet_resource.projectiles, bullet_resource.projectile_spacing)
bullet.position = position + Vector2((b - (projectiles- 1) / 2.0) * PROJECTILE_SPACING /2, MUZZLE_HEIGHT)
# Get weapon speed and spacing for equidistant calculations
#weapon_rate = bullet.shot_data.rate
weapon_spacing = PROJECTILE_SPACING
#bullet_speed = bullet_resource.speed
# Print which weapon is firing
prints(bullet.shot_data.shot_name, "weapon fired")
#travel -= weapon_spacing