コード例 #1
0
        public int GetDir(DockingPort dockingTarget = null)
        {
            if (DockingDir != 0)
            {
                return(DockingDir);
            }

            if (door != null)
            {
                if (door.LinkedGap.linkedTo.Count == 1)
                {
                    return(IsHorizontal ?
                           Math.Sign(door.Item.WorldPosition.X - door.LinkedGap.linkedTo[0].WorldPosition.X) :
                           Math.Sign(door.Item.WorldPosition.Y - door.LinkedGap.linkedTo[0].WorldPosition.Y));
                }
            }
            if (dockingTarget != null)
            {
                return(IsHorizontal ?
                       Math.Sign(dockingTarget.item.WorldPosition.X - item.WorldPosition.X) :
                       Math.Sign(dockingTarget.item.WorldPosition.Y - item.WorldPosition.Y));
            }
            if (item.Submarine != null)
            {
                return(IsHorizontal ?
                       Math.Sign(item.WorldPosition.X - item.Submarine.WorldPosition.X) :
                       Math.Sign(item.WorldPosition.Y - item.Submarine.WorldPosition.Y));
            }

            return(0);
        }
コード例 #2
0
ファイル: DockingPort.cs プロジェクト: darthsmiley/Barotrauma
        public void ClientRead(ServerNetObject type, Lidgren.Network.NetBuffer msg, float sendingTime)
        {
            bool isDocked = msg.ReadBoolean();

            for (int i = 0; i < 2; i++)
            {
                if (hulls[i] == null)
                {
                    continue;
                }
                item.linkedTo.Remove(hulls[i]);
                hulls[i].Remove();
                hulls[i] = null;
            }

            if (gap != null)
            {
                item.linkedTo.Remove(gap);
                gap.Remove();
                gap = null;
            }

            if (isDocked)
            {
                ushort dockingTargetID = msg.ReadUInt16();

                bool isLocked = msg.ReadBoolean();

                Entity targetEntity = Entity.FindEntityByID(dockingTargetID);
                if (targetEntity == null || !(targetEntity is Item))
                {
                    DebugConsole.ThrowError("Invalid docking port network event (can't dock to " + targetEntity.ToString() + ")");
                    return;
                }

                dockingTarget = (targetEntity as Item).GetComponent <DockingPort>();
                if (dockingTarget == null)
                {
                    DebugConsole.ThrowError("Invalid docking port network event (" + targetEntity + " doesn't have a docking port component)");
                    return;
                }

                Dock(dockingTarget);

                if (isLocked)
                {
                    Lock(true);
                }
            }
            else
            {
                Undock();
            }
        }
コード例 #3
0
ファイル: DockingPort.cs プロジェクト: parhelia512/Barotrauma
        public override void ReceiveSignal(Signal signal, Connection connection)
        {
            if (GameMain.NetworkMember != null && GameMain.NetworkMember.IsClient)
            {
                return;
            }

            if (dockingCooldown > 0.0f)
            {
                return;
            }

            bool        wasDocked         = docked;
            DockingPort prevDockingTarget = DockingTarget;

            switch (connection.Name)
            {
            case "toggle":
                if (signal.value != "0")
                {
                    Docked = !docked;
                }
                break;

            case "set_active":
            case "set_state":
                Docked = signal.value != "0";
                break;
            }

#if SERVER
            if (signal.sender != null && docked != wasDocked)
            {
                if (docked)
                {
                    if (item.Submarine != null && DockingTarget?.item?.Submarine != null)
                    {
                        GameServer.Log(GameServer.CharacterLogName(signal.sender) + " docked " + item.Submarine.Info.Name + " to " + DockingTarget.item.Submarine.Info.Name, ServerLog.MessageType.ItemInteraction);
                    }
                }
                else
                {
                    if (item.Submarine != null && prevDockingTarget?.item?.Submarine != null)
                    {
                        GameServer.Log(GameServer.CharacterLogName(signal.sender) + " undocked " + item.Submarine.Info.Name + " from " + prevDockingTarget.item.Submarine.Info.Name, ServerLog.MessageType.ItemInteraction);
                    }
                }
            }
#endif
        }
コード例 #4
0
ファイル: DockingPort.cs プロジェクト: jeffercize/Barotrauma
        public void Dock(DockingPort target)
        {
            if (item.Submarine.DockedTo.Contains(target.item.Submarine)) return;

            forceLockTimer = 0.0f;

            if (DockingTarget != null)
            {
                Undock();
            }         

            if (target.item.Submarine == item.Submarine)
            {
                DebugConsole.ThrowError("Error - tried to dock a submarine to itself");
                DockingTarget = null;
                return;
            }

            target.InitializeLinks();

            if (!item.linkedTo.Contains(target.item)) item.linkedTo.Add(target.item);
            if (!target.item.linkedTo.Contains(item)) target.item.linkedTo.Add(item);

            if (!target.item.Submarine.DockedTo.Contains(item.Submarine)) target.item.Submarine.ConnectedDockingPorts.Add(item.Submarine, target);
            if (!item.Submarine.DockedTo.Contains(target.item.Submarine)) item.Submarine.ConnectedDockingPorts.Add(target.item.Submarine, this);

            DockingTarget = target;
            DockingTarget.DockingTarget = this;

            docked = true;
            DockingTarget.Docked = true;

            if (Character.Controlled != null &&
                (Character.Controlled.Submarine == DockingTarget.item.Submarine || Character.Controlled.Submarine == item.Submarine))
            {
                GameMain.GameScreen.Cam.Shake = Vector2.Distance(DockingTarget.item.Submarine.Velocity, item.Submarine.Velocity);
            }

            DockingDir = GetDir(DockingTarget);
            DockingTarget.DockingDir = -DockingDir;
           
            CreateJoint(false);

#if SERVER
            if (GameMain.Server != null && (!item.Submarine?.Loading ?? true))
            {
                item.CreateServerEvent(this);
            }
#endif
        }
