Equidistent code working in component form! Also cleaned up spacing for

increased projectiles in stock weapon by rounding remainders from
division.
This commit is contained in:
Henry 2026-04-07 13:54:30 +01:00
parent d419635d7f
commit db57a0234e
17 changed files with 58 additions and 48 deletions

108
scenes/player.gd Normal file
View file

@ -0,0 +1,108 @@
extends Area2D
@export var speed: int = 150
@export var shield_max: int = 10
#var ship = %Ship
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
@export var weapon_current : PackedScene
#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
var weapon_spacing: float = 0.1
@onready var screensize = get_viewport_rect().size
@onready var muzzle_damage = %MuzzleBox
func _process(delta):
var input = Input.get_vector("left", "right", "up","down")
ship_displacement = position.y - previous_position.y
travel += bullet_speed * delta + abs(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:
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 = 3
const PROJECTILE_SPACING: float = 10
const MUZZLE_HEIGHT: int = -23
for b in projectiles:
# Instantiate the bullets
var bullet = weapon_current.instantiate()
get_tree().root.add_child(bullet)
bullet.position = position + Vector2((b - (projectiles - 1) / 2.0) * PROJECTILE_SPACING, MUZZLE_HEIGHT)
# Get weapon speed and spacing for equidistant calculations
weapon_rate = bullet.shot_data.rate
weapon_spacing = bullet.shot_data.projectile_spacing
bullet_speed = bullet.shot_data.speed
# Print which weapon is firing
prints(bullet.shot_data.shot_name, "weapon fired")

1
scenes/player.gd.uid Normal file
View file

@ -0,0 +1 @@
uid://otm88638j7f8

View file

@ -1,6 +1,6 @@
[gd_scene format=3 uid="uid://coix5dqblmu7r"]
[ext_resource type="Script" uid="uid://bqxrdf7mtx0ev" path="res://scripts/player_refactor.gd" id="1_qlg0r"]
[ext_resource type="Script" uid="uid://bqxrdf7mtx0ev" path="res://scenes/player_refactor.gd" id="1_qlg0r"]
[ext_resource type="Texture2D" uid="uid://cq4we1m1yv22s" path="res://graphics/ship.png" id="2_qhqgy"]
[ext_resource type="Texture2D" uid="uid://crmbupr3qg0j" path="res://graphics/muzzle_flash.png" id="4_dqkch"]
[ext_resource type="Texture2D" uid="uid://b0iavxi8vaxtj" path="res://graphics/ship_thrusters.png" id="5_qlg0r"]

115
scenes/player_refactor.gd Normal file
View file

@ -0,0 +1,115 @@
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

View file

@ -0,0 +1 @@
uid://bqxrdf7mtx0ev

View file

@ -140,6 +140,7 @@ metadata/_custom_type_script = "uid://dss0dbwr71y6m"
unique_name_in_owner = true
script = ExtResource("6_l5qtw")
player = NodePath("..")
speed = 200.0
metadata/_custom_type_script = "uid://c0rikbakpcags"
[node name="ShootComponent" type="Node" parent="." unique_id=623642425]

12
scenes/stock_weapon.gd Normal file
View file

@ -0,0 +1,12 @@
extends Area2D
@onready var bullet: PackedScene = load("res://scenes/stock_weapon.tscn")
@export var shot_data: PlayerShot
func _process(delta):
position.y -= shot_data.speed * delta # This works normally
func _on_visible_on_screen_notifier_2d_screen_exited() -> void:
queue_free()

View file

@ -0,0 +1 @@
uid://d1rwqotmrag1r

View file

@ -1,6 +1,6 @@
[gd_scene format=3 uid="uid://ddpclu2vdy2ve"]
[ext_resource type="Script" uid="uid://d1rwqotmrag1r" path="res://scripts/stock_weapon.gd" id="1_hsma2"]
[ext_resource type="Script" uid="uid://d1rwqotmrag1r" path="res://scenes/stock_weapon.gd" id="1_hsma2"]
[ext_resource type="Resource" uid="uid://cels8t3hqjtsu" path="res://resources/player_weapons/shot_stock.tres" id="2_mvdrj"]
[ext_resource type="Texture2D" uid="uid://ti1uy42vnnhw" path="res://graphics/shot.png" id="3_mvdrj"]

View file

@ -1,6 +1,6 @@
[gd_scene format=3 uid="uid://b2ltmeb14nc17"]
[ext_resource type="Script" uid="uid://d1rwqotmrag1r" path="res://scripts/stock_weapon.gd" id="1_kx6bj"]
[ext_resource type="Script" uid="uid://d1rwqotmrag1r" path="res://scenes/stock_weapon.gd" id="1_kx6bj"]
[ext_resource type="Script" uid="uid://ccdohs4gduee5" path="res://scripts/player_shot.gd" id="2_5286c"]
[ext_resource type="Texture2D" uid="uid://ti1uy42vnnhw" path="res://graphics/shot.png" id="3_58qml"]
@ -10,7 +10,6 @@ shot_name = "Stock"
speed = 350
projectiles = 3
projectile_spacing = 10.0
cooldown = 0.0
metadata/_custom_type_script = "uid://ccdohs4gduee5"
[sub_resource type="RectangleShape2D" id="RectangleShape2D_mvdrj"]

View file

@ -1,8 +1,8 @@
[gd_scene format=3 uid="uid://bj4fytc3sy482"]
[ext_resource type="PackedScene" uid="uid://14fhj8834pkq" path="res://scenes/ui.tscn" id="1_nnsk1"]
[ext_resource type="PackedScene" uid="uid://coix5dqblmu7r" path="res://scenes/player.tscn" id="2_rwgxs"]
[ext_resource type="Texture2D" uid="uid://b3dqvd23nbmrm" path="res://graphics/cloud layers.png" id="3_4wyf3"]
[ext_resource type="PackedScene" uid="uid://6wq3ynesnsha" path="res://scenes/player_revision.tscn" id="3_k0juu"]
[node name="World" type="Node2D" unique_id=1317852169]
@ -10,11 +10,6 @@
[node name="UI" parent="Level" unique_id=480295610 instance=ExtResource("1_nnsk1")]
[node name="Player" parent="Level" unique_id=652131079 instance=ExtResource("2_rwgxs")]
z_index = 1
position = Vector2(97, 173)
speed = 200
[node name="Background" type="Node2D" parent="Level" unique_id=485120278]
z_index = -1
@ -81,3 +76,6 @@ texture = ExtResource("3_4wyf3")
flip_h = true
hframes = 4
frame = 3
[node name="Player" parent="Level" unique_id=652131079 instance=ExtResource("3_k0juu")]
position = Vector2(106, 312)