public ShipHangarGears(ButtonPanel button)
        {
            _button          = button;
            HangarGearsState = HangarState.Closed;

            Log.Write("Hangar {0} gears created: ", button.HangarCode);
        }
Exemple #2
0
    private void Awake()
    {
        if (!alreadyLoaded)
        {
            instance = this;

            #region Instantiate State Machine & Level States

            gameSM              = new GameStateMachine();
            introState          = new GameIntroMenuState(this, gameSM);
            hangarState         = new HangarState(this, gameSM);
            asteroidLevelState  = new AsteroidLevelState(this, gameSM);
            nebulaLevelState    = new NebulaLevelState(this, gameSM);
            blackHoleLevelState = new BlackHoleLevelState(this, gameSM);
            deathState          = new DeathState(this, gameSM);
            highScoresState     = new HighScoresState(this, gameSM);

            #endregion

            #region Create Events

            SceneManager.sceneLoaded += OnSceneLoaded;
            AddEvents();

            #endregion
        }
    }
Exemple #3
0
        private void MovePlatform(SpacePortPistonController ctrl, bool toOpen)
        {
            var options = new Options(_button.Entity.CustomName);

            var velocity = options.Get("V", 1f);

            //Log.Write("There is {0} pistons totally", Entities.Pistons.Count);
            //Log.Write("There is {0} pistons in group {1} totally", Entities.Pistons.Count(p => p.HangarCode == _button.HangarCode), _button.HangarCode);

            var pistons =
                Entities.Pistons.Where(
                    p => p.Entity.CustomName.StartsWith(Constants.SpacePortPrefix, StringComparison.OrdinalIgnoreCase))
                .Where(p => p.HangarCode.Equals(_button.HangarCode, StringComparison.OrdinalIgnoreCase))
                .ToList();

            //Log.Write("Got {0} pistons, within {1}m while button is {2}", pistons.Count, distance, _button.Entity.CustomName);

            if (toOpen)
            {
                MyAPIGateway.Utilities.ShowNotification("Hangar: Opening");

                foreach (var piston in pistons)
                {
                    piston.SetVelocity(velocity);
                }

                Log.Write("HANGAR OPEN");
                HangarState = HangarState.Open;
            }
            else
            {
                // ======= CLOSE DOOR =======

                MyAPIGateway.Utilities.ShowNotification("Hangar: Closing");

                foreach (var piston in pistons)
                {
                    piston.SetVelocity(-velocity);
                }

                Log.Write("HANGAR CLOSED");
                HangarState = HangarState.Closed;
            }
        }
        private void SwitchGearsLock(SpacePortGearsController ctrl, bool toOpen)
        {
            //Log.Write("There is {0} landing gears totally", Entities.LandingGears.Count);
            //Log.Write("There is {0} landing gears in group {1} totally", Entities.LandingGears.Count(p => p.HangarCode == _button.HangarCode), _button.HangarCode);

            var gears =
                Entities.LandingGears.Where(
                    p => p.Entity.CustomName.StartsWith(Constants.SpacePortPrefix, StringComparison.OrdinalIgnoreCase))
                .Where(p => p.HangarCode.Equals(_button.HangarCode, StringComparison.OrdinalIgnoreCase))
                .ToList();

            Log.Write("Got {0} landing gears, while button is {1}", gears.Count, _button.Entity.CustomName);

            if (toOpen)
            {
                MyAPIGateway.Utilities.ShowNotification("Landing Gears: Unlocking");

                foreach (var gear in gears)
                {
                    gear.Unlock();
                }

                Log.Write("HANGAR GEARS UNLOCKED");
                HangarGearsState = HangarState.Open;
            }
            else
            {
                MyAPIGateway.Utilities.ShowNotification("Landing Gears: Locking");

                foreach (var gear in gears)
                {
                    gear.Lock();
                }

                Log.Write("HANGAR GEARS LOCKED");
                HangarGearsState = HangarState.Closed;
            }
        }
    // Update is called once per frame
    void Update()
    {
        // If we don't have a plane or something has happened to it (was destroyed)
        // We set the state as idle and we have nothing else to do.
        if (planeComingToHangar == null)
        {
            currentState = HangarState.IDLE;
            return;
        }



        Vector3 deltaPosition = targetPosition - planeComingToHangar.transform.position;

        float currentDistanceToPlane = deltaPosition.magnitude;

        // We get how close we are to the "landing zone" from 0 to 1
        float normalizedDistance = (currentDistanceToPlane) / distanceToStartLanding;


        // Just in case, you never know with floating point stuff, C# and Unity
        // all trying to make something explode at the same time :)
        normalizedDistance = Mathf.Clamp(normalizedDistance, 0.0f, 1.0f);


        switch (currentState)
        {
        case HangarState.PLANE_COMING_IN:

            if (currentDistanceToPlane <= planeIsInsideThreshold)
            {     //Yeah, I know this cannot be lower than Zero
                  //If the plane is close enough then we set the state as inside and we stop rendering it

                this.currentState      = HangarState.PLANE_INSIDE;
                this.timeLeftRepairing = timeToRepair;
                this.planeComingToHangar.GetComponentInChildren <Renderer>().enabled = false;
                planeComingToHangar.GetComponent <PlaneTouchReciever>().DestroyTrail();

                repairingSource.PlayOneShot(repairingSound, 1.0f);
            }

            // We move it and point to to the right direction
            planeComingToHangar.transform.position = Vector3.Lerp(planeComingToHangar.transform.position, targetPosition, Time.deltaTime * landingSpeed);
            planeComingToHangar.transform.LookAt(this.transform);

            //  We scale the plane to make it seem like it's going down
            planeComingToHangar.transform.localScale = Vector3.Lerp(this.originalPlaneScale, Vector3.zero, 1.0f - normalizedDistance);

            timeToLand += Time.deltaTime;

            break;

        case HangarState.PLANE_INSIDE:
            timeLeftRepairing -= Time.deltaTime;
            if (timeLeftRepairing <= 0.0f)
            {
                //We have "repaired" the plane
                this.planeComingToHangar.GetComponent <AllyPlane>().Repair();

                //We make it visible
                this.planeComingToHangar.GetComponentInChildren <Renderer>().enabled = true;

                //And the plane can come out again
                this.currentState    = HangarState.PLANE_COMING_OUT;
                currentTimeTakingOff = 0.0f;

                //We now make it move in the original circle
                planeComingToHangar.GetComponent <PlaneTouchReciever>().ActivateHoldingPattern();
                planeComingToHangar.GetComponent <SplineInterpolator>().enabled = true;
            }

            break;

        case HangarState.PLANE_COMING_OUT:


            currentTimeTakingOff += Time.deltaTime;

            planeComingToHangar.transform.localScale = Vector3.Lerp(Vector3.zero, originalPlaneScale, currentTimeTakingOff / timeToLand);

            if (currentTimeTakingOff > timeToLand)
            {     //The plane just left completely from the hangar
                //And we come back to our default state
                ResetPlaneComingToHangar();
                currentState = HangarState.IDLE;

                //@@ TODO: See if we reach this state correctly and why can't we select the plane after taking off (also the null reference exception in 143)
            }

            break;


        case HangarState.IDLE:
        default:
            if (currentDistanceToPlane < distanceToStartLanding)
            {
                currentState            = HangarState.PLANE_COMING_IN;
                timeToLand             += 0.0f;
                this.originalPlaneScale = planeComingToHangar.transform.localScale;
                planeComingToHangar.GetComponent <SplineInterpolator>().enabled = false;
            }
            break;
        }
    }
