Exemple #1
0
        public override void Simulate()
        {
            if (!Host.IsServer)
            {
                return;
            }

            using (Prediction.Off())
            {
                var input = Owner.Input;

                if (!input.Pressed(InputButton.Attack1))
                {
                    return;
                }

                var startPos = Owner.EyePos;
                var dir      = Owner.EyeRot.Forward;

                var tr = Trace.Ray(startPos, startPos + dir * MaxTraceDistance)
                         .Ignore(Owner)
                         .Run();

                if (!tr.Hit || !tr.Entity.IsValid())
                {
                    return;
                }

                CreateHitEffects(tr.EndPos);

                if (tr.Entity is LampEntity lamp)
                {
                    // TODO: Set properties

                    lamp.Flicker = !lamp.Flicker;

                    return;
                }

                lamp = new LampEntity
                {
                    Enabled              = true,
                    DynamicShadows       = true,
                    Range                = 512,
                    Falloff              = 1.0f,
                    LinearAttenuation    = 0.0f,
                    QuadraticAttenuation = 1.0f,
                    InnerConeAngle       = 25,
                    OuterConeAngle       = 45,
                    Brightness           = 10,
                    Color                = Color.Random,
                    Rotation             = Rotation.Identity
                };

                lamp.SetModel(Model);
                lamp.SetupPhysicsFromModel(PhysicsMotionType.Dynamic, false);
                lamp.Position = tr.EndPos + -lamp.CollisionBounds.Center + tr.Normal * lamp.CollisionBounds.Size * 0.5f;
            }
        }