コード例 #5
0
        public void ClientRead(ServerNetObject type, Lidgren.Network.NetBuffer msg, float sendingTime)
        {
            bool isDocked = msg.ReadBoolean();

            if (isDocked)
            {
                ushort dockingTargetID = msg.ReadUInt16();

                bool isLocked = msg.ReadBoolean();

                if (isLocked)
                {
                    hullIds[0] = msg.ReadUInt16();
                    hullIds[1] = msg.ReadUInt16();
                    gapId      = msg.ReadUInt16();
                }

                Entity targetEntity = Entity.FindEntityByID(dockingTargetID);
                if (targetEntity == null || !(targetEntity is Item))
                {
                    DebugConsole.ThrowError("Invalid docking port network event (can't dock to " + targetEntity.ToString() + ")");
                    return;
                }

                dockingTarget = (targetEntity as Item).GetComponent <DockingPort>();
                if (dockingTarget == null)
                {
                    DebugConsole.ThrowError("Invalid docking port network event (" + targetEntity + " doesn't have a docking port component)");
                    return;
                }

                Dock(dockingTarget);

                if (isLocked)
                {
                    Lock(true);

                    hulls[0].ID = (ushort)hullIds[0];
                    hulls[1].ID = (ushort)hullIds[1];
                    gap.ID      = (ushort)gapId;
                }
            }
            else
            {
                Undock();
            }
        }
コード例 #6
0
        public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item source, Character sender, float power = 0.0f, float signalStrength = 1.0f)
        {
#if CLIENT
            if (GameMain.Client != null)
            {
                return;
            }
#endif

            bool        wasDocked         = docked;
            DockingPort prevDockingTarget = DockingTarget;

            switch (connection.Name)
            {
            case "toggle":
                Docked = !docked;
                break;

            case "set_active":
            case "set_state":
                Docked = signal != "0";
                break;
            }

#if SERVER
            if (sender != null && docked != wasDocked)
            {
                if (docked)
                {
                    if (item.Submarine != null && DockingTarget?.item?.Submarine != null)
                    {
                        GameServer.Log(sender.LogName + " docked " + item.Submarine.Name + " to " + DockingTarget.item.Submarine.Name, ServerLog.MessageType.ItemInteraction);
                    }
                }
                else
                {
                    if (item.Submarine != null && prevDockingTarget?.item?.Submarine != null)
                    {
                        GameServer.Log(sender.LogName + " undocked " + item.Submarine.Name + " from " + prevDockingTarget.item.Submarine.Name, ServerLog.MessageType.ItemInteraction);
                    }
                }
            }
#endif
        }
コード例 #7
0
ファイル: DockingPort.cs プロジェクト: parhelia512/Barotrauma
        public int GetDir(DockingPort dockingTarget = null)
        {
            int forcedDockingDir = GetForcedDockingDir();

            if (forcedDockingDir != 0)
            {
                return(forcedDockingDir);
            }
            if (dockingTarget != null)
            {
                forcedDockingDir = -dockingTarget.GetForcedDockingDir();
                if (forcedDockingDir != 0)
                {
                    return(forcedDockingDir);
                }
            }

            if (DockingDir != 0)
            {
                return(DockingDir);
            }

            if (Door != null && Door.LinkedGap.linkedTo.Count > 0)
            {
                Hull  refHull         = null;
                float largestHullSize = 0.0f;
                foreach (MapEntity linked in Door.LinkedGap.linkedTo)
                {
                    if (!(linked is Hull hull))
                    {
                        continue;
                    }
                    if (hull.Volume > largestHullSize)
                    {
                        refHull         = hull;
                        largestHullSize = hull.Volume;
                    }
                }
                if (refHull != null)
                {
                    return(IsHorizontal ?
                           Math.Sign(Door.Item.WorldPosition.X - refHull.WorldPosition.X) :
                           Math.Sign(Door.Item.WorldPosition.Y - refHull.WorldPosition.Y));
                }
            }
            if (dockingTarget?.Door?.LinkedGap != null && dockingTarget.Door.LinkedGap.linkedTo.Count > 0)
            {
                Hull  refHull         = null;
                float largestHullSize = 0.0f;
                foreach (MapEntity linked in dockingTarget.Door.LinkedGap.linkedTo)
                {
                    if (!(linked is Hull hull))
                    {
                        continue;
                    }
                    if (hull.Volume > largestHullSize)
                    {
                        refHull         = hull;
                        largestHullSize = hull.Volume;
                    }
                }
                if (refHull != null)
                {
                    return(IsHorizontal ?
                           Math.Sign(refHull.WorldPosition.X - dockingTarget.Door.Item.WorldPosition.X) :
                           Math.Sign(refHull.WorldPosition.Y - dockingTarget.Door.Item.WorldPosition.Y));
                }
            }
            if (dockingTarget != null)
            {
                int dir = IsHorizontal ?
                          Math.Sign(dockingTarget.item.WorldPosition.X - item.WorldPosition.X) :
                          Math.Sign(dockingTarget.item.WorldPosition.Y - item.WorldPosition.Y);
                if (dir != 0)
                {
                    return(dir);
                }
            }
            if (item.Submarine != null)
            {
                return(IsHorizontal ?
                       Math.Sign(item.WorldPosition.X - item.Submarine.WorldPosition.X) :
                       Math.Sign(item.WorldPosition.Y - item.Submarine.WorldPosition.Y));
            }

            return(0);
        }
