Advertisement
443eb9

Untitled

Sep 9th, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.11 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using HexagonSurvive.Environment.Components;
  3. using HexagonSurvive.Environment.Settings;
  4. using HexagonSurvive.Game.Managers;
  5. using HexagonSurvive.Map.Components;
  6. using Unity.Collections;
  7. using Unity.Entities;
  8. using UnityEngine;
  9.  
  10. namespace HexagonSurvive.Containers.ECS
  11. {
  12.     public class EntityContainerBaker : Baker<EntityContainerMono>
  13.     {
  14.         public override void Bake(EntityContainerMono authoring)
  15.         {
  16.             var builder = new BlobBuilder(Allocator.Temp);
  17.             ref BlobArray<Entity> trees = ref builder.ConstructRoot<BlobArray<Entity>>();
  18.             List<GameObject> treePrefabs = PrefabsContainer.Instance.treePrefabs;
  19.             BlobBuilderArray<Entity> arrayBuilder = builder.Allocate(ref trees, treePrefabs.Count);
  20.  
  21.             for (int i = 0; i < treePrefabs.Count; i++)
  22.                 arrayBuilder[i] = GetEntity(treePrefabs[i], TransformUsageFlags.Renderable);
  23.  
  24.             Entity e = GetEntity(authoring, TransformUsageFlags.None);
  25.             GlobalEnvironment gEnv = EnvironmentManager.Instance.globalEnvironment;
  26.             AddComponent(e, new GlobalEnvironmentData
  27.             {
  28.                 planeMeshRes = gEnv.planeMeshRes,
  29.                 planeMeshSize = gEnv.planeMeshSize
  30.             });
  31.             AddComponent(e, new HexMapPrefabsData
  32.             {
  33.                 hexUnit = GetEntity(authoring.hexUnit, TransformUsageFlags.NonUniformScale),
  34.                 trees = builder.CreateBlobAssetReference<BlobArray<Entity>>(Allocator.Persistent),
  35.                 waterSurface = GetEntity(PrefabsContainer.Instance.waterSurface, TransformUsageFlags.Renderable)
  36.             });
  37.             builder.Dispose();
  38.         }
  39.     }
  40. }
  41.  
  42. -------------------------------------
  43.  
  44. [UpdateInGroup(typeof(InitializationSystemGroup))]
  45. public partial struct HexMapInitSystem : ISystem
  46. {
  47. ...
  48. // there's too much code in this class, so I picked the related code only
  49. private void InstantiateUnit(ref SystemState state, float2 pos)
  50. {
  51.     Entity hexUnit = state.EntityManager.Instantiate(_prefabs.hexUnit);
  52.     HexUnitAspect unitAspect = SystemAPI.GetAspect<HexUnitAspect>(hexUnit);
  53.     float height = GetUnitHeight(pos);
  54.     int altitude = StepAltitude(height);
  55.     TerrainNoiseProperties settings = _map.Data.tnProp;
  56.     float unitHeight = altitude * settings.stepHeight + settings.baseHeight;
  57.     Biome biome = height > settings.seaLevel
  58.         ? GetBiomeLand(unitAspect, pos, unitHeight)
  59.         : GetBiomeWater(unitAspect, pos, unitHeight);
  60.     unitAspect.AdjustData
  61.     (
  62.         altitude,
  63.         unitHeight / 2,
  64.         unitHeight > _map.Data.tnProp.seaLevel,
  65.         biome
  66.     );
  67.     unitAspect.Transform
  68.     (
  69.         new float3(pos.x, unitHeight / 4, pos.y),
  70.         float3.zero,
  71.         new float3(1, unitHeight / 2, 1)
  72.     );
  73.     // GenerateVegetation(ref state, unitAspect);
  74. }
  75. ...
  76. }
  77.  
  78. ------------------------------------
  79.  
  80. using UnityEngine;
  81.  
  82. namespace HexagonSurvive.Containers.ECS
  83. {
  84.     public class EntityContainerMono : MonoBehaviour
  85.     {
  86.         public PrefabsContainer prefabs;
  87.         public GameObject hexUnit;
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement