| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- namespace TowerDevil
- {
- public sealed partial class TowerGame
- {
- [Serializable] sealed class ArtPart
- {
- public string key;
- public int w, h, u, v;
- }
- [Serializable] sealed class KeepLayout { public ArtPart[] parts; }
- readonly Dictionary<string, ArtPart> keepParts = new Dictionary<string, ArtPart>();
- static readonly int[] PortraitIds = { 3, 0, 1, 2, 4, 5, 6, 7 };
- static readonly string[] UpgradeIcons = { "boots", "tower", "hammer", "scope" };
- static readonly string[] UpgradeEffects = { "+1 MOVE", "+1 TOWER", "+8% DAMAGE", "BOSS FORECAST" };
- static readonly string[] ChestIcons = { "woodChest", "silverChest", "goldChest", "legendChest" };
- Texture2D keepAtlas;
- KeepBackdrop keepBackdrop;
- string displayedPlayerID, gameCenterName;
- Texture2D gameCenterPhoto;
- Text headerName, profileName;
- RawImage headerPortrait, profilePortrait;
- RectTransform avatarChoices;
- void SetGameCenterProfile(string playerID, string displayName, byte[] png)
- {
- if (displayedPlayerID != playerID || png?.Length == 0)
- {
- Destroy(gameCenterPhoto);
- gameCenterPhoto = null;
- }
- displayedPlayerID = playerID;
- gameCenterName = displayName;
- if (png != null && png.Length > 0)
- {
- var photo = new Texture2D(2, 2, TextureFormat.RGBA32, false);
- if (photo.LoadImage(png, true))
- {
- Destroy(gameCenterPhoto);
- gameCenterPhoto = photo;
- }
- else Destroy(photo);
- }
- RefreshPlayerIdentity();
- }
- void RefreshPlayerIdentity()
- {
- string name = string.IsNullOrWhiteSpace(gameCenterName) ? profile.name : gameCenterName;
- if (headerName) headerName.text = name;
- if (profileName) profileName.text = name;
- UpdatePlayerPortrait(headerPortrait);
- UpdatePlayerPortrait(profilePortrait);
- if (avatarChoices) avatarChoices.gameObject.SetActive(!gameCenterPhoto);
- }
- void UpdatePlayerPortrait(RawImage image)
- {
- if (!image) return;
- if (gameCenterPhoto)
- {
- image.texture = gameCenterPhoto;
- image.uvRect = new Rect(0, 0, 1, 1);
- }
- else SetArtPart(image, keepParts["avatar" + Array.IndexOf(PortraitIds, profile.avatar)]);
- }
- void LoadKeepArt()
- {
- if (keepAtlas && keepParts.Count > 0) return;
- keepParts.Clear();
- keepAtlas = Art("Keep/controls");
- var json = Resources.Load<TextAsset>("DevilArt/Keep/layout");
- foreach (var part in JsonUtility.FromJson<KeepLayout>(json.text).parts)
- keepParts.Add(part.key, part);
- }
- void KeepScene(string routeName, string painting)
- {
- NewPage(routeName);
- LoadKeepArt();
- keepBackdrop = Box(page, "Keep scenery", 0, 0, 750, 1334).gameObject.AddComponent<KeepBackdrop>();
- keepBackdrop.SetPainting(Art("Keep/" + painting));
- keepBackdrop.Fit(page, screen);
- }
- RawImage DrawPart(Transform parent, string art, float x, float y, float w, float h, bool contain = false)
- {
- var part = keepParts[art];
- if (contain)
- {
- float scale = Mathf.Min(w / part.w, h / part.h);
- x += (w - part.w * scale) * .5f;
- y += (h - part.h * scale) * .5f;
- w = part.w * scale;
- h = part.h * scale;
- }
- var image = Box(parent, art, x, y, w, h).gameObject.AddComponent<RawImage>();
- SetArtPart(image, part);
- image.raycastTarget = false;
- return image;
- }
- void SetArtPart(RawImage image, ArtPart part)
- {
- image.texture = keepAtlas;
- image.uvRect = new Rect((float)part.u / keepAtlas.width, (float)part.v / keepAtlas.height,
- (float)part.w / keepAtlas.width, (float)part.h / keepAtlas.height);
- }
- Button ArtButton(Transform parent, string name, string art, int x, int y, int w, int h, Action action,
- bool enabled = true)
- {
- var face = DrawPart(parent, art, x, y, w, h);
- face.name = name;
- face.raycastTarget = true;
- var button = face.gameObject.AddComponent<Button>();
- button.targetGraphic = face;
- var colors = button.colors;
- colors.pressedColor = Hex("CBA079");
- colors.disabledColor = Hex("837E79");
- button.colors = colors;
- button.interactable = enabled;
- button.onClick.AddListener(() => { Sound(tapSound); action(); });
- return button;
- }
- RawImage KeepLayer(Transform parent, string art, int x, int y, int w, int h)
- {
- return Image(parent, art, "Keep/" + art, x, y, w, h);
- }
- Button LayeredButton(Transform parent, string name, string plate, int x, int y, int w, int h,
- Action action)
- {
- var button = Hotspot(parent, name, x, y, w, h, action);
- button.targetGraphic = KeepLayer(button.transform, plate, 0, 0, w, h);
- button.transition = Selectable.Transition.ColorTint;
- var colors = button.colors;
- colors.pressedColor = Hex("CBA079");
- colors.disabledColor = Hex("837E79");
- button.colors = colors;
- return button;
- }
- Text KeepText(Transform parent, string value, float x, float y, float w, float h, int size,
- TextAnchor align = TextAnchor.MiddleCenter)
- {
- var text = Label(parent, value, x, y, w, h, size, Ink, align, true);
- text.name = "Inscription";
- text.resizeTextForBestFit = true;
- text.resizeTextMinSize = Mathf.Min(size, 14);
- text.resizeTextMaxSize = size;
- var shadow = text.gameObject.AddComponent<Shadow>();
- shadow.effectDistance = new Vector2(1, -2);
- shadow.effectColor = new Color(.05f, .018f, .012f, .92f);
- return text;
- }
- RawImage KeepPortrait(Transform parent, string name, int id, float x, float y, float side)
- {
- LoadKeepArt();
- var rim = Round(parent, name, x, y, side, side, "221811", "B68A4B", side / 2);
- var clip = Round(rim.transform, "Portrait mask", 2, 2, side - 4, side - 4, "261D17", "261D17", side / 2);
- clip.gameObject.AddComponent<Mask>().showMaskGraphic = false;
- return DrawPart(clip.transform, "avatar" + Array.IndexOf(PortraitIds, id), 0, 0, side - 4, side - 4);
- }
- void ShowStart()
- {
- KeepScene("Start", "launch");
- var play = ArtButton(page, "PLAY", "play", 130, 949, 490, 128, ShowLobby);
- KeepText(play.transform, "PLAY", 18, 4, 454, 112, 72);
- }
- void ShowLobby()
- {
- KeepScene("Lobby", "keep");
- Panel(page, "Warden nameplate", 56, 62, 211, 57, "28201D", "96703F", 13, "161317");
- headerPortrait = KeepPortrait(page, "Warden portrait", profile.avatar, 24, 46, 86);
- headerName = KeepText(page, profile.name, 110, 66, 143, 46, 29);
- RefreshPlayerIdentity();
- Hotspot(page, "Open profile", 19, 39, 251, 98, ProfileSheet);
- var wallet = LayeredButton(page, "Gold wallet", "walletPlate", 468, 54, 192, 70, CoinStore);
- KeepLayer(wallet.transform, "walletCoin", 17, 18, 35, 35);
- KeepText(wallet.transform, profile.coins.ToString(), 57, 8, 116, 51, 27);
- ArtButton(page, "Game menu", "gear", 672, 59, 59, 59, Settings);
- KeepText(page, "YOUR KEEP", 119, 595, 512, 79, 58);
- var rail = Panel(page, "Garrison statistics", 36, 673, 678, 91, "322724", "A17A48", 12, "19171A");
- LobbyStat(rail.transform, 0, "shield", "SEAL", (5 + 2 * (profile.gateLevel - 1)).ToString());
- LobbyStat(rail.transform, 1, "boots", "MARCH", (5 + profile.workshop[0]).ToString());
- LobbyStat(rail.transform, 2, "hammer", "TEMPER", "+" + (8 * profile.workshop[2]) + "%");
- LobbyStat(rail.transform, 3, "tower", "VANGUARD", profile.workshop[1].ToString());
- var defend = LayeredButton(page, "DEFEND THE GATE", "defendPlate", 29, 768, 692, 184, StartGame);
- KeepLayer(defend.transform, "defendCrest", 49, 42, 96, 100);
- KeepText(defend.transform, "DEFEND THE GATE", 146, 49, 500, 86, 49);
- var forge = LayeredButton(page, "WORKSHOP", "workshopPlate", 163, 956, 424, 102, Workshop);
- KeepLayer(forge.transform, "workshopAnvil", 29, 17, 108, 67);
- KeepText(forge.transform, "WORKSHOP", 137, 11, 252, 44, 33);
- DrawPart(forge.transform, "shard", 223, 56, 22, 29, true);
- KeepText(forge.transform, profile.shards.ToString(), 252, 51, 93, 38, 25, TextAnchor.MiddleLeft);
- KeepText(page, "BEST NIGHT " + profile.bestNight, 255, 1072, 240, 44, 23);
- var rank = Hotspot(page, "RANK", 302, 1139, 146, 153, () => Rank(false));
- rank.targetGraphic = DrawPart(rank.transform, "crown", 17, 4, 112, 108, true);
- KeepText(rank.transform, "RANK", 0, 113, 146, 36, 25);
- }
- void LobbyStat(Transform rail, int index, string art, string label, string value)
- {
- int x = 8 + 165 * index;
- DrawPart(rail, art, x + 3, 18, 54, 55, true);
- KeepText(rail, label, x + 58, 11, 102, 30, 16);
- KeepText(rail, value, x + 58, 39, 102, 42, 28);
- if (index > 0) Solid(rail, "Divider", x - 3, 16, 1, 58, Hex("70533A"));
- }
- void OpenKeepVeil()
- {
- CloseSheet();
- modal = Box(frame, "Modal", 0, 0, 750, 1334);
- Solid(modal, "Veil", 0, 0, 750, 1334, new Color(.02f, .015f, .02f, .75f), true);
- FitModalVeil();
- }
- void OpenKeepSheet(string name, string skin, string title)
- {
- OpenKeepVeil();
- var face = Box(modal, name, 41, 157, 668, 1000).gameObject.AddComponent<RawImage>();
- face.raycastTarget = false;
- face.texture = Art("Keep/" + skin);
- float titleY = skin == "profile" ? 323 : 335;
- KeepText(modal, title, 129, titleY, 492, 73, 46);
- ArtButton(modal, "CLOSE", "close", 619, (int)titleY - 25, 72, 72, CloseSheet);
- }
- void OpenCompactMenu(string title, bool closable = true, Action onDismiss = null, int extraHeight = 0)
- {
- LoadKeepArt();
- OpenKeepVeil();
- sheetDismissed = onDismiss;
- int lift = extraHeight / 2;
- var face = Box(modal, title, 49, 334 - lift, 652, 666 + extraHeight).gameObject.AddComponent<Image>();
- face.sprite = Resources.Load<Sprite>("DevilArt/Keep/menu");
- face.type = UnityEngine.UI.Image.Type.Sliced;
- face.raycastTarget = false;
- KeepText(modal, title, 214, 468 - lift, 322, 58, 39);
- if (closable)
- ArtButton(modal, onDismiss == null ? "CLOSE" : "×", "close", 608, 434 - lift, 65, 65, DismissSheet);
- }
- void Settings()
- {
- OpenCompactMenu("GAME MENU");
- SettingsRows(577, 126);
- }
- void SettingsRows(int top, int spacing)
- {
- SetSwitch(top, "Sound effects", "bell", () => profile.sound, () => profile.sound = !profile.sound);
- refreshMusicSwitch = SetSwitch(top + spacing, "Music", "lyre", () => profile.music,
- () => { profile.music = !profile.music; SyncMusic(); });
- SetSwitch(top + 2 * spacing, "Haptics", "gauntlet", () => profile.haptics, () => profile.haptics = !profile.haptics);
- for (int i = 0; i < 2; i++)
- Solid(modal, "Divider", 127, top + i * spacing + (70 + spacing) / 2, 490, 1, Hex("765738"));
- }
- Action SetSwitch(int y, string name, string icon, Func<bool> isOn, Action toggle)
- {
- var row = Hotspot(modal, name, 124, y, 502, 70, () =>
- {
- toggle();
- profile.Save();
- });
- DrawPart(row.transform, icon, 4, 5, 60, 60, true);
- KeepText(row.transform, name.ToUpperInvariant(), 83, 0, 238, 70, 24, TextAnchor.MiddleLeft);
- var face = DrawPart(row.transform, "switchOn", 328, 7, 164, 56, true);
- var uv = face.uvRect;
- var stateText = KeepText(face.transform, "", 10, 0, 95, 55, 22);
- void ShowState()
- {
- bool on = isOn();
- face.uvRect = on ? uv : new Rect(uv.xMax, uv.y, -uv.width, uv.height);
- face.color = on ? Color.white : Hex("938477");
- stateText.text = on ? "ON" : "OFF";
- stateText.rectTransform.anchoredPosition = new Vector2(on ? 10 : 54, 0);
- }
- row.targetGraphic = face;
- row.transition = Selectable.Transition.ColorTint;
- row.onClick.AddListener(ShowState);
- ShowState();
- return ShowState;
- }
- void ProfileSheet()
- {
- OpenKeepSheet("WARDEN PROFILE", "profile", "WARDEN PROFILE");
- Panel(modal, "Portrait mount", 101, 446, 228, 246, "211A18", "A6814B", 4);
- profilePortrait = DrawPart(modal, "avatar" + Array.IndexOf(PortraitIds, profile.avatar), 105, 451, 220, 226);
- profileName = KeepText(modal, profile.name, 95, 691, 240, 47, 30);
- string[] names = { "Best Night", "Total Score", "Kills", "Towers Forged", "Coins" };
- int[] values = { profile.bestNight, profile.score, profile.kills, profile.forged, profile.coins };
- for (int i = 0; i < names.Length; i++)
- {
- int y = 458 + i * 49;
- KeepText(modal, names[i], 354, y, 175, 43, 22, TextAnchor.MiddleLeft);
- KeepText(modal, values[i].ToString(), 527, y, 109, 43, 24, TextAnchor.MiddleRight);
- Solid(modal, "Rule", 355, y + 42, 280, 1, Hex("684B32"));
- }
- avatarChoices = Box(modal, "Local avatar choices", 0, 0, 750, 1334);
- KeepText(avatarChoices, "CHOOSE AVATAR", 182, 745, 386, 49, 27);
- for (int i = 0; i < PortraitIds.Length; i++)
- {
- int id = PortraitIds[i], x = 112 + i % 4 * 135, y = 817 + i / 4 * 132;
- bool selected = profile.avatar == id;
- Round(avatarChoices, "Portrait rim", x, y, 118, 118, selected ? "E9BB60" : "5B4633",
- selected ? "FFCF72" : "503927", 59);
- KeepPortrait(avatarChoices, "Portrait " + id, id, x + 4, y + 4, 110);
- Hotspot(avatarChoices, "Choose " + id, x, y, 118, 118, () =>
- {
- profile.avatar = id;
- profile.Save();
- ShowLobby();
- ProfileSheet();
- });
- }
- RefreshPlayerIdentity();
- }
- void Workshop()
- {
- OpenKeepSheet("Keep forge", "forge", "WORKSHOP");
- DrawPart(modal, "shard", 315, 438, 40, 46, true);
- KeepText(modal, profile.shards.ToString(), 363, 435, 104, 50, 32, TextAnchor.MiddleLeft);
- for (int i = 0; i < Rules.UpgradeCosts.Length; i++)
- {
- int node = i, level = profile.workshop[i], x = 123 + i % 2 * 262, y = 518 + i / 2 * 209;
- var costs = Rules.UpgradeCosts[i];
- bool max = level >= costs.Length;
- var card = Panel(modal, "Upgrade card", x, y, 242, 185, "332923", "7F6141", 8, "1B181A");
- DrawPart(card.transform, UpgradeIcons[i], 12, 28, 68, 110, true);
- KeepText(card.transform, Rules.WorkshopNames[i], 83, 18, 149, 39, 24);
- KeepText(card.transform, UpgradeEffects[i], 83, 65, 149, 34, 17);
- var buy = LayeredButton(card.transform, Rules.WorkshopNames[i], "walletPlate", 84, 107, 147, 67, () =>
- {
- if (!profile.BuyUpgrade(node)) return;
- ShowLobby();
- Workshop();
- });
- buy.interactable = !max && profile.shards >= costs[level];
- DrawPart(buy.transform, "shard", 13, 20, 23, 28, true);
- KeepText(buy.transform, max ? "MAX" : costs[level].ToString(), 39, 10, 90, 46, 24);
- }
- long wait = WaitForAd(profile.lastShardAd);
- var ad = ArtButton(modal, "Shard reward", "bronze", 135, 969, 480, 69, () => Ad(() =>
- {
- if (WaitForAd(profile.lastShardAd) == 0)
- {
- profile.lastShardAd = Now;
- profile.shards += UnityEngine.Random.Range(1, 4);
- profile.Save();
- }
- ShowLobby();
- Workshop();
- }), wait == 0);
- KeepText(ad.transform, wait > 0 ? AdLabel("", wait) : "WATCH AD · +1–3 SHARDS", 18, 4, 444, 57, 23);
- }
- void Vault()
- {
- OpenKeepSheet("CHEST VAULT", "vault", "CHEST VAULT");
- DrawPart(modal, "coins", 310, 429, 52, 46, true);
- KeepText(modal, profile.coins.ToString(), 375, 426, 114, 48, 30, TextAnchor.MiddleLeft);
- for (int i = 0; i < Rules.ChestPrice.Length; i++)
- {
- int tier = i + 1, price = Rules.ChestPrice[i];
- bool affordable = profile.coins >= price;
- int x = 113 + i % 2 * 271, y = 488 + i / 2 * 290;
- var slot = Box(modal, "Chest display", x, y, 248, 283);
- DrawPart(slot, ChestIcons[i], 23, 2, 202, 136, true);
- KeepText(slot, Rules.ChestNames[i].ToUpperInvariant(), 0, 144, 248, 36, 24);
- KeepText(slot, "+" + Rules.ChestMoves[i] + " MOVES", 8, 181, 232, 28, 17);
- void Purchase()
- {
- if (!profile.BuyStoredChest(tier)) return;
- ShowLobby();
- Vault();
- }
- var buy = affordable
- ? ArtButton(slot, Rules.ChestNames[i], "goldPrice", 38, 216, 172, 53, Purchase)
- : LayeredButton(slot, Rules.ChestNames[i], "walletPlate", 38, 216, 172, 53, Purchase);
- if (!affordable)
- {
- KeepLayer(buy.transform, "walletCoin", 12, 11, 30, 30);
- buy.interactable = false;
- }
- KeepText(buy.transform, price.ToString(), 44, 1, 108, 47, 25);
- if (profile.stock[i] > 0)
- {
- var badge = Round(slot, "Stored chests", 176, 7, 57, 32, "39291B", "AD7B37", 11);
- KeepText(badge.transform, "×" + profile.stock[i], 1, 0, 55, 32, 20);
- }
- }
- }
- }
- }
|