Files
VirtualChemistry.Demo/Scenes/SingleBond.cs
2024-06-26 14:23:02 +08:00

98 lines
2.3 KiB
C#
Executable File

using System;
using Godot;
using VirtualChemDemo.Concepts;
public partial class SingleBond : Bond2D
{
private static int BIndex(int index, int multi)
{
if (multi == 1)
return 0;
if (multi % 2 == 1)
{
int center = multi / 2 + 1;
return center - index;
}
if (multi % 2 == 0)
{
double center = multi / 2 + 0.5;
return (int)(center > index ? Math.Ceiling(center - index) : Math.Floor(center - index));
}
return 0;
}
private void MakeCurve(float curveRate=20f, int cDir = 1)
{
Vector2 Start = AtomFrom.Position + new Vector2(32, 32);;
Vector2 End = AtomTo.Position + new Vector2(32, 32);;
Vector2[] xs = new Vector2[17];
int[] x2 = new[] { 4, 12 };
int[] x3 = new[] { 2, 6, 10, 14};
int[] x4 = new[] { 1, 3, 5, 7, 9, 11, 13, 15 };
Vector2 vd = End - Start;
Vector2 nor = vd.Rotated(Mathf.Pi / 2 * cDir);
Vector2 center = vd / 2 + 4f * nor / curveRate;
xs[0] = Start;
xs[16] = End;
xs[8] = center + xs[0];
foreach (int i in x2)
{
Vector2 v = xs[i + 4] - xs[i - 4];
Vector2 n = v.Rotated(Mathf.Pi / 2 * cDir);
Vector2 w = v / 2 + 2f * n / curveRate;
xs[i] = w + xs[i - 4];
}
foreach (int i in x3)
{
Vector2 v = xs[i + 2] - xs[i - 2];
Vector2 n = v.Rotated(Mathf.Pi / 2 * cDir);
Vector2 w = v / 2 + n / curveRate;
xs[i] = w + xs[i - 2];
}
foreach (int i in x4)
{
Vector2 v = xs[i + 1] - xs[i - 1];
Vector2 n = v.Rotated(Mathf.Pi / 2 * cDir);
Vector2 w = v / 2 + n / curveRate;
xs[i] = w + xs[i - 1];
}
BondTypeA.Position = xs[10];
BondTypeB.Position = xs[6];
Points = xs;
}
public override void BondUpdate()
{
Reindex();
int bindex = BIndex(Index, Multi);
if (bindex == 0)
{
Vector2 v1 = AtomFrom.Position + new Vector2(32, 32);
Vector2 v2 = AtomTo.Position + new Vector2(32, 32);
Vector2 m = (v1 + v2) / 2;
Points = new[] { v1, m, v2 };
BondTypeA.Position = m - new Vector2(10, 10);
BondTypeB.Position = m + new Vector2(10, 10);
}
else
{
float curveRate = 25 - Math.Abs(bindex)*5;
int cDir = bindex > 0 ? 1 : -1;
MakeCurve(curveRate, cDir);
}
BondTypeA.Text = BondFrom.BondType;
BondTypeB.Text = BondTo.BondType;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
}