From 5d7fc429a679f6a336b5ad3267dd9d996cfdf788 Mon Sep 17 00:00:00 2001 From: subversion23 Date: Sun, 31 Dec 2023 14:14:19 +0100 Subject: [PATCH] selection passt --- Main.cs | 19 +++++++ PlantLife2D.csproj | 8 +++ PlantLife2D.sln | 19 +++++++ PlantWorld.cs | 158 +++++++++++++++++++++++++++++++++++++++++++++++++++++ plantWorld.tscn | 10 ++++ 5 files changed, 214 insertions(+) create mode 100644 Main.cs create mode 100644 PlantLife2D.csproj create mode 100644 PlantLife2D.sln create mode 100644 PlantWorld.cs create mode 100644 plantWorld.tscn diff --git a/Main.cs b/Main.cs new file mode 100644 index 0000000..3540d7b --- /dev/null +++ b/Main.cs @@ -0,0 +1,19 @@ +using Godot; +using System; + +public partial class Main : Node2D +{ + private PlantWorld _plantWorld; + private Camera2D _camera; + public override void _Ready() + { + _plantWorld = GetNode("PlantWorld"); + _camera = GetNode("Camera2D"); + + } + + + public override void _Process(double delta) + { + } +} diff --git a/PlantLife2D.csproj b/PlantLife2D.csproj new file mode 100644 index 0000000..d7de074 --- /dev/null +++ b/PlantLife2D.csproj @@ -0,0 +1,8 @@ + + + net6.0 + net7.0 + net8.0 + true + + \ No newline at end of file diff --git a/PlantLife2D.sln b/PlantLife2D.sln new file mode 100644 index 0000000..f508546 --- /dev/null +++ b/PlantLife2D.sln @@ -0,0 +1,19 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PlantLife2D", "PlantLife2D.csproj", "{E5DD50FC-19E9-4310-8B9A-081F7C32D1F3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + ExportDebug|Any CPU = ExportDebug|Any CPU + ExportRelease|Any CPU = ExportRelease|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E5DD50FC-19E9-4310-8B9A-081F7C32D1F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E5DD50FC-19E9-4310-8B9A-081F7C32D1F3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E5DD50FC-19E9-4310-8B9A-081F7C32D1F3}.ExportDebug|Any CPU.ActiveCfg = ExportDebug|Any CPU + {E5DD50FC-19E9-4310-8B9A-081F7C32D1F3}.ExportDebug|Any CPU.Build.0 = ExportDebug|Any CPU + {E5DD50FC-19E9-4310-8B9A-081F7C32D1F3}.ExportRelease|Any CPU.ActiveCfg = ExportRelease|Any CPU + {E5DD50FC-19E9-4310-8B9A-081F7C32D1F3}.ExportRelease|Any CPU.Build.0 = ExportRelease|Any CPU + EndGlobalSection +EndGlobal diff --git a/PlantWorld.cs b/PlantWorld.cs new file mode 100644 index 0000000..522e720 --- /dev/null +++ b/PlantWorld.cs @@ -0,0 +1,158 @@ +using Godot; +using System; +using System.Collections.Generic; +using System.Linq; + +public partial class PlantWorld : Node2D +{ + [Export] public Camera2D Camera; + + [Signal] public delegate void PlantSelectedWithArgumentEventHandler(Plant plantRef); + + public PlantCollection Plants; + // Called when the node enters the scene tree for the first time. + public override void _Ready() + { + if (Camera == null) + { + GD.PrintErr("Keine Camera!!!!"); + return; + } + + Plants = new PlantCollection(); + Init(); + } + + public void Init() + { + CreatePlant(Vector2.Zero, 1024); + } + + public void CreatePlant(Vector2 position, int initNodeCount = 512) + { + var plant = new Plant(); + plant.Position = position; + plant.Init(initNodeCount); + AddChild(plant); + Plants.AddPlant(plant); + GD.Print("Plant erstellt: ", position); + } + public override void _Input(InputEvent @event) + { + if (@event is InputEventMouseButton eventMouseButton) + { + if (eventMouseButton.ButtonIndex == MouseButton.Left && eventMouseButton.Pressed) + { + var pos = eventMouseButton.Position * GetViewportTransform(); + var pos2 = eventMouseButton.Position * Camera.GetViewportTransform(); + var pos3 = eventMouseButton.Position * GetGlobalTransform(); + + GD.Print("clicked mpos:", eventMouseButton.Position, ", ", pos, pos2, pos3 ); + if (Plants.ClickedAtPlant(pos2) is { } plant) + { + + GD.Print("HuRRA: ", plant.Segments.Count); + Plants.Selected = plant; + EmitSignal(SignalName.PlantSelectedWithArgument, plant); + } + } + + if (eventMouseButton.ButtonIndex == MouseButton.Right && eventMouseButton.Pressed) + { + var pos = eventMouseButton.Position * GetViewportTransform(); + CreatePlant(pos, 128); + } + } + } + + + + // Called every frame. 'delta' is the elapsed time since the previous frame. + public override void _Process(double delta) + { + QueueRedraw(); + } + + public override void _Draw() + { + foreach (var plant in Plants.Plants) + { + var rect = plant.ClientRectangle.Grow(20); + //rect.Position = plant.Position + rect.Position; + DrawRect(rect, Colors.Green, filled:false); + } + + if (Plants.Selected != null) + { + var plant = Plants.Selected; + var rect = plant.ClientRectangle.Grow(20) * Transform; + //rect.Position = plant.Position + rect.Position; + + DrawRect(rect.Grow(20), Colors.Yellow, filled: false, 12f); + } + } +} + + +public class PlantCollection +{ + private List _plants; + + public Plant Selected = null; + + public Plant this[int index] + { + get => _plants[index]; + } + + public List Plants + { + get { return _plants; } + } + + public PlantCollection() + { + _plants = new List(); + } + + public void AddPlant(Plant plant) + { + plant.CalulateClientRectangle(); + if (CollidesWithAny(plant.ClientRectangle)) + GD.PrintErr("Auf Pos ist bereits etwas vorhanden."); + _plants.Add(plant); + + } + public bool CollidesWithAny(Rect2 rect) + { + var intersections = _plants.Where(x => x.ClientRectangle.Intersects(rect)).ToList(); + if (intersections.Count > 0) + return true; + else + return false; + + } + public void CreateNew(Vector2 originPos, int nodes = 512) + { + Plant plantLife = new Plant(); + plantLife.Position = originPos; + plantLife.GrowDemo(nodes); + plantLife.CalulateClientRectangle(); + _plants.Add(plantLife); + } + + public Plant ClickedAtPlant(Vector2 posGlob) + { + foreach (var plant in _plants) + { + if (plant.ClientRectangle.HasPoint(posGlob)) + { + Selected = plant; + return plant; + } + } + + return null; + } + +} \ No newline at end of file diff --git a/plantWorld.tscn b/plantWorld.tscn new file mode 100644 index 0000000..e97e415 --- /dev/null +++ b/plantWorld.tscn @@ -0,0 +1,10 @@ +[gd_scene load_steps=3 format=3 uid="uid://cvjqldltcr7nl"] + +[ext_resource type="Script" path="res://PlantWorld.cs" id="1_0as8v"] +[ext_resource type="Script" path="res://src/Plant.cs" id="1_68w2o"] + +[node name="PlantWorld" type="Node2D"] +script = ExtResource("1_0as8v") + +[node name="Plant" type="MeshInstance2D" parent="."] +script = ExtResource("1_68w2o")