WiP: deactivated array elements

This commit is contained in:
2024-11-03 14:56:07 +01:00
parent d1dac0b855
commit 383e1343ef
6 changed files with 52 additions and 31 deletions

View File

@@ -2,7 +2,8 @@ using Godot;
public partial class FPSLabel : Label public partial class FPSLabel : Label
{ {
bool ShowFPS = true; [Export]
public bool ShowFPS = true;
// Called every frame. 'delta' is the elapsed time since the previous frame. // Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta) { public override void _Process(double delta) {

View File

@@ -1,11 +1,13 @@
[gd_scene load_steps=4 format=3 uid="uid://cf34vk5r055dx"] [gd_scene load_steps=5 format=3 uid="uid://cf34vk5r055dx"]
[ext_resource type="Script" path="res://Scripts/Main.cs" id="1_k1i8e"] [ext_resource type="Script" path="res://Scripts/Main.cs" id="1_k1i8e"]
[ext_resource type="Script" path="res://Scenes/FPSLabel.cs" id="2_8cb7y"] [ext_resource type="Script" path="res://Scenes/FPSLabel.cs" id="2_8cb7y"]
[ext_resource type="Script" path="res://Scenes/SettingsController.cs" id="3_a4w6m"] [ext_resource type="Script" path="res://Scenes/SettingsController.cs" id="3_a4w6m"]
[ext_resource type="Script" path="res://Scenes/PerfDetails.cs" id="3_u2p48"]
[node name="Main" type="Node2D"] [node name="Main" type="Node2D"]
script = ExtResource("1_k1i8e") script = ExtResource("1_k1i8e")
DebugVisualization = true
BrushSize = 2 BrushSize = 2
[node name="CanvasLayer" type="CanvasLayer" parent="."] [node name="CanvasLayer" type="CanvasLayer" parent="."]
@@ -32,6 +34,15 @@ text = "FPS: 0
" "
script = ExtResource("2_8cb7y") script = ExtResource("2_8cb7y")
[node name="PerfDetails" type="Label" parent="CanvasLayer/TopUI"]
layout_mode = 0
offset_top = 30.0
offset_right = 76.0
offset_bottom = 67.0
theme_override_font_sizes/font_size = 10
text = "Active Elements: 0"
script = ExtResource("3_u2p48")
[node name="Settings" type="VBoxContainer" parent="CanvasLayer/TopUI"] [node name="Settings" type="VBoxContainer" parent="CanvasLayer/TopUI"]
layout_mode = 1 layout_mode = 1
anchors_preset = 1 anchors_preset = 1

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using FOU.Scripts.Elements; using FOU.Scripts.Elements;
using Godot; using Godot;
@@ -16,7 +17,7 @@ public class Chunk {
private readonly Image image; private readonly Image image;
private readonly int sizeX; private readonly int sizeX;
private readonly int sizeY; private readonly int sizeY;
private readonly List<Element> activeElements = new List<Element>(); private readonly HashSet<Element> activeElements;
public Chunk(int x, int y, int index) { public Chunk(int x, int y, int index) {
Index = index; Index = index;
@@ -30,6 +31,8 @@ public class Chunk {
} }
} }
activeElements = new HashSet<Element>(sizeX * sizeY);
image = Image.Create(sizeX, sizeY, false, Image.Format.Rgb8); image = Image.Create(sizeX, sizeY, false, Image.Format.Rgb8);
image.Fill(Colors.Black); image.Fill(Colors.Black);
} }
@@ -59,7 +62,7 @@ public class Chunk {
if (Elements[X,Y].GetType() != type) { if (Elements[X,Y].GetType() != type) {
Elements[X, Y] = o as Element; Elements[X, Y] = o as Element;
activeElements.Add(Elements[X, Y]); Elements[X, Y].Active = true;
} }
} }
} }
@@ -78,12 +81,17 @@ public class Chunk {
public void SetElementActive(Element e, bool active) { public void SetElementActive(Element e, bool active) {
if (e.Active == active) return; if (e.Active == active) return;
if (active) GD.Print($"setting {e} to {active}");
if (active && e.GetType() != typeof(Element))
activeElements.Add(e); activeElements.Add(e);
else else
activeElements.Remove(e); activeElements.Remove(e);
} }
public int ActiveElemetnsCount() {
return activeElements.Count;
}
public void Swap(Element what, Vector2I pos) { public void Swap(Element what, Vector2I pos) {
Swap(what, Get(pos)); Swap(what, Get(pos));
} }

View File

@@ -18,7 +18,7 @@ public class Element {
protected const float MAX_COLOR_VARIANCE = 0.1f; protected const float MAX_COLOR_VARIANCE = 0.1f;
protected static readonly Vector2I VERTICAL_OPPOSITE = new Vector2I(-1, 1); protected static readonly Vector2I VERTICAL_OPPOSITE = new Vector2I(-1, 1);
private bool active = true; private bool active = false;
private Color originalColor; private Color originalColor;
public Element(Element e) { public Element(Element e) {
@@ -32,18 +32,17 @@ public class Element {
Position.Y = y; Position.Y = y;
Chunk = chunk; Chunk = chunk;
LastMove = Engine.GetFramesDrawn(); LastMove = Engine.GetFramesDrawn();
Active = false;
} }
public bool Active { public bool Active {
get => active; get => active;
set { set {
active = value; if (active == value) return;
Chunk.SetElementActive(this, active);
if (active) active = value;
Color = originalColor; Chunk.SetElementActive(this, value);
else if (Main.Instance.DebugVisualization) SetDebugColors(value);
Color = new Color(0.2f, 0.2f, 0.2f);
} }
} }
@@ -99,4 +98,11 @@ public class Element {
originalColor = c; originalColor = c;
return c; return c;
} }
private void SetDebugColors(bool isActive) {
if (isActive)
Color = originalColor;
else if (Main.Instance.DebugVisualization)
Color = new Color(0.2f, 0.2f, 0.2f);
}
} }

