/// <param name="tile">The tile to get the transition for.</param> /// <param name="metadata">The metadata for the tile.</param> /// <param name="environment">The environment the tile is in.</param> /// <param name="pointOnTexture">The point on the texture the tile is on.</param> /// <returns>The texture for the specified tile.</returns> public Texture2D TextureForTile(Tile tile, TileMetadata metadata, Map.Environment environment, out Point pointOnTexture) { if (!HasTransition(tile, metadata, environment)) { GenerateTileTransition(tile, metadata, environment); } TileTransition transition = _cachedTransitions[tile.UniqueIdentity(metadata, environment)]; pointOnTexture = transition.Point; return(_textures[transition.Texture]); }
private void Awake() { if (_instance != null && _instance != this) { Destroy(gameObject); } else { _instance = this; } StartCoroutine(CoroutineCoordinator()); }
private void GenerateTileTransition(Tile tile, TileMetadata metadata, Map.Environment environment) { string transitionBase = tile.TransitionTexture(metadata, environment); if (!transitionBase.Contains("_color")) { if (!_masks.ContainsKey(transitionBase)) { _masks.Add(transitionBase, new BitArray(16 * 16)); Color[] data = new Color[16 * 16]; Game.Assets.Get <Texture2D>(transitionBase).GetData(data); if (!_usedAssets.Any(a => a == transitionBase)) { _usedAssets.Add(transitionBase); } for (int i = 0; i < data.Length; i++) { _masks[transitionBase][i] = data[i] == Color.Black; } Game.Assets.UnloadAsset(transitionBase); } } if (_textures.Count == 0 || _currentPointOnTexture.Y >= 16) { _textures.Add(new Texture2D(Game.GraphicsDevice, CACHE_ATLAS_SIZE, CACHE_ATLAS_SIZE)); _currentPointOnTexture = new Point(0, 0); } TileTransition transition = new TileTransition(); transition.Point = _currentPointOnTexture; transition.Texture = _textures.Count - 1; TileAtlas.Region region = Map.Atlas[tile.TextureName(metadata, environment)]; Rectangle textureRectangle = new Rectangle( region.Rectangle.X, region.Rectangle.Y, 16, 16); Color[] tileData = new Color[16 * 16]; region.Texture.GetData(0, textureRectangle, tileData, 0, 16 * 16); Color[] tileTransition = new Color[16 * 16]; if (transitionBase.Contains("_color")) { Game.Assets.Get <Texture2D>(transitionBase).GetData(tileTransition); if (!_usedAssets.Any(a => a == transitionBase)) { _usedAssets.Add(transitionBase); } Game.Assets.UnloadAsset(transitionBase); } else { for (int i = 0; i < tileTransition.Length; i++) { tileTransition[i] = _masks[transitionBase][i] ? tileData[i] : Color.Transparent; } } _textures.Last().SetData(0, new Rectangle(_currentPointOnTexture.X * 16, _currentPointOnTexture.Y * 16, 16, 16), tileTransition, 0, 16 * 16); _currentPointOnTexture.X++; if (_currentPointOnTexture.X >= 16) { _currentPointOnTexture.X = 0; _currentPointOnTexture.Y++; } _cachedTransitions.Add(tile.UniqueIdentity(metadata, environment), transition); }
public void buildLevel(Level level, Gate gate = null, Texture2D texture2D = null) { //Level level = AddLevel (); level.root = new Vector3(20f * (levels.Count - 1.0f), 0.0f, 0.0f); Texture2D leveltext; if (texture2D) { leveltext = texture2D; } else { leveltext = OtherLevels[Random.Range(0, OtherLevels.Length)]; } // Debug.Log("texture name " + leveltext.name); //Debug.Log("building level " + level.GetHashCode()); for (int x = 0; x < 11; x++) { for (int y = 0; y < 11; y++) { Color color = leveltext.GetPixel(x, y); Tile tile; if (color == Color.black) { tile = new TileTransition(level.root, x, y); } else { tile = new TileSimple(level.root, x, y); } tile.cord = new Cord(x, y); tile.level = level; //Debug.Log("tile " + x + " " + y + " assing"); level.tiles[x, y] = tile; if (objectPoolByColor.ContainsKey(color)) { ObjectOnTile objectOnTile = objectPoolByColor[color].Get() as ObjectOnTile; if (objectOnTile is SnakeHead) { onSnakeSpawn.Invoke(objectOnTile as SnakeHead); } if (objectOnTile is AEnemyBody) { EnemyAI enemyAI = GameManager.instanse.GetEnemyAI(); enemyAI.SetObject(objectOnTile as AEnemyBody); } objectOnTile.gameObject.SetActive(true); //Debug.Log("adding " + objectOnTile.GetType()); tile.AddObject(objectOnTile); } else { //Debug.Log($"{x}:{y} missing Color " + color); } } } //Debug.Log("init gates in level " + level.GetHashCode()); //Debug.Log("up"); Gate levelgate = new Gate { direction = Direction.Up }; //Debug.Log(levelgate.direction); level.GateByDirection.Add(levelgate.direction, levelgate); for (int i = 0; i < level.tiles.GetLength(1); i++) { if (level.tiles[level.tiles.GetLength(1) - 1, i] is TileTransition) { levelgate.tileTransitions.Add(level.tiles[level.tiles.GetLength(1) - 1, i] as TileTransition); } } //Debug.Log(levelgate.GetHashCode() + " Gate" + levelgate.direction + " " + levelgate.tileTransitions.Count); //Debug.Log("right"); levelgate = new Gate { direction = Direction.Right }; //Debug.Log(levelgate.direction); level.GateByDirection.Add(levelgate.direction, levelgate); for (int i = 0; i < level.tiles.GetLength(0); i++) { if (level.tiles[i, level.tiles.GetLength(0) - 1] is TileTransition) { levelgate.tileTransitions.Add(level.tiles[i, level.tiles.GetLength(0) - 1] as TileTransition); } } //Debug.Log(levelgate.GetHashCode() + " Gate" + levelgate.direction + " " + levelgate.tileTransitions.Count); //Debug.Log("down"); levelgate = new Gate { direction = Direction.Down }; //Debug.Log(levelgate.direction); level.GateByDirection.Add(levelgate.direction, levelgate); for (int i = 0; i < level.tiles.GetLength(0); i++) { if (level.tiles[0, i] is TileTransition) { levelgate.tileTransitions.Add(level.tiles[0, i] as TileTransition); } } //Debug.Log(levelgate.GetHashCode() + " Gate" + levelgate.direction + " " + levelgate.tileTransitions.Count); //Debug.Log("left"); levelgate = new Gate { direction = Direction.Left }; //Debug.Log(levelgate.direction); level.GateByDirection.Add(levelgate.direction, levelgate); for (int i = 0; i < level.tiles.GetLength(1); i++) { if (level.tiles[i, 0] is TileTransition) { levelgate.tileTransitions.Add(level.tiles[i, 0] as TileTransition); } } //Debug.Log(levelgate.GetHashCode() + " Gate" + levelgate.direction + " " + levelgate.tileTransitions.Count); if (gate != null) { //Debug.Log((Direction)(((int)gate.direction + 2) % 4)); gate.LinkGate(level.GateByDirection[(Direction)(((int)gate.direction + 2) % 4)]); } }