コード例 #8
0
ファイル: Steering.cs プロジェクト: timgagliardi/Barotrauma
        public override void UpdateHUD(Character character, float deltaTime, Camera cam)
        {
            if (swapDestinationOrder == null)
            {
                swapDestinationOrder = item.Submarine != null && item.Submarine.FlippedX;
                if (swapDestinationOrder.Value)
                {
                    levelStartTickBox.RectTransform.SetAsLastChild();
                }
            }

            if (steerArea.Rect.Contains(PlayerInput.MousePosition))
            {
                if (!PlayerInput.KeyDown(InputType.Deselect) && !PlayerInput.KeyHit(InputType.Deselect))
                {
                    Character.DisableControls = true;
                }
            }

            dockingContainer.Visible = DockingModeEnabled;
            statusContainer.Visible  = !DockingModeEnabled;
            if (!DockingModeEnabled)
            {
                enterOutpostPrompt?.Close();
            }

            if (DockingModeEnabled && ActiveDockingSource != null)
            {
                if (Math.Abs(ActiveDockingSource.Item.WorldPosition.X - DockingTarget.Item.WorldPosition.X) < ActiveDockingSource.DistanceTolerance.X &&
                    Math.Abs(ActiveDockingSource.Item.WorldPosition.Y - DockingTarget.Item.WorldPosition.Y) < ActiveDockingSource.DistanceTolerance.Y)
                {
                    dockingButton.Text = dockText;
                    if (dockingButton.FlashTimer <= 0.0f)
                    {
                        dockingButton.Flash(GUI.Style.Blue, 0.5f, useCircularFlash: true);
                        dockingButton.Pulsate(Vector2.One, Vector2.One * 1.2f, dockingButton.FlashTimer);
                    }
                }
                else
                {
                    enterOutpostPrompt?.Close();
                }
            }
            else if (DockingSources.Any(d => d.Docked))
            {
                dockingButton.Text       = undockText;
                dockingContainer.Visible = true;
                statusContainer.Visible  = false;
                if (dockingButton.FlashTimer <= 0.0f)
                {
                    dockingButton.Flash(GUI.Style.Orange, useCircularFlash: true);
                    dockingButton.Pulsate(Vector2.One, Vector2.One * 1.2f, dockingButton.FlashTimer);
                }
            }
            else
            {
                dockingButton.Text = dockText;
            }

            if (Voltage < MinVoltage)
            {
                tipContainer.Visible = true;
                tipContainer.Text    = noPowerTip;
                return;
            }

            tipContainer.Visible = AutoPilot;
            if (AutoPilot)
            {
                if (maintainPos)
                {
                    tipContainer.Text = autoPilotMaintainPosTip;
                }
                else if (LevelStartSelected)
                {
                    tipContainer.Text = autoPilotLevelStartTip;
                }
                else if (LevelEndSelected)
                {
                    tipContainer.Text = autoPilotLevelEndTip;
                }

                if (DockingModeEnabled && DockingTarget != null)
                {
                    posToMaintain += ConvertUnits.ToDisplayUnits(DockingTarget.Item.Submarine.Velocity) * deltaTime;
                }
            }

            pressureWarningText.Visible = item.Submarine != null && item.Submarine.AtDamageDepth && Timing.TotalTime % 1.0f < 0.5f;

            if (Vector2.DistanceSquared(PlayerInput.MousePosition, steerArea.Rect.Center.ToVector2()) < steerRadius * steerRadius)
            {
                if (PlayerInput.PrimaryMouseButtonHeld() && !CrewManager.IsCommandInterfaceOpen && !GameSession.IsTabMenuOpen &&
                    (!GameMain.GameSession?.Campaign?.ShowCampaignUI ?? true) && !GUIMessageBox.MessageBoxes.Any())
                {
                    Vector2 inputPos = PlayerInput.MousePosition - steerArea.Rect.Center.ToVector2();
                    inputPos.Y = -inputPos.Y;
                    if (AutoPilot && !LevelStartSelected && !LevelEndSelected)
                    {
                        posToMaintain = controlledSub != null ?
                                        controlledSub.WorldPosition + inputPos / sonar.DisplayRadius * sonar.Range / sonar.Zoom :
                                        item.Submarine == null ? item.WorldPosition : item.Submarine.WorldPosition;
                    }
                    else
                    {
                        SteeringInput = inputPos;
                    }
                    unsentChanges = true;
                    user          = Character.Controlled;
                }
            }
            if (!AutoPilot && Character.DisableControls && GUI.KeyboardDispatcher.Subscriber == null)
            {
                steeringAdjustSpeed = character == null ? 0.2f : MathHelper.Lerp(0.2f, 1.0f, character.GetSkillLevel("helm") / 100.0f);
                Vector2 input = Vector2.Zero;
                if (PlayerInput.KeyDown(InputType.Left))
                {
                    input -= Vector2.UnitX;
                }
                if (PlayerInput.KeyDown(InputType.Right))
                {
                    input += Vector2.UnitX;
                }
                if (PlayerInput.KeyDown(InputType.Up))
                {
                    input += Vector2.UnitY;
                }
                if (PlayerInput.KeyDown(InputType.Down))
                {
                    input -= Vector2.UnitY;
                }
                if (PlayerInput.KeyDown(InputType.Run))
                {
                    SteeringInput  += input * deltaTime * 200;
                    inputCumulation = 0;
                    keyboardInput   = Vector2.Zero;
                    unsentChanges   = true;
                }
                else
                {
                    float step = deltaTime * 5;
                    if (input.Length() > 0)
                    {
                        inputCumulation += step;
                    }
                    else
                    {
                        inputCumulation -= step;
                    }
                    float maxCumulation = 1;
                    inputCumulation = MathHelper.Clamp(inputCumulation, 0, maxCumulation);
                    float length          = MathHelper.Lerp(0, 0.2f, MathUtils.InverseLerp(0, maxCumulation, inputCumulation));
                    var   normalizedInput = Vector2.Normalize(input);
                    if (MathUtils.IsValid(normalizedInput))
                    {
                        keyboardInput += normalizedInput * length;
                    }
                    if (keyboardInput.LengthSquared() > 0.01f)
                    {
                        SteeringInput += keyboardInput;
                        unsentChanges  = true;
                        user           = Character.Controlled;
                        keyboardInput *= MathHelper.Clamp(1 - step, 0, 1);
                    }
                }
            }
            else
            {
                inputCumulation = 0;
                keyboardInput   = Vector2.Zero;
            }

            if (!UseAutoDocking)
            {
                return;
            }

            if (checkConnectedPortsTimer <= 0.0f)
            {
                Connection dockingConnection = item.Connections?.FirstOrDefault(c => c.Name == "toggle_docking");
                if (dockingConnection != null)
                {
                    connectedPorts = item.GetConnectedComponentsRecursive <DockingPort>(dockingConnection);
                }
                checkConnectedPortsTimer = CheckConnectedPortsInterval;
            }
            else
            {
                checkConnectedPortsTimer -= deltaTime;
            }

            DockingModeEnabled = false;

            if (connectedPorts.None())
            {
                return;
            }

            float closestDist = DockingAssistThreshold * DockingAssistThreshold;

            foreach (DockingPort sourcePort in connectedPorts)
            {
                if (sourcePort.Docked || sourcePort.Item.Submarine == null)
                {
                    continue;
                }
                if (sourcePort.Item.Submarine != controlledSub)
                {
                    continue;
                }

                int sourceDir = sourcePort.GetDir();

                foreach (DockingPort targetPort in DockingPort.List)
                {
                    if (targetPort.Docked || targetPort.Item.Submarine == null)
                    {
                        continue;
                    }
                    if (targetPort.Item.Submarine == controlledSub || targetPort.IsHorizontal != sourcePort.IsHorizontal)
                    {
                        continue;
                    }
                    if (targetPort.Item.Submarine.DockedTo?.Contains(sourcePort.Item.Submarine) ?? false)
                    {
                        continue;
                    }
                    if (Level.Loaded != null && targetPort.Item.Submarine.WorldPosition.Y > Level.Loaded.Size.Y)
                    {
                        continue;
                    }
                    if (sourceDir == targetPort.GetDir())
                    {
                        continue;
                    }

                    float dist = Vector2.DistanceSquared(sourcePort.Item.WorldPosition, targetPort.Item.WorldPosition);
                    if (dist < closestDist)
                    {
                        closestDist         = dist;
                        DockingModeEnabled  = true;
                        ActiveDockingSource = sourcePort;
                        DockingTarget       = targetPort;
                    }
                }
            }
        }
