diff --git a/resources/player_weapon_resources/weapon_shot_stock.tres b/resources/player_weapon_resources/weapon_shot_stock.tres index 3d3bd7b..9b73ebe 100644 --- a/resources/player_weapon_resources/weapon_shot_stock.tres +++ b/resources/player_weapon_resources/weapon_shot_stock.tres @@ -13,5 +13,5 @@ spacing = 20.0 origin = -25 horizontal_offset = 6.0 stagger_offset = 5.5 -angle = 2.0 +rotate_to_velocity = false metadata/_custom_type_script = "uid://7n1itonn35fm" diff --git a/resources/weapon_shot.gd b/resources/weapon_shot.gd index ece62bb..2f2d0f4 100644 --- a/resources/weapon_shot.gd +++ b/resources/weapon_shot.gd @@ -17,3 +17,4 @@ class_name WeaponShot extends Resource # Angle in degrees, default is 0 @export var angle: float = 0.0 # Angle at which the bullet should be fired +@export var rotate_to_velocity: bool = true diff --git a/scenes/player_weapons/weapon_stock.gd b/scenes/player_weapons/weapon_stock.gd index 78a7762..ae9638b 100644 --- a/scenes/player_weapons/weapon_stock.gd +++ b/scenes/player_weapons/weapon_stock.gd @@ -1,8 +1,6 @@ extends Area2D -@onready var bullet: PackedScene = load("res://scenes/player_weapons/weapon_stock.tscn") -@onready var shot_data: = load("res://resources/player_weapon_resources/weapon_shot_stock.tres") - +@onready var shot_data = load("res://resources/player_weapon_resources/weapon_shot_stock.tres") # Time offset when this bullet was fired (relative to other bullets in the same shot) var time_offset: float = 0.5 @@ -16,6 +14,9 @@ var is_active: bool = false # Angle at which this bullet should fire (in degrees) var angle: float = 0.0 +# Whether to rotate bullet to face its movement direction +var rotate_to_velocity: bool = true + func _process(delta): var current_time = Time.get_ticks_msec() / 1000.0 @@ -32,13 +33,19 @@ func _process(delta): position.x += move_x position.y += move_y + # Rotate to face movement direction - ensure proper rotation calculation + if rotate_to_velocity: + var velocity = Vector2(cos(angle_rad) * shot_data.speed, sin(angle_rad) * shot_data.speed) + rotation = velocity.angle() + + # Debug output - this will show you what's really happening + print("Bullet angle: ", angle, "Velocity angle:", velocity.angle(), "Rotation set to:", rotation) + + func activate(): is_active = true visible = true - # Emit signal for hit detection (optional) - # emit_signal("activate") - func _on_visible_on_screen_notifier_2d_screen_exited(): queue_free() diff --git a/scenes/player_weapons/weapon_stock_fixed.gd b/scenes/player_weapons/weapon_stock_fixed.gd new file mode 100644 index 0000000..529b05c --- /dev/null +++ b/scenes/player_weapons/weapon_stock_fixed.gd @@ -0,0 +1,54 @@ +extends Area2D + +@onready var shot_data = load("res://resources/player_weapon_resources/weapon_shot_stock.tres") + +# Time offset when this bullet was fired (relative to other bullets in the same shot) +var time_offset: float = 0.5 + +# When this bullet should fire (absolute time) +var fire_time: float = 0.0 + +# Track if this bullet is currently active in the shot +var is_active: bool = false + +# Angle at which this bullet should fire (in degrees) +var angle: float = 0.0 + +# Whether to rotate bullet to face its movement direction +var rotate_to_velocity: bool = true + +func _process(delta): + var current_time = Time.get_ticks_msec() / 1000.0 + + # Fire if it's time and bullet isn't already active + if !is_active and current_time >= fire_time: + activate() + + # Calculate the movement vector based on speed, angle, and delta + var angle_rad = deg_to_rad(angle) + var move_x = cos(angle_rad) * shot_data.speed * delta + var move_y = sin(angle_rad) * shot_data.speed * delta + + # Move the bullet + position.x += move_x + position.y += move_y + + # Rotate to face movement direction - ensure proper rotation calculation + if rotate_to_velocity: + var velocity = Vector2(cos(angle_rad) * shot_data.speed, sin(angle_rad) * shot_data.speed) + rotation = velocity.angle() + + # Debug output - this will show you what's really happening + print("Bullet angle: ", angle, "Velocity angle:", velocity.angle(), "Rotation set to:", rotation) + + +func activate(): + is_active = true + visible = true + +func _on_visible_on_screen_notifier_2d_screen_exited(): + queue_free() + + # Set fire_time for the next shot (staggered firing) + var current_time = Time.get_ticks_msec() / 1000.0 + fire_time = current_time + shot_data.stagger_offset diff --git a/scenes/player_weapons/weapon_stock_fixed.gd.uid b/scenes/player_weapons/weapon_stock_fixed.gd.uid new file mode 100644 index 0000000..1afd8cf --- /dev/null +++ b/scenes/player_weapons/weapon_stock_fixed.gd.uid @@ -0,0 +1 @@ +uid://7wuopfptd3qe diff --git a/scenes/player_weapons/weapon_stock_fixed2.gd b/scenes/player_weapons/weapon_stock_fixed2.gd new file mode 100644 index 0000000..529b05c --- /dev/null +++ b/scenes/player_weapons/weapon_stock_fixed2.gd @@ -0,0 +1,54 @@ +extends Area2D + +@onready var shot_data = load("res://resources/player_weapon_resources/weapon_shot_stock.tres") + +# Time offset when this bullet was fired (relative to other bullets in the same shot) +var time_offset: float = 0.5 + +# When this bullet should fire (absolute time) +var fire_time: float = 0.0 + +# Track if this bullet is currently active in the shot +var is_active: bool = false + +# Angle at which this bullet should fire (in degrees) +var angle: float = 0.0 + +# Whether to rotate bullet to face its movement direction +var rotate_to_velocity: bool = true + +func _process(delta): + var current_time = Time.get_ticks_msec() / 1000.0 + + # Fire if it's time and bullet isn't already active + if !is_active and current_time >= fire_time: + activate() + + # Calculate the movement vector based on speed, angle, and delta + var angle_rad = deg_to_rad(angle) + var move_x = cos(angle_rad) * shot_data.speed * delta + var move_y = sin(angle_rad) * shot_data.speed * delta + + # Move the bullet + position.x += move_x + position.y += move_y + + # Rotate to face movement direction - ensure proper rotation calculation + if rotate_to_velocity: + var velocity = Vector2(cos(angle_rad) * shot_data.speed, sin(angle_rad) * shot_data.speed) + rotation = velocity.angle() + + # Debug output - this will show you what's really happening + print("Bullet angle: ", angle, "Velocity angle:", velocity.angle(), "Rotation set to:", rotation) + + +func activate(): + is_active = true + visible = true + +func _on_visible_on_screen_notifier_2d_screen_exited(): + queue_free() + + # Set fire_time for the next shot (staggered firing) + var current_time = Time.get_ticks_msec() / 1000.0 + fire_time = current_time + shot_data.stagger_offset diff --git a/scenes/player_weapons/weapon_stock_fixed2.gd.uid b/scenes/player_weapons/weapon_stock_fixed2.gd.uid new file mode 100644 index 0000000..3bc96d0 --- /dev/null +++ b/scenes/player_weapons/weapon_stock_fixed2.gd.uid @@ -0,0 +1 @@ +uid://c1u6yrjkp26i5 diff --git a/scripts/shoot_component.gd b/scripts/shoot_component.gd index 9881d14..f9d7315 100644 --- a/scripts/shoot_component.gd +++ b/scripts/shoot_component.gd @@ -38,12 +38,14 @@ func shoot(): # Final position combines symmetrical horizontal spread with fixed vertical offset from weapon origin bullet.position = player.position + Vector2(bullet_horizontal_offset, weapon_data.origin) - - # Set timing properties on the bullet bullet.time_offset = time_offset bullet.fire_time = current_time + time_offset + # Set bullet rotation properties + bullet.rotate_to_velocity = weapon_data.rotate_to_velocity # This should be true if you want rotation + + # Set the angle for bullets - symmetrical spread around vertical (straight up = -90°) if weapon_data.angle == 0: # If angle is 0, all projectiles fire straight up diff --git a/test_rotation.gd b/test_rotation.gd new file mode 100644 index 0000000..6c43919 --- /dev/null +++ b/test_rotation.gd @@ -0,0 +1,25 @@ +extends Area2D + +# Simple test to verify rotation behavior +var test_angle: float = 0.0 +var rotate_to_velocity: bool = true + +func _process(delta): + # Move in a straight line at 100 pixels per second + var move_x = cos(deg_to_rad(test_angle)) * 100 * delta + var move_y = sin(deg_to_rad(test_angle)) * 100 * delta + + position.x += move_x + position.y += move_y + + # Rotate to face movement direction (this is the key fix) + if rotate_to_velocity: + var velocity = Vector2(cos(deg_to_rad(test_angle)) * 100, sin(deg_to_rad(test_angle)) * 100) + rotation = velocity.angle() + + # Debug output + print("Test angle: ", test_angle, "Velocity angle:", velocity.angle(), "Rotation set to:", rotation) + +func _ready(): + # Set initial rotation for testing + test_angle = 0.0 diff --git a/test_rotation.gd.uid b/test_rotation.gd.uid new file mode 100644 index 0000000..e08c04f --- /dev/null +++ b/test_rotation.gd.uid @@ -0,0 +1 @@ +uid://b1bhb673ef5po diff --git a/test_rotation.tscn b/test_rotation.tscn new file mode 100644 index 0000000..452b89d --- /dev/null +++ b/test_rotation.tscn @@ -0,0 +1,6 @@ +[gd_scene format=3 uid="uid://d4ls4srw6qyap"] + +[ext_resource type="Script" uid="uid://d1rwqotmrag1r" path="res://test_rotation.gd" id="1_kx6bj"] + +[node name="TestRotation" type="Area2D" unique_id=1832200900] +script = ExtResource("1_kx6bj")