Exemple #2
0
    // Called during instantiation
    public override void Awake()
    {
        if (CompareTag("ActiveItem"))
        {
            id = idCounter++;
            if (_lampEntity == null)
            {
                _lampEntity = new LampEntity();
            }

            SetAndInitializeConnectors();

            GameObject componentIdManager = GameObject.Find("_ComponentIdManager");
            GenerateId script             = componentIdManager.GetComponent <GenerateId>();
            script.generatedIds[2]++;
            _name += script.generatedIds[2].ToString();
        }
    }
    public void Load(string fileName)
    {
        if (string.IsNullOrEmpty(fileName))
        {
            throw new ArgumentNullException("fileName");
        }

        LastFileName = fileName;
        ClearScene();
        BinaryFormatter binaryFormatter = new BinaryFormatter();
        FileStream      fileStream      = new FileStream(LastFileName, FileMode.Open, FileAccess.Read);

        object o = binaryFormatter.Deserialize(fileStream);
        SerializationPackage package = (SerializationPackage)o;

        foreach (SimulationElement simulationElement in package.SimulationElements)
        {
            Type entityType = simulationElement.GetType();
            if (entityType == typeof(BatteryEntity))
            {
                BatteryEntity concreteEntity     = simulationElement as BatteryEntity;
                GameObject    concreteGameObject = InstantiateGameObject(_batteryPrefab);
                concreteGameObject.GetComponent <GUIBattery>().Awake();
                concreteGameObject.GetComponent <GUIBattery>().Entity = concreteEntity;
            }
            else if (entityType == typeof(ResistorEntity))
            {
                ResistorEntity concreteEntity     = simulationElement as ResistorEntity;
                GameObject     concreteGameObject = InstantiateGameObject(_resistorPrefab);
                concreteGameObject.GetComponent <GUIResistor>().Awake();
                concreteGameObject.GetComponent <GUIResistor>().Entity = concreteEntity;
            }
            else if (entityType == typeof(VoltmeterEntity))
            {
                VoltmeterEntity concreteEntity     = simulationElement as VoltmeterEntity;
                GameObject      concreteGameObject = InstantiateGameObject(_voltmeterPrefab);
                concreteGameObject.GetComponent <GUIVoltmeter>().Awake();
                concreteGameObject.GetComponent <GUIVoltmeter>().Entity = concreteEntity;
            }
            else if (entityType == typeof(AmpermeterEntity))
            {
                AmpermeterEntity concreteEntity     = simulationElement as AmpermeterEntity;
                GameObject       concreteGameObject = InstantiateGameObject(_ampermeterPrefab);
                concreteGameObject.GetComponent <GUIAmpermeter>().Awake();
                concreteGameObject.GetComponent <GUIAmpermeter>().Entity = concreteEntity;
            }
            else if (entityType == typeof(AnalogSwitchEntity))
            {
                AnalogSwitchEntity concreteEntity     = simulationElement as AnalogSwitchEntity;
                GameObject         concreteGameObject = InstantiateGameObject(_analogSwitchPrefab);
                concreteGameObject.GetComponent <GUIAnalogSwitch>().Awake();
                concreteGameObject.GetComponent <GUIAnalogSwitch>().Entity = concreteEntity;
            }
            else if (entityType == typeof(CapacitorEntity))
            {
                CapacitorEntity concreteEntity     = simulationElement as CapacitorEntity;
                GameObject      concreteGameObject = InstantiateGameObject(_capacitorPrefab);
                concreteGameObject.GetComponent <GUICapacitor>().Awake();
                concreteGameObject.GetComponent <GUICapacitor>().Entity = concreteEntity;
            }
            else if (entityType == typeof(InductorEntity))
            {
                InductorEntity concreteEntity     = simulationElement as InductorEntity;
                GameObject     concreteGameObject = InstantiateGameObject(_inductorPrefab);
                concreteGameObject.GetComponent <GUIInductor>().Awake();
                concreteGameObject.GetComponent <GUIInductor>().Entity = concreteEntity;
            }
            else if (entityType == typeof(LampEntity))
            {
                LampEntity concreteEntity     = simulationElement as LampEntity;
                GameObject concreteGameObject = InstantiateGameObject(_lampPrefab);
                concreteGameObject.GetComponent <GUILamp>().Awake();
                concreteGameObject.GetComponent <GUILamp>().Entity = concreteEntity;
            }
            else if (entityType == typeof(NodeEntity))
            {
                NodeEntity concreteEntity     = simulationElement as NodeEntity;
                GameObject concreteGameObject = InstantiateGameObject(_nodePrefab);
                concreteGameObject.GetComponent <GUINode>().Awake();
                concreteGameObject.GetComponent <GUINode>().Entity = concreteEntity;
            }
            else if (entityType == typeof(LedDiodeEntity))
            {
                LedDiodeEntity concreteEntity     = simulationElement as LedDiodeEntity;
                GameObject     concreteGameObject = InstantiateGameObject(_ledDiodePrefab);
                concreteGameObject.GetComponent <GUILedDiode>().Awake();
                concreteGameObject.GetComponent <GUILedDiode>().Entity = concreteEntity;
            }
            else if (entityType == typeof(TransistorNPNEntity))
            {
                TransistorNPNEntity concreteEntity     = simulationElement as TransistorNPNEntity;
                GameObject          concreteGameObject = InstantiateGameObject(_transistorNPNPrefab);
                concreteGameObject.GetComponent <GUITransistorNPN>().Awake();
                concreteGameObject.GetComponent <GUITransistorNPN>().Entity = concreteEntity;
            }
            else if (entityType == typeof(TransistorPNPEntity))
            {
                TransistorPNPEntity concreteEntity     = simulationElement as TransistorPNPEntity;
                GameObject          concreteGameObject = InstantiateGameObject(_transistorPNPPrefab);
                concreteGameObject.GetComponent <GUITransistorPNP>().Awake();
                concreteGameObject.GetComponent <GUITransistorPNP>().Entity = concreteEntity;
            }
            else if (entityType == typeof(ZenerDiodeEntity))
            {
                ZenerDiodeEntity concreteEntity     = simulationElement as ZenerDiodeEntity;
                GameObject       concreteGameObject = InstantiateGameObject(_zenerDiodePrefab);
                concreteGameObject.GetComponent <GUIZenerDiode>().Awake();
                concreteGameObject.GetComponent <GUIZenerDiode>().Entity = concreteEntity;
            }
            else if (entityType == typeof(PotentiometerEntity))
            {
                PotentiometerEntity concreteEntity     = simulationElement as PotentiometerEntity;
                GameObject          concreteGameObject = InstantiateGameObject(_potentiometerPrefab);
                concreteGameObject.GetComponent <GUIPotentiometer>().Awake();
                concreteGameObject.GetComponent <GUIPotentiometer>().Entity = concreteEntity;
            }
        }

        List <Connector> connectors = FindObjectsOfType <Connector>().ToList();

        connectors =
            connectors.Where(
                x => x.transform.parent != null && (x.transform.parent.CompareTag("ActiveItem") || x.transform.parent.CompareTag("ActiveNode"))).ToList();

        foreach (LineEntity packageLineEntity in package.LineEntities)
        {
            Connector start = connectors.Find(x => x.TemporaryId == packageLineEntity.StartConnectorId);
            Connector end   = connectors.Find(x => x.TemporaryId == packageLineEntity.EndConnectorId);

            GameObject linePrefab = Instantiate(_linePrefab);
            Line       line       = linePrefab.AddComponent <Line>();
            line.Begin      = start.gameObject;
            line.End        = end.gameObject;
            line.TypeOfLine = packageLineEntity.LineType;
            line.EndPos     = end.transform.position;
            line.StartPos   = start.transform.position;

            linePrefab.tag = "ActiveLine";
            linePrefab.transform.position = new Vector2((line.Begin.transform.position.x + line.EndPos.x) / 2,
                                                        (line.Begin.transform.position.y + line.EndPos.y) / 2);

            end.GetComponent <Connectable>().AddConnected(start.gameObject);
            start.GetComponent <Connectable>().AddConnected(end.gameObject);

            start.ConnectedConnectors.Add(end);
            end.ConnectedConnectors.Add(start);
        }

        fileStream.Close();
    }