From 95837b75feebd8a5c380e091b5ba726c2e1b2647 Mon Sep 17 00:00:00 2001 From: MaddoScientisto Date: Sun, 23 Mar 2025 16:39:47 +0100 Subject: [PATCH] Enemies drop items with speed --- Scenes/Items/PhysicsDropWrapper.tscn | 15 +++++++ Scenes/Maps/Level2.tscn | 9 ++-- Scenes/Maps/PlayerFSMTest.tscn | 13 ++++-- .../FSM/Enemy/EnemyDropsProvider.cs | 42 ++++++++++++------- Scripts/Interactables/ItemDrop.cs | 25 +++++++++++ Scripts/Interactables/ItemDrop.cs.uid | 1 + Scripts/Interactables/ItemPickup.cs | 2 +- Tilesets/factory_tileset.tres | 2 +- 8 files changed, 84 insertions(+), 25 deletions(-) create mode 100644 Scenes/Items/PhysicsDropWrapper.tscn create mode 100644 Scripts/Interactables/ItemDrop.cs create mode 100644 Scripts/Interactables/ItemDrop.cs.uid diff --git a/Scenes/Items/PhysicsDropWrapper.tscn b/Scenes/Items/PhysicsDropWrapper.tscn new file mode 100644 index 00000000..2bb81ad0 --- /dev/null +++ b/Scenes/Items/PhysicsDropWrapper.tscn @@ -0,0 +1,15 @@ +[gd_scene load_steps=3 format=3 uid="uid://d3hds3dbosfcm"] + +[ext_resource type="Script" uid="uid://q806qngbukt8" path="res://Scripts/Interactables/ItemDrop.cs" id="1_lk7fn"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_k5j4v"] +radius = 4.47214 + +[node name="PhysicsDropWrapper" type="RigidBody2D"] +gravity_scale = 0.0 +lock_rotation = true +linear_damp = 2.5 +script = ExtResource("1_lk7fn") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("CircleShape2D_k5j4v") diff --git a/Scenes/Maps/Level2.tscn b/Scenes/Maps/Level2.tscn index 49b188c4..b59f7808 100644 --- a/Scenes/Maps/Level2.tscn +++ b/Scenes/Maps/Level2.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=82 format=4 uid="uid://6a6tjohypmmb"] +[gd_scene load_steps=81 format=4 uid="uid://6a6tjohypmmb"] [ext_resource type="Script" uid="uid://doxmbokehw8ci" path="res://Scripts/GameManager.cs" id="1_t2k72"] [ext_resource type="PackedScene" uid="uid://c4pr2707hbeph" path="res://Scenes/Actors/fsm_player.tscn" id="2_2jsgm"] @@ -16,7 +16,6 @@ [ext_resource type="PackedScene" uid="uid://dkwi1hu1bixoe" path="res://Scenes/HUD/HUD.tscn" id="9_mnq21"] [ext_resource type="Script" uid="uid://bdshph801ac2i" path="res://Scenes/CameraTarget.gd" id="10_4nd4a"] [ext_resource type="Script" uid="uid://cnkipcolyj61w" path="res://Scripts/AlarmManager.cs" id="11_7an4p"] -[ext_resource type="Script" uid="uid://b5fesrd4lv8t1" path="res://Scripts/Resources/Events/EventResource.cs" id="14_5j8ks"] [ext_resource type="Script" uid="uid://bwox5lmgiijcs" path="res://Scripts/Resources/Events/ActivateEvent.cs" id="14_s23f3"] [ext_resource type="PackedScene" uid="uid://oi8svqsspcsj" path="res://Scenes/Interactable/ScriptableMovementSensor.tscn" id="16_ehuec"] [ext_resource type="PackedScene" uid="uid://crph24e6e0v0q" path="res://Scenes/Interactable/Control_Pad.tscn" id="16_s23f3"] @@ -357,7 +356,7 @@ position = Vector2(196, 296) [node name="MotionSensor" parent="Tilemaps/Actors" instance=ExtResource("16_ehuec")] position = Vector2(254, 296) -Events = Array[ExtResource("14_5j8ks")]([SubResource("Resource_s23f3")]) +Events = [SubResource("Resource_s23f3")] [node name="ControlPad" parent="Tilemaps/Actors" node_paths=PackedStringArray("Targets") instance=ExtResource("16_s23f3")] position = Vector2(205, 292) @@ -378,11 +377,11 @@ position = Vector2(504, 468) [node name="MovementSensorLonger" parent="Tilemaps/Actors" instance=ExtResource("20_fdi4d")] position = Vector2(226, 486) -Events = Array[ExtResource("14_5j8ks")]([SubResource("Resource_qi6uc")]) +Events = [SubResource("Resource_qi6uc")] [node name="MovementSensorLonger2" parent="Tilemaps/Actors" instance=ExtResource("20_fdi4d")] position = Vector2(391, 455) -Events = Array[ExtResource("14_5j8ks")]([SubResource("Resource_mq37i")]) +Events = [SubResource("Resource_mq37i")] [node name="WallEmitter" parent="Tilemaps/Actors" instance=ExtResource("21_qi6uc")] position = Vector2(264, 482) diff --git a/Scenes/Maps/PlayerFSMTest.tscn b/Scenes/Maps/PlayerFSMTest.tscn index 0b71b34c..5eea371b 100644 --- a/Scenes/Maps/PlayerFSMTest.tscn +++ b/Scenes/Maps/PlayerFSMTest.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=105 format=4 uid="uid://dqyfnby0t7gu1"] +[gd_scene load_steps=106 format=4 uid="uid://dqyfnby0t7gu1"] [ext_resource type="Script" uid="uid://doxmbokehw8ci" path="res://Scripts/GameManager.cs" id="1_c3v4x"] [ext_resource type="Resource" uid="uid://cs3ihltcn2166" path="res://Resources/Items/IcicleGun.tres" id="3_6314l"] @@ -69,6 +69,7 @@ [ext_resource type="Texture2D" uid="uid://cnayqn8jgb8jj" path="res://Models/carr/carr_rotating.png" id="56_5glx6"] [ext_resource type="PackedScene" uid="uid://iooa4gqy2uu2" path="res://Scenes/Items/Nuclear_Gun_Pickup.tscn" id="56_iv5wi"] [ext_resource type="PackedScene" uid="uid://ey71mxa5ocpn" path="res://Scenes/Items/Shield_Extend_Pickup.tscn" id="60_bjd5y"] +[ext_resource type="PackedScene" uid="uid://d3hds3dbosfcm" path="res://Scenes/Items/PhysicsDropWrapper.tscn" id="62_5glx6"] [ext_resource type="PackedScene" uid="uid://d360rx247qywn" path="res://3D/3DTest.tscn" id="68_8n581"] [sub_resource type="Resource" id="Resource_6wo78"] @@ -160,7 +161,7 @@ TimeModifiers = Array[ExtResource("43_8n581")]([]) WaitForCompletion = true metadata/_custom_type_script = "uid://c0ndqalsc4jve" -[sub_resource type="Resource" id="Resource_bjd5y"] +[sub_resource type="Resource" id="Resource_5glx6"] resource_local_to_scene = true script = ExtResource("39_83jc5") Patterns = Array[Object]([SubResource("Resource_kuo18")]) @@ -553,7 +554,7 @@ InvertSignal = true [node name="FloorEmitter2" parent="Tilemaps/Actors" instance=ExtResource("44_b8ffn")] position = Vector2(601, 147) -Script = SubResource("Resource_bjd5y") +Script = SubResource("Resource_5glx6") InvertSignal = true [node name="Camera" parent="Tilemaps/Actors" instance=ExtResource("48_r562v")] @@ -632,6 +633,12 @@ position = Vector2(616, 88) [node name="ShieldExtendPickup" parent="Tilemaps" instance=ExtResource("60_bjd5y")] position = Vector2(664, 38) +[node name="PhysicsDropWrapper" parent="Tilemaps" instance=ExtResource("62_5glx6")] +position = Vector2(616, 18) +collision_layer = 4 +collision_mask = 33 +ItemToDrop = ExtResource("23_1ajuh") + [node name="CameraController" type="Camera2D" parent="."] process_mode = 1 script = ExtResource("9_dj0ui") diff --git a/Scripts/Components/FSM/Enemy/EnemyDropsProvider.cs b/Scripts/Components/FSM/Enemy/EnemyDropsProvider.cs index 6c22b29c..ec026b83 100644 --- a/Scripts/Components/FSM/Enemy/EnemyDropsProvider.cs +++ b/Scripts/Components/FSM/Enemy/EnemyDropsProvider.cs @@ -1,4 +1,5 @@ -using Cirno.Scripts.Resources; +using Cirno.Scripts.Interactables; +using Cirno.Scripts.Resources; using Godot; namespace Cirno.Scripts.Components.FSM.Enemy; @@ -9,8 +10,12 @@ public partial class EnemyDropsProvider : Node2D [Export] public float DropRadius { get; private set; } = 8f; + [Export] public float DropSpeed { get; set; } = 40f; + [Export] - public EnemyStorageModule StorageModule { get; private set; } + public EnemyStorageModule StorageModule { get; private set; } + + private readonly StringName _dropPhysicsWrapperScene = "uid://d3hds3dbosfcm"; public void DropLoot() { @@ -29,23 +34,30 @@ public partial class EnemyDropsProvider : Node2D private void DropItem(LootItem item) { + + if (!string.IsNullOrWhiteSpace(item.DropScenePath)) { GD.Print($"Dropped item: {item.ItemName}"); - var scene = GD.Load(item.DropScenePath); - var droppedItem = StorageModule.Root.CreateSibling(scene); + var dropScene = GD.Load(_dropPhysicsWrapperScene); + var dropInstance = StorageModule.Root.CreateSibling(dropScene); + dropInstance.ItemToDrop = item; + dropInstance.StartingSpeed = DropSpeed; - // Generate random point within DropRadius - float angle = _rng.RandfRange(0, Mathf.Tau); // Random angle (0 to 2π) - float radius = _rng.RandfRange(0, DropRadius); // Random radius within range - - // Convert polar to cartesian coordinates - float xOffset = radius * Mathf.Cos(angle); - float yOffset = radius * Mathf.Sin(angle); - - Vector2 spawnPosition = StorageModule.Root.GlobalPosition + new Vector2(xOffset, yOffset); - - droppedItem.GlobalPosition = spawnPosition; + // var scene = GD.Load(item.DropScenePath); + // var droppedItem = StorageModule.Root.CreateSibling(scene); + // + // // Generate random point within DropRadius + // float angle = _rng.RandfRange(0, Mathf.Tau); // Random angle (0 to 2π) + // float radius = _rng.RandfRange(0, DropRadius); // Random radius within range + // + // // Convert polar to cartesian coordinates + // float xOffset = radius * Mathf.Cos(angle); + // float yOffset = radius * Mathf.Sin(angle); + // + // Vector2 spawnPosition = StorageModule.Root.GlobalPosition + new Vector2(xOffset, yOffset); + // + // droppedItem.GlobalPosition = spawnPosition; } else { diff --git a/Scripts/Interactables/ItemDrop.cs b/Scripts/Interactables/ItemDrop.cs new file mode 100644 index 00000000..0ea8656e --- /dev/null +++ b/Scripts/Interactables/ItemDrop.cs @@ -0,0 +1,25 @@ +using Cirno.Scripts.Resources; +using Godot; + +namespace Cirno.Scripts.Interactables; + +public partial class ItemDrop : RigidBody2D +{ + [Export] public LootItem ItemToDrop { get; set; } + + [Export] public float StartingSpeed { get; set; } = 40f; + + private RandomNumberGenerator _rng = new(); + public override void _Ready() + { + PackedScene dropScene = GD.Load(ItemToDrop.DropScenePath); + Node dropInstance = dropScene.Instantiate(); + AddChild(dropInstance); + + float angle = _rng.RandfRange(0, Mathf.Tau); // 0 to 2π + Vector2 direction = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)).Normalized(); + + // Apply impulse in that direction + ApplyImpulse(direction * StartingSpeed); + } +} \ No newline at end of file diff --git a/Scripts/Interactables/ItemDrop.cs.uid b/Scripts/Interactables/ItemDrop.cs.uid new file mode 100644 index 00000000..3e9845bc --- /dev/null +++ b/Scripts/Interactables/ItemDrop.cs.uid @@ -0,0 +1 @@ +uid://q806qngbukt8 diff --git a/Scripts/Interactables/ItemPickup.cs b/Scripts/Interactables/ItemPickup.cs index 3bcc5667..3ed9e082 100644 --- a/Scripts/Interactables/ItemPickup.cs +++ b/Scripts/Interactables/ItemPickup.cs @@ -6,7 +6,7 @@ namespace Cirno.Scripts.Interactables; public partial class ItemPickup : Interactable { - [Export] public Array LootTable = new Array(); + [Export] public Array LootTable = []; public override bool Activate(ActivationType activationType = ActivationType.Toggle) { diff --git a/Tilesets/factory_tileset.tres b/Tilesets/factory_tileset.tres index 538db070..8afd8453 100644 --- a/Tilesets/factory_tileset.tres +++ b/Tilesets/factory_tileset.tres @@ -1292,7 +1292,7 @@ texture = ExtResource("21_u4jco") 0:0/animation_frame_3/duration = 1.0 0:0/0 = 0 0:0/0/physics_layer_2/linear_velocity = Vector2(40, 0) -0:0/0/physics_layer_2/polygon_0/points = PackedVector2Array(-6, -6, 6, -6, 6, 6, -6, 6) +0:0/0/physics_layer_2/polygon_0/points = PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8) 0:0/0/custom_data_0 = Vector2(20, 0) 0:1/animation_columns = 4 0:1/animation_frame_0/duration = 1.0