protected void SetComponentData(Entity entity, SpawnItemData item)
 {
     manager.SetComponentData(entity, new Position {
         Value = item.position
     });
     manager.SetComponentData(entity, new Rotation {
         Value = item.rotation
     });
     manager.AddBuffer <NeighboursBufferElement>(entity);
 }
    protected void ComputePlacement(ref SpawnItemData item)
    {
        var height   = terrain.SampleHeight(item.position);
        var normal   = terrainData.GetInterpolatedNormal((item.position.x - terrainOrigin.x) / terrainSize.x, (item.position.z - terrainOrigin.z) / terrainSize.z);
        var rotation = Quaternion.FromToRotation(Vector3.up, normal);

        item.position.y = height + terrainOrigin.y;
        item.rotation   = new float4 {
            x = rotation.x, y = rotation.y, z = rotation.z, w = rotation.w
        };
    }
    public void Spawn(int amount)
    {
        var spawnedEntities = new Queue <GameObject>();

        for (var i = 0; i < amount; i++)
        {
            var plant = UnityEngine.Object.Instantiate(settings.plantsPrefab);

            var x = UnityEngine.Random.Range(terrainOrigin.x, terrainOrigin.x + terrainSize.x);
            var z = UnityEngine.Random.Range(terrainOrigin.z, terrainOrigin.z + terrainSize.z);

            var item = new SpawnItemData {
                position = new float3 {
                    x = x, y = 0, z = z
                }
            };

            ComputePlacement(ref item);
            var entity = plant.GetComponent <GameObjectEntity>().Entity;

            SetComponentData(entity, item);

            database.Add(plant);
            spawnedEntities.Enqueue(plant);
        }

        var distance = settings.neighborSize;

        while (spawnedEntities.Count > 0)
        {
            var entity = spawnedEntities.Dequeue().GetComponent <GameObjectEntity>().Entity;

            var pos = manager.GetComponentData <Position>(entity).Value;
            var neighboursBuffer = manager.GetBuffer <NeighboursBufferElement>(entity);

            var neighbours = database.GetNeighbours(
                (int)(pos.x - distance / 2),
                (int)(pos.z - distance / 2),
                (int)distance,
                (int)distance
                );

            for (int e = 0; e < neighbours.Count; e++)
            {
                if (entity != neighbours[e].entity)
                {
                    neighboursBuffer.Add(new NeighboursBufferElement {
                        entity = neighbours[e].entity
                    });
                }
            }
        }
    }
Пример #4
0
    public void Spawn(float worldX, float worldZ)
    {
        var plant = Object.Instantiate(settings.plantsPrefab);
        var item  = new SpawnItemData {
            position = new float3 {
                x = worldX, y = 0, z = worldZ
            }
        };

        ComputePlacement(ref item);
        var entity = plant.GetComponent <GameObjectEntity>().Entity;

        SetComponentData(entity, item);

        database.Add(plant);

        AddNeighbors(plant);
    }