TouchyTickets/ThemeParkClicker/Attractions/AttractionType.cs

59 lines
2.2 KiB
C#
Raw Normal View History

2020-06-01 14:45:20 +02:00
using System;
using System.Collections.Generic;
using MLEM.Textures;
using Newtonsoft.Json;
namespace ThemeParkClicker.Attractions {
[JsonConverter(typeof(Converter))]
public class AttractionType {
public static readonly Dictionary<string, AttractionType> Attractions = new Dictionary<string, AttractionType>();
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<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;
}
}
}
}