-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell.cs
72 lines (60 loc) · 2.08 KB
/
cell.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Godot;
public partial class cell : Node3D
{
[Export]
public float step = 0.5f;
public bool alive_next;
private CsgBox3D cube;
private List<cell> Neighbours = new();
private bool need_update = false;
public void AddNeighbour(cell neighbour)
{
if (neighbour is not null && this != neighbour && !Neighbours.Contains(neighbour))
{
Neighbours.Add(neighbour);
}
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
var nextTimer = GetNode<NextTimer>("/root/NextTimer");
nextTimer.NextStep += () => { need_update = true; };
Visible = Random.Shared.Next(0, 2) % 2 == 0;
alive_next = Visible;
cube = GetNode<CsgBox3D>("Cube");
cube.MaterialOverride = cube.MaterialOverride.Duplicate() as Material;
(cube.MaterialOverride as StandardMaterial3D).AlbedoColor = Colors.Aqua;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
if (Visible != alive_next)
{
if (alive_next)
Visible = alive_next;
(cube.MaterialOverride as StandardMaterial3D).AlbedoColor = alive_next ? Colors.Aqua : Colors.Red;
var tween = CreateTween();
tween.TweenProperty(cube, "scale", alive_next ? Vector3.One : Vector3.Zero, 0.8f);
tween.TweenCallback(Callable.From(() => { Visible = alive_next; }));
}
if (need_update)
{
var count = CountNeighbours();
alive_next = count == 4 || (Visible && (count == 5 || count == 6));
need_update = false;
}
}
private int CountNeighbours()
{
Debug.Assert(Neighbours.Count < 27, $"neighbours count is {Neighbours.Count}");
int count = 0;
foreach (var cell in Neighbours)
{
count = cell.Visible ? count + 1 : count;
}
return count;
}
}