Attempting to refactor the previously working self contained equidistent

code in the player.gd.
This commit is contained in:
Henry 2026-04-06 12:46:36 +01:00
parent 5d2efe9983
commit d419635d7f
15 changed files with 153 additions and 46 deletions

View file

@ -6,8 +6,8 @@
[resource] [resource]
script = ExtResource("1_t07sl") script = ExtResource("1_t07sl")
shot_name = "Stock" shot_name = "Stock"
speed = -350 speed = 350
projectiles = 3 projectiles = 4
projectile_spacing = 20.0
cooldown = 0.0 cooldown = 0.0
projectile_spacing = 10.0
metadata/_custom_type_script = "uid://ccdohs4gduee5" metadata/_custom_type_script = "uid://ccdohs4gduee5"

View file

@ -1,7 +1,6 @@
[gd_scene format=3 uid="uid://coix5dqblmu7r"] [gd_scene format=3 uid="uid://coix5dqblmu7r"]
[ext_resource type="Script" uid="uid://otm88638j7f8" path="res://scripts/player.gd" id="1_g2els"] [ext_resource type="Script" uid="uid://bqxrdf7mtx0ev" path="res://scripts/player_refactor.gd" id="1_qlg0r"]
[ext_resource type="PackedScene" uid="uid://ddpclu2vdy2ve" path="res://scenes/stock_weapon.tscn" id="2_dqkch"]
[ext_resource type="Texture2D" uid="uid://cq4we1m1yv22s" path="res://graphics/ship.png" id="2_qhqgy"] [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://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"] [ext_resource type="Texture2D" uid="uid://b0iavxi8vaxtj" path="res://graphics/ship_thrusters.png" id="5_qlg0r"]
@ -86,9 +85,7 @@ size = Vector2(24, 30)
size = Vector2(6, 5.75) size = Vector2(6, 5.75)
[node name="Player" type="Area2D" unique_id=652131079] [node name="Player" type="Area2D" unique_id=652131079]
script = ExtResource("1_g2els") script = ExtResource("1_qlg0r")
speed = 275
weapon_current = ExtResource("2_dqkch")
[node name="Ship" type="Sprite2D" parent="." unique_id=1155866924] [node name="Ship" type="Sprite2D" parent="." unique_id=1155866924]
unique_name_in_owner = true unique_name_in_owner = true

View file

@ -1,6 +1,6 @@
[gd_scene format=3 uid="uid://6wq3ynesnsha"] [gd_scene format=3 uid="uid://6wq3ynesnsha"]
[ext_resource type="Script" uid="uid://dulfr27gg4evl" path="res://scripts/player_revision.gd" id="1_3ef36"] [ext_resource type="Script" uid="uid://dulfr27gg4evl" path="res://scripts/player_component.gd" id="1_3ef36"]
[ext_resource type="PackedScene" uid="uid://ddpclu2vdy2ve" path="res://scenes/stock_weapon.tscn" id="2_l5qtw"] [ext_resource type="PackedScene" uid="uid://ddpclu2vdy2ve" path="res://scenes/stock_weapon.tscn" id="2_l5qtw"]
[ext_resource type="Texture2D" uid="uid://cq4we1m1yv22s" path="res://graphics/ship.png" id="3_63g5k"] [ext_resource type="Texture2D" uid="uid://cq4we1m1yv22s" path="res://graphics/ship.png" id="3_63g5k"]
[ext_resource type="Script" uid="uid://dss0dbwr71y6m" path="res://scripts/input_component.gd" id="4_14jeh"] [ext_resource type="Script" uid="uid://dss0dbwr71y6m" path="res://scripts/input_component.gd" id="4_14jeh"]

View file

@ -1,24 +1,15 @@
[gd_scene format=3 uid="uid://ddpclu2vdy2ve"] [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://scripts/stock_weapon.gd" id="1_hsma2"]
[ext_resource type="Script" uid="uid://ccdohs4gduee5" path="res://scripts/player_shot.gd" id="2_mvdrj"] [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"] [ext_resource type="Texture2D" uid="uid://ti1uy42vnnhw" path="res://graphics/shot.png" id="3_mvdrj"]
[sub_resource type="Resource" id="Resource_pax02"]
script = ExtResource("2_mvdrj")
shot_name = "Stock"
damage = 3
speed = -350
projectiles = 1
cooldown = 0.0
metadata/_custom_type_script = "uid://ccdohs4gduee5"
[sub_resource type="RectangleShape2D" id="RectangleShape2D_mvdrj"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_mvdrj"]
size = Vector2(6, 14) size = Vector2(6, 14)
[node name="StockWeapon" type="Area2D" unique_id=1832200900] [node name="StockWeapon" type="Area2D" unique_id=1832200900]
script = ExtResource("1_hsma2") script = ExtResource("1_hsma2")
shot_data = SubResource("Resource_pax02") shot_data = ExtResource("2_mvdrj")
[node name="Sprite2D" type="Sprite2D" parent="." unique_id=2134507225] [node name="Sprite2D" type="Sprite2D" parent="." unique_id=2134507225]
texture = ExtResource("3_mvdrj") texture = ExtResource("3_mvdrj")

View file

@ -1,15 +1,24 @@
[gd_scene format=3 uid="uid://b2ltmeb14nc17"] [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://scripts/stock_weapon.gd" id="1_kx6bj"]
[ext_resource type="Resource" uid="uid://cels8t3hqjtsu" path="res://resources/player_weapons/shot_stock.tres" id="2_hefae"] [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"] [ext_resource type="Texture2D" uid="uid://ti1uy42vnnhw" path="res://graphics/shot.png" id="3_58qml"]
[sub_resource type="Resource" id="Resource_4ihvg"]
script = ExtResource("2_5286c")
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"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_mvdrj"]
size = Vector2(12, 8) size = Vector2(12, 8)
[node name="StockWeapon" type="Area2D" unique_id=1832200900] [node name="StockWeapon" type="Area2D" unique_id=1832200900]
script = ExtResource("1_kx6bj") script = ExtResource("1_kx6bj")
shot_data = ExtResource("2_hefae") shot_data = SubResource("Resource_4ihvg")
[node name="Sprite2D" type="Sprite2D" parent="." unique_id=2134507225] [node name="Sprite2D" type="Sprite2D" parent="." unique_id=2134507225]
texture = ExtResource("3_58qml") texture = ExtResource("3_58qml")

View file

@ -16,7 +16,7 @@ func tick(delta: float):
# Check to see if there's a player to move # Check to see if there's a player to move
if player == null: return if player == null: return
# Calculate ship to bullet displacemnt # Calculate ship to bullet displacement
player.ship_displacement = player.position.y - player.previous_position.y player.ship_displacement = player.position.y - player.previous_position.y
player.travel += abs(weapon_component.bullet_data.speed) * delta + (player.ship_displacement) player.travel += abs(weapon_component.bullet_data.speed) * delta + (player.ship_displacement)
player.travel = clamp(player.travel, 0, 1.9 * weapon_component.bullet_data.projectile_spacing) player.travel = clamp(player.travel, 0, 1.9 * weapon_component.bullet_data.projectile_spacing)

View file

@ -28,7 +28,7 @@ var weapon_spacing: float = 0.1
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
travel += bullet_speed * delta + (ship_displacement) travel += bullet_speed * delta + abs(ship_displacement)
travel = clamp(travel, 0, 1.9 * weapon_spacing) travel = clamp(travel, 0, 1.9 * weapon_spacing)
# Sprite feedback based one player input # Sprite feedback based one player input
@ -100,7 +100,7 @@ func shoot():
# 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
weapon_spacing = bullet.shot_data.projectile_spacing weapon_spacing = bullet.shot_data.projectile_spacing
bullet_speed = abs(bullet.shot_data.speed) bullet_speed = bullet.shot_data.speed
# Print which weapon is firing # Print which weapon is firing
prints(bullet.shot_data.shot_name, "weapon fired") prints(bullet.shot_data.shot_name, "weapon fired")

View file

@ -22,12 +22,10 @@ func _process(delta) -> void:
movement_component.input = input_component.move_dir movement_component.input = input_component.move_dir
movement_component.tick(delta) movement_component.tick(delta)
# Set up shooting # Read shooting
if input_component.shooting: if input_component.shooting:
is_shooting = true is_shooting = true
if travel > weapon_component.bullet_data.projectile_spacing:
shoot_component.shoot()
travel -= weapon_component.bullet_data.projectile_spacing
else: is_shooting = false else: is_shooting = false
if is_shooting == true:
shoot_component.shoot()

112
scripts/player_refactor.gd Normal file
View file

@ -0,0 +1,112 @@
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
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")
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
var bullet_resource: PlayerShot = load("res://resources/player_weapons/shot_stock.tres")
for b in range(bullet_resource.projectiles):
# Instantiate the bullets
var bullet = weapon_current.instantiate()
get_tree().root.add_child(bullet)
bullet.position = position + Vector2((b - (bullet.shot_data.projectiles - 1) / 2.0) * bullet.shot_data.projectile_spacing / 2.0, bullet.shot_data.projectile_origin)
# Get weapon speed and spacing for equidistant calculations
#weapon_rate = bullet.shot_data.rate
weapon_spacing = bullet.shot_data.projectile_spacing
bullet_speed = abs(bullet.shot_data.speed)
# Print which weapon is firing
prints(bullet.shot_data.shot_name, "weapon fired")

View file

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

View file

@ -9,7 +9,7 @@ extends Resource
@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 cooldown: float = 0.25
@export var projectile_spacing: float = 35 @export var projectile_spacing: float = 35
@export var projectile_origin: int = -23 @export var projectile_origin: int = -23
@export var rate: float = 0.1
@export var cooldown: float = 0.25

View file

@ -8,27 +8,26 @@ var weapon_spacing: float = 0.1
func shoot(): func shoot():
if player.travel > weapon_component.bullet_data.projectile_spacing:
# Continuous fire print("Travel is greater!")
if player.is_shooting == true:
# Adjust bullet spacing before firing
for b in range(weapon_component.bullet_data.projectiles): for b in range(weapon_component.bullet_data.projectiles):
# Instantiate the bullets # Instantiate the bullets
var bullet = weapon_component.weapon_current.instantiate() var bullet = weapon_component.weapon_current.instantiate()
get_tree().root.add_child(bullet) get_tree().root.add_child(bullet)
# Adjust bullet spacing before firing
bullet.position = player.position + Vector2((b - (weapon_component.bullet_data.projectiles - 1) / 2.0) * weapon_component.bullet_data.projectile_spacing, weapon_component.bullet_data.projectile_origin) bullet.position = player.position + Vector2((b - (weapon_component.bullet_data.projectiles - 1) / 2.0) * weapon_component.bullet_data.projectile_spacing, weapon_component.bullet_data.projectile_origin)
## Get weapon speed and spacing for equidistant calculations ## Get weapon speed and spacing for equidistant calculations
#weapon_rate = bullet.weapon_component.bullet_data.rate #weapon_rate = bullet.weapon_component.bullet_data.rate
#weapon_spacing = bullet.weapon_component.bullet_data.projectile_spacing #weapon_component.bullet_data.projectile_spacing = bullet.weapon_component.bullet_data.projectile_spacing
#bullet.weapon_component.bullet_data.speed = bullet.weapon_component.bullet_data.speed #weapon_component.bullet_data.speed = bullet.weapon_component.bullet_data.speed
# Print which weapon is firing # Print which weapon is firing
prints(bullet.shot_data.shot_name, "weapon fired") prints(bullet.shot_data.shot_name, "weapon fired")
player.travel -= weapon_component.bullet_data.projectile_spacing
else: return

View file

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

View file

@ -1,8 +1,8 @@
class_name WeaponComponent extends Node class_name WeaponComponent extends Node
@export var weapon_current : PackedScene @export var weapon_current : PackedScene
var bullet_data: PlayerShot = load("res://resources/player_weapons/shot_stock.tres")
#@export var projectile_origin: int = -23 #@export var projectile_origin: int = -23
var bullet_data: PlayerShot = load("res://resources/player_weapons/shot_stock_static.tres")
#@export_category("Temp Bullet Data") #@export_category("Temp Bullet Data")
#@export var projectiles: int = 3 #@export var projectiles: int = 3