using System; using System.Collections.Generic; using MLEM.Textures; using Newtonsoft.Json; namespace TouchyTickets.Attractions { [JsonConverter(typeof(Converter))] public class AttractionType { public static readonly Dictionary Attractions = new Dictionary(); static AttractionType() { Register(new AttractionType("Carousel", new[,] {{true}}, Attraction.Texture[0, 0, 1, 1], 0.5F, 50)); Register(new AttractionType("FoodCourt", new[,] {{true, true}}, Attraction.Texture[1, 0, 2, 1], 1.1F, 300)); } public readonly string Name; public readonly bool[,] Area; public int Width => this.Area.GetLength(1); public int Height => this.Area.GetLength(0); public readonly TextureRegion TextureRegion; public readonly float GenerationPerSecond; public readonly long InitialPrice; private readonly Constructor create; public AttractionType(string name, bool[,] area, TextureRegion texture, float generationPerSecond, long initialPrice, Constructor constructor = null) { this.Name = name; this.Area = area; this.TextureRegion = texture; this.GenerationPerSecond = generationPerSecond; this.InitialPrice = initialPrice; this.create = constructor ?? (t => new Attraction(t)); } public Attraction Create() { return this.create(this); } private static void Register(AttractionType type) { Attractions[type.Name] = type; } public delegate Attraction Constructor(AttractionType type); public class Converter : JsonConverter { public override void WriteJson(JsonWriter writer, AttractionType value, JsonSerializer serializer) { if (value != null) writer.WriteValue(value.Name); } public override AttractionType ReadJson(JsonReader reader, Type objectType, AttractionType existingValue, bool hasExistingValue, JsonSerializer serializer) { return reader.Value != null ? Attractions[reader.Value.ToString()] : null; } } } }