private static bool IsSolidTile(int x, int y, int mapWidth, int mapHeight, ref TileMapBlobAsset mapData, ComponentDataFromEntity <MapTile> tileData) { // if coordinate is out of bounds consider it solid if (x < 0 || y < 0 || x == mapWidth || y == mapHeight) { return(true); } // find MapTile entity in our TileMapBlobAsset reference var tileEntity = mapData.Map[x + y * mapWidth]; // get MapTile data for entity var tile = tileData[tileEntity]; return(tile.Type.IsSolid()); }
protected override void OnUpdate() { // when left mouse is clicked if (UnityEngine.Input.GetMouseButtonDown(0)) { int mapTileTypes = Enum.GetValues(typeof(MapTileType)).Length; // we need to read and write to MapTile data var mapTileData = GetComponentDataFromEntity <MapTile>(); //get current collision world var collisionWorld = _buildPhysicsSystem.PhysicsWorld.CollisionWorld; //get ray from camera var ray = _camera.ScreenPointToRay(UnityEngine.Input.mousePosition); if (RayCast(ray.origin, ray.origin + ray.direction * 100, out RaycastHit result, collisionWorld)) { // get the hit entity via rigidbodyindex var entity = _buildPhysicsSystem.PhysicsWorld.Bodies[result.RigidBodyIndex].Entity; // get MapTile data var tileData = mapTileData[entity]; // change MaptileType to the next type in enum tileData.Type = (MapTileType)(((int)tileData.Type + 1) % mapTileTypes); // apply data to Entity mapTileData[entity] = tileData; // we need to update the changed MapTile and all 8 surrounding neighbours var gameMap = GetSingleton <GameMap>(); ref TileMapBlobAsset mapData = ref gameMap.TileMap.Value; var position = tileData.Position; for (int y = position.y - 1; y <= position.y + 1; y++) { for (int x = position.x - 1; x <= position.x + 1; x++) { if (x < 0 || y < 0 || x == gameMap.Width || y == gameMap.Height) { continue; } var tileEntity = mapData.Map[x + y * gameMap.Width]; EntityManager.AddComponentData(tileEntity, new UpdateTileView()); } } } }
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) { //define Archetype for every MapTile var mapTileArchetype = dstManager.CreateArchetype( typeof(MapTile), typeof(Translation), typeof(Rotation), typeof(LocalToWorld), typeof(UpdateTileView) ); int mapWidth = GameMap.Terrain.width; int mapHeight = GameMap.Terrain.height; // create entities for every tile var tileEntities = new NativeArray <Entity>(mapWidth * mapHeight, Allocator.Temp); dstManager.CreateEntity(mapTileArchetype, tileEntities); // get pixels from the terrain and territory textures var terrainPixels = GameMap.Terrain.GetPixels32(); var territoryPixels = GameMap.Territory.GetPixels32(); for (int y = 0; y < mapHeight; y++) { for (int x = 0; x < mapWidth; x++) { int tileIndex = x + y * mapWidth; var tileEntity = tileEntities[tileIndex]; dstManager.SetName(tileEntity, $"Tile {x} {y}"); // define owner and MapTile type by pixel Color var owner = GameMap.Palette.GetPlayer(territoryPixels[tileIndex]); var terrainType = GameMap.Palette.GetTerrain(terrainPixels[tileIndex]); MapTileType type = terrainType; // owned empty tiles will become MapTileType.Tile, owned earth tiles will become MapTileType.Wall if (type == MapTileType.Empty && owner > 0) { type = MapTileType.Tile; } else if (type == MapTileType.Earth && owner > 0) { type = MapTileType.Wall; } // assign information to entity dstManager.SetComponentData(tileEntity, new MapTile { Type = type, Owner = owner, Position = new int2(x, y) }); dstManager.SetComponentData(tileEntity, new Translation { Value = new float3(x + 0.5f, 0, y + 0.5f) }); dstManager.SetComponentData(tileEntity, new Rotation { Value = quaternion.identity }); } } // construct BlobAsset containing the Entity id for every cell of the map using (BlobBuilder blobBuilder = new BlobBuilder(Allocator.Temp)) { ref TileMapBlobAsset tileMapAsset = ref blobBuilder.ConstructRoot <TileMapBlobAsset>(); BlobBuilderArray <Entity> tileArray = blobBuilder.Allocate(ref tileMapAsset.Map, mapWidth * mapHeight); //copy MapTile entities to blob array for (int t = 0; t < mapWidth * mapHeight; t++) { tileArray[t] = tileEntities[t]; } // create immutable BlobAssetReference var assetReference = blobBuilder.CreateBlobAssetReference <TileMapBlobAsset>(Allocator.Persistent); // assign BlobAssetReference to GameMap dstManager.AddComponentData(entity, new GameMap { TileMap = assetReference, Width = mapWidth, Height = mapHeight }); }