Evolution/scripts/World.cs

62 lines
1.4 KiB
C#

using System.Collections.Generic;
using System.Linq;
using Godot;
using Godot.Collections;
namespace Evolution.scripts;
public partial class World : Node3D {
public static World Instance { get; private set; }
public int BoxCount { get; private set; }
public int Collisions {
get => collisions;
set {
collisions = value;
collisionsLabel.Text = $"Collisions: {collisions}";
}
}
[Export] private Array<PackedScene> boxScenes = new();
[Export] private double maxTimer = 0.01;
private double timer;
private double fpsTimer;
[Export] private Label fpsLabel;
[Export] private Label boxesLabel;
[Export] private Label collisionsLabel;
private int frameCount;
private readonly List<int> frameCounts = new();
private int collisions;
public override void _Ready() => Instance = this;
public override void _Process(double delta) {
frameCount++;
fpsTimer += delta;
if (fpsTimer >= 1) {
frameCounts.Add(frameCount);
fpsLabel.Text = $"FPS: {(int)frameCounts.Average()}";
frameCount = 0;
fpsTimer = 0;
}
timer += delta;
if (timer >= maxTimer) {
CreateNewBox();
timer = 0;
}
}
private void CreateNewBox() {
BoxCount++;
Node newBox = boxScenes[GD.RandRange(0, boxScenes.Count - 1)].Instantiate();
AddChild(newBox);
boxesLabel.Text = $"Boxes: {BoxCount}";
}
public void DestroyBox() {
BoxCount--;
boxesLabel.Text = $"Boxes: {BoxCount}";
}
}