コード例 #1
0
        public void Generate(World world)
        {
            int size = Math.Clamp(this.m_Random.Next() % this.m_MaxSize, this.m_MaxSize / 2, this.m_MaxSize);

            var archetype = world.CreateArchetype(typeof(Position), typeof(TileChunk));

            var tileCollection = new RandomTileMapCollection(this.m_Random, size);

            tileCollection.Add(TileMap.Load(Assets.NameToPath("tm_flatland.tiles")), 30);
            tileCollection.Add(TileMap.Load(Assets.NameToPath("tm_hill.tiles")), 20);
            tileCollection.Add(TileMap.Load(Assets.NameToPath("tm_mountain.tiles")), 15);
            tileCollection.Add(TileMap.Load(Assets.NameToPath("tm_cliff.tiles")), 15);
            tileCollection.Add(TileMap.Load(Assets.NameToPath("tm_valley.tiles")), 15);

            var spawnLeft = GenerateSpawn(
                world,
                TileMap.Load(Assets.NameToPath("tm_spawn_left.tiles")),
                new Position(Vector2.Zero),
                new Spawn {
                damage = 0f, playerId = 0
            });

            var position = new Vector2(spawnLeft.Width, 0f);

            float maxDeepness = 0f;

            for (int i = 0; i < size; ++i)
            {
                var map  = tileCollection.Choose();
                var rect = map.Instantiate(world, new Position(position));

                position.X += rect.Width;

                float deepness = rect.Top + rect.Height;

                if (deepness > maxDeepness)
                {
                    maxDeepness = deepness;
                }
            }

            var spawnRight = GenerateSpawn(
                world,
                TileMap.Load(Assets.NameToPath("tm_spawn_right.tiles")),
                new Position(position.X, 0f),
                new Spawn {
                damage = 0f, playerId = 1
            });

            position.X += spawnRight.Width;

            world.ForEach(archetype, (batch) => {
                var chunks    = batch.GetComponentDataReadWrite <TileChunk>();
                var positions = batch.GetComponentData <Position>();

                for (int i = 0; i < batch.length; ++i)
                {
                    GenerateRandomizedTiles(positions[i].value, ref chunks[i]);
                }
            });

            Vector2 bedrockPosition = new Vector2(0f, maxDeepness);

            while (bedrockPosition.X < position.X)
            {
                var entity = world.CreateEntity(archetype);
                world.SetComponentData <Position>(entity, new Position(bedrockPosition));
                ref var chunk = ref world.RefComponentData <TileChunk>(entity);
                chunk.SetAll(new Tile(TileType.Bedrock));
                chunk.flags = chunk.flags | TileChunkFlags.NoneDestructible | TileChunkFlags.StaticNotEmpty;

                bedrockPosition.X += TileChunk.PixelSize;
            }