コード例 #9
0
        public void Dock(DockingPort target)
        {
            if (item.Submarine.DockedTo.Contains(target.item.Submarine))
            {
                return;
            }

            forceLockTimer = 0.0f;

            if (DockingTarget != null)
            {
                Undock();
            }

            if (target.item.Submarine == item.Submarine)
            {
                DebugConsole.ThrowError("Error - tried to dock a submarine to itself");
                DockingTarget = null;
                return;
            }

            target.InitializeLinks();

            if (!item.linkedTo.Contains(target.item))
            {
                item.linkedTo.Add(target.item);
            }
            if (!target.item.linkedTo.Contains(item))
            {
                target.item.linkedTo.Add(item);
            }

            if (!target.item.Submarine.DockedTo.Contains(item.Submarine))
            {
                target.item.Submarine.DockedTo.Add(item.Submarine);
            }
            if (!item.Submarine.DockedTo.Contains(target.item.Submarine))
            {
                item.Submarine.DockedTo.Add(target.item.Submarine);
            }

            DockingTarget = target;
            DockingTarget.DockingTarget = this;

            docked = true;
            DockingTarget.Docked = true;

            if (Character.Controlled != null &&
                (Character.Controlled.Submarine == DockingTarget.item.Submarine || Character.Controlled.Submarine == item.Submarine))
            {
                GameMain.GameScreen.Cam.Shake = Vector2.Distance(DockingTarget.item.Submarine.Velocity, item.Submarine.Velocity);
            }

            DockingDir = GetDir(DockingTarget);
            DockingTarget.DockingDir = -DockingDir;

            if (door != null && DockingTarget.door != null)
            {
                WayPoint myWayPoint     = WayPoint.WayPointList.Find(wp => door.LinkedGap == wp.ConnectedGap);
                WayPoint targetWayPoint = WayPoint.WayPointList.Find(wp => DockingTarget.door.LinkedGap == wp.ConnectedGap);

                if (myWayPoint != null && targetWayPoint != null)
                {
                    myWayPoint.linkedTo.Add(targetWayPoint);
                    targetWayPoint.linkedTo.Add(myWayPoint);
                }
            }

            CreateJoint(false);

#if SERVER
            if (GameMain.Server != null)
            {
                item.CreateServerEvent(this);
            }
#endif
        }
