5 changed files with 214 additions and 0 deletions
@ -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>("PlantWorld"); |
||||||
|
_camera = GetNode<Camera2D>("Camera2D"); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public override void _Process(double delta) |
||||||
|
{ |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,8 @@ |
|||||||
|
<Project Sdk="Godot.NET.Sdk/4.2.0"> |
||||||
|
<PropertyGroup> |
||||||
|
<TargetFramework>net6.0</TargetFramework> |
||||||
|
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'android' ">net7.0</TargetFramework> |
||||||
|
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'ios' ">net8.0</TargetFramework> |
||||||
|
<EnableDynamicLoading>true</EnableDynamicLoading> |
||||||
|
</PropertyGroup> |
||||||
|
</Project> |
||||||
@ -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 |
||||||
@ -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<Plant> _plants; |
||||||
|
|
||||||
|
public Plant Selected = null; |
||||||
|
|
||||||
|
public Plant this[int index] |
||||||
|
{ |
||||||
|
get => _plants[index]; |
||||||
|
} |
||||||
|
|
||||||
|
public List<Plant> Plants |
||||||
|
{ |
||||||
|
get { return _plants; } |
||||||
|
} |
||||||
|
|
||||||
|
public PlantCollection() |
||||||
|
{ |
||||||
|
_plants = new List<Plant>(); |
||||||
|
} |
||||||
|
|
||||||
|
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; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -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") |
||||||
Loading…
Reference in new issue