| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- 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" };
- [NonSerialized] LevelData generated;
- public string Chapter(int number) => number > levels.Length ? "Endless Vault" : Chapters[Mathf.Clamp((number - 1) / 5, 0, Chapters.Length - 1)];
- public LevelData GetLevel(int number)
- {
- if (number < 1) throw new ArgumentOutOfRangeException(nameof(number));
- if (number <= levels.Length) return levels[number - 1];
- if (generated == null || generated.number != number)
- generated = EndlessLevels.Generate(number, levels[levels.Length - 1]);
- return generated;
- }
- 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 == 0) { errors.Add("No configured 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 || l.arrows.Length==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 ReviveUsed { get; private set; }
- public bool CanRevive => !ReviveUsed && (Status == RunStatus.FailedTime || Status == RunStatus.FailedHearts);
- public bool Revive()
- {
- if (!CanRevive) return false;
- if (Status == RunStatus.FailedTime) TimeLeft = 30;
- Hearts++;
- ReviveUsed = true;
- 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, 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 long lastRewardedChestUnixMs;
- public bool hasGuestIdentity;
- public bool InitializeGuest(System.Random random)
- {
- if (hasGuestIdentity) return false;
- string[] names = { "Lucky Vega", "Nova Spark", "Ruby Ace", "Neon Iris", "Crystal Kai",
- "Aqua Juno", "Violet Ray", "Diamond Nyx", "Jade Sky", "Amber Star" };
- name = names[random.Next(names.Length)];
- avatar = AvatarCatalog.LegacyCount + random.Next(AvatarCatalog.NewCount);
- hasGuestIdentity = true;
- return true;
- }
- public int TotalStars => stars.Sum();
- public int Cleared => stars.Count(x=>x>0);
- public int NextLevel
- {
- get
- {
- int next = Array.IndexOf(stars, 0);
- return next < 0 ? stars.Length + 1 : next + 1;
- }
- }
- public bool Unlocked(int number) => number > 0 && number <= NextLevel;
- internal void EnsureLevelCapacity(int count)
- {
- if (stars == null || stars.Length < count) Array.Resize(ref stars, count);
- if (best == null || best.Length < count) Array.Resize(ref best, count);
- }
- public void Normalize()
- {
- int count = Math.Max(30, Math.Max(stars?.Length ?? 0, best?.Length ?? 0));
- EnsureLevelCapacity(count);
- for (int i = 0; i < count; i++)
- {
- stars[i] = Mathf.Clamp(stars[i], 0, 3);
- if (float.IsNaN(best[i]) || float.IsInfinity(best[i]) || best[i] < 0) best[i] = 0;
- }
- 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; 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;
- save.EnsureLevelCapacity(run.Level.number);
- 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+Math.Min(10,run.Level.number/3),first=first?5:0,time=Mathf.Min(15,Mathf.FloorToInt(run.TimeLeft/8))};
- 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; return r;
- }
- public static readonly string[] Products={"Spotlight","Heart refill","Hammer","Power bundle"};
- public static readonly int[] Prices={90,120,160,320};
- 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; }
- return true;
- }
- public static readonly string[] CheckinLabels={"30 diamonds","40 diamonds","60 diamonds","1 Spotlight","80 diamonds","100 diamonds","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:gems=40;break;case 3:gems=60;break;case 4:s.spotlight++;break;case 5:gems=80;break;case 6:gems=100;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 int ChestWaitSeconds(PlayerSave save, DateTimeOffset now)
- {
- if (save.lastRewardedChestUnixMs <= 0) return 0;
- double elapsed = (now.ToUnixTimeMilliseconds() - save.lastRewardedChestUnixMs) / 1000d;
- return (int)Math.Ceiling(Math.Max(0, Math.Min(300, 300 - elapsed)));
- }
- public static bool IsFreeChest(PlayerSave save, DateTimeOffset now) =>
- save.chestDate != now.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
- public static string OpenChest(PlayerSave s, DateTimeOffset now, bool earnedAd, double roll)
- {
- if (ChestWaitSeconds(s, now) > 0 || (!earnedAd && !IsFreeChest(s, now))) return null;
- // A paid opening spanning midnight leaves the new day's free claim available.
- if (earnedAd) s.lastRewardedChestUnixMs = now.ToUnixTimeMilliseconds();
- else s.chestDate = now.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
- double[] limits={.26,.46,.58,.70,.78,.86,.96,1}; int[] gems={20,40,70,100,150,70,0,300};
- int index=0; while(index<7&&roll>=limits[index]) index++;
- s.gems+=gems[index];s.earned+=gems[index];
- 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); }
- }
- }
|