using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using TMPro; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; namespace BuffaloSiege { public partial class GameApp : MonoBehaviour { public ArtCatalog art; public Canvas canvas; public RectTransform safeRoot, pageRoot, overlayRoot; public SaveStore Store { get; private set; } public BattleSimulation Battle { get; private set; } public string Page { get; private set; } public string Modal => modals.Count > 0 ? modals[modals.Count - 1].Id : ""; public BattleView battleView; UIFactory ui; GameAudio audioSystem; readonly List modals = new List(); TMP_Text toastLabel; float toastUntil; float stepAccumulator, hudTimer; int speed = 1; bool appPaused; BattleResult result; class ModalFrame { public string Id; public GameObject Root; public bool Locked; } public bool IsBattlePaused => appPaused || modals.Count > 0; public void BuildShell() { var go = new GameObject("Canvas_750x1334", typeof(RectTransform), typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster)); go.transform.SetParent(transform, false); canvas = go.GetComponent(); canvas.renderMode = RenderMode.ScreenSpaceOverlay; canvas.sortingOrder = 0; var scaler = go.GetComponent(); scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; scaler.referenceResolution = art.referenceResolution; scaler.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight; scaler.matchWidthOrHeight = 0; ui = new UIFactory(art); var bg = ui.Image(go.transform, "illustration.menu-desert", 0, 0, 750, 1334, false); UIFactory.Fill(bg.rectTransform); var fit = bg.gameObject.AddComponent(); fit.aspectMode = AspectRatioFitter.AspectMode.EnvelopeParent; fit.aspectRatio = 941f / 1672; var tint = ui.Solid(go.transform, "Backdrop tint", new Color(.13f, .06f, .09f, .24f), 0, 0, 750, 1334); UIFactory.Fill(tint.rectTransform); safeRoot = UIFactory.Root("Safe Area", go.transform); UIFactory.Fill(safeRoot); safeRoot.gameObject.AddComponent(); pageRoot = UIFactory.Root("Pages", safeRoot); UIFactory.Fill(pageRoot); overlayRoot = UIFactory.Root("Modal Layer", safeRoot); UIFactory.Fill(overlayRoot); if (!FindFirstObjectByType()) new GameObject("Event System", typeof(EventSystem), typeof(StandaloneInputModule)); } void Awake() { Application.targetFrameRate = 60; Screen.sleepTimeout = SleepTimeout.NeverSleep; } IEnumerator Start() { AppRuntimes.Initialization(); Store = new SaveStore(Path.Combine(Application.persistentDataPath, "buffalo-siege-v1.json")); if (!canvas) BuildShell(); SetupFactory(); audioSystem = gameObject.AddComponent(); audioSystem.Initialize(Store); Loading(); yield return null; yield return new WaitForSecondsRealtime(.65f); ShowStart(); if (Store.Recovered) Toast("Local save recovered from backup."); } void SetupFactory() { ui = new UIFactory(art) { ClickSound = () => audioSystem?.Play("click") }; } public void PreviewLobby() { Store = new SaveStore(null); SetupFactory(); ShowLobby(); } #if UNITY_EDITOR public void UseTestSave() { StopAllCoroutines(); if (Battle != null) Battle = null; Store = new SaveStore(null); if (audioSystem) audioSystem.Initialize(Store); SetupFactory(); ShowLobby(); } #endif void ResetPage(string name) { Page = name; UIFactory.Clear(pageRoot); UIFactory.Clear(overlayRoot); modals.Clear(); toastLabel = null; hudGold = null; hudHp = null; hudWave = null; hudScore = null; if (name != "battle" && battleView) { Destroy(battleView.gameObject); battleView = null; } if (name != "battle") Battle = null; } void Loading() { ResetPage("loading"); ui.Image(pageRoot, "brand.buffalo", 230, 250, 290, 290); var t = ui.Text(pageRoot, "BUFFALO SIEGE", 40, 540, 670, 90, 50, TextAlignmentOptions.Midline); t.font = art.brand; ui.Text(pageRoot, "Preparing the frontier...", 100, 650, 550, 50, 28, TextAlignmentOptions.Midline); ui.Panel(pageRoot, "Loading track", 140, 740, 470, 24, "ui.progress-track"); ui.Panel(pageRoot, "Loading progress", 146, 746, 452, 12, "ui.progress-fill"); } public async void ShowStart() { ResetPage("start"); var center = UIFactory.Root("Title composition", pageRoot); center.anchorMin = center.anchorMax = new Vector2(.5f, .5f); center.pivot = new Vector2(.5f, .5f); center.sizeDelta = new Vector2(750, 900); ui.Image(center, "brand.buffalo", 210, 30, 330, 330); var title = ui.Text(center, "BUFFALO\nSIEGE", 50, 350, 650, 210, 78, TextAlignmentOptions.Midline); title.font = art.brand; ui.Text(center, "D E S E R T D E F E N S E", 70, 575, 610, 45, 28, TextAlignmentOptions.Midline, art.gold, true); var playBtn = ui.Button(center, "PLAY", 145, 690, 460, 100, ShowLobby, true, 44); Footer(pageRoot, false); playBtn.interactable = false; if (!await AppRuntimes.CheckVersion()) { playBtn.interactable = true; } } void Header(string heading, bool back = false) { ui.Panel(pageRoot, "Header", 0, 0, 750, 104, "ui.button.wood"); if (back) { ui.IconButton(pageRoot, "back", 18, 17, 68, ShowLobby); ui.Text(pageRoot, heading, 104, 17, 390, 70, 38); } else { ui.Image(pageRoot, "brand.buffalo", 12, 3, 90, 90); var brand = ui.Text(pageRoot, "BUFFALO SIEGE", 102, 6, 385, 53, 33); brand.font = art.brand; ui.Text(pageRoot, "D E S E R T D E F E N S E", 107, 59, 350, 28, 18, TextAlignmentOptions.MidlineLeft, art.gold, true); } ui.IconButton(pageRoot, "calendar", 500, 17, 66, OpenCheckin); var coin = ui.Button(pageRoot, "", 578, 16, 156, 70, OpenShop); ui.Image(coin.transform, "ui.coin", 11, 16, 37, 37); ui.Text(coin.transform, Fmt(Store.Data.coins), 48, 7, 99, 53, 30, TextAlignmentOptions.Midline); if (Store.CanClaim(DateTime.Now)) ui.Image(pageRoot, "ui.badge", 547, 11, 20, 20); } void Footer(Transform parent, bool showLocalSave = true) { var r = UIFactory.Root("Footer", parent); r.anchorMin = new Vector2(0, 0); r.anchorMax = new Vector2(1, 0); r.pivot = new Vector2(0, 0); r.sizeDelta = new Vector2(0, 42); if (showLocalSave) ui.Text(r, "LOCAL SAVE", 24, 0, 300, 40, 19, TextAlignmentOptions.MidlineLeft, art.gold, true); ui.Text(r, "BUFFALO SIEGE 1.0", 380, 0, 346, 40, 19, TextAlignmentOptions.MidlineRight, art.gold, true); } public void ShowLobby() { ResetPage("lobby"); Header(""); var c = ui.Scroll(pageRoot, "Lobby scroll", 1081, 114, 129, 22, 22); var d = Store.Data; var profile = ui.Panel(c, "Player card", 0, 0, 706, 108); var hit = profile.gameObject.AddComponent