-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoveDrones.cs
92 lines (80 loc) · 2.56 KB
/
MoveDrones.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved.
// See LICENSE file in the project root for full license information.
using System;
using System.Numerics;
using Example.Systems;
using Godot;
namespace Example;
public partial class MoveDrones : MultiMeshInstance3D
{
private readonly Drones drones = new Drones();
private int entityCount = 1024;
private Shape shape;
private Label labelCount;
private Label labelFps;
public override void _Ready()
{
var root = GetTree().Root;
labelCount = root.GetNode<Label>("Test/Panel/Info/Entities");
labelFps = root.GetNode<Label>("Test/Panel/Info/FPS");
labelCount.Text = $"Entities {entityCount}";
Multimesh.InstanceCount = entityCount;
for(int i = 0; i < Multimesh.InstanceCount; i++)
{
var m = Matrix4x4.CreateTranslation(new System.Numerics.Vector3(i * 2f, 0, 0));
Multimesh.SetInstanceTransform(i, m.AsTransform3D());
}
drones.Initialize();
drones.SetEntityCount(entityCount);
drones.SetTargetPlane(2000, 1.2f);
var plane = (Node3D)GetParent().FindChild("Plane");
plane.Hide();
}
public void SetTargetPlane() => SetShape(Shape.Plane);
public void SetTargetCube() => SetShape(Shape.Cube);
public void SetTargetRing() => SetShape(Shape.Ring);
public void SetTargetRings() => SetShape(Shape.Rings);
private void SetShape (Shape shape)
{
this.shape = shape;
switch (shape)
{
case Shape.Plane: drones.SetTargetPlane(500, 1.2f); break;
case Shape.Cube: drones.SetTargetCube (500, 1.2f); break;
case Shape.Ring: drones.SetTargetRings(500, 24, 1.2f, 1); break;
case Shape.Rings: drones.SetTargetRings(500, 20, 1.2f, 10); break;
}
}
public void IncreaseEntities() {
entityCount = Math.Min(drones.maxDroneCount, entityCount * 2);
drones.SetEntityCount(entityCount);
labelCount.Text = $"Entities {entityCount}";
SetShape(shape);
}
public void DecreaseEntities() {
entityCount = Math.Max(4, entityCount / 2);
drones.SetEntityCount(entityCount);
labelCount.Text = $"Entities {entityCount}";
SetShape(shape);
}
public override void _Process(double delta)
{
var deltaTime = (float)delta * 1000;
drones.UpdateTransforms(deltaTime, default);
DrawEntities();
}
private void DrawEntities()
{
labelFps.Text = $"fps {Engine.GetFramesPerSecond()}";
var mesh = Multimesh;
mesh.InstanceCount = entityCount;
foreach (var (transforms, _) in drones.transQuery.Chunks)
{
int n = 0;
foreach (ref var transform in transforms.Span)
{
mesh.SetInstanceTransform(n++, transform.value.AsTransform3D());
}
}
}
}