Exemple #6
0
        IEnumerator <YieldInstruction> store_constructs()
        {
            if (FlightGlobals.fetch == null ||
                FlightGlobals.ActiveVessel == null ||
                packed_constructs.Count == 0)
            {
                yield break;
            }
            //wait for hangar.vessel to be loaded
            VesselWaiter self = new VesselWaiter(vessel);

            while (!self.launched)
            {
                yield return(new WaitForFixedUpdate());
            }
            while (!enabled)
            {
                yield return(new WaitForFixedUpdate());
            }
            //create vessels from constructs and store them
            HangarState cur_state = hangar_state; Deactivate();

            foreach (PackedConstruct pc in packed_constructs.Values)
            {
                remove_construct(pc);
                GetLaunchTransform();
                if (!pc.LoadConstruct())
                {
                    Utils.Log("PackedConstruct: unable to load ShipConstruct {0}. " +
                              "This usually means that some parts are missing " +
                              "or some modules failed to initialize.", pc.name);
                    ScreenMessager.showMessage(string.Format("Unable to load {0}", pc.name), 3);
                    continue;
                }
                ShipConstruction.PutShipToGround(pc.construct, launchTransform);
                ShipConstruction.AssembleForLaunch(pc.construct, "Hangar", pc.flag,
                                                   FlightDriver.FlightStateCache,
                                                   new VesselCrewManifest());
                VesselWaiter vsl = new VesselWaiter(FlightGlobals.Vessels[FlightGlobals.Vessels.Count - 1]);
                FlightGlobals.ForceSetActiveVessel(vsl.vessel);
                Staging.beginFlight();
                //wait for vsl to be launched
                while (!vsl.launched)
                {
                    yield return(new WaitForFixedUpdate());
                }
                store_vessel(vsl.vessel, false);
                //wait a 0.1 sec, otherwise the vessel may not be destroyed properly
                yield return(new WaitForSeconds(0.1f));
            }
            stored_mass = Utils.formatMass(vessels_mass);
            if (cur_state == HangarState.Active)
            {
                Activate();
            }
            //save game afterwards
            FlightGlobals.ForceSetActiveVessel(vessel);
            while (!self.launched)
            {
                yield return(null);
            }
            yield return(new WaitForSeconds(0.5f));

            GamePersistence.SaveGame("persistent", HighLogic.SaveFolder, SaveMode.OVERWRITE);
        }
            public void Update()
            {
                try
                {
                    string input = connector.CustomData;
                    connector.CustomData = string.Empty;

                    if (state == HangarState.Retracted)
                    {
                        for (int i = 0; i < lights.Count; i++)
                        {
                            lights[i].Color = Color.White;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < lights.Count; i++)
                        {
                            lights[i].Color = Color.Red;
                        }
                    }

                    if (state == HangarState.Extending)
                    {
                        if (input.Equals("Retract"))
                        {
                            state = HangarState.Retracting;
                        }

                        bool gatesOK  = true;
                        bool pistonOK = false;
                        for (int i = 0; i < gates.Count; i++)
                        {
                            if (gates[i].Status != DoorStatus.Open)
                            {
                                gatesOK = false;
                                gates[i].OpenDoor();
                            }
                        }

                        if (gatesOK)
                        {
                            extender.Velocity = 1;
                            if (extender.CurrentPosition == extender.MaxLimit)
                            {
                                pistonOK = true;
                            }
                        }

                        if (pistonOK)
                        {
                            state = HangarState.Extended;
                        }
                    }
                    else if (state == HangarState.Retracting)
                    {
                        if (input.Equals("Extend"))
                        {
                            state = HangarState.Extending;
                        }

                        bool rotorOK  = false;
                        bool pistonOK = false;
                        bool gatesOK  = false;

                        double targetAngle = getWorldBoxTargetAngle();
                        rotorOK = connector.Status != MyShipConnectorStatus.Connected || Math.Abs(AngleSubtract(rotor.Angle, targetAngle)) < 0.001;

                        if (!rotorOK)
                        {
                            MoveRotorTowards(targetAngle);
                        }

                        if (rotorOK)
                        {
                            rotor.TargetVelocityRad = 0;
                            extender.Velocity       = -1;
                            if (extender.CurrentPosition == extender.MinLimit)
                            {
                                pistonOK = true;
                            }
                        }

                        if (pistonOK)
                        {
                            gatesOK = true;
                            for (int i = 0; i < gates.Count; i++)
                            {
                                if (gates[i].Status != DoorStatus.Closed)
                                {
                                    gatesOK = false;
                                    gates[i].CloseDoor();
                                }
                            }
                        }

                        if (gatesOK)
                        {
                            state = HangarState.Retracted;
                        }
                    }
                    else if (state == HangarState.Calibrate)
                    {
                        bool rotorOK = false;

                        double targetAngle = getWorldBoxTargetAngle();
                        rotorOK = connector.Status != MyShipConnectorStatus.Connected || Math.Abs(AngleSubtract(rotor.Angle, targetAngle)) < 0.001;

                        if (!rotorOK)
                        {
                            MoveRotorTowards(targetAngle);
                        }

                        if (rotorOK)
                        {
                            rotor.TargetVelocityRad = 0;
                            state = HangarState.Extended;
                        }
                    }
                    else if (state == HangarState.Extended)
                    {
                        if (input.Equals("Retract"))
                        {
                            state = HangarState.Retracting;
                        }
                        if (input.Equals("Calibrate"))
                        {
                            state = HangarState.Calibrate;
                        }
                        if (input.Equals("Launch") && connector.Status == MyShipConnectorStatus.Connected)
                        {
                            connector.OtherConnector.CustomData = "Launch";
                        }
                    }
                    else if (state == HangarState.Retracted)
                    {
                        var angle = getWorldBoxTargetAngle();
                        if (Math.Abs(AngleSubtract(rotor.Angle, angle)) > 0.001)
                        {
                            MoveRotorTowards(angle);
                        }
                        if (input.Equals("Extend"))
                        {
                            state = HangarState.Extending;
                        }
                    }
                }
                catch
                {
                }

                display.ClearImagesFromSelection();
                display.WriteText(GetStatus());
            }