View File

@@ -18,7 +18,6 @@ public class Level {
private int chunkResX; private int chunkResX;
private int chunkResY; private int chunkResY;
private bool enableRain = false;
private float rainAmount = 0; private float rainAmount = 0;
public Level(Main main, int sizeX, int sizeY) { public Level(Main main, int sizeX, int sizeY) {
@@ -75,6 +74,10 @@ public class Level {
} }
} }
public Chunk[,] GetChunks() {
return chunks;
}
private void MakeItRain() { private void MakeItRain() {
if (rainAmount < .1f) return; if (rainAmount < .1f) return;

View File

@@ -9,35 +9,27 @@ public partial class Main : Node2D {
[Export] public int BrushSize = 5; [Export] public int BrushSize = 5;
[Export] public float TextureResolution = 0.5f; [Export] public float TextureResolution = 0.5f;
[Export] public int ChunksPerAxis = 2; [Export] public int ChunksPerAxis = 2;
[Export] public float RainAmount = 0;
[Export]
public float RainAmount {
get => rainAmount;
set {
rainAmount = value;
mLevel.SetRainAmount(rainAmount);
}
}
public static Main Instance; public static Main Instance;
public Level Level;
private TextureRect mLevelDrawer; private TextureRect mLevelDrawer;
private Level mLevel;
private bool enableRain; private bool enableRain;
private float rainAmount; private float rainAmount;
public override void _Ready() { public override void _Ready() {
mLevel = new Level(this, (int)(GetViewportRect().Size.X * TextureResolution), Level = new Level(this, (int)(GetViewportRect().Size.X * TextureResolution),
(int)(GetViewportRect().Size.Y * TextureResolution)); (int)(GetViewportRect().Size.Y * TextureResolution));
mLevel.SetRainAmount(rainAmount); Level.SetRainAmount(rainAmount);
mLevelDrawer = GetNode<TextureRect>("CanvasLayer/LevelDrawer"); mLevelDrawer = GetNode<TextureRect>("CanvasLayer/LevelDrawer");
Instance = this; Instance = this;
} }
public override void _Process(double delta) { public override void _Process(double delta) {
mLevel.Update(); Level.Update();
mLevelDrawer.Texture = ImageTexture.CreateFromImage(mLevel.DrawLevel()); mLevelDrawer.Texture = ImageTexture.CreateFromImage(Level.DrawLevel());
} }
public override void _UnhandledInput(InputEvent @event) { public override void _UnhandledInput(InputEvent @event) {
@@ -46,14 +38,14 @@ public partial class Main : Node2D {
if (eventMouseButton.IsPressed()) { if (eventMouseButton.IsPressed()) {
Vector2 mouse = GetViewport().GetMousePosition(); Vector2 mouse = GetViewport().GetMousePosition();
float mappedX = mouse.X / (GetViewportRect().Size.X / mLevel.Resolution.X); float mappedX = mouse.X / (GetViewportRect().Size.X / Level.Resolution.X);
float mappedY = mouse.Y / (GetViewportRect().Size.Y / mLevel.Resolution.Y); float mappedY = mouse.Y / (GetViewportRect().Size.Y / Level.Resolution.Y);
mLevel.WritePixel<Dirt>((int)mappedX, (int)mappedY, BrushSize); Level.WritePixel<Dirt>((int)mappedX, (int)mappedY, BrushSize);
} }
} else if (@event is InputEventKey keyEvent && keyEvent.Pressed) { } else if (@event is InputEventKey keyEvent && keyEvent.Pressed) {
if (keyEvent.Keycode == Key.F9) if (keyEvent.Keycode == Key.F9)
mLevel.StartBenchmark(); Level.StartBenchmark();
} }
else base._UnhandledInput(@event); else base._UnhandledInput(@event);
} }