Example #1
0
        /// <summary>
        /// Called when the tile has been send from the server to this client.
        /// Here the data needs to be read and immediately applied.
        /// Default implementation reads the <see cref="BaseSpriteTint"/>, and all components.
        /// </summary>
        /// <param name="msg">The message to read data from.</param>
        /// <param name="forSpawn">If <see langword="true"/>, then all required data should be read, since this client knows nothing about this tile. If <see langword="false"/>, then only data that might have changed since spawned needs to be read.</param>
        public virtual void ReadData(NetBuffer msg, bool forSpawn)
        {
            this.BaseSpriteTint = msg.ReadColor();

            byte compCount = msg.ReadByte();

            for (int i = 0; i < compCount; i++)
            {
                byte index = msg.ReadByte();

                if (forSpawn)
                {
                    ushort id      = msg.ReadUInt16();
                    var    newComp = TileComponent.Create(id);
                    RemoveComponent(index, false);
                    AddComponent(newComp, index, false);
                }

                var current = components[index];
                current.ReadData(msg, forSpawn);
            }
        }
Example #2
0
        private void GenerateMap()
        {
            var map = Main.Map;

            Rand.ReseedRandom();
            Noise n = new Noise(Rand.Range(0, 10000));

            LoadingScreenText = "Generating map... Placing tiles...";

            // First iteration to place tiles.
            for (int x = 0; x < map.Width; x++)
            {
                for (int y = 0; y < map.Depth; y++)
                {
                    for (int z = 0; z < map.Height; z++)
                    {
                        const float SCALE  = 0.035f;
                        Vector2     offset = new Vector2(500, 300);
                        float       perlin = n.GetPerlin(x * SCALE + offset.X, y * SCALE + offset.Y, z * SCALE);
                        bool        place  = z == 0 || perlin >= 0.7f;

                        // Prevent floating tiles.
                        var below = map.GetTile(x, y, z - 1);
                        if (z != 0 && (below == null || below.IsType("Water")))
                        {
                            place = false;
                        }

                        if (place)
                        {
                            const float WATER_HEIGHT = 0.5f;
                            const float SAND_HEIGHT  = 0.55f;

                            Tile t;
                            if (z == 0 && perlin < WATER_HEIGHT)
                            {
                                t = Tile.Create("Water");
                            }
                            else if (perlin < SAND_HEIGHT)
                            {
                                t = Tile.Create(Rand.Chance(0.5f) ? "Sand" : "RedSand");
                            }
                            else
                            {
                                t = Tile.Create("Grass");
                            }

                            map.SetTileInternal(x, y, z, t);
                        }
                    }
                }
            }

            LoadingScreenText = "Generating map... Decorating tiles...";

            // Second iteration to place mountains, trees and all that stuff.
            for (int x = 0; x < map.Width; x++)
            {
                for (int y = 0; y < map.Depth; y++)
                {
                    for (int z = 0; z < map.Height; z++)
                    {
                        Tile t = map.GetTile(x, y, z);
                        if (t == null)
                        {
                            continue;
                        }
                        if (t.IsType("Water") || t.IsType("Sand"))
                        {
                            continue;
                        }

                        Tile above = map.GetTile(x, y, z + 1);
                        if (above == null)
                        {
                            if (Rand.Chance(0.04f))
                            {
                                t.AddComponent(TileComponent.Create("Mountain"), 0);
                            }
                            else if (Rand.Chance(0.05f))
                            {
                                t.AddComponent(TileComponent.Create("Trees"), 0);
                            }
                            else if (Rand.Chance(0.01f))
                            {
                                t.AddComponent(TileComponent.Create("House"), 0);
                            }
                            else if (Rand.Chance(0.001f))
                            {
                                t.AddComponent(TileComponent.Create("RedHouse"), 0);
                            }
                        }
                    }
                }
            }

            map.SendPlaceMessageToAll();
        }