TouchyTickets/TouchyTickets/Attractions/AttractionType.cs
2020-06-02 12:20:16 +02:00

58 lines
2.5 KiB
C#

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<string, AttractionType> Attractions = new Dictionary<string, AttractionType>();
public static readonly AttractionType Carousel = Register(new AttractionType("Carousel", new[,] {{true}}, Attraction.Texture[0, 0, 1, 1], 0.5F, 50));
public static readonly AttractionType FoodCourt = Register(new AttractionType("FoodCourt", new[,] {{true, true}}, Attraction.Texture[1, 0, 2, 1], 1.1F, 300));
public static readonly AttractionType FerrisWheel = Register(new AttractionType("FerrisWheel", new[,] {{true, true}, {true, true}}, Attraction.Texture[0, 1, 2, 2], 3.5F, 2000));
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 AttractionType Register(AttractionType type) {
Attractions.Add(type.Name, type);
return type;
}
public delegate Attraction Constructor(AttractionType type);
public class Converter : JsonConverter<AttractionType> {
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;
}
}
}
}