using UnityEngine; using GatherAndDefend.LevelEditor; using System.Collections; using UnityEngine.Tilemaps; using System.Threading.Tasks; using System; public delegate Task PlacementAnimationHandler(Vector3 position, TileBase tile, Action tilePlacement); public class LevelManagerScript : SingletonBehaviour { private Action updateAction = null; public bool loadOnStart = false; public Level firstLevel; async void Start() { LevelManager.Instance.LevelLoaded += Instance_LevelLoaded; if (loadOnStart && firstLevel) { await LevelManager.Instance.LoadLevel(firstLevel, placementAnimation: PlacementAnimation); } } void OnDestroy() { updateAction = null; } void Update() { updateAction?.Invoke(); } private void Instance_LevelLoaded(Level level) { updateAction = LevelManager.Instance.UpdateLevel; } public static async Task PlacementAnimation(Vector3 position, TileBase tile, System.Action putTileOnTilemap) { float speed = 0; float acceleration = -9.81f; var tilePlaceholder = new GameObject("tile"); var rend = tilePlaceholder.AddComponent(); rend.sortingOrder = 10; rend.sortingLayerName = "Unit"; if (tile is LevelTile) { rend.sprite = (tile as LevelTile).Sprite; } else { rend.sprite = (tile as Tile).sprite; } tilePlaceholder.transform.position = position + Vector3.up * Camera.main.orthographicSize * 2; var delta = tilePlaceholder.transform.position - position; var direction = delta.normalized; var signY = delta.y / Mathf.Abs(delta.y); while (true) { if (!tilePlaceholder) break; speed += acceleration * Time.deltaTime; tilePlaceholder.transform.position += direction * Time.deltaTime * speed; var newDelta = tilePlaceholder.transform.position - position; var newSign = newDelta.y / Mathf.Abs(newDelta.y); if (newSign != signY) break; await Task.Yield(); } putTileOnTilemap.Invoke(); Destroy(tilePlaceholder); } }