コード例 #10
0
ファイル: Steering.cs プロジェクト: juanjp600/Barotrauma
        public override void UpdateHUD(Character character, float deltaTime, Camera cam)
        {
            if (steerArea.Rect.Contains(PlayerInput.MousePosition))
            {
                if (!PlayerInput.KeyDown(InputType.Select) && !PlayerInput.KeyHit(InputType.Select))
                {
                    Character.DisableControls = true;
                }
            }

            autoPilotControlsDisabler.Visible = !AutoPilot;

            if (voltage < minVoltage && currPowerConsumption > 0.0f)
            {
                tipContainer.Visible = true;
                tipContainer.Text    = noPowerTip;
                return;
            }

            tipContainer.Visible = AutoPilot;
            if (AutoPilot)
            {
                if (maintainPos)
                {
                    tipContainer.Text = autoPilotMaintainPosTip;
                }
                else if (LevelStartSelected)
                {
                    tipContainer.Text = autoPilotLevelStartTip;
                }
                else if (LevelEndSelected)
                {
                    tipContainer.Text = autoPilotLevelEndTip;
                }

                if (DockingModeEnabled && DockingTarget != null)
                {
                    posToMaintain += ConvertUnits.ToDisplayUnits(DockingTarget.Item.Submarine.Velocity) * deltaTime;
                }
            }

            pressureWarningText.Visible = item.Submarine != null && item.Submarine.AtDamageDepth && Timing.TotalTime % 1.0f < 0.5f;

            if (Vector2.Distance(PlayerInput.MousePosition, steerArea.Rect.Center.ToVector2()) < steerArea.Rect.Width / 2)
            {
                if (PlayerInput.LeftButtonHeld())
                {
                    Vector2 inputPos = PlayerInput.MousePosition - steerArea.Rect.Center.ToVector2();
                    inputPos.Y = -inputPos.Y;
                    if (AutoPilot && !LevelStartSelected && !LevelEndSelected)
                    {
                        posToMaintain = controlledSub == null ?
                                        item.WorldPosition :
                                        controlledSub.WorldPosition + (sonar.DisplayOffset * sonar.Zoom) + inputPos / sonar.DisplayRadius * sonar.Range / sonar.Zoom;
                    }
                    else
                    {
                        SteeringInput = inputPos;
                    }
                    unsentChanges = true;
                    user          = Character.Controlled;
                }
            }
            if (!AutoPilot && Character.DisableControls && GUI.KeyboardDispatcher.Subscriber == null)
            {
                steeringAdjustSpeed = character == null ? 0.2f : MathHelper.Lerp(0.2f, 1.0f, character.GetSkillLevel("helm") / 100.0f);
                Vector2 input = Vector2.Zero;
                if (PlayerInput.KeyDown(InputType.Left))
                {
                    input -= Vector2.UnitX;
                }
                if (PlayerInput.KeyDown(InputType.Right))
                {
                    input += Vector2.UnitX;
                }
                if (PlayerInput.KeyDown(InputType.Up))
                {
                    input += Vector2.UnitY;
                }
                if (PlayerInput.KeyDown(InputType.Down))
                {
                    input -= Vector2.UnitY;
                }
                if (PlayerInput.KeyDown(Keys.LeftShift))
                {
                    SteeringInput  += input * deltaTime * 200;
                    inputCumulation = 0;
                    keyboardInput   = Vector2.Zero;
                    unsentChanges   = true;
                }
                else
                {
                    float step = deltaTime * 5;
                    if (input.Length() > 0)
                    {
                        inputCumulation += step;
                    }
                    else
                    {
                        inputCumulation -= step;
                    }
                    float maxCumulation = 1;
                    inputCumulation = MathHelper.Clamp(inputCumulation, 0, maxCumulation);
                    float length          = MathHelper.Lerp(0, 0.2f, MathUtils.InverseLerp(0, maxCumulation, inputCumulation));
                    var   normalizedInput = Vector2.Normalize(input);
                    if (MathUtils.IsValid(normalizedInput))
                    {
                        keyboardInput += normalizedInput * length;
                    }
                    if (keyboardInput.LengthSquared() > 0.01f)
                    {
                        SteeringInput += keyboardInput;
                        unsentChanges  = true;
                        user           = Character.Controlled;
                        keyboardInput *= MathHelper.Clamp(1 - step, 0, 1);
                    }
                }
            }
            else
            {
                inputCumulation = 0;
                keyboardInput   = Vector2.Zero;
            }


            float closestDist = DockingAssistThreshold * DockingAssistThreshold;

            DockingModeEnabled = false;
            DockingSource      = null;
            DockingTarget      = null;
            foreach (DockingPort sourcePort in DockingPort.List)
            {
                if (sourcePort.Docked || sourcePort.Item.Submarine == null)
                {
                    continue;
                }
                if (sourcePort.Item.Submarine != controlledSub)
                {
                    continue;
                }

                int sourceDir = sourcePort.IsHorizontal ?
                                Math.Sign(sourcePort.Item.WorldPosition.X - sourcePort.Item.Submarine.WorldPosition.X) :
                                Math.Sign(sourcePort.Item.WorldPosition.Y - sourcePort.Item.Submarine.WorldPosition.Y);

                foreach (DockingPort targetPort in DockingPort.List)
                {
                    if (targetPort.Docked || targetPort.Item.Submarine == null)
                    {
                        continue;
                    }
                    if (targetPort.Item.Submarine == controlledSub || targetPort.IsHorizontal != sourcePort.IsHorizontal)
                    {
                        continue;
                    }
                    if (Level.Loaded != null && targetPort.Item.Submarine.WorldPosition.Y > Level.Loaded.Size.Y)
                    {
                        continue;
                    }

                    int targetDir = targetPort.IsHorizontal ?
                                    Math.Sign(targetPort.Item.WorldPosition.X - targetPort.Item.Submarine.WorldPosition.X) :
                                    Math.Sign(targetPort.Item.WorldPosition.Y - targetPort.Item.Submarine.WorldPosition.Y);

                    if (sourceDir == targetDir)
                    {
                        continue;
                    }

                    float dist = Vector2.DistanceSquared(sourcePort.Item.WorldPosition, targetPort.Item.WorldPosition);
                    if (dist < closestDist)
                    {
                        DockingModeEnabled = true;
                        DockingSource      = sourcePort;
                        DockingTarget      = targetPort;
                    }
                }
            }
        }
