using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; namespace TowerDevil { public sealed partial class TowerGame : MonoBehaviour { const string SaveKey = "TowerDevil.iOS.profile.v1"; static readonly Color Ink = Hex("F8E9CC"), Gold = Hex("E6B36F"), Muted = Hex("BEAEA0"); readonly Dictionary textures = new Dictionary(); RectTransform screen, frame, page, modal, toastRoot; RawImage environment; Font sans, serif; Profile profile; Run run; string route; int tutorial = -1; Rect lastSafe; Vector2Int lastSize; Vector2 lastCanvasSize; AudioSource effects, music; AudioClip tapSound, forgeSound, shotSound, gateSound, tune; public Run CurrentRun => run; public string Page => route; public bool CanUseBoard => route == "Run" && run != null && run.CanArrange && modal == null && !animating && tutorial <= 0; void Awake() { sans = Font.CreateDynamicFontFromOSFont(new[] { "Arial", "Helvetica Neue", "Helvetica" }, 24); if (!sans) sans = Resources.GetBuiltinResource("LegacyRuntime.ttf"); serif = Font.CreateDynamicFontFromOSFont(new[] { "Georgia", "Times New Roman", "Times" }, 32); if (!serif) serif = sans; profile = Profile.Decode(PlayerPrefs.GetString(SaveKey, "")); profile.OnSave = () => { PlayerPrefs.SetString(SaveKey, JsonUtility.ToJson(profile)); PlayerPrefs.Save(); }; effects = gameObject.AddComponent(); effects.playOnAwake = false; music = gameObject.AddComponent(); music.playOnAwake = false; music.loop = true; tapSound = Tone("Tap", 330, .07f); forgeSound = Tone("Forge", 630, .2f); shotSound = Tone("Shot", 116, .09f); gateSound = Tone("Gate", 53, .32f); tune = Ambience(); music.clip = tune; music.volume = .14f; SyncMusic(); var canvasObject = new GameObject("Game Canvas", typeof(RectTransform), typeof(Canvas), typeof(GraphicRaycaster)); canvasObject.transform.SetParent(transform, false); var canvas = canvasObject.GetComponent(); canvas.renderMode = RenderMode.ScreenSpaceOverlay; screen = (RectTransform)canvasObject.transform; if (!EventSystem.current) new GameObject("Event System", typeof(EventSystem), typeof(StandaloneInputModule)) .transform.SetParent(transform, false); environment = Image(screen, "Environment", "bg_board", 0, 0, 750, 1334, false); Stretch(environment.rectTransform); var wash = Solid(screen, "Environment shadow", 0, 0, 750, 1334, new Color(.04f, .025f, .035f, .57f)); Stretch(wash.rectTransform); frame = Box(screen, "Safe play area", 0, 0, 750, 1334); frame.pivot = new Vector2(.5f, .5f); frame.anchorMin = frame.anchorMax = new Vector2(.5f, .5f); Fit(); Application.targetFrameRate = 60; ShowLoading(); } void Start() { StartCoroutine(Boot()); } IEnumerator Boot() { yield return new WaitForSecondsRealtime(.8f); if (route != "Loading") yield break; if (string.IsNullOrEmpty(profile.auth)) ShowStart(); else if (!profile.played) StartGame(); else ShowLobby(); } void OnEnable() => Canvas.preWillRenderCanvases += CheckLayout; void OnDisable() => Canvas.preWillRenderCanvases -= CheckLayout; void CheckLayout() { if (frame && (lastSize.x != Screen.width || lastSize.y != Screen.height || lastSafe != Screen.safeArea || lastCanvasSize != screen.rect.size)) Fit(); } void Fit() { lastSize = new Vector2Int(Mathf.Max(1, Screen.width), Mathf.Max(1, Screen.height)); lastSafe = Screen.safeArea; lastCanvasSize = screen.rect.size; if (lastCanvasSize.x < 1 || lastCanvasSize.y < 1) return; var safe = lastSafe; if (safe.width < 1 || safe.height < 1) safe = new Rect(Vector2.zero, lastSize); // The simulator and Game View can render the same canvas at different pixel sizes. var ratio = lastCanvasSize / (Vector2)lastSize; safe = new Rect(safe.position * ratio, safe.size * ratio); float factor = Mathf.Min(safe.width / 750f, safe.height / 1334f); frame.localScale = new Vector3(factor, factor, 1); frame.anchoredPosition = safe.center - lastCanvasSize * .5f; if (route == "Run" && battleFooter) FitBattle(safe.height / factor); if (modal) FitModalVeil(); if (environment && environment.texture) Cover(environment, lastCanvasSize.x, lastCanvasSize.y); } void Update() { CheckLayout(); if (route == "Run") UpdateRun(); if (Input.GetKeyDown(KeyCode.Escape)) { if (modal != null && !watchingAd) { if (run?.Phase != Phase.RestChoice) CloseSheet(); } else if (route == "Run" && !animating) PauseSheet(); } } static Color Hex(string s) { ColorUtility.TryParseHtmlString("#" + s, out var color); return color; } static RectTransform Box(Transform parent, string name, float x, float y, float w, float h) { var rt = new GameObject(name, typeof(RectTransform)).GetComponent(); rt.SetParent(parent, false); rt.anchorMin = rt.anchorMax = rt.pivot = new Vector2(0, 1); rt.anchoredPosition = new Vector2(x, -y); rt.sizeDelta = new Vector2(w, h); return rt; } static void Stretch(RectTransform rt) { rt.anchorMin = Vector2.zero; rt.anchorMax = Vector2.one; rt.offsetMin = rt.offsetMax = Vector2.zero; } static Image Solid(Transform parent, string name, float x, float y, float w, float h, Color color, bool hit = false) { var im = Box(parent, name, x, y, w, h).gameObject.AddComponent(); im.color = color; im.raycastTarget = hit; return im; } static IronPanel Panel(Transform p, string name, float x, float y, float w, float h, string face, string rim, float bevel = 5, string lower = null) { var g = Box(p, name, x, y, w, h).gameObject.AddComponent(); g.top = Hex(face); g.bottom = Hex(lower ?? face); g.border = Hex(rim); g.bevel = bevel; g.raycastTarget = false; return g; } Texture2D Art(string key) { if (!textures.TryGetValue(key, out var t)) { t = Resources.Load("DevilArt/" + key); textures[key] = t; } return t; } RawImage Image(Transform parent, string name, string art, float x, float y, float w, float h, bool contain = true) { var im = Box(parent, name, x, y, w, h).gameObject.AddComponent(); im.raycastTarget = false; Vector2 source = Vector2.zero; if (contain && route == "Run" && courtSprites.TryGetValue(art, out var sprite)) source = SetRegion(im, sprite, false); else { im.texture = Art(art); if (im.texture) source = new Vector2(im.texture.width, im.texture.height); } if (im.texture) { if (contain) { float scale = Mathf.Min(w / source.x, h / source.y); var size = source * scale; im.rectTransform.sizeDelta = size; im.rectTransform.anchoredPosition = new Vector2(x + (w - size.x) * .5f, -y - (h - size.y) * .5f); } else Cover(im, w, h); } return im; } static void Cover(RawImage im, float w, float h) { if (!im.texture || h <= 0 || w <= 0) return; float ratio = (w / h) / ((float)im.texture.width / im.texture.height); float u = Mathf.Min(1, ratio), v = Mathf.Min(1, 1 / ratio); im.uvRect = new Rect((1 - u) * .5f, (1 - v) * .5f, u, v); } Text Label(Transform p, string value, float x, float y, float w, float h, int size, Color color, TextAnchor anchor = TextAnchor.MiddleLeft, bool title = false, bool bold = true) { var label = Box(p, value, x, y, w, h).gameObject.AddComponent(); label.font = title ? serif : sans; label.fontSize = size; label.fontStyle = bold ? FontStyle.Bold : FontStyle.Normal; label.text = value; label.color = color; label.alignment = anchor; label.supportRichText = false; label.raycastTarget = false; label.horizontalOverflow = HorizontalWrapMode.Wrap; label.verticalOverflow = VerticalWrapMode.Truncate; return label; } Button ButtonAt(Transform p, string caption, float x, float y, float w, float h, Action action, bool main = false, int size = 24, bool enabled = true) { var plate = Panel(p, caption, x, y, w, h, main ? "F4A34A" : "353033", main ? "F8CE88" : "896B4E", main ? 9 : 6, main ? "B95620" : "201C20"); plate.raycastTarget = true; var button = plate.gameObject.AddComponent