Esempio n. 1
0
        private void handleCreateInteriorBtn()
        {
            var interiorDimensions = new Point(20, 20);
            var player             = SimulationGame.Player;
            var currentWorldPart   = player.InteriorID == Interior.Outside ? (WorldPart)SimulationGame.World.GetFromRealPoint((int)player.Position.X, (int)player.Position.Y) : SimulationGame.World.InteriorManager.Get(player.InteriorID);
            var dialog             = new InputDialog("Interior Dimensions", JToken.FromObject(interiorDimensions, SerializationUtils.Serializer).ToString(Formatting.Indented));

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                interiorDimensions = SerializationUtils.Serializer.Deserialize <Point>(new JTokenReader(JToken.Parse(dialog.ResultText)));

                var newInterior = new Interior(interiorDimensions);

                newInterior.SetPersistent(true);

                for (int i = 0; i < interiorDimensions.X; i++)
                {
                    for (int j = 0; j < interiorDimensions.Y; j++)
                    {
                        newInterior.SetBlockType(i, j, 1);
                    }
                }

                var interiorWorldLink = new WorldLink(new WorldPosition(0, 0, newInterior.ID), player.Position.ToBlockPosition());

                newInterior.AddWorldLink(interiorWorldLink);

                WorldLoader.SaveInterior(newInterior);

                currentWorldPart.SetPersistent(true);
                currentWorldPart.AddWorldLink(interiorWorldLink.SwapFromTo());
            }
        }
        public static Interior CreateInterior(WorldLink entranceLink, Point fromBlock, string fromInteriorID = null)
        {
            /*int dimX = SimulationGame.WorldGenerator.random.Next(10, 40);
             * int dimY = SimulationGame.WorldGenerator.random.Next(10, 40);
             *
             * int linkX = SimulationGame.WorldGenerator.random.Next(1, dimX);
             * int linkY = SimulationGame.WorldGenerator.random.Next(1, dimY);
             *
             * Interior retInterior = new Interior(new Point(dimX, dimY));
             *
             * for(int i=0;i<dimX;i++)
             * {
             *  for(int j=0;j<dimY;j++)
             *  {
             *      retInterior.SetBlockType(i, j, 1);
             *  }
             * }
             *
             * entranceLink = new WorldLink(fromBlock, fromInteriorID, new Point(linkX, linkY), retInterior.ID);
             * retInterior.AddWorldLink(new WorldLink(new Point(linkX, linkY), retInterior.ID, fromBlock, fromInteriorID));
             *
             * return retInterior;*/

            return(null);
        }
Esempio n. 3
0
 public void Deselect()
 {
     SelectedBlockPosition = Point.Zero;
     SelectedBlockType     = null;
     SelectedGameObjects.Clear();
     SelectedWorldLink = null;
 }
Esempio n. 4
0
 public void SelectGameObject(GameObject gameObject)
 {
     SelectedBlockPosition = Point.Zero;
     SelectedBlockType     = null;
     SelectedGameObjects.Add(gameObject);
     SelectedWorldLink = null;
 }
Esempio n. 5
0
        private void handleRemoveInteriorBtn()
        {
            var confirmResult = System.Windows.Forms.MessageBox.Show("Are you sure to delete the interior?", "Confirm Delete!", System.Windows.Forms.MessageBoxButtons.YesNo);

            if (confirmResult == System.Windows.Forms.DialogResult.Yes)
            {
                Interior interior = SimulationGame.World.InteriorManager.Get(SimulationGame.Player.InteriorID);

                if (interior.WorldLinks == null || interior.WorldLinks.Count == 0)
                {
                    SimulationGame.Player.UpdatePosition(new WorldPosition(0, 0, Interior.Outside));
                }

                // Check if durable entities are inside besides player
                if (interior.ContainedObjects != null)
                {
                    foreach (var containedObject in interior.ContainedObjects)
                    {
                        if (containedObject is DurableEntity && containedObject is Player == false)
                        {
                            System.Windows.Forms.MessageBox.Show("Cannot delete interior because a durable entity is inside");
                            return;
                        }
                    }
                }

                // Delete all WorldLinks
                if (interior.WorldLinks != null)
                {
                    bool playerTeleported = false;

                    WorldLink[] wordLinks = new WorldLink[interior.WorldLinks.Count];
                    interior.WorldLinks.Values.CopyTo(wordLinks, 0);

                    foreach (var worldLink in wordLinks)
                    {
                        if (playerTeleported == false)
                        {
                            SimulationGame.Player.UpdatePosition(worldLink.ToRealWorldPositionTo());
                            playerTeleported = true;
                        }

                        try
                        {
                            SimulationGame.World.UpdateWorldLink(worldLink);
                        }
                        catch (Exception e)
                        {
                            System.Windows.Forms.MessageBox.Show("Couldn't delete WorldLink: " + e.Message);
                        }
                    }
                }

                // Unload and Erase File
                SimulationGame.World.InteriorManager.RemoveChunk(interior.ID);
                WorldLoader.EraseInterior(interior);
            }
        }
Esempio n. 6
0
    public void PrepareSceneForPlayer(LitePlayer player, int sceneId, WorldLink fromLink, System.Action <WorldChunk> callbackOnComplete)
    {
        if (!Networking.isServer)
        {
            Debug.LogError("Attempt to call server fn on client");
            return;
        }

        WorldChunk xy = ChunkHandler.i.RequestChunk(sceneId, callbackOnComplete);
    }
Esempio n. 7
0
        private bool executeWorldLink(WorldPosition newPosition = null)
        {
            WorldLink oldWorldLink = SimulationGame.World.GetWorldLinkFromRealPosition(Position);
            WorldLink newWorldLink = newPosition != null?SimulationGame.World.GetWorldLinkFromRealPosition(newPosition) : null;

            if (oldWorldLink == null && newWorldLink != null)
            {
                Vector2 newWorldPosition = new Vector2(newWorldLink.ToBlock.X * WorldGrid.BlockSize.X + WorldGrid.BlockSize.X / 2, newWorldLink.ToBlock.Y * WorldGrid.BlockSize.Y + WorldGrid.BlockSize.Y - 1);
                UpdatePosition(new WorldPosition(newWorldPosition, newWorldLink.ToInteriorID));

                return(true);
            }

            return(false);
        }
Esempio n. 8
0
        private void handleCreateWorldLinkBtn()
        {
            var newWorldLink = new WorldLink()
            {
                FromBlock      = SimulationGame.Player.BlockPosition,
                FromInteriorID = SimulationGame.Player.InteriorID,
                ToBlock        = Point.Zero,
                ToInteriorID   = Interior.Outside
            };

            var dialog = new InputDialog("Create WorldLink", JToken.FromObject(newWorldLink, SerializationUtils.Serializer).ToString(Formatting.Indented));

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                newWorldLink = SerializationUtils.Serializer.Deserialize <WorldLink>(new JTokenReader(JToken.Parse(dialog.ResultText)));
                SimulationGame.World.UpdateWorldLink(null, newWorldLink);
            }
        }
Esempio n. 9
0
    public void MovePlayerToScene(LitePlayer player, int sceneId, WorldLink fromLink, System.Action <WorldChunk> callback = null)
    {
        if (!Networking.isServer)
        {
            Debug.LogError("Attempt to call server fn on client");
            return;
        }

        // Remove player from old scene
        WorldChunk oldChunk = ChunkHandler.i.GetChunk(player.GetChunkId());

        oldChunk.connectedPlayers.Remove(player);

        LobbyGoodbyePacket pkt = new LobbyGoodbyePacket
        {
            playerId = player.id
        };

        foreach (LitePlayer plyr in oldChunk.connectedPlayers)
        {
            PacketSender.SendLobbyGoodbyePacket(pkt, plyr.GetConnectionId());
        }


        // Add player to new scene

        WorldChunk chunk = ChunkHandler.i.GetChunkForPlayer(player, sceneId);

        if (chunk == null)
        {
            PrepareSceneForPlayer(player, sceneId, fromLink, chk =>
            {
                ChunkReady(chk, player);
                callback?.Invoke(chk);
            });
        }
        else
        {
            ChunkReady(chunk, player);
            callback?.Invoke(chunk);
        }
    }
Esempio n. 10
0
        private void handleKeyRight()
        {
            if (SelectedGameObjects.Count > 0)
            {
                foreach (var gameObject in SelectedGameObjects)
                {
                    gameObject.DisconnectFromWorld();

                    WorldPosition newPosition;

                    if (SimulationGame.KeyboardState.IsKeyDown(Keys.LeftControl) || SimulationGame.KeyboardState.IsKeyDown(Keys.RightControl))
                    {
                        newPosition = new WorldPosition(gameObject.Position.X + 1, gameObject.Position.Y, gameObject.InteriorID);
                    }
                    else if (SimulationGame.KeyboardState.IsKeyDown(Keys.LeftShift) || SimulationGame.KeyboardState.IsKeyDown(Keys.RightShift))
                    {
                        newPosition = new WorldPosition(gameObject.BlockPosition.X + 1, gameObject.BlockPosition.Y, gameObject.InteriorID).ToRealPosition();
                    }
                    else
                    {
                        newPosition = new WorldPosition(gameObject.Position.X + 16, gameObject.Position.Y, gameObject.InteriorID);
                    }

                    gameObject.UpdatePosition(newPosition);
                    gameObject.ConnectToWorld();
                }
            }

            if (SelectedWorldLink != null)
            {
                WorldLink clonedSelected = SelectedWorldLink.Clone();

                clonedSelected.FromBlock.X += 1;

                SimulationGame.World.UpdateWorldLink(SelectedWorldLink, clonedSelected);
                SelectedWorldLink = clonedSelected;
            }
        }
Esempio n. 11
0
 private void handleInspectWorldLinkSelection(WorldLink worldLink)
 {
     placementType = PlacementType.Inspect;
     placementMode = PlacementMode.NoPlacement;
 }
Esempio n. 12
0
        private void selectGameObjects(Rect selectionRect)
        {
            // Is Deselection Mode?
            if (SimulationGame.KeyboardState.IsKeyDown(Keys.LeftAlt) || SimulationGame.KeyboardState.IsKeyDown(Keys.RightAlt))
            {
                if (SimulationGame.Player.InteriorID == Interior.Outside)
                {
                    // Check collision with interactive && contained objects
                    Point chunkTopLeft     = GeometryUtils.GetChunkPosition(selectionRect.Left, selectionRect.Top, WorldGrid.WorldChunkPixelSize.X, WorldGrid.WorldChunkPixelSize.Y);
                    Point chunkBottomRight = GeometryUtils.GetChunkPosition(selectionRect.Right, selectionRect.Bottom, WorldGrid.WorldChunkPixelSize.X, WorldGrid.WorldChunkPixelSize.Y);

                    for (int chunkX = chunkTopLeft.X - 1; chunkX <= chunkBottomRight.X + 1; chunkX++)
                    {
                        for (int chunkY = chunkTopLeft.Y - 1; chunkY <= chunkBottomRight.Y + 1; chunkY++)
                        {
                            WorldGridChunk worldGridChunk = SimulationGame.World.GetFromChunkPoint(chunkX, chunkY);

                            addSelectedObjectsFromWorldPart(selectionRect, worldGridChunk, true);
                        }
                    }
                }
                else
                {
                    addSelectedObjectsFromWorldPart(selectionRect, SimulationGame.World.InteriorManager.Get(SimulationGame.Player.InteriorID), true);
                }

                if (SelectedGameObjects.Count > 0)
                {
                    gameObjectSelection?.Invoke(SelectedGameObjects);
                }
            }
            else
            {
                if (SimulationGame.KeyboardState.IsKeyDown(Keys.LeftShift) == false && SimulationGame.KeyboardState.IsKeyDown(Keys.RightShift) == false)
                {
                    SelectedBlockPosition = Point.Zero;
                    SelectedBlockType     = null;
                    SelectedWorldLink     = null;
                    SelectedGameObjects.Clear();
                }

                if (SimulationGame.Player.InteriorID == Interior.Outside)
                {
                    // Check collision with interactive && contained objects
                    Point chunkTopLeft     = GeometryUtils.GetChunkPosition(selectionRect.Left, selectionRect.Top, WorldGrid.WorldChunkPixelSize.X, WorldGrid.WorldChunkPixelSize.Y);
                    Point chunkBottomRight = GeometryUtils.GetChunkPosition(selectionRect.Right, selectionRect.Bottom, WorldGrid.WorldChunkPixelSize.X, WorldGrid.WorldChunkPixelSize.Y);

                    for (int chunkX = chunkTopLeft.X - 1; chunkX <= chunkBottomRight.X + 1; chunkX++)
                    {
                        for (int chunkY = chunkTopLeft.Y - 1; chunkY <= chunkBottomRight.Y + 1; chunkY++)
                        {
                            WorldGridChunk worldGridChunk = SimulationGame.World.GetFromChunkPoint(chunkX, chunkY);

                            addSelectedObjectsFromWorldPart(selectionRect, worldGridChunk);
                        }
                    }
                }
                else
                {
                    addSelectedObjectsFromWorldPart(selectionRect, SimulationGame.World.InteriorManager.Get(SimulationGame.Player.InteriorID));
                }

                if (SelectedGameObjects.Count == 0)
                {
                    WorldPart worldPart = (SimulationGame.Player.InteriorID == Interior.Outside) ? SimulationGame.World.GetFromRealPoint((int)selectionRect.X, (int)selectionRect.Y) : (WorldPart)SimulationGame.World.InteriorManager.Get(SimulationGame.Player.InteriorID);

                    if (worldPart.WorldLinks != null)
                    {
                        foreach (var worldLinkItem in worldPart.WorldLinks)
                        {
                            Rect renderPosition = new Rect(worldLinkItem.Value.FromBlock.X * WorldGrid.BlockSize.X, worldLinkItem.Value.FromBlock.Y * WorldGrid.BlockSize.Y, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y);

                            if (renderPosition.Contains(selectionRect.GetPosition()))
                            {
                                SelectedWorldLink = worldLinkItem.Value;
                                worldLinkSelection?.Invoke(SelectedWorldLink);

                                return;
                            }
                        }
                    }

                    // We get the block
                    SelectedBlockPosition = GeometryUtils.GetBlockFromReal((int)SimulationGame.RealWorldMousePosition.X, (int)SimulationGame.RealWorldMousePosition.Y);
                    SelectedBlockType     = BlockType.lookup[SimulationGame.World.GetBlockType(SelectedBlockPosition.X, SelectedBlockPosition.Y, SimulationGame.Player.InteriorID)];

                    blockSelection?.Invoke(SelectedBlockType);
                }
                else
                {
                    gameObjectSelection?.Invoke(SelectedGameObjects);
                }
            }
        }
