Bug Fix
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Godot;
|
||||
using VirtualChemDemo;
|
||||
using VirtualChemDemo.Concepts;
|
||||
using VirtualChemistry.Chemistry.Atoms.Implements;
|
||||
using VirtualChemistry.Chemistry.Atoms.Resolver;
|
||||
using VirtualChemistry.Chemistry.Bonds.Implements;
|
||||
@@ -15,7 +18,7 @@ public partial class CompoundConstructor : Panel
|
||||
public HashSet<Atom> Atoms { get; set; } = new();
|
||||
public BaseBond ConnectPending { get; set; } = BaseBond.Null;
|
||||
public Atom PendingAtom { get; set; }
|
||||
public Dictionary<Atom, HashSet<Bond>> BondMap { get; set; } = new();
|
||||
public Dictionary<Atom, HashSet<Bond2D>> BondMap { get; set; } = new();
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready()
|
||||
@@ -25,9 +28,65 @@ public partial class CompoundConstructor : Panel
|
||||
HomogeneousMixture = HeterogeneousMixture.AddLayer();
|
||||
}
|
||||
|
||||
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
private static class AtomPositionFixMemory
|
||||
{
|
||||
public static Atom[] SavedAtoms { get; set; } = null;
|
||||
private static int I { get; set; }
|
||||
private static int J { get; set; }
|
||||
public static int Max => SavedAtoms.Length;
|
||||
|
||||
public static void Reset()
|
||||
{
|
||||
SavedAtoms = null;
|
||||
I = 0;
|
||||
J = 0;
|
||||
}
|
||||
|
||||
public static void OneStep()
|
||||
{
|
||||
if (J >= Max)
|
||||
{
|
||||
J = 0;
|
||||
I++;
|
||||
}
|
||||
if (I >= Max)
|
||||
{
|
||||
Reset();
|
||||
return;
|
||||
}
|
||||
|
||||
if (I == J)
|
||||
{
|
||||
J++;
|
||||
return;
|
||||
}
|
||||
|
||||
Atom a1 = SavedAtoms[I];
|
||||
Atom a2 = SavedAtoms[J];
|
||||
|
||||
if ((a1.Position - a2.Position).Length() < 64)
|
||||
{
|
||||
Vector2 pd = a1.Position - a2.Position;
|
||||
if (pd == Vector2.Zero)
|
||||
pd = new Vector2(J % 3 - 2, I % 3 - 1);
|
||||
a1.Position += pd;
|
||||
a2.Position -= pd;
|
||||
if (GlobalScene.CompoundConstructor.BondMap.TryGetValue(a1, out var bonds1))
|
||||
foreach (Bond2D b1 in bonds1)
|
||||
b1.BondUpdate();
|
||||
if(GlobalScene.CompoundConstructor.BondMap.TryGetValue(a2, out var bonds2))
|
||||
foreach (Bond2D b2 in bonds2)
|
||||
b2.BondUpdate();
|
||||
}
|
||||
J++;
|
||||
}
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (AtomPositionFixMemory.SavedAtoms == null)
|
||||
AtomPositionFixMemory.SavedAtoms = Atoms.ToArray();
|
||||
AtomPositionFixMemory.OneStep();
|
||||
}
|
||||
|
||||
public override bool _CanDropData(Vector2 atPosition, Variant data)
|
||||
@@ -40,7 +99,7 @@ public partial class CompoundConstructor : Panel
|
||||
Atom a = data.As<Atom>();
|
||||
a.Position = atPosition;
|
||||
if(BondMap.ContainsKey(a))
|
||||
foreach (Bond b in BondMap[a])
|
||||
foreach (Bond2D b in BondMap[a])
|
||||
b.BondUpdate();
|
||||
}
|
||||
|
||||
@@ -56,6 +115,7 @@ public partial class CompoundConstructor : Panel
|
||||
at.Position = new(500, 500);
|
||||
HomogeneousMixture.AddCompound(c, true, true);
|
||||
Atoms.Add(at);
|
||||
AtomPositionFixMemory.Reset();
|
||||
}
|
||||
|
||||
public void RemoveAtomOf(Atom atom)
|
||||
@@ -63,11 +123,11 @@ public partial class CompoundConstructor : Panel
|
||||
foreach (BaseBond b in atom.BaseAtom.ConnectedBonds)
|
||||
b.Disconnect();
|
||||
|
||||
Bond[] toRemove = Array.Empty<Bond>();
|
||||
if (BondMap.TryGetValue(atom, out HashSet<Bond> value))
|
||||
Bond2D[] toRemove = Array.Empty<Bond2D>();
|
||||
if (BondMap.TryGetValue(atom, out HashSet<Bond2D> value))
|
||||
toRemove = value.ToArray();
|
||||
|
||||
foreach (Bond b in toRemove)
|
||||
foreach (Bond2D b in toRemove)
|
||||
{
|
||||
BondMap[b.AtomFrom].Remove(b);
|
||||
BondMap[b.AtomTo].Remove(b);
|
||||
@@ -79,29 +139,32 @@ public partial class CompoundConstructor : Panel
|
||||
atom.BaseAtom.Compound = Compound.Null;
|
||||
RemoveChild(atom);
|
||||
Atoms.Remove(atom);
|
||||
AtomPositionFixMemory.Reset();
|
||||
}
|
||||
|
||||
public void ConnectAtoms(Atom a, BaseBond b)
|
||||
{
|
||||
b.Connect(ConnectPending);
|
||||
Bond bond = ResourceLoader.Load<PackedScene>("res://Scenes/Bond.tscn").Instantiate<Bond>();
|
||||
Bond2D bond = Bond2D.Resolve(b, ConnectPending);
|
||||
bond.AtomFrom = PendingAtom;
|
||||
bond.AtomTo = a;
|
||||
bond.TypeA = ConnectPending.BondType;
|
||||
bond.TypeB = b.BondType;
|
||||
bond.BondFrom = ConnectPending;
|
||||
bond.BondTo = b;
|
||||
|
||||
if (!BondMap.ContainsKey(a))
|
||||
BondMap[a] = new HashSet<Bond>();
|
||||
BondMap[a] = new HashSet<Bond2D>();
|
||||
BondMap[a].Add(bond);
|
||||
if (!BondMap.ContainsKey(PendingAtom!))
|
||||
BondMap[PendingAtom] = new HashSet<Bond>();
|
||||
BondMap[PendingAtom] = new HashSet<Bond2D>();
|
||||
BondMap[PendingAtom].Add(bond);
|
||||
ConnectPending = BaseBond.Null;
|
||||
a.AtomActions.BuildMenu();
|
||||
PendingAtom.AtomActions.BuildMenu();
|
||||
PendingAtom = null;
|
||||
AddChild(bond);
|
||||
bond.BondUpdate();
|
||||
foreach (Bond2D bd in BondMap[a])
|
||||
bd.BondUpdate();
|
||||
|
||||
}
|
||||
|
||||
private void Back()
|
||||
@@ -111,11 +174,14 @@ public partial class CompoundConstructor : Panel
|
||||
|
||||
private void Build()
|
||||
{
|
||||
if(HomogeneousMixture.Compounds.Count==0)
|
||||
return;
|
||||
HeterogeneousMixture.Container = GlobalScene.Demo.SelectedBottle;
|
||||
GlobalScene.Demo.SelectedBottle.Content = HeterogeneousMixture;
|
||||
GlobalScene.Demo.SelectedBottle.BuildMenu();
|
||||
|
||||
Clear();
|
||||
Back();
|
||||
}
|
||||
|
||||
private void Clear(bool save = true)
|
||||
@@ -127,13 +193,13 @@ public partial class CompoundConstructor : Panel
|
||||
return;
|
||||
}
|
||||
|
||||
HashSet<Bond> removedBonds = new();
|
||||
HashSet<Bond2D> removedBonds = new();
|
||||
foreach (Atom atom in Atoms)
|
||||
{
|
||||
RemoveChild(atom);
|
||||
if (!BondMap.ContainsKey(atom))
|
||||
continue;
|
||||
foreach (Bond bond in BondMap[atom])
|
||||
foreach (Bond2D bond in BondMap[atom])
|
||||
{
|
||||
if (!removedBonds.Contains(bond))
|
||||
RemoveChild(bond);
|
||||
|
||||
Reference in New Issue
Block a user