Advertisement
443eb9

Untitled

Sep 9th, 2023 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.42 KB | None | 0 0
  1. using HexagonSurvive.Map.Components;
  2. using HexagonSurvive.Map.Enums;
  3. using Unity.Entities;
  4. using Unity.Mathematics;
  5. using Unity.Transforms;
  6.  
  7. namespace HexagonSurvive.Map.Aspects
  8. {
  9.     public readonly partial struct HexUnitAspect : IAspect
  10.     {
  11.         public readonly Entity self;
  12.  
  13.         private readonly RefRW<LocalToWorld> _localToWorld;
  14.         private readonly RefRW<PostTransformMatrix> _transformMatrix;
  15.         private readonly RefRW<HexUnitData> _data;
  16.  
  17.         public HexUnitData Data => _data.ValueRO;
  18.  
  19.         public Entity NaturalEntity
  20.         {
  21.             get => _data.ValueRO.naturalEntity;
  22.             set => _data.ValueRW.naturalEntity = value;
  23.         }
  24.  
  25.         /// <summary>
  26.         /// Only used in map generation
  27.         /// </summary>
  28.         public void AdjustData(int altitude, float height, bool walkable, Biome biome)
  29.         {
  30.             _data.ValueRW.altitude = altitude;
  31.             _data.ValueRW.height = height;
  32.             _data.ValueRW.walkable = walkable;
  33.             _data.ValueRW.biome = biome;
  34.         }
  35.  
  36.         public void Transform(float3 position, float3 rotation, float3 scale)
  37.         {
  38.             // _localToWorld.ValueRW.Value = float4x4.TRS(position, quaternion.Euler(rotation), scale);
  39.             _localToWorld.ValueRW.Value = float4x4.Translate(position);
  40.             _transformMatrix.ValueRW.Value = float4x4.Scale(scale);
  41.         }
  42.     }
  43. }
  44.  
  45. ------------------------------------------------
  46. using System.Collections.Generic;
  47. using HexagonSurvive.Environment.Components;
  48. using HexagonSurvive.Environment.Settings;
  49. using HexagonSurvive.Game.Managers;
  50. using HexagonSurvive.Map.Components;
  51. using Unity.Collections;
  52. using Unity.Entities;
  53. using UnityEngine;
  54.  
  55. namespace HexagonSurvive.Containers.ECS
  56. {
  57.     public class EntityContainerBaker : Baker<EntityContainerMono>
  58.     {
  59.         public override void Bake(EntityContainerMono authoring)
  60.         {
  61.             var builder = new BlobBuilder(Allocator.Temp);
  62.             ref BlobArray<Entity> trees = ref builder.ConstructRoot<BlobArray<Entity>>();
  63.             List<GameObject> treePrefabs = PrefabsContainer.Instance.treePrefabs;
  64.             BlobBuilderArray<Entity> arrayBuilder = builder.Allocate(ref trees, treePrefabs.Count);
  65.  
  66.             for (int i = 0; i < treePrefabs.Count; i++)
  67.                 arrayBuilder[i] = GetEntity(treePrefabs[i], TransformUsageFlags.Renderable);
  68.  
  69.             Entity e = GetEntity(authoring, TransformUsageFlags.None);
  70.             GlobalEnvironment gEnv = EnvironmentManager.Instance.globalEnvironment;
  71.             AddComponent(e, new GlobalEnvironmentData
  72.             {
  73.                 planeMeshRes = gEnv.planeMeshRes,
  74.                 planeMeshSize = gEnv.planeMeshSize
  75.             });
  76.             AddComponent(e, new HexMapPrefabsData
  77.             {
  78.                 hexUnit = GetEntity(authoring.hexUnit, TransformUsageFlags.NonUniformScale),
  79.                 trees = builder.CreateBlobAssetReference<BlobArray<Entity>>(Allocator.Persistent),
  80.                 waterSurface = GetEntity(PrefabsContainer.Instance.waterSurface, TransformUsageFlags.Renderable)
  81.             });
  82.             builder.Dispose();
  83.         }
  84.     }
  85. }
  86.  
  87. ------------------------------------
  88.  
  89. using UnityEngine;
  90.  
  91. namespace HexagonSurvive.Containers.ECS
  92. {
  93.     public class EntityContainerMono : MonoBehaviour
  94.     {
  95.         public PrefabsContainer prefabs;
  96.         public GameObject hexUnit;
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement