示例#1
0
        public static TileDefinition ReadNetworkableTileDefinition(this NetworkReader reader)
        {
            TileDefinition tileDefinition = new TileDefinition();

            string turfName    = reader.ReadString();
            string fixtureName = reader.ReadString();

            if (!string.IsNullOrEmpty(turfName))
            {
                tileDefinition.turf = turfs.FirstOrDefault(turf => turf.name == turfName);
                if (tileDefinition.turf == null)
                {
                    Debug.LogError($"Network recieved turf with name {turfName} could not be found");
                }
            }

            if (!string.IsNullOrEmpty(fixtureName))
            {
                tileDefinition.fixture = fixtures.FirstOrDefault(fixture => fixture.name == fixtureName);
                if (tileDefinition.fixture == null)
                {
                    Debug.LogError($"Network recieved fixture with name {fixtureName} could not be found");
                }
            }

            // If the boolean is false, subStates should be null.
            if (reader.ReadBoolean())
            {
                using (var stream = new MemoryStream(reader.ReadBytesAndSize())) {
                    tileDefinition.subStates = new BinaryFormatter().Deserialize(stream) as object[];
                }
            }

            return(tileDefinition);
        }
示例#2
0
        /**
         * Updates every tile in the tilemap
         */
        private void UpdateAllTileAdjacencies()
        {
            // Once they are all made go through and update all adjacencies.
            var adjacentTiles = new TileDefinition[8];

            foreach (var item in tiles)
            {
                int x = (int)(item.Key & 0xffff);
                int y = (int)(item.Key >> 32);

                // Find each adjacent tile and put into a map
                for (Direction direction = Direction.North; direction <= Direction.NorthWest; direction++)
                {
                    // Take the cardinal direction, but use it in negative, so direction means the direction from OTHER to the just updated tile.
                    var modifier = DirectionHelper.ToCardinalVector(direction);

                    if (y + modifier.Item2 < 0 || x + modifier.Item1 < 0)
                    {
                        adjacentTiles[(int)direction] = TileDefinition.NullObject;
                    }

                    ulong otherKey = GetKey(x + modifier.Item1, y + modifier.Item2);
                    if (tiles.ContainsKey(otherKey))
                    {
                        adjacentTiles[(int)direction] = tiles[otherKey].Tile;
                    }
                    else
                    {
                        adjacentTiles[(int)direction] = TileDefinition.NullObject;
                    }
                }

                item.Value.UpdateAllAdjacencies(adjacentTiles);
            }
        }
示例#3
0
        /**
         * Call UpdateSingleAdjacency on every tile adjacent to the provided one.
         *
         * Returns adjacent tiles which may be used to update the central tile
         */
        private TileDefinition[] GetAndUpdateAdjacentTiles(int x, int y, TileDefinition tileInfo)
        {
            TileDefinition[] adjacents = new TileDefinition[8];

            // Then go for each adjacent and update
            for (Direction direction = Direction.North; direction <= Direction.NorthWest; direction++)
            {
                // Take the cardinal direction, but use it in negative, so direction means the direction from OTHER to the just updated tile.
                var modifier = DirectionHelper.ToCardinalVector(direction);

                if (y + modifier.Item2 < 0 || x + modifier.Item1 < 0)
                {
                    continue;
                }

                ulong otherKey = GetKey(x + modifier.Item1, y + modifier.Item2);
                if (tiles.ContainsKey(otherKey))
                {
                    adjacents[(int)direction] = tiles[otherKey].Tile;
                    tiles[otherKey].UpdateSingleAdjacency(DirectionHelper.GetOpposite(direction), tileInfo);
                }
            }

            return(adjacents);
        }
示例#4
0
        /*
         * The internals contain the actual logic of the above functions. They aren't put in the
         * public method as the public method needs to have warnings when not called from server
         */
        private void InternalCreateTile(int x, int y, TileDefinition definition)
        {
            ulong key = GetKey(x, y);

            if (tiles.ContainsKey(key))
            {
                var message = "Tried to create tile that already exists at position" + GetPosition(x, y).ToString() + ", index [" + x.ToString() + ", " + y.ToString() + "]";
                Debug.LogError(message);
                throw new Exception(message);
            }

            var tileObject = SpawnTileObject(x, y);

            tiles[key] = tileObject;

            tileObject.Tile = definition;
            var adjacents = GetAndUpdateAdjacentTiles(x, y, definition);

            tileObject.UpdateAllAdjacencies(adjacents);

            // Run on every client.
            if (Application.isPlaying && isServer)
            {
                RpcCreateTile(x, y, definition);
            }
        }
示例#5
0
 private void RpcUpdateTile(int x, int y, TileDefinition definition)
 {
     if (!isServer)
     {
         InternalUpdateTile(x, y, definition);
     }
 }
示例#6
0
        /**
         * Update a tile at the given index with the given information.
         *
         * Note: Throws if there is no tile
         */
        public void UpdateTile(int x, int y, TileDefinition tileInfo)
        {
            ulong key = GetKey(x, y);

            if (!tiles.ContainsKey(key))
            {
                throw new Exception("Tried to update tile that doesn't exist at position" + GetPosition(x, y).ToString() + ", index [" + x.ToString() + ", " + y.ToString() + "]");
            }

            var obj = tiles[key];

            obj.Tile = tileInfo;

            var adjacents = GetAndUpdateAdjacentTiles(x, y, tileInfo);

            obj.UpdateAllAdjacencies(adjacents);
        }
示例#7
0
        public static void WriteNetworkableTileDefinition(this NetworkWriter writer, TileDefinition definition)
        {
            writer.WriteString(definition.turf?.name ?? "");
            writer.WriteString(definition.fixture?.name ?? "");

            // Use C# serializer to serialize the object array, cos the Mirror one isn't powerful enough.

            // Can't serialize null values so put a boolean indicating array presence first
            writer.WriteBoolean(definition.subStates != null);

            if (definition.subStates == null)
            {
                return;
            }

            using (var stream = new MemoryStream()) {
                new BinaryFormatter().Serialize(stream, definition.subStates);
                writer.WriteBytesAndSize(stream.ToArray());
            }
        }
示例#8
0
        /**
         * Create a tile in the given grid position
         *
         * Note: throws if a tile already exists
         */
        public void CreateTile(int x, int y, TileDefinition tileInfo)
        {
            ulong key = GetKey(x, y);

            if (tiles.ContainsKey(key))
            {
                var message = "Tried to create tile that already exists at position" + GetPosition(x, y).ToString() + ", index [" + x.ToString() + ", " + y.ToString() + "]";
                Debug.LogError(message);
                throw new Exception(message);
            }

            var tileObject = SpawnTileObject(x, y);

            tiles[key] = tileObject;

            tileObject.Tile = tileInfo;
            var adjacents = GetAndUpdateAdjacentTiles(x, y, tileInfo);

            tileObject.UpdateAllAdjacencies(adjacents);
        }
示例#9
0
        private void InternalUpdateTile(int x, int y, TileDefinition definition)
        {
            ulong key = GetKey(x, y);

            if (!tiles.ContainsKey(key))
            {
                throw new Exception("Tried to update tile that doesn't exist at position" + GetPosition(x, y).ToString() + ", index [" + x.ToString() + ", " + y.ToString() + "]");
            }

            var obj = tiles[key];

            obj.Tile = definition;

            var adjacents = GetAndUpdateAdjacentTiles(x, y, definition);

            obj.UpdateAllAdjacencies(adjacents);

            if (Application.isPlaying && isServer)
            {
                RpcUpdateTile(x, y, definition);
            }
        }
示例#10
0
 private void CmdUpdateTile(int x, int y, TileDefinition definition) => tileManager.UpdateTile(x, y, definition);
示例#11
0
        public void UpdateTile(TileObject tile, TileDefinition definition)
        {
            var pos = tileManager.GetIndexAt(tile.transform.position);

            CmdUpdateTile(pos.x, pos.y, definition);
        }
示例#12
0
 public void EditorUpdateTile(int x, int y, TileDefinition definition) => InternalUpdateTile(x, y, definition);
示例#13
0
        public void UpdateTile(Vector3 position, TileDefinition tileInfo)
        {
            var index = GetIndexAt(position);

            UpdateTile(index.x, index.y, tileInfo);
        }
示例#14
0
 public void CreateTile(int x, int y, TileDefinition definition) => InternalCreateTile(x, y, definition);
示例#15
0
        public void CreateTile(Vector3 position, TileDefinition definition)
        {
            var index = GetIndexAt(position);

            CreateTile(index.x, index.y, definition);
        }