Esempio n. 13
0
        private static Point?findNextWorldLink(WorldPosition startBlockPos, WorldPosition endBlockPos)
        {
            if (startBlockPos.InteriorID == endBlockPos.InteriorID)
            {
                return(null);
            }

            bool switched = false;

            // We want to start at the end, because starting outside can be problematic
            if (startBlockPos.InteriorID == Interior.Outside)
            {
                WorldPosition temp = startBlockPos;
                startBlockPos = endBlockPos;
                endBlockPos   = temp;

                switched = true;
            }

            var done = new List <string>()
            {
                startBlockPos.InteriorID
            };
            var open          = new SortedList <float, List <WorldLink> >();
            var startInterior = SimulationGame.World.InteriorManager.Get(startBlockPos.InteriorID);

            foreach (var worldLinkItem in startInterior.WorldLinks)
            {
                if (worldLinkItem.Value.ToInteriorID == endBlockPos.InteriorID)
                {
                    return(switched ? worldLinkItem.Value.ToBlock : worldLinkItem.Value.FromBlock);
                }
                if (worldLinkItem.Value.ToInteriorID == Interior.Outside || done.Contains(worldLinkItem.Value.ToInteriorID))
                {
                    continue;
                }

                var distance = GeometryUtils.GetDiagonalDistance(startBlockPos.X, startBlockPos.Y, worldLinkItem.Value.FromBlock.X, worldLinkItem.Value.FromBlock.Y);

                while (open.ContainsKey(distance))
                {
                    distance += 0.01f;
                }


                open.Add(distance, new List <WorldLink>()
                {
                    worldLinkItem.Value
                });
                done.Add(worldLinkItem.Value.ToInteriorID);
            }

            while (open.Count > 0)
            {
                float            distance      = open.Keys[0];
                List <WorldLink> worldLinkList = open.Values[0];
                WorldLink        worldLink     = worldLinkList[worldLinkList.Count - 1];

                open.RemoveAt(0);

                // Get neighbors
                Interior interior = SimulationGame.World.InteriorManager.Get(worldLink.ToInteriorID);

                foreach (var worldLinkItem in interior.WorldLinks)
                {
                    if (worldLinkItem.Value.ToInteriorID == endBlockPos.InteriorID)
                    {
                        return(switched ? worldLinkItem.Value.ToBlock : worldLinkList[0].FromBlock);
                    }
                    if (worldLinkItem.Value.ToInteriorID == Interior.Outside || done.Contains(worldLinkItem.Value.ToInteriorID))
                    {
                        continue;
                    }

                    List <WorldLink> clonedList = new List <WorldLink>(worldLinkList);

                    clonedList.Add(worldLinkItem.Value);

                    distance = distance + GeometryUtils.GetDiagonalDistance(worldLink.ToBlock.X, worldLink.ToBlock.Y, worldLinkItem.Value.FromBlock.X, worldLinkItem.Value.FromBlock.Y);

                    while (open.ContainsKey(distance))
                    {
                        distance += 0.01f;
                    }

                    open.Add(distance, clonedList);
                    done.Add(worldLinkItem.Value.ToInteriorID);
                }
            }

            return(null);
        }