コード例 #11
0
        public void Undock()
        {
            if (dockingTarget == null || !docked)
            {
                return;
            }

#if CLIENT
            PlaySound(ActionType.OnUse, item.WorldPosition);
#endif

            dockingTarget.item.Submarine.DockedTo.Remove(item.Submarine);
            item.Submarine.DockedTo.Remove(dockingTarget.item.Submarine);

            //remove all waypoint links between this sub and the dockingtarget
            foreach (WayPoint wp in WayPoint.WayPointList)
            {
                if (wp.Submarine != item.Submarine || wp.SpawnType != SpawnType.Path)
                {
                    continue;
                }

                for (int i = wp.linkedTo.Count - 1; i >= 0; i--)
                {
                    var wp2 = wp.linkedTo[i] as WayPoint;
                    if (wp2 == null)
                    {
                        continue;
                    }

                    if (wp.Submarine == dockingTarget.item.Submarine)
                    {
                        wp.linkedTo.RemoveAt(i);
                        wp2.linkedTo.Remove(wp);
                    }
                }
            }

            item.linkedTo.Clear();

            docked = false;

            dockingTarget.Undock();
            dockingTarget = null;

            if (doorBody != null)
            {
                GameMain.World.RemoveBody(doorBody);
                doorBody = null;
            }

            var wire = item.GetComponent <Wire>();
            if (wire != null)
            {
                wire.Drop(null);
            }

            if (joint != null)
            {
                GameMain.World.RemoveJoint(joint);
                joint = null;
            }

            if (hulls != null)
            {
                hulls[0].Remove();
                hulls[1].Remove();
                hulls = null;
            }

            if (gap != null)
            {
                gap.Remove();
                gap = null;
            }

            hullIds[0] = null;
            hullIds[1] = null;

            gapId = null;

            if (bodies != null)
            {
                foreach (Body body in bodies)
                {
                    if (body == null)
                    {
                        continue;
                    }
                    GameMain.World.RemoveBody(body);
                }
                bodies = null;
            }

            if (GameMain.Server != null)
            {
                item.CreateServerEvent(this);
            }
        }
コード例 #12
0
        public void Dock(DockingPort target)
        {
            if (item.Submarine.DockedTo.Contains(target.item.Submarine))
            {
                return;
            }

            if (dockingTarget != null)
            {
                Undock();
            }

            if (target.item.Submarine == item.Submarine)
            {
                DebugConsole.ThrowError("Error - tried to dock a submarine to itself");
                dockingTarget = null;
                return;
            }

#if CLIENT
            PlaySound(ActionType.OnUse, item.WorldPosition);
#endif

            if (!item.linkedTo.Contains(target.item))
            {
                item.linkedTo.Add(target.item);
            }
            if (!target.item.linkedTo.Contains(item))
            {
                target.item.linkedTo.Add(item);
            }

            if (!target.item.Submarine.DockedTo.Contains(item.Submarine))
            {
                target.item.Submarine.DockedTo.Add(item.Submarine);
            }
            if (!item.Submarine.DockedTo.Contains(target.item.Submarine))
            {
                item.Submarine.DockedTo.Add(target.item.Submarine);
            }

            dockingTarget = target;
            dockingTarget.dockingTarget = this;

            docked = true;
            dockingTarget.Docked = true;

            if (Character.Controlled != null &&
                (Character.Controlled.Submarine == dockingTarget.item.Submarine || Character.Controlled.Submarine == item.Submarine))
            {
                GameMain.GameScreen.Cam.Shake = Vector2.Distance(dockingTarget.item.Submarine.Velocity, item.Submarine.Velocity);
            }

            dockingDir = IsHorizontal ?
                         Math.Sign(dockingTarget.item.WorldPosition.X - item.WorldPosition.X) :
                         Math.Sign(dockingTarget.item.WorldPosition.Y - item.WorldPosition.Y);
            dockingTarget.dockingDir = -dockingDir;


            foreach (WayPoint wp in WayPoint.WayPointList)
            {
                if (wp.Submarine != item.Submarine || wp.SpawnType != SpawnType.Path)
                {
                    continue;
                }

                if (!Submarine.RectContains(item.Rect, wp.Position))
                {
                    continue;
                }

                foreach (WayPoint wp2 in WayPoint.WayPointList)
                {
                    if (wp2.Submarine != dockingTarget.item.Submarine || wp2.SpawnType != SpawnType.Path)
                    {
                        continue;
                    }

                    if (!Submarine.RectContains(dockingTarget.item.Rect, wp2.Position))
                    {
                        continue;
                    }

                    wp.linkedTo.Add(wp2);
                    wp2.linkedTo.Add(wp);
                }
            }

            CreateJoint(false);

            if (GameMain.Server != null)
            {
                item.CreateServerEvent(this);
            }
        }
