TouchyTickets/TouchyTickets/Achievement.cs
2020-07-21 21:51:30 +02:00

60 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using Microsoft.Xna.Framework;
using TouchyTickets.Attractions;
namespace TouchyTickets {
public class Achievement {
public static readonly Dictionary<string, Achievement> Achievements = new Dictionary<string, Achievement>();
static Achievement() {
foreach (var amount in new[] {1, 10, 100})
Register(new Achievement($"{amount}Stars", g => g.Stars >= amount));
Register(new Achievement("FullMap", g => {
for (var x = 0; x < g.Map.Width; x++) {
for (var y = 0; y < g.Map.Height; y++) {
if (g.Map.GetAttractionAt(new Point(x, y)) == null)
return false;
}
}
return true;
}));
foreach (var flag in new[] {AttractionFlags.Small, AttractionFlags.Relaxed, AttractionFlags.Walking, AttractionFlags.NonTechnology})
Register(new Achievement($"Only{flag}Rides", g => g.Map.GetAttractionAmount(null) >= 100 && g.Map.GetAttractions().All(a => a.Item2.Type.Flags.HasFlag(flag))));
foreach (var amount in new[] {100, 500, 1000, 5000})
Register(new Achievement($"{amount}Modifiers", g => g.Map.GetAttractions().All(a => a.Item2.GetModifierAmount(null) >= amount)));
for (var i = 1; i <= 10; i++) {
var amount = BigInteger.Pow(1000, i + 1);
Register(new Achievement($"{Ui.PrettyPrintNumber(amount).Replace(" ", "")}Tickets", g => g.Tickets >= amount));
}
}
public readonly string Name;
private readonly Func<GameImpl, bool> condition;
// this value doesn't save between game runs, since achievements
// are only displayed inside of the respective stores anyway
private bool unlocked;
public Achievement(string name, Func<GameImpl, bool> condition) {
this.Name = name;
this.condition = condition;
}
public void Update() {
if (this.unlocked)
return;
if (!this.condition.Invoke(GameImpl.Instance))
return;
GameImpl.Instance.Platform.GainAchievement(this);
this.unlocked = true;
}
public static void Register(Achievement achievement) {
Achievements.Add(achievement.Name, achievement);
}
}
}