KeepInterface.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace TowerDevil
  6. {
  7. public sealed partial class TowerGame
  8. {
  9. [Serializable] sealed class ArtPart
  10. {
  11. public string key;
  12. public int w, h, u, v;
  13. }
  14. [Serializable] sealed class KeepLayout { public ArtPart[] parts; }
  15. readonly Dictionary<string, ArtPart> keepParts = new Dictionary<string, ArtPart>();
  16. static readonly int[] PortraitIds = { 3, 0, 1, 2, 4, 5, 6, 7 };
  17. static readonly string[] UpgradeIcons = { "boots", "tower", "hammer", "scope" };
  18. static readonly string[] UpgradeEffects = { "+1 MOVE", "+1 TOWER", "+8% DAMAGE", "BOSS FORECAST" };
  19. static readonly string[] ChestIcons = { "woodChest", "silverChest", "goldChest", "legendChest" };
  20. Texture2D keepAtlas;
  21. KeepBackdrop keepBackdrop;
  22. string displayedPlayerID, gameCenterName;
  23. Texture2D gameCenterPhoto;
  24. Text headerName, profileName;
  25. RawImage headerPortrait, profilePortrait;
  26. RectTransform avatarChoices;
  27. void SetGameCenterProfile(string playerID, string displayName, byte[] png)
  28. {
  29. if (displayedPlayerID != playerID || png?.Length == 0)
  30. {
  31. Destroy(gameCenterPhoto);
  32. gameCenterPhoto = null;
  33. }
  34. displayedPlayerID = playerID;
  35. gameCenterName = displayName;
  36. if (png != null && png.Length > 0)
  37. {
  38. var photo = new Texture2D(2, 2, TextureFormat.RGBA32, false);
  39. if (photo.LoadImage(png, true))
  40. {
  41. Destroy(gameCenterPhoto);
  42. gameCenterPhoto = photo;
  43. }
  44. else Destroy(photo);
  45. }
  46. RefreshPlayerIdentity();
  47. }
  48. void RefreshPlayerIdentity()
  49. {
  50. string name = string.IsNullOrWhiteSpace(gameCenterName) ? profile.name : gameCenterName;
  51. if (headerName) headerName.text = name;
  52. if (profileName) profileName.text = name;
  53. UpdatePlayerPortrait(headerPortrait);
  54. UpdatePlayerPortrait(profilePortrait);
  55. if (avatarChoices) avatarChoices.gameObject.SetActive(!gameCenterPhoto);
  56. }
  57. void UpdatePlayerPortrait(RawImage image)
  58. {
  59. if (!image) return;
  60. if (gameCenterPhoto)
  61. {
  62. image.texture = gameCenterPhoto;
  63. image.uvRect = new Rect(0, 0, 1, 1);
  64. }
  65. else SetArtPart(image, keepParts["avatar" + Array.IndexOf(PortraitIds, profile.avatar)]);
  66. }
  67. void LoadKeepArt()
  68. {
  69. if (keepAtlas && keepParts.Count > 0) return;
  70. keepParts.Clear();
  71. keepAtlas = Art("Keep/controls");
  72. var json = Resources.Load<TextAsset>("DevilArt/Keep/layout");
  73. foreach (var part in JsonUtility.FromJson<KeepLayout>(json.text).parts)
  74. keepParts.Add(part.key, part);
  75. }
  76. void KeepScene(string routeName, string painting)
  77. {
  78. NewPage(routeName);
  79. LoadKeepArt();
  80. keepBackdrop = Box(page, "Keep scenery", 0, 0, 750, 1334).gameObject.AddComponent<KeepBackdrop>();
  81. keepBackdrop.SetPainting(Art("Keep/" + painting));
  82. keepBackdrop.Fit(page, screen);
  83. }
  84. RawImage DrawPart(Transform parent, string art, float x, float y, float w, float h, bool contain = false)
  85. {
  86. var part = keepParts[art];
  87. if (contain)
  88. {
  89. float scale = Mathf.Min(w / part.w, h / part.h);
  90. x += (w - part.w * scale) * .5f;
  91. y += (h - part.h * scale) * .5f;
  92. w = part.w * scale;
  93. h = part.h * scale;
  94. }
  95. var image = Box(parent, art, x, y, w, h).gameObject.AddComponent<RawImage>();
  96. SetArtPart(image, part);
  97. image.raycastTarget = false;
  98. return image;
  99. }
  100. void SetArtPart(RawImage image, ArtPart part)
  101. {
  102. image.texture = keepAtlas;
  103. image.uvRect = new Rect((float)part.u / keepAtlas.width, (float)part.v / keepAtlas.height,
  104. (float)part.w / keepAtlas.width, (float)part.h / keepAtlas.height);
  105. }
  106. Button ArtButton(Transform parent, string name, string art, int x, int y, int w, int h, Action action,
  107. bool enabled = true)
  108. {
  109. var face = DrawPart(parent, art, x, y, w, h);
  110. face.name = name;
  111. face.raycastTarget = true;
  112. var button = face.gameObject.AddComponent<Button>();
  113. button.targetGraphic = face;
  114. var colors = button.colors;
  115. colors.pressedColor = Hex("CBA079");
  116. colors.disabledColor = Hex("837E79");
  117. button.colors = colors;
  118. button.interactable = enabled;
  119. button.onClick.AddListener(() => { Sound(tapSound); action(); });
  120. return button;
  121. }
  122. RawImage KeepLayer(Transform parent, string art, int x, int y, int w, int h)
  123. {
  124. return Image(parent, art, "Keep/" + art, x, y, w, h);
  125. }
  126. Button LayeredButton(Transform parent, string name, string plate, int x, int y, int w, int h,
  127. Action action)
  128. {
  129. var button = Hotspot(parent, name, x, y, w, h, action);
  130. button.targetGraphic = KeepLayer(button.transform, plate, 0, 0, w, h);
  131. button.transition = Selectable.Transition.ColorTint;
  132. var colors = button.colors;
  133. colors.pressedColor = Hex("CBA079");
  134. colors.disabledColor = Hex("837E79");
  135. button.colors = colors;
  136. return button;
  137. }
  138. Text KeepText(Transform parent, string value, float x, float y, float w, float h, int size,
  139. TextAnchor align = TextAnchor.MiddleCenter)
  140. {
  141. var text = Label(parent, value, x, y, w, h, size, Ink, align, true);
  142. text.name = "Inscription";
  143. text.resizeTextForBestFit = true;
  144. text.resizeTextMinSize = Mathf.Min(size, 14);
  145. text.resizeTextMaxSize = size;
  146. var shadow = text.gameObject.AddComponent<Shadow>();
  147. shadow.effectDistance = new Vector2(1, -2);
  148. shadow.effectColor = new Color(.05f, .018f, .012f, .92f);
  149. return text;
  150. }
  151. RawImage KeepPortrait(Transform parent, string name, int id, float x, float y, float side)
  152. {
  153. LoadKeepArt();
  154. var rim = Round(parent, name, x, y, side, side, "221811", "B68A4B", side / 2);
  155. var clip = Round(rim.transform, "Portrait mask", 2, 2, side - 4, side - 4, "261D17", "261D17", side / 2);
  156. clip.gameObject.AddComponent<Mask>().showMaskGraphic = false;
  157. return DrawPart(clip.transform, "avatar" + Array.IndexOf(PortraitIds, id), 0, 0, side - 4, side - 4);
  158. }
  159. void ShowStart()
  160. {
  161. KeepScene("Start", "launch");
  162. var play = ArtButton(page, "PLAY", "play", 130, 949, 490, 128, ShowLobby);
  163. KeepText(play.transform, "PLAY", 18, 4, 454, 112, 72);
  164. }
  165. void ShowLobby()
  166. {
  167. KeepScene("Lobby", "keep");
  168. Panel(page, "Warden nameplate", 56, 62, 211, 57, "28201D", "96703F", 13, "161317");
  169. headerPortrait = KeepPortrait(page, "Warden portrait", profile.avatar, 24, 46, 86);
  170. headerName = KeepText(page, profile.name, 110, 66, 143, 46, 29);
  171. RefreshPlayerIdentity();
  172. Hotspot(page, "Open profile", 19, 39, 251, 98, ProfileSheet);
  173. var wallet = LayeredButton(page, "Gold wallet", "walletPlate", 468, 54, 192, 70, CoinStore);
  174. KeepLayer(wallet.transform, "walletCoin", 17, 18, 35, 35);
  175. KeepText(wallet.transform, profile.coins.ToString(), 57, 8, 116, 51, 27);
  176. ArtButton(page, "Game menu", "gear", 672, 59, 59, 59, Settings);
  177. KeepText(page, "YOUR KEEP", 119, 595, 512, 79, 58);
  178. var rail = Panel(page, "Garrison statistics", 36, 673, 678, 91, "322724", "A17A48", 12, "19171A");
  179. LobbyStat(rail.transform, 0, "shield", "SEAL", (5 + 2 * (profile.gateLevel - 1)).ToString());
  180. LobbyStat(rail.transform, 1, "boots", "MARCH", (5 + profile.workshop[0]).ToString());
  181. LobbyStat(rail.transform, 2, "hammer", "TEMPER", "+" + (8 * profile.workshop[2]) + "%");
  182. LobbyStat(rail.transform, 3, "tower", "VANGUARD", profile.workshop[1].ToString());
  183. var defend = LayeredButton(page, "DEFEND THE GATE", "defendPlate", 29, 768, 692, 184, StartGame);
  184. KeepLayer(defend.transform, "defendCrest", 49, 42, 96, 100);
  185. KeepText(defend.transform, "DEFEND THE GATE", 146, 49, 500, 86, 49);
  186. var forge = LayeredButton(page, "WORKSHOP", "workshopPlate", 163, 956, 424, 102, Workshop);
  187. KeepLayer(forge.transform, "workshopAnvil", 29, 17, 108, 67);
  188. KeepText(forge.transform, "WORKSHOP", 137, 11, 252, 44, 33);
  189. DrawPart(forge.transform, "shard", 223, 56, 22, 29, true);
  190. KeepText(forge.transform, profile.shards.ToString(), 252, 51, 93, 38, 25, TextAnchor.MiddleLeft);
  191. KeepText(page, "BEST NIGHT " + profile.bestNight, 255, 1072, 240, 44, 23);
  192. var rank = Hotspot(page, "RANK", 302, 1139, 146, 153, () => Rank(false));
  193. rank.targetGraphic = DrawPart(rank.transform, "crown", 17, 4, 112, 108, true);
  194. KeepText(rank.transform, "RANK", 0, 113, 146, 36, 25);
  195. }
  196. void LobbyStat(Transform rail, int index, string art, string label, string value)
  197. {
  198. int x = 8 + 165 * index;
  199. DrawPart(rail, art, x + 3, 18, 54, 55, true);
  200. KeepText(rail, label, x + 58, 11, 102, 30, 16);
  201. KeepText(rail, value, x + 58, 39, 102, 42, 28);
  202. if (index > 0) Solid(rail, "Divider", x - 3, 16, 1, 58, Hex("70533A"));
  203. }
  204. void OpenKeepVeil()
  205. {
  206. CloseSheet();
  207. modal = Box(frame, "Modal", 0, 0, 750, 1334);
  208. Solid(modal, "Veil", 0, 0, 750, 1334, new Color(.02f, .015f, .02f, .75f), true);
  209. FitModalVeil();
  210. }
  211. void OpenKeepSheet(string name, string skin, string title)
  212. {
  213. OpenKeepVeil();
  214. var face = Box(modal, name, 41, 157, 668, 1000).gameObject.AddComponent<RawImage>();
  215. face.raycastTarget = false;
  216. face.texture = Art("Keep/" + skin);
  217. float titleY = skin == "profile" ? 323 : 335;
  218. KeepText(modal, title, 129, titleY, 492, 73, 46);
  219. ArtButton(modal, "CLOSE", "close", 619, (int)titleY - 25, 72, 72, CloseSheet);
  220. }
  221. void OpenCompactMenu(string title, bool closable = true, Action onDismiss = null, int extraHeight = 0)
  222. {
  223. LoadKeepArt();
  224. OpenKeepVeil();
  225. sheetDismissed = onDismiss;
  226. int lift = extraHeight / 2;
  227. var face = Box(modal, title, 49, 334 - lift, 652, 666 + extraHeight).gameObject.AddComponent<Image>();
  228. face.sprite = Resources.Load<Sprite>("DevilArt/Keep/menu");
  229. face.type = UnityEngine.UI.Image.Type.Sliced;
  230. face.raycastTarget = false;
  231. KeepText(modal, title, 214, 468 - lift, 322, 58, 39);
  232. if (closable)
  233. ArtButton(modal, onDismiss == null ? "CLOSE" : "×", "close", 608, 434 - lift, 65, 65, DismissSheet);
  234. }
  235. void Settings()
  236. {
  237. OpenCompactMenu("GAME MENU");
  238. SettingsRows(577, 126);
  239. }
  240. void SettingsRows(int top, int spacing)
  241. {
  242. SetSwitch(top, "Sound effects", "bell", () => profile.sound, () => profile.sound = !profile.sound);
  243. refreshMusicSwitch = SetSwitch(top + spacing, "Music", "lyre", () => profile.music,
  244. () => { profile.music = !profile.music; SyncMusic(); });
  245. SetSwitch(top + 2 * spacing, "Haptics", "gauntlet", () => profile.haptics, () => profile.haptics = !profile.haptics);
  246. for (int i = 0; i < 2; i++)
  247. Solid(modal, "Divider", 127, top + i * spacing + (70 + spacing) / 2, 490, 1, Hex("765738"));
  248. }
  249. Action SetSwitch(int y, string name, string icon, Func<bool> isOn, Action toggle)
  250. {
  251. var row = Hotspot(modal, name, 124, y, 502, 70, () =>
  252. {
  253. toggle();
  254. profile.Save();
  255. });
  256. DrawPart(row.transform, icon, 4, 5, 60, 60, true);
  257. KeepText(row.transform, name.ToUpperInvariant(), 83, 0, 238, 70, 24, TextAnchor.MiddleLeft);
  258. var face = DrawPart(row.transform, "switchOn", 328, 7, 164, 56, true);
  259. var uv = face.uvRect;
  260. var stateText = KeepText(face.transform, "", 10, 0, 95, 55, 22);
  261. void ShowState()
  262. {
  263. bool on = isOn();
  264. face.uvRect = on ? uv : new Rect(uv.xMax, uv.y, -uv.width, uv.height);
  265. face.color = on ? Color.white : Hex("938477");
  266. stateText.text = on ? "ON" : "OFF";
  267. stateText.rectTransform.anchoredPosition = new Vector2(on ? 10 : 54, 0);
  268. }
  269. row.targetGraphic = face;
  270. row.transition = Selectable.Transition.ColorTint;
  271. row.onClick.AddListener(ShowState);
  272. ShowState();
  273. return ShowState;
  274. }
  275. void ProfileSheet()
  276. {
  277. OpenKeepSheet("WARDEN PROFILE", "profile", "WARDEN PROFILE");
  278. Panel(modal, "Portrait mount", 101, 446, 228, 246, "211A18", "A6814B", 4);
  279. profilePortrait = DrawPart(modal, "avatar" + Array.IndexOf(PortraitIds, profile.avatar), 105, 451, 220, 226);
  280. profileName = KeepText(modal, profile.name, 95, 691, 240, 47, 30);
  281. string[] names = { "Best Night", "Total Score", "Kills", "Towers Forged", "Coins" };
  282. int[] values = { profile.bestNight, profile.score, profile.kills, profile.forged, profile.coins };
  283. for (int i = 0; i < names.Length; i++)
  284. {
  285. int y = 458 + i * 49;
  286. KeepText(modal, names[i], 354, y, 175, 43, 22, TextAnchor.MiddleLeft);
  287. KeepText(modal, values[i].ToString(), 527, y, 109, 43, 24, TextAnchor.MiddleRight);
  288. Solid(modal, "Rule", 355, y + 42, 280, 1, Hex("684B32"));
  289. }
  290. avatarChoices = Box(modal, "Local avatar choices", 0, 0, 750, 1334);
  291. KeepText(avatarChoices, "CHOOSE AVATAR", 182, 745, 386, 49, 27);
  292. for (int i = 0; i < PortraitIds.Length; i++)
  293. {
  294. int id = PortraitIds[i], x = 112 + i % 4 * 135, y = 817 + i / 4 * 132;
  295. bool selected = profile.avatar == id;
  296. Round(avatarChoices, "Portrait rim", x, y, 118, 118, selected ? "E9BB60" : "5B4633",
  297. selected ? "FFCF72" : "503927", 59);
  298. KeepPortrait(avatarChoices, "Portrait " + id, id, x + 4, y + 4, 110);
  299. Hotspot(avatarChoices, "Choose " + id, x, y, 118, 118, () =>
  300. {
  301. profile.avatar = id;
  302. profile.Save();
  303. ShowLobby();
  304. ProfileSheet();
  305. });
  306. }
  307. RefreshPlayerIdentity();
  308. }
  309. void Workshop()
  310. {
  311. OpenKeepSheet("Keep forge", "forge", "WORKSHOP");
  312. DrawPart(modal, "shard", 315, 438, 40, 46, true);
  313. KeepText(modal, profile.shards.ToString(), 363, 435, 104, 50, 32, TextAnchor.MiddleLeft);
  314. for (int i = 0; i < Rules.UpgradeCosts.Length; i++)
  315. {
  316. int node = i, level = profile.workshop[i], x = 123 + i % 2 * 262, y = 518 + i / 2 * 209;
  317. var costs = Rules.UpgradeCosts[i];
  318. bool max = level >= costs.Length;
  319. var card = Panel(modal, "Upgrade card", x, y, 242, 185, "332923", "7F6141", 8, "1B181A");
  320. DrawPart(card.transform, UpgradeIcons[i], 12, 28, 68, 110, true);
  321. KeepText(card.transform, Rules.WorkshopNames[i], 83, 18, 149, 39, 24);
  322. KeepText(card.transform, UpgradeEffects[i], 83, 65, 149, 34, 17);
  323. var buy = LayeredButton(card.transform, Rules.WorkshopNames[i], "walletPlate", 84, 107, 147, 67, () =>
  324. {
  325. if (!profile.BuyUpgrade(node)) return;
  326. ShowLobby();
  327. Workshop();
  328. });
  329. buy.interactable = !max && profile.shards >= costs[level];
  330. DrawPart(buy.transform, "shard", 13, 20, 23, 28, true);
  331. KeepText(buy.transform, max ? "MAX" : costs[level].ToString(), 39, 10, 90, 46, 24);
  332. }
  333. long wait = WaitForAd(profile.lastShardAd);
  334. var ad = ArtButton(modal, "Shard reward", "bronze", 135, 969, 480, 69, () => Ad(() =>
  335. {
  336. if (WaitForAd(profile.lastShardAd) == 0)
  337. {
  338. profile.lastShardAd = Now;
  339. profile.shards += UnityEngine.Random.Range(1, 4);
  340. profile.Save();
  341. }
  342. ShowLobby();
  343. Workshop();
  344. }), wait == 0);
  345. KeepText(ad.transform, wait > 0 ? AdLabel("", wait) : "WATCH AD · +1–3 SHARDS", 18, 4, 444, 57, 23);
  346. }
  347. void Vault()
  348. {
  349. OpenKeepSheet("CHEST VAULT", "vault", "CHEST VAULT");
  350. DrawPart(modal, "coins", 310, 429, 52, 46, true);
  351. KeepText(modal, profile.coins.ToString(), 375, 426, 114, 48, 30, TextAnchor.MiddleLeft);
  352. for (int i = 0; i < Rules.ChestPrice.Length; i++)
  353. {
  354. int tier = i + 1, price = Rules.ChestPrice[i];
  355. bool affordable = profile.coins >= price;
  356. int x = 113 + i % 2 * 271, y = 488 + i / 2 * 290;
  357. var slot = Box(modal, "Chest display", x, y, 248, 283);
  358. DrawPart(slot, ChestIcons[i], 23, 2, 202, 136, true);
  359. KeepText(slot, Rules.ChestNames[i].ToUpperInvariant(), 0, 144, 248, 36, 24);
  360. KeepText(slot, "+" + Rules.ChestMoves[i] + " MOVES", 8, 181, 232, 28, 17);
  361. void Purchase()
  362. {
  363. if (!profile.BuyStoredChest(tier)) return;
  364. ShowLobby();
  365. Vault();
  366. }
  367. var buy = affordable
  368. ? ArtButton(slot, Rules.ChestNames[i], "goldPrice", 38, 216, 172, 53, Purchase)
  369. : LayeredButton(slot, Rules.ChestNames[i], "walletPlate", 38, 216, 172, 53, Purchase);
  370. if (!affordable)
  371. {
  372. KeepLayer(buy.transform, "walletCoin", 12, 11, 30, 30);
  373. buy.interactable = false;
  374. }
  375. KeepText(buy.transform, price.ToString(), 44, 1, 108, 47, 25);
  376. if (profile.stock[i] > 0)
  377. {
  378. var badge = Round(slot, "Stored chests", 176, 7, 57, 32, "39291B", "AD7B37", 11);
  379. KeepText(badge.transform, "×" + profile.stock[i], 1, 0, 55, 32, 20);
  380. }
  381. }
  382. }
  383. }
  384. }