From e9a9653c20c3c01515bb154d92a42fc15c91dce6 Mon Sep 17 00:00:00 2001 From: Marco Date: Fri, 14 Feb 2025 19:35:13 +0100 Subject: [PATCH] Bullet emitter --- Scenes/Activable/BulletEmitter.tscn | 23 ++++++++++++ Scenes/Activable/DialogueStarter.tscn | 4 +-- Scenes/test.tscn | 14 ++++---- Scripts/Activables/BulletEmitter.cs | 50 +++++++++++++++++++++++++++ Scripts/Components/BulletSpawner.cs | 2 +- Scripts/Door.cs | 2 -- 6 files changed, 84 insertions(+), 11 deletions(-) create mode 100644 Scenes/Activable/BulletEmitter.tscn create mode 100644 Scripts/Activables/BulletEmitter.cs diff --git a/Scenes/Activable/BulletEmitter.tscn b/Scenes/Activable/BulletEmitter.tscn new file mode 100644 index 00000000..4e14a2fb --- /dev/null +++ b/Scenes/Activable/BulletEmitter.tscn @@ -0,0 +1,23 @@ +[gd_scene load_steps=6 format=3 uid="uid://cd36ch65jijg0"] + +[ext_resource type="Script" path="res://Scripts/Activables/BulletEmitter.cs" id="1_11khd"] +[ext_resource type="Script" path="res://Scripts/Components/BulletSpawner.cs" id="2_cr25o"] +[ext_resource type="PackedScene" uid="uid://bi3f14klscvlw" path="res://Scenes/Weapons/Bullets/enemyBullet_mid_red.tscn" id="2_tvufl"] +[ext_resource type="Script" path="res://Scripts/Resources/BulletResource.cs" id="3_njoox"] + +[sub_resource type="Resource" id="Resource_pcsuf"] +script = ExtResource("3_njoox") +BulletScene = ExtResource("2_tvufl") +BulletSpeed = 20.0 +BulletDamage = 4.0 +LifeTime = 4.0 +Owner = 0 +DamageType = 2 +TimeModifiers = Array[Object]([]) + +[node name="BulletEmitter" type="Node2D"] +script = ExtResource("1_11khd") +BulletResource = SubResource("Resource_pcsuf") + +[node name="BulletSpawner" type="Node2D" parent="."] +script = ExtResource("2_cr25o") diff --git a/Scenes/Activable/DialogueStarter.tscn b/Scenes/Activable/DialogueStarter.tscn index 593b2c99..a397fc01 100644 --- a/Scenes/Activable/DialogueStarter.tscn +++ b/Scenes/Activable/DialogueStarter.tscn @@ -4,12 +4,12 @@ [sub_resource type="CircleShape2D" id="CircleShape2D_glhl1"] -[node name="DialogueStarter" type="Area2D" node_paths=PackedStringArray("_dialogueEndActivationTargets")] +[node name="DialogueStarter" type="Area2D" node_paths=PackedStringArray("_targets")] monitoring = false monitorable = false script = ExtResource("1_nlikr") _trackName = "" -_dialogueEndActivationTargets = [] +_targets = [] [node name="CollisionShape2D" type="CollisionShape2D" parent="."] shape = SubResource("CircleShape2D_glhl1") diff --git a/Scenes/test.tscn b/Scenes/test.tscn index dfcfdeeb..fde001a1 100644 --- a/Scenes/test.tscn +++ b/Scenes/test.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=56 format=4 uid="uid://bv451a8wgty4u"] +[gd_scene load_steps=57 format=4 uid="uid://bv451a8wgty4u"] [ext_resource type="Script" path="res://Scripts/GameManager.cs" id="1_8tmoj"] [ext_resource type="PackedScene" uid="uid://bghghp5ep4w2j" path="res://Scenes/player.tscn" id="2_8mh54"] @@ -48,6 +48,7 @@ [ext_resource type="Script" path="res://Scripts/Resources/Events/DialogueStartEvent.cs" id="46_i0omr"] [ext_resource type="PackedScene" uid="uid://cdti0hnbs3e63" path="res://Scenes/Actors/RoamingSusan.tscn" id="47_u1ve6"] [ext_resource type="PackedScene" uid="uid://b0pb078xylxy" path="res://Scenes/Interactable/Valve.tscn" id="48_8usll"] +[ext_resource type="PackedScene" uid="uid://cd36ch65jijg0" path="res://Scenes/Activable/BulletEmitter.tscn" id="49_64oga"] [sub_resource type="CircleShape2D" id="CircleShape2D_8wuck"] @@ -383,18 +384,16 @@ DefeatScript = NodePath("../BossBattleEndScript") position = Vector2(-794, -127) Target = NodePath("DialogueStarter") -[node name="DialogueStarter" parent="Computer" node_paths=PackedStringArray("_targets") instance=ExtResource("32_68v02")] +[node name="DialogueStarter" parent="Computer" instance=ExtResource("32_68v02")] _trackName = "computer1" -_targets = [] [node name="Computer2" parent="." node_paths=PackedStringArray("Target") instance=ExtResource("31_243ec")] position = Vector2(-1464, -276) Target = NodePath("DialogueStarter") -[node name="DialogueStarter" parent="Computer2" node_paths=PackedStringArray("_targets") instance=ExtResource("32_68v02")] +[node name="DialogueStarter" parent="Computer2" instance=ExtResource("32_68v02")] position = Vector2(291.847, -32.7126) _trackName = "computer2" -_targets = [] [node name="Ammo1" parent="." instance=ExtResource("34_17pjh")] position = Vector2(-790, -381) @@ -440,4 +439,7 @@ position = Vector2(-707, 53) [node name="Valve" parent="." node_paths=PackedStringArray("Target") instance=ExtResource("48_8usll")] position = Vector2(-697, 25) -Target = NodePath("") +Target = NodePath("../BulletEmitter") + +[node name="BulletEmitter" parent="." instance=ExtResource("49_64oga")] +position = Vector2(-728, 57) diff --git a/Scripts/Activables/BulletEmitter.cs b/Scripts/Activables/BulletEmitter.cs new file mode 100644 index 00000000..ad94ae4f --- /dev/null +++ b/Scripts/Activables/BulletEmitter.cs @@ -0,0 +1,50 @@ +using System; +using Cirno.Scripts.Components; +using Cirno.Scripts.Resources; +using Godot; + +namespace Cirno.Scripts.Activables; + +public partial class BulletEmitter : Node2D, IActivable +{ + + [Export] + public BulletResource BulletResource { get; set; } + + [Export] + private bool _isEmitting { get; set; } = false; + + [Export] public float Spread { get; set; } = 0f; + + [Export] public float Rotation { get; set; } = 0f; + + private BulletSpawner _bulletSpawner; + + public override void _Ready() + { + _bulletSpawner = GetNode("BulletSpawner"); + } + + public void Activate(ActivationType activationType = ActivationType.Toggle) + { + switch (activationType) + { + case ActivationType.Toggle: + break; + case ActivationType.Enable: + _bulletSpawner.SpawnBullet(BulletResource.MakeBullet(this.GlobalPosition, 1, Spread, Rotation)); + break; + case ActivationType.Disable: + break; + case ActivationType.Use: + _bulletSpawner.SpawnBullet(BulletResource.MakeBullet(this.GlobalPosition, 1, Spread, Rotation)); + break; + case ActivationType.Destroy: + break; + default: + throw new ArgumentOutOfRangeException(nameof(activationType), activationType, null); + } + + + } +} \ No newline at end of file diff --git a/Scripts/Components/BulletSpawner.cs b/Scripts/Components/BulletSpawner.cs index d87507f0..7aee8223 100644 --- a/Scripts/Components/BulletSpawner.cs +++ b/Scripts/Components/BulletSpawner.cs @@ -40,7 +40,7 @@ public partial class BulletSpawner : Node2D float baseAngle = Mathf.Atan2(baseDirection.Y, baseDirection.X); float offsetRadians = Mathf.DegToRad(bulletInfo.RotationOffset); - float spreadStep = Mathf.DegToRad(bulletInfo.Spread) / (bulletInfo.BulletCount - 1); // Ensure proper spread spacing + float spreadStep = Mathf.DegToRad(bulletInfo.Spread) / Mathf.Max(1,bulletInfo.BulletCount - 1); // Ensure proper spread spacing, also add 1 if 0 float angle = baseAngle + offsetRadians + (spreadStep * i); // float angle = baseAngle + Mathf.DegToRad(bulletInfo.RotationOffset + (bulletInfo.Spread / bulletInfo.BulletCount) * i); diff --git a/Scripts/Door.cs b/Scripts/Door.cs index 355db5cf..95e8427a 100644 --- a/Scripts/Door.cs +++ b/Scripts/Door.cs @@ -74,8 +74,6 @@ public partial class Door : Activable default: throw new ArgumentOutOfRangeException(nameof(activationType), activationType, null); } - - } private void SetState(DoorState state)