コード例 #13
0
        public override void UpdateHUD(Character character, float deltaTime, Camera cam)
        {
            if (swapDestinationOrder == null)
            {
                swapDestinationOrder = item.Submarine != null && item.Submarine.FlippedX;
                if (swapDestinationOrder.Value)
                {
                    levelStartTickBox.RectTransform.SetAsLastChild();
                }
            }

            if (!searchedConnectedDockingPort)
            {
                FindConnectedDockingPort();
                searchedConnectedDockingPort = true;
            }

            if (steerArea.Rect.Contains(PlayerInput.MousePosition))
            {
                if (!PlayerInput.KeyDown(InputType.Deselect) && !PlayerInput.KeyHit(InputType.Deselect))
                {
                    Character.DisableControls = true;
                }
            }

            dockingContainer.Visible = DockingModeEnabled;
            statusContainer.Visible  = !DockingModeEnabled;

            if (DockingModeEnabled && ActiveDockingSource != null)
            {
                if (Math.Abs(ActiveDockingSource.Item.WorldPosition.X - DockingTarget.Item.WorldPosition.X) < ActiveDockingSource.DistanceTolerance.X &&
                    Math.Abs(ActiveDockingSource.Item.WorldPosition.Y - DockingTarget.Item.WorldPosition.Y) < ActiveDockingSource.DistanceTolerance.Y)
                {
                    dockingButton.Text = dockText;
                    if (dockingButton.FlashTimer <= 0.0f)
                    {
                        dockingButton.Flash(Color.LightGreen, 0.5f);
                        dockingButton.Pulsate(Vector2.One, Vector2.One * 1.2f, dockingButton.FlashTimer);
                    }
                }
            }
            else if (DockingSources.Any(d => d.Docked))
            {
                dockingButton.Text       = undockText;
                dockingContainer.Visible = true;
                statusContainer.Visible  = false;
                if (dockingButton.FlashTimer <= 0.0f)
                {
                    dockingButton.Flash(Color.OrangeRed);
                    dockingButton.Pulsate(Vector2.One, Vector2.One * 1.2f, dockingButton.FlashTimer);
                }
            }
            else
            {
                dockingButton.Text = dockText;
            }

            autoPilotControlsDisabler.Visible = !AutoPilot;

            if (voltage < minVoltage && currPowerConsumption > 0.0f)
            {
                tipContainer.Visible = true;
                tipContainer.Text    = noPowerTip;
                return;
            }

            tipContainer.Visible = AutoPilot;
            if (AutoPilot)
            {
                if (maintainPos)
                {
                    tipContainer.Text = autoPilotMaintainPosTip;
                }
                else if (LevelStartSelected)
                {
                    tipContainer.Text = autoPilotLevelStartTip;
                }
                else if (LevelEndSelected)
                {
                    tipContainer.Text = autoPilotLevelEndTip;
                }

                if (DockingModeEnabled && DockingTarget != null)
                {
                    posToMaintain += ConvertUnits.ToDisplayUnits(DockingTarget.Item.Submarine.Velocity) * deltaTime;
                }
            }

            pressureWarningText.Visible = item.Submarine != null && item.Submarine.AtDamageDepth && Timing.TotalTime % 1.0f < 0.5f;

            if (Vector2.Distance(PlayerInput.MousePosition, steerArea.Rect.Center.ToVector2()) < steerArea.Rect.Width / 2)
            {
                if (PlayerInput.LeftButtonHeld())
                {
                    Vector2 displaySubPos = (-sonar.DisplayOffset * sonar.Zoom) / sonar.Range * sonar.DisplayRadius * sonar.Zoom;
                    displaySubPos.Y = -displaySubPos.Y;
                    displaySubPos   = steerArea.Rect.Center.ToVector2() + displaySubPos;

                    Vector2 inputPos = PlayerInput.MousePosition - displaySubPos;
                    inputPos.Y = -inputPos.Y;
                    if (AutoPilot && !LevelStartSelected && !LevelEndSelected)
                    {
                        posToMaintain = controlledSub != null ?
                                        controlledSub.WorldPosition + inputPos / sonar.DisplayRadius * sonar.Range / sonar.Zoom :
                                        item.Submarine == null ? item.WorldPosition : item.Submarine.WorldPosition;
                    }
                    else
                    {
                        SteeringInput = inputPos;
                    }
                    unsentChanges = true;
                    user          = Character.Controlled;
                }
            }
            if (!AutoPilot && Character.DisableControls && GUI.KeyboardDispatcher.Subscriber == null)
            {
                steeringAdjustSpeed = character == null ? 0.2f : MathHelper.Lerp(0.2f, 1.0f, character.GetSkillLevel("helm") / 100.0f);
                Vector2 input = Vector2.Zero;
                if (PlayerInput.KeyDown(InputType.Left))
                {
                    input -= Vector2.UnitX;
                }
                if (PlayerInput.KeyDown(InputType.Right))
                {
                    input += Vector2.UnitX;
                }
                if (PlayerInput.KeyDown(InputType.Up))
                {
                    input += Vector2.UnitY;
                }
                if (PlayerInput.KeyDown(InputType.Down))
                {
                    input -= Vector2.UnitY;
                }
                if (PlayerInput.KeyDown(Keys.LeftShift))
                {
                    SteeringInput  += input * deltaTime * 200;
                    inputCumulation = 0;
                    keyboardInput   = Vector2.Zero;
                    unsentChanges   = true;
                }
                else
                {
                    float step = deltaTime * 5;
                    if (input.Length() > 0)
                    {
                        inputCumulation += step;
                    }
                    else
                    {
                        inputCumulation -= step;
                    }
                    float maxCumulation = 1;
                    inputCumulation = MathHelper.Clamp(inputCumulation, 0, maxCumulation);
                    float length          = MathHelper.Lerp(0, 0.2f, MathUtils.InverseLerp(0, maxCumulation, inputCumulation));
                    var   normalizedInput = Vector2.Normalize(input);
                    if (MathUtils.IsValid(normalizedInput))
                    {
                        keyboardInput += normalizedInput * length;
                    }
                    if (keyboardInput.LengthSquared() > 0.01f)
                    {
                        SteeringInput += keyboardInput;
                        unsentChanges  = true;
                        user           = Character.Controlled;
                        keyboardInput *= MathHelper.Clamp(1 - step, 0, 1);
                    }
                }
            }
            else
            {
                inputCumulation = 0;
                keyboardInput   = Vector2.Zero;
            }

            if (!UseAutoDocking)
            {
                return;
            }

            float closestDist = DockingAssistThreshold * DockingAssistThreshold;

            DockingModeEnabled = false;
            foreach (DockingPort sourcePort in DockingPort.List)
            {
                if (sourcePort.Docked || sourcePort.Item.Submarine == null)
                {
                    continue;
                }
                if (sourcePort.Item.Submarine != controlledSub)
                {
                    continue;
                }

                int sourceDir = sourcePort.GetDir();

                foreach (DockingPort targetPort in DockingPort.List)
                {
                    if (targetPort.Docked || targetPort.Item.Submarine == null)
                    {
                        continue;
                    }
                    if (targetPort.Item.Submarine == controlledSub || targetPort.IsHorizontal != sourcePort.IsHorizontal)
                    {
                        continue;
                    }
                    if (Level.Loaded != null && targetPort.Item.Submarine.WorldPosition.Y > Level.Loaded.Size.Y)
                    {
                        continue;
                    }

                    int targetDir = targetPort.GetDir();

                    if (sourceDir == targetDir)
                    {
                        continue;
                    }

                    float dist = Vector2.DistanceSquared(sourcePort.Item.WorldPosition, targetPort.Item.WorldPosition);
                    if (dist < closestDist)
                    {
                        DockingModeEnabled  = true;
                        ActiveDockingSource = sourcePort;
                        DockingTarget       = targetPort;
                    }
                }
            }
        }
