Пример #1
0
        /// <summary>
        /// Searches for local powernets to connect to, otherwise creates its own, and spreads powernet to nearby entities
        /// </summary>
        public void SpreadPowernet()
        {
            var _emanager = IoCManager.Resolve <IServerEntityManager>();
            var position  = Owner.GetComponent <TransformComponent>().WorldPosition;
            var wires     = _emanager.GetEntitiesInRange(Owner, 1.1f) //arbitrarily low, just scrape things //wip
                            .Where(x => x.HasComponent <PowerTransferComponent>());

            //we have no parent so lets find a partner we can join his powernet
            if (Parent == null || Regenerating)
            {
                foreach (var wire in wires)
                {
                    var ptc = wire.GetComponent <PowerTransferComponent>();
                    if (ptc.CanConnectTo())
                    {
                        ConnectToPowernet(ptc.Parent);
                        break;
                    }
                }

                //we couldn't find a partner so none must have spread yet, lets make our own powernet to spread
                if (Parent == null || Regenerating)
                {
                    var powernew = new Powernet();
                    ConnectToPowernet(powernew);
                }
            }

            //Find nodes intersecting us and if not already assigned to a powernet assign them to us
            var nodes = _emanager.GetEntitiesIntersecting(Owner)
                        .Where(x => x.HasComponent <PowerNodeComponent>())
                        .Select(x => x.GetComponent <PowerNodeComponent>());

            foreach (var node in nodes)
            {
                if (node.Parent == null)
                {
                    node.ConnectToPowernet(Parent);
                }
                else if (node.Parent.Dirty)
                {
                    node.RegeneratePowernet(Parent);
                }
            }

            //spread powernet to nearby wires which haven't got one yet, and tell them to spread as well
            foreach (var wire in wires)
            {
                var ptc = wire.GetComponent <PowerTransferComponent>();
                if (ptc.Parent == null || Regenerating)
                {
                    ptc.ConnectToPowernet(Parent);
                    ptc.SpreadPowernet();
                }
                else if (ptc.Parent != Parent && !ptc.Parent.Dirty)
                {
                    Parent.MergePowernets(ptc.Parent);
                }
            }
        }
        /// <summary>
        /// Triggers event telling power components that we haven't disconnected but have readded ourselves to a regenerated powernet
        /// </summary>
        /// <param name="toconnect"></param>
        public void RegeneratePowernet(Powernet toconnect)
        {
            //This removes the device from things that will be powernet disconnected when dirty powernet is killed
            Parent.NodeList.Remove(this);

            Parent = toconnect;
            Parent.NodeList.Add(this);
            OnPowernetRegenerate?.Invoke(this, new PowernetEventArgs(Parent));
        }
 /// <summary>
 /// Triggers event telling power components we have exited any powernets
 /// </summary>
 public void DisconnectFromPowernet()
 {
     if (Parent == null)
     {
         return;
     }
     Parent.NodeList.Remove(this);
     OnPowernetDisconnect?.Invoke(this, new PowernetEventArgs(Parent));
     Parent = null;
 }
Пример #4
0
        /// <summary>
        /// Combines two powernets when they connect via powertransfer components
        /// </summary>
        public void MergePowernets(Powernet toMerge)
        {
            //TODO: load balance reconciliation between powernets on merge tick here

            foreach (var wire in toMerge.WireList)
            {
                wire.Parent = this;
            }

            WireList.AddRange(toMerge.WireList);
            toMerge.WireList.Clear();

            foreach (var node in toMerge.NodeList)
            {
                node.Parent = this;
            }

            NodeList.AddRange(toMerge.NodeList);
            toMerge.NodeList.Clear();

            foreach (var generator in toMerge.GeneratorList)
            {
                GeneratorList.Add(generator.Key, generator.Value);
            }

            Supply        += toMerge.Supply;
            toMerge.Supply = 0;
            toMerge.GeneratorList.Clear();

            foreach (var device in toMerge.DeviceLoadList)
            {
                DeviceLoadList.Add(device);
            }

            Load        += toMerge.Load;
            toMerge.Load = 0;
            toMerge.DeviceLoadList.Clear();

            DepoweredDevices.AddRange(toMerge.DepoweredDevices);
            toMerge.DepoweredDevices.Clear();

            PowerStorageSupplierList.AddRange(toMerge.PowerStorageSupplierList);
            toMerge.PowerStorageSupplierList.Clear();

            PowerStorageConsumerList.AddRange(toMerge.PowerStorageConsumerList);
            toMerge.PowerStorageConsumerList.Clear();

            toMerge.RemoveFromSystem();
        }
Пример #5
0
 /// <summary>
 /// Called when we are removed and telling the powernet that it is now dirty and must regenerate
 /// </summary>
 public void DisconnectFromPowernet()
 {
     Parent.WireList.Remove(this);
     Parent.Dirty = true;
     Parent       = null;
 }
Пример #6
0
 /// <summary>
 /// Called when connecting to a new powernet, either on creation or on regeneration
 /// </summary>
 /// <param name="toconnect"></param>
 public void ConnectToPowernet(Powernet toconnect)
 {
     Parent = toconnect;
     Parent.WireList.Add(this);
     Regenerating = false;
 }
 /// <summary>
 /// Triggers event telling power components that we connected to a powernet
 /// </summary>
 /// <param name="toconnect"></param>
 public void ConnectToPowernet(Powernet toconnect)
 {
     Parent = toconnect;
     Parent.NodeList.Add(this);
     OnPowernetConnect?.Invoke(this, new PowernetEventArgs(Parent));
 }
 public PowernetEventArgs(Powernet powernet)
 {
     Powernet = powernet;
 }
 /// <summary>
 /// Triggers event telling power components we have exited any powernets
 /// </summary>
 public void DisconnectFromPowernet()
 {
     Parent.Nodelist.Remove(this);
     OnPowernetDisconnect?.Invoke(this, new PowernetEventArgs(Parent));
     Parent = null;
 }