private List <IPowerable> getPowerables(IPowerer source) { HashSet <IPowerable> powerables = new HashSet <IPowerable>(); List <IPowerTransferer> wires = connectionMap[source].ToList() .FindAll(ipc => ipc is IPowerTransferer) .ConvertAll(ipc => (IPowerTransferer)ipc); int i = 0; while (i < wires.Count) { connectionMap[wires[i]].ToList().ForEach( ipc => { if (ipc is IPowerable && ipc.GameObject != source.GameObject) { powerables.Add((IPowerable)ipc); } if (ipc is IPowerTransferer) { if (!wires.Contains(ipc)) { wires.Add((IPowerTransferer)ipc); } } } ); i++; } return(powerables.ToList()); }
private void FixedUpdate() { //Reset all power wires powerConduits .FindAll(ipc => ipc is PowerWire) .ConvertAll(ipc => (PowerWire)ipc) .ForEach(pw => pw.reset()); //Process powerables with no power noPowerPowerables.ForEach(pwr => pwr.acceptPower(0)); //Have powerers dish out their power powerPaths.ForEach( entry => { IPowerer ipr = entry.Key; float powerToEach = ipr.ThroughPut * Time.fixedDeltaTime / entry.Value.Count; entry.Value.ForEach(path => { IPowerable pwr = path.powerable; ipr.givePower( -pwr.acceptPower(ipr.givePower(powerToEach)) ); path.path.ForEach(wire => wire.transferPower(powerToEach)); }); }); }
private List <PowerPath> getPowerPaths(IPowerer source) { List <PowerPath> paths = new List <PowerPath>(); getPowerables(source).ForEach(pwr => { paths.Add(new PowerPath( pwr, getTransferPath(source, pwr) )); }); return(paths); }
/// <summary> /// Tells the wires between these two conduits that power is transferring through them /// </summary> /// <param name="ipr"></param> /// <param name="pwr"></param> List <IPowerTransferer> getTransferPath(IPowerer source, IPowerable usage) { Stack <IPowerTransferer> path = new Stack <IPowerTransferer>(); Stack <IPowerTransferer> stack = new Stack <IPowerTransferer>( connectionMap[source].ToList() .FindAll(ipc => ipc is IPowerTransferer) .ConvertAll(ipc => (IPowerTransferer)ipc) ); HashSet <IPowerTransferer> tried = new HashSet <IPowerTransferer>(); bool foundPath = false; IPowerTransferer currentNode; while (!foundPath) { currentNode = stack.Pop(); path.Push(currentNode); tried.Add(currentNode); bool foundBranch = false; connectionMap[currentNode].ToList().ForEach( ipc => { if (ipc is IPowerable && ipc == usage) { foundPath = true; } if (ipc is IPowerTransferer) { if (!tried.Contains(ipc)) { foundBranch = true; stack.Push((IPowerTransferer)ipc); } } } ); if (!foundBranch && !foundPath) { path.Pop(); } } //Return the found wires return(path.ToList()); }