コード例 #14
0
ファイル: DockingPort.cs プロジェクト: tralezab/Barotrauma
        public void Undock()
        {
            if (dockingTarget == null || !docked)
            {
                return;
            }

#if CLIENT
            PlaySound(ActionType.OnUse, item.WorldPosition);
#endif

            dockingTarget.item.Submarine.DockedTo.Remove(item.Submarine);
            item.Submarine.DockedTo.Remove(dockingTarget.item.Submarine);

            if (door != null && dockingTarget.door != null)
            {
                WayPoint myWayPoint     = WayPoint.WayPointList.Find(wp => door.LinkedGap == wp.ConnectedGap);
                WayPoint targetWayPoint = WayPoint.WayPointList.Find(wp => dockingTarget.door.LinkedGap == wp.ConnectedGap);

                if (myWayPoint != null && targetWayPoint != null)
                {
                    myWayPoint.linkedTo.Remove(targetWayPoint);
                    targetWayPoint.linkedTo.Remove(myWayPoint);
                }
            }

            item.linkedTo.Clear();

            docked = false;

            dockingTarget.Undock();
            dockingTarget = null;

            if (doorBody != null)
            {
                GameMain.World.RemoveBody(doorBody);
                doorBody = null;
            }

            var wire = item.GetComponent <Wire>();
            if (wire != null)
            {
                wire.Drop(null);
            }

            if (joint != null)
            {
                GameMain.World.RemoveJoint(joint);
                joint = null;
            }

            hulls[0]?.Remove(); hulls[0] = null;
            hulls[1]?.Remove(); hulls[1] = null;

            if (gap != null)
            {
                gap.Remove();
                gap = null;
            }

            if (bodies != null)
            {
                foreach (Body body in bodies)
                {
                    if (body == null)
                    {
                        continue;
                    }
                    GameMain.World.RemoveBody(body);
                }
                bodies = null;
            }

            if (GameMain.Server != null)
            {
                item.CreateServerEvent(this);
            }
        }
コード例 #15
0
ファイル: DockingPort.cs プロジェクト: tralezab/Barotrauma
        public void Dock(DockingPort target)
        {
            if (item.Submarine.DockedTo.Contains(target.item.Submarine))
            {
                return;
            }

            if (dockingTarget != null)
            {
                Undock();
            }

            if (target.item.Submarine == item.Submarine)
            {
                DebugConsole.ThrowError("Error - tried to dock a submarine to itself");
                dockingTarget = null;
                return;
            }

#if CLIENT
            PlaySound(ActionType.OnUse, item.WorldPosition);
#endif

            if (!item.linkedTo.Contains(target.item))
            {
                item.linkedTo.Add(target.item);
            }
            if (!target.item.linkedTo.Contains(item))
            {
                target.item.linkedTo.Add(item);
            }

            if (!target.item.Submarine.DockedTo.Contains(item.Submarine))
            {
                target.item.Submarine.DockedTo.Add(item.Submarine);
            }
            if (!item.Submarine.DockedTo.Contains(target.item.Submarine))
            {
                item.Submarine.DockedTo.Add(target.item.Submarine);
            }

            dockingTarget = target;
            dockingTarget.dockingTarget = this;

            docked = true;
            dockingTarget.Docked = true;

            if (Character.Controlled != null &&
                (Character.Controlled.Submarine == dockingTarget.item.Submarine || Character.Controlled.Submarine == item.Submarine))
            {
                GameMain.GameScreen.Cam.Shake = Vector2.Distance(dockingTarget.item.Submarine.Velocity, item.Submarine.Velocity);
            }

            dockingDir = IsHorizontal ?
                         Math.Sign(dockingTarget.item.WorldPosition.X - item.WorldPosition.X) :
                         Math.Sign(dockingTarget.item.WorldPosition.Y - item.WorldPosition.Y);
            dockingTarget.dockingDir = -dockingDir;

            if (door != null && dockingTarget.door != null)
            {
                WayPoint myWayPoint     = WayPoint.WayPointList.Find(wp => door.LinkedGap == wp.ConnectedGap);
                WayPoint targetWayPoint = WayPoint.WayPointList.Find(wp => dockingTarget.door.LinkedGap == wp.ConnectedGap);

                if (myWayPoint != null && targetWayPoint != null)
                {
                    myWayPoint.linkedTo.Add(targetWayPoint);
                    targetWayPoint.linkedTo.Add(myWayPoint);
                }
            }

            CreateJoint(false);

            if (GameMain.Server != null)
            {
                item.CreateServerEvent(this);
            }
        }