You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

158 lines
3.4 KiB

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;
}
}