Exemplo n.º 1
0
        public override void UpdateAfterSimulation()
        {
            Initilize();

            if (!_isInitialized)
            {
                return;
            }

            _frameCounter++;
            if (_frameCounter < 30)
            {
                return;
            }

            _frameCounter = 0;

            _debug = ((IMyTerminalBlock)Entity).IsWorking && ((IMyTerminalBlock)Entity).ShowOnHUD;

            if (!string.IsNullOrEmpty(_exceptionInfo))
            {
                WriteDebug("Info", _exceptionInfo);
            }

            if (!_isIsValid)
            {
                return;
            }

            if (!_autoTrackOn)
            {
                return;
            }

            var terminalEntity = (IMyTerminalBlock)Entity;

            if (string.IsNullOrEmpty(terminalEntity.CustomName) || !terminalEntity.IsWorking || !terminalEntity.IsFunctional)
            {
                return;
            }

            //WriteDebug("UpdateAfterSimulation", "Start = {0} {1} {2} {3}", _isInitialized, _isIsValid, _autoTrackOn, _attachedRotors.Count);

            Vector2 ang = Vector2.Zero;

            // TODO: run as background, if I can find a suitable world to test it on FIRST!

            MyAPIGateway.Parallel.Start(delegate
                                        // Background processing occurs within this block.
            {
                try
                {
                    var sunDirection = Support.GetSunDirection();
                    //ang = GetRotationAngle(Entity.WorldMatrix, sunDirection);
                    ang = GetRotationAngle(Entity.WorldMatrix.Forward, Entity.WorldMatrix.Up, Entity.WorldMatrix.Right, sunDirection);
                }
                catch (Exception ex)
                {
                }
            },
                                        // when the background processing is finished, this block will run foreground.
                                        delegate {
                try
                {
                    // Check Ownership.
                    var block = (IMyCubeBlock)Entity;

                    //WriteDebug("Panel offset", "{0} {1}", ang.X, ang.Y);

                    // ang.Y Azimuth  Yaw.
                    // any.X Elevation  Pitch.

                    if (_attachedRotors.Count >= 2)
                    {
                        var rotorBase = _attachedRotors[0] as IMyTerminalBlock;
                        if (rotorBase == null)
                        {
                            rotorBase = _attachedRotors[1] as IMyTerminalBlock;
                        }

                        if (rotorBase != null && !rotorBase.Closed && rotorBase.HasPlayerAccess(block.OwnerId))
                        {
                            //WriteDebug("Turn", "'{0}'", rotorBase.CustomName);
                            TurnRotor(_attachedRotors[0], _attachedRotors[1], ang, _rotorDirections[0]);
                        }
                    }
                    if (_attachedRotors.Count >= 3)
                    {
                        var rotorBase = _attachedRotors[2] as IMyTerminalBlock;
                        if (rotorBase == null)
                        {
                            rotorBase = _attachedRotors[3] as IMyTerminalBlock;
                        }

                        if (rotorBase != null && !rotorBase.Closed && rotorBase.HasPlayerAccess(block.OwnerId))
                        {
                            //WriteDebug("Turn", "'{0}'", rotorBase.CustomName);
                            TurnRotor(_attachedRotors[2], _attachedRotors[3], ang, _rotorDirections[1]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    var message = ex.Message.Replace("\r", " ").Replace("\n", " ");
                    message     = message.Substring(0, Math.Min(message.Length, 100));
                    MyAPIGateway.Utilities.ShowMessage("Error", string.Format("{0}", message));
                }
            });
        }