| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using UnityEngine;
- namespace DiamondArrow
- {
- [Serializable] public struct Cell { public int x, y; public Cell(int x, int y) { this.x=x; this.y=y; } }
- [Serializable] public sealed class ArrowData { public Cell[] cells; }
- [Serializable] public sealed class LevelData { public int number, size, seconds; public ArrowData[] arrows; }
- [Serializable] public sealed class LevelBook
- {
- public LevelData[] levels;
- public static readonly string[] Chapters = { "Neon Alley", "Chrome Vault", "Cherry Circuit", "Jackpot Row", "Midnight Hall", "Diamond Crown" };
- public static string Chapter(int n) => Chapters[Mathf.Clamp((n-1)/5,0,5)];
- public static LevelBook Parse(string json) => JsonUtility.FromJson<LevelBook>(json.TrimStart('\uFEFF'));
- public List<string> Validate()
- {
- var errors = new List<string>();
- if (levels == null || levels.Length != 30) { errors.Add("Expected 30 levels"); return errors; }
- for (int n=0;n<levels.Length;n++)
- {
- var l=levels[n]; var occupied=new HashSet<int>();
- if(l.number!=n+1 || l.size<5 || l.size>11 || l.seconds<=0) errors.Add("Invalid level header "+(n+1));
- foreach(var a in l.arrows)
- {
- if(a.cells.Length<2) { errors.Add("Short arrow "+l.number); continue; }
- for(int i=0;i<a.cells.Length;i++)
- {
- var c=a.cells[i];
- 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);
- 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);
- }
- }
- var game=new GameSession(l);
- while(game.Status==RunStatus.Playing)
- {
- var free=game.FreeArrows();
- if(free.Count==0) { errors.Add("Unsolvable level "+l.number); break; }
- game.Tap(free[0]);
- }
- }
- return errors;
- }
- }
- public enum RunStatus { Playing, Cleared, FailedHearts, FailedTime }
- public enum TapResult { Ignored, Blocked, Removed, Smashed }
- public sealed class GameSession
- {
- public readonly LevelData Level;
- public readonly bool[] Alive;
- public int Hearts=3, Mistakes, Remaining;
- public float TimeLeft, Elapsed;
- public bool HammerArmed, RewardClaimed;
- public RunStatus Status=RunStatus.Playing;
- public GameSession(LevelData level) { Level=level; Alive=Enumerable.Repeat(true,level.arrows.Length).ToArray(); Remaining=Alive.Length; TimeLeft=level.seconds; }
- public int Blocker(int index)
- {
- 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;
- for(int x=h.x+dx,y=h.y+dy;x>=0&&y>=0&&x<Level.size&&y<Level.size;x+=dx,y+=dy)
- for(int j=0;j<Alive.Length;j++) if(j!=index&&Alive[j])
- foreach(var c in Level.arrows[j].cells) if(c.x==x&&c.y==y) return j;
- return -1;
- }
- 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; }
- public TapResult Tap(int index)
- {
- if(Status!=RunStatus.Playing||index<0||index>=Alive.Length||!Alive[index]) return TapResult.Ignored;
- bool smash=HammerArmed;
- if(!smash && Blocker(index)>=0) { Mistakes++; Hearts--; if(Hearts<=0) Status=RunStatus.FailedHearts; return TapResult.Blocked; }
- HammerArmed=false; Alive[index]=false; Remaining--; if(Remaining==0) Status=RunStatus.Cleared;
- return smash?TapResult.Smashed:TapResult.Removed;
- }
- 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; }
- public bool RefillHeart() { if(Status!=RunStatus.Playing||Hearts>=5) return false; Hearts++; return true; }
- public bool Revive(PlayerSave save)
- {
- if((Status!=RunStatus.FailedTime&&Status!=RunStatus.FailedHearts)||save.gems<40) return false;
- save.gems-=40; if(Status==RunStatus.FailedTime) TimeLeft=30; else Hearts=3; Status=RunStatus.Playing; return true;
- }
- public int Stars => Mistakes==0?3:Mistakes==1?2:1;
- }
- [Serializable] public sealed class PlayerSave
- {
- public int version=1;
- public string name="Lucky Vega";
- public int avatar, gems=150, keys=1, earned, spotlight=1, hammer=1, heart=1;
- public int[] stars=new int[30];
- public float[] best=new float[30];
- public int checkinDay=1, checkinTotal;
- public string checkinDate="", chestDate="";
- public bool seen, tutorial, sfx=true, music=true, haptics;
- public int TotalStars => stars.Sum();
- public int Cleared => stars.Count(x=>x>0);
- public int NextLevel { get { for(int i=0;i<30;i++) if(stars[i]==0) return i+1; return 30; } }
- public bool Unlocked(int n) => n>=1 && n<=30 && (n==1||stars[n-2]>0);
- public void Normalize()
- {
- if(stars==null) stars=new int[30]; if(stars.Length!=30) Array.Resize(ref stars,30);
- if(best==null) best=new float[30]; if(best.Length!=30) Array.Resize(ref best,30);
- 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]); }
- 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);
- checkinDay=Mathf.Clamp(checkinDay,1,7); avatar=Mathf.Clamp(avatar,0,AvatarCatalog.Count-1);
- if(string.IsNullOrWhiteSpace(name)) name="Lucky Vega"; if(name.Length>18) name=name.Substring(0,18);
- }
- }
- public struct ClearReward { public int stars, basic, first, time, keys; public bool newBest; public int Total=>basic+first+time; }
- public static class Economy
- {
- public static ClearReward Clear(GameSession run, PlayerSave save)
- {
- if(run.Status!=RunStatus.Cleared||run.RewardClaimed) return default;
- run.RewardClaimed=true; int i=run.Level.number-1; bool first=save.stars[i]==0;
- 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};
- float used=Mathf.Max(.1f,Mathf.Round(run.Elapsed*10)/10); r.newBest=save.best[i]<=0||used<save.best[i];
- 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;
- }
- public static readonly string[] Products={"Spotlight","Heart refill","Hammer","Power bundle","1 chest key","3 chest keys","5 chest keys"};
- public static readonly int[] Prices={90,120,160,320,70,180,260};
- public static bool Buy(PlayerSave s,int product)
- {
- if(product<0||product>=Prices.Length||s.gems<Prices[product]) return false;
- s.gems-=Prices[product];
- 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; }
- return true;
- }
- public static readonly string[] CheckinLabels={"30 diamonds","1 chest key","60 diamonds","1 Spotlight","80 diamonds","2 chest keys","120 diamonds + Hammer"};
- public static bool CheckIn(PlayerSave s,string today)
- {
- if(s.checkinDate==today) return false; int gems=0;
- 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; }
- s.gems+=gems;s.earned+=gems;s.checkinDate=today;s.checkinTotal++;s.checkinDay=s.checkinDay%7+1;return true;
- }
- public static bool CanOpen(PlayerSave s,string today)=>s.chestDate!=today||s.keys>0||s.gems>=60;
- public static string OpenChest(PlayerSave s,string today,double roll)
- {
- if(!CanOpen(s,today)) return null;
- if(s.chestDate!=today) s.chestDate=today; else if(s.keys>0) s.keys--; else s.gems-=60;
- double[] limits={.26,.46,.58,.70,.78,.86,.96,1}; int[] gems={20,40,70,100,150,0,0,300};
- int index=0; while(index<7&&roll>=limits[index]) index++;
- s.gems+=gems[index];s.earned+=gems[index];
- if(index==5) { s.keys+=3;return "3 CHEST KEYS"; }
- if(index==6) { s.spotlight++;s.hammer++;return "SPOTLIGHT + HAMMER"; }
- return (index==7?"JACKPOT! ":"")+gems[index]+" DIAMONDS";
- }
- }
- public sealed class SaveStore
- {
- public readonly string Path;
- public string Warning { get; private set; }
- public SaveStore(string folder) { Path=System.IO.Path.Combine(folder,"diamond_arrow_v1.json"); }
- public PlayerSave Load()
- {
- foreach(var path in new[]{Path,Path+".bak"})
- {
- if(!File.Exists(path)) continue;
- 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; }
- catch(Exception e) { Warning="Save could not be read: "+e.Message; }
- }
- return new PlayerSave();
- }
- public bool Save(PlayerSave s)
- {
- 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; }
- catch(Exception e) { Warning="Progress could not be saved: "+e.Message;Debug.LogWarning(Warning);return false; }
- }
- public void Reset() { foreach(var p in new[]{Path,Path+".bak",Path+".tmp"})if(File.Exists(p))File.Delete(p); }
- }
- }
|