GameModel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEngine;
  6. namespace DiamondArrow
  7. {
  8. [Serializable] public struct Cell { public int x, y; public Cell(int x, int y) { this.x=x; this.y=y; } }
  9. [Serializable] public sealed class ArrowData { public Cell[] cells; }
  10. [Serializable] public sealed class LevelData { public int number, size, seconds; public ArrowData[] arrows; }
  11. [Serializable] public sealed class LevelBook
  12. {
  13. public LevelData[] levels;
  14. public static readonly string[] Chapters = { "Neon Alley", "Chrome Vault", "Cherry Circuit", "Jackpot Row", "Midnight Hall", "Diamond Crown" };
  15. public static string Chapter(int n) => Chapters[Mathf.Clamp((n-1)/5,0,5)];
  16. public static LevelBook Parse(string json) => JsonUtility.FromJson<LevelBook>(json.TrimStart('\uFEFF'));
  17. public List<string> Validate()
  18. {
  19. var errors = new List<string>();
  20. if (levels == null || levels.Length != 30) { errors.Add("Expected 30 levels"); return errors; }
  21. for (int n=0;n<levels.Length;n++)
  22. {
  23. var l=levels[n]; var occupied=new HashSet<int>();
  24. if(l.number!=n+1 || l.size<5 || l.size>11 || l.seconds<=0) errors.Add("Invalid level header "+(n+1));
  25. foreach(var a in l.arrows)
  26. {
  27. if(a.cells.Length<2) { errors.Add("Short arrow "+l.number); continue; }
  28. for(int i=0;i<a.cells.Length;i++)
  29. {
  30. var c=a.cells[i];
  31. if(c.x<0 || c.y<0 || c.x>=l.size || c.y>=l.size || !occupied.Add(c.y*l.size+c.x)) errors.Add("Overlapping/outside cell "+l.number);
  32. if(i>0 && Math.Abs(c.x-a.cells[i-1].x)+Math.Abs(c.y-a.cells[i-1].y)!=1) errors.Add("Disconnected arrow "+l.number);
  33. }
  34. }
  35. var game=new GameSession(l);
  36. while(game.Status==RunStatus.Playing)
  37. {
  38. var free=game.FreeArrows();
  39. if(free.Count==0) { errors.Add("Unsolvable level "+l.number); break; }
  40. game.Tap(free[0]);
  41. }
  42. }
  43. return errors;
  44. }
  45. }
  46. public enum RunStatus { Playing, Cleared, FailedHearts, FailedTime }
  47. public enum TapResult { Ignored, Blocked, Removed, Smashed }
  48. public sealed class GameSession
  49. {
  50. public readonly LevelData Level;
  51. public readonly bool[] Alive;
  52. public int Hearts=3, Mistakes, Remaining;
  53. public float TimeLeft, Elapsed;
  54. public bool HammerArmed, RewardClaimed;
  55. public RunStatus Status=RunStatus.Playing;
  56. public GameSession(LevelData level) { Level=level; Alive=Enumerable.Repeat(true,level.arrows.Length).ToArray(); Remaining=Alive.Length; TimeLeft=level.seconds; }
  57. public int Blocker(int index)
  58. {
  59. var a=Level.arrows[index].cells; var h=a[a.Length-1]; var p=a[a.Length-2]; int dx=h.x-p.x,dy=h.y-p.y;
  60. for(int x=h.x+dx,y=h.y+dy;x>=0&&y>=0&&x<Level.size&&y<Level.size;x+=dx,y+=dy)
  61. for(int j=0;j<Alive.Length;j++) if(j!=index&&Alive[j])
  62. foreach(var c in Level.arrows[j].cells) if(c.x==x&&c.y==y) return j;
  63. return -1;
  64. }
  65. public List<int> FreeArrows() { var r=new List<int>(); for(int i=0;i<Alive.Length;i++) if(Alive[i]&&Blocker(i)<0) r.Add(i); return r; }
  66. public TapResult Tap(int index)
  67. {
  68. if(Status!=RunStatus.Playing||index<0||index>=Alive.Length||!Alive[index]) return TapResult.Ignored;
  69. bool smash=HammerArmed;
  70. if(!smash && Blocker(index)>=0) { Mistakes++; Hearts--; if(Hearts<=0) Status=RunStatus.FailedHearts; return TapResult.Blocked; }
  71. HammerArmed=false; Alive[index]=false; Remaining--; if(Remaining==0) Status=RunStatus.Cleared;
  72. return smash?TapResult.Smashed:TapResult.Removed;
  73. }
  74. public void Tick(float seconds) { if(Status!=RunStatus.Playing) return; seconds=Mathf.Max(0,seconds); Elapsed+=Mathf.Min(seconds,TimeLeft); TimeLeft=Mathf.Max(0,TimeLeft-seconds); if(TimeLeft<=0) Status=RunStatus.FailedTime; }
  75. public bool RefillHeart() { if(Status!=RunStatus.Playing||Hearts>=5) return false; Hearts++; return true; }
  76. public bool Revive(PlayerSave save)
  77. {
  78. if((Status!=RunStatus.FailedTime&&Status!=RunStatus.FailedHearts)||save.gems<40) return false;
  79. save.gems-=40; if(Status==RunStatus.FailedTime) TimeLeft=30; else Hearts=3; Status=RunStatus.Playing; return true;
  80. }
  81. public int Stars => Mistakes==0?3:Mistakes==1?2:1;
  82. }
  83. [Serializable] public sealed class PlayerSave
  84. {
  85. public int version=1;
  86. public string name="Lucky Vega";
  87. public int avatar, gems=150, keys=1, earned, spotlight=1, hammer=1, heart=1;
  88. public int[] stars=new int[30];
  89. public float[] best=new float[30];
  90. public int checkinDay=1, checkinTotal;
  91. public string checkinDate="", chestDate="";
  92. public bool seen, tutorial, sfx=true, music=true, haptics;
  93. public int TotalStars => stars.Sum();
  94. public int Cleared => stars.Count(x=>x>0);
  95. public int NextLevel { get { for(int i=0;i<30;i++) if(stars[i]==0) return i+1; return 30; } }
  96. public bool Unlocked(int n) => n>=1 && n<=30 && (n==1||stars[n-2]>0);
  97. public void Normalize()
  98. {
  99. if(stars==null) stars=new int[30]; if(stars.Length!=30) Array.Resize(ref stars,30);
  100. if(best==null) best=new float[30]; if(best.Length!=30) Array.Resize(ref best,30);
  101. for(int i=0;i<30;i++) { stars[i]=Mathf.Clamp(stars[i],0,3); best[i]=float.IsNaN(best[i])||float.IsInfinity(best[i])?0:Mathf.Max(0,best[i]); }
  102. gems=Mathf.Max(0,gems); keys=Mathf.Max(0,keys); spotlight=Mathf.Max(0,spotlight); hammer=Mathf.Max(0,hammer); heart=Mathf.Max(0,heart);
  103. checkinDay=Mathf.Clamp(checkinDay,1,7); avatar=Mathf.Clamp(avatar,0,AvatarCatalog.Count-1);
  104. if(string.IsNullOrWhiteSpace(name)) name="Lucky Vega"; if(name.Length>18) name=name.Substring(0,18);
  105. }
  106. }
  107. public struct ClearReward { public int stars, basic, first, time, keys; public bool newBest; public int Total=>basic+first+time; }
  108. public static class Economy
  109. {
  110. public static ClearReward Clear(GameSession run, PlayerSave save)
  111. {
  112. if(run.Status!=RunStatus.Cleared||run.RewardClaimed) return default;
  113. run.RewardClaimed=true; int i=run.Level.number-1; bool first=save.stars[i]==0;
  114. var r=new ClearReward {stars=run.Stars,basic=6+run.Stars*3+run.Level.number/3,first=first?5:0,time=Mathf.Min(15,Mathf.FloorToInt(run.TimeLeft/8)),keys=first&&run.Level.number%5==0?1:0};
  115. float used=Mathf.Max(.1f,Mathf.Round(run.Elapsed*10)/10); r.newBest=save.best[i]<=0||used<save.best[i];
  116. if(r.newBest) save.best[i]=used; save.stars[i]=Math.Max(save.stars[i],r.stars); save.gems+=r.Total; save.earned+=r.Total; save.keys+=r.keys; return r;
  117. }
  118. public static readonly string[] Products={"Spotlight","Heart refill","Hammer","Power bundle","1 chest key","3 chest keys","5 chest keys"};
  119. public static readonly int[] Prices={90,120,160,320,70,180,260};
  120. public static bool Buy(PlayerSave s,int product)
  121. {
  122. if(product<0||product>=Prices.Length||s.gems<Prices[product]) return false;
  123. s.gems-=Prices[product];
  124. switch(product) { case 0:s.spotlight++;break; case 1:s.heart++;break; case 2:s.hammer++;break; case 3:s.spotlight++;s.heart++;s.hammer++;break; case 4:s.keys++;break; case 5:s.keys+=3;break; case 6:s.keys+=5;break; }
  125. return true;
  126. }
  127. public static readonly string[] CheckinLabels={"30 diamonds","1 chest key","60 diamonds","1 Spotlight","80 diamonds","2 chest keys","120 diamonds + Hammer"};
  128. public static bool CheckIn(PlayerSave s,string today)
  129. {
  130. if(s.checkinDate==today) return false; int gems=0;
  131. switch(s.checkinDay) { case 1:gems=30;break;case 2:s.keys++;break;case 3:gems=60;break;case 4:s.spotlight++;break;case 5:gems=80;break;case 6:s.keys+=2;break;case 7:gems=120;s.hammer++;break; }
  132. s.gems+=gems;s.earned+=gems;s.checkinDate=today;s.checkinTotal++;s.checkinDay=s.checkinDay%7+1;return true;
  133. }
  134. public static bool CanOpen(PlayerSave s,string today)=>s.chestDate!=today||s.keys>0||s.gems>=60;
  135. public static string OpenChest(PlayerSave s,string today,double roll)
  136. {
  137. if(!CanOpen(s,today)) return null;
  138. if(s.chestDate!=today) s.chestDate=today; else if(s.keys>0) s.keys--; else s.gems-=60;
  139. double[] limits={.26,.46,.58,.70,.78,.86,.96,1}; int[] gems={20,40,70,100,150,0,0,300};
  140. int index=0; while(index<7&&roll>=limits[index]) index++;
  141. s.gems+=gems[index];s.earned+=gems[index];
  142. if(index==5) { s.keys+=3;return "3 CHEST KEYS"; }
  143. if(index==6) { s.spotlight++;s.hammer++;return "SPOTLIGHT + HAMMER"; }
  144. return (index==7?"JACKPOT! ":"")+gems[index]+" DIAMONDS";
  145. }
  146. }
  147. public sealed class SaveStore
  148. {
  149. public readonly string Path;
  150. public string Warning { get; private set; }
  151. public SaveStore(string folder) { Path=System.IO.Path.Combine(folder,"diamond_arrow_v1.json"); }
  152. public PlayerSave Load()
  153. {
  154. foreach(var path in new[]{Path,Path+".bak"})
  155. {
  156. if(!File.Exists(path)) continue;
  157. try { var s=JsonUtility.FromJson<PlayerSave>(File.ReadAllText(path));if(s==null)throw new IOException("Empty save");s.Normalize();if(path!=Path)Warning="Recovered your backup save.";return s; }
  158. catch(Exception e) { Warning="Save could not be read: "+e.Message; }
  159. }
  160. return new PlayerSave();
  161. }
  162. public bool Save(PlayerSave s)
  163. {
  164. try { Directory.CreateDirectory(System.IO.Path.GetDirectoryName(Path));File.WriteAllText(Path+".tmp",JsonUtility.ToJson(s,true)); if(File.Exists(Path))File.Copy(Path,Path+".bak",true);File.Copy(Path+".tmp",Path,true);File.Delete(Path+".tmp");return true; }
  165. catch(Exception e) { Warning="Progress could not be saved: "+e.Message;Debug.LogWarning(Warning);return false; }
  166. }
  167. public void Reset() { foreach(var p in new[]{Path,Path+".bak",Path+".tmp"})if(File.Exists(p))File.Delete(p); }
  168. }
  169. }