|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | + |
| 4 | +namespace Sandbox |
| 5 | +{ |
| 6 | + public class WireOutput |
| 7 | + { |
| 8 | + public int value; |
| 9 | + public Entity entity; |
| 10 | + public string outputName; |
| 11 | + public List<WireInput> connected = new List<WireInput>(); |
| 12 | + |
| 13 | + public WireOutput( Entity entity, string newOutputName ) |
| 14 | + { |
| 15 | + this.entity = entity; |
| 16 | + outputName = newOutputName; |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + public interface WireOutputEntity |
| 21 | + { |
| 22 | + protected static Dictionary<int, Dictionary<string, WireOutput>> allOutputs = new Dictionary<int, Dictionary<string, WireOutput>>(); |
| 23 | + |
| 24 | + public int NetworkIdent { get; } |
| 25 | + |
| 26 | + public static void WireTriggerOutput( WireOutputEntity ent, string outputName, int value ) |
| 27 | + { |
| 28 | + var output = ent.GetOutput( outputName ); |
| 29 | + foreach ( var input in output.connected ) { |
| 30 | + if ( !input.entity.IsValid() ) continue; |
| 31 | + if ( input.entity is WireInputEntity inputEntity ) { |
| 32 | + inputEntity.WireTriggerInput( input.inputName, value ); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + public void WireConnect( WireInputEntity inputEnt, string outputName, string inputName ) |
| 37 | + { |
| 38 | + GetOutput( outputName ).connected.Add( inputEnt.GetInput( inputName ) ); |
| 39 | + } |
| 40 | + |
| 41 | + public WireOutput GetOutput( string inputName ) |
| 42 | + { |
| 43 | + if ( !allOutputs.ContainsKey( this.NetworkIdent ) ) { |
| 44 | + InitializeOutputs(); |
| 45 | + } |
| 46 | + return allOutputs[this.NetworkIdent][inputName]; |
| 47 | + } |
| 48 | + public string[] GetOutputNames() |
| 49 | + { |
| 50 | + if ( !allOutputs.ContainsKey( this.NetworkIdent ) ) { |
| 51 | + InitializeOutputs(); |
| 52 | + } |
| 53 | + return allOutputs[this.NetworkIdent].Keys.ToArray(); |
| 54 | + } |
| 55 | + |
| 56 | + protected void InitializeOutputs() |
| 57 | + { |
| 58 | + allOutputs[this.NetworkIdent] = new Dictionary<string, WireOutput>(); |
| 59 | + foreach ( var outputName in WireGetOutputs() ) { |
| 60 | + allOutputs[this.NetworkIdent][outputName] = new WireOutput( (Entity)this, outputName ); |
| 61 | + } |
| 62 | + } |
| 63 | + abstract public string[] WireGetOutputs(); |
| 64 | + } |
| 65 | +} |
0 commit comments