Esempio n. 14
0
        public override void Update(GameTime gameTime)
        {
            if (IsDead())
            {
                StopWalking();
                base.Update(gameTime);

                return;
            }

            loadWalkpath(gameTime);

            if (walkPath != null)
            {
                var destPos = new Vector2(walkPath[0].x * WorldGrid.BlockSize.X + 16, walkPath[0].y * WorldGrid.BlockSize.Y + 20);

                // Check if we are at position
                if (Math.Abs(destPos.X - Position.X) < GeometryUtils.SmallFloat && Math.Abs(destPos.Y - Position.Y) < GeometryUtils.SmallFloat)
                {
                    if (walkPath.Count > 1)
                    {
                        walkPath.RemoveAt(0);
                        destPos = new Vector2(walkPath[0].x * WorldGrid.BlockSize.X + 16, walkPath[0].y * WorldGrid.BlockSize.Y + 31);
                    }
                    else
                    {
                        StopWalking();

                        // We call this because we now want to check if we are on a world link
                        WorldLink newWorldLink = SimulationGame.World.GetWorldLinkFromRealPosition(Position);

                        if (newWorldLink != null)
                        {
                            Vector2 newWorldPosition = new Vector2(newWorldLink.ToBlock.X * WorldGrid.BlockSize.X + WorldGrid.BlockSize.X / 2, newWorldLink.ToBlock.Y * WorldGrid.BlockSize.Y + WorldGrid.BlockSize.Y - 1);
                            UpdatePosition(new WorldPosition(newWorldPosition, newWorldLink.ToInteriorID));
                        }
                    }
                }

                if (IsWalking && destPos != Vector2.Zero)
                {
                    Direction = new Vector2(destPos.X - Position.X, destPos.Y - Position.Y);
                    Direction.Normalize();

                    bool couldWalk = changePosition(gameTime, destPos);

                    if (!couldWalk)
                    {
                        StopWalking();
                    }
                }
            }
            else if (DestRealPosition != null)
            {
                if (Direction == Vector2.Zero)
                {
                    Direction = new Vector2(DestRealPosition.X - Position.X, DestRealPosition.Y - Position.Y);
                    Direction.Normalize();
                }

                bool couldWalk = changePosition(gameTime, DestRealPosition.ToVector());

                // Check if we couldn't move to position
                if (!couldWalk)
                {
                    WalkToBlock(DestRealPosition.ToBlockPosition());
                }
                else if (Math.Abs(DestRealPosition.X - Position.X) < GeometryUtils.SmallFloat && Math.Abs(DestRealPosition.Y - Position.Y) < GeometryUtils.SmallFloat)
                {
                    StopWalking();

                    // We call this because we now want to check if we are on a world link
                    WorldLink newWorldLink = SimulationGame.World.GetWorldLinkFromRealPosition(Position);

                    if (newWorldLink != null)
                    {
                        Vector2 newWorldPosition = new Vector2(newWorldLink.ToBlock.X * WorldGrid.BlockSize.X + WorldGrid.BlockSize.X / 2, newWorldLink.ToBlock.Y * WorldGrid.BlockSize.Y + WorldGrid.BlockSize.Y - 1);
                        UpdatePosition(new WorldPosition(newWorldPosition, newWorldLink.ToInteriorID));
                    }
                }
            }
            else if (this is Player)
            {
                if (Direction != Vector2.Zero)
                {
                    float newPosX = Position.X + Direction.X * Velocity * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                    float newPosY = Position.Y + Direction.Y * Velocity * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                    var   newPos  = new WorldPosition(newPosX, newPosY, InteriorID);

                    if (SimulationGame.IsGodMode || (CanWalk && CanMove(newPos)))
                    {
                        var executedWorldLink = executeWorldLink(newPos);

                        if (executedWorldLink == false)
                        {
                            UpdatePosition(newPos);
                        }
                    }
                    else
                    {
                        StopWalking();
                    }
                }
            }

            base.Update(gameTime);
        }