TouchyTickets/TouchyTickets/Attractions/Attraction.cs
2020-06-02 14:16:14 +02:00

65 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions;
using MLEM.Misc;
using MLEM.Startup;
using MLEM.Textures;
namespace TouchyTickets.Attractions {
[DataContract]
public class Attraction {
public static readonly UniformTextureAtlas Texture = new UniformTextureAtlas(MlemGame.LoadContent<Texture2D>("Textures/Attractions"), 16, 16);
public static readonly Vector2 TileSize = new Vector2(Texture.RegionWidth, Texture.RegionHeight);
[DataMember]
public readonly AttractionType Type;
[DataMember]
private float ticketPercentage;
public Attraction(AttractionType type) {
this.Type = type;
}
public IEnumerable<Point> GetCoveredTiles() {
for (var x = 0; x < this.Type.Width; x++) {
for (var y = 0; y < this.Type.Height; y++) {
if (this.Type.Area[y, x])
yield return new Point(x, y);
}
}
}
public float Update(GameTime time, TimeSpan passed, ParkMap map, Point position) {
var genRate = this.Type.GetGenerationRate();
// only apply dynamic upgrades here, static ones go into the type!
if (Upgrade.FoodCourtModifier.IsActive() && this.GetSurrounding(map, position, AttractionType.FoodCourt).Any())
genRate *= 3;
this.ticketPercentage += genRate * (float) passed.TotalSeconds;
var amount = this.ticketPercentage.Floor();
if (amount > 0) {
GameImpl.Instance.Tickets += amount;
this.ticketPercentage -= amount;
}
// return the generation rate per second
return genRate;
}
public IEnumerable<Attraction> GetSurrounding(ParkMap map, Point position, AttractionType type) {
foreach (var tile in this.GetCoveredTiles()) {
foreach (var dir in Direction2Helper.Adjacent) {
var other = map.GetAttractionAt(position + tile + dir.Offset());
if (other != null && other != this && other.Type == type)
yield return other;
}
}
}
}
}