TouchyTickets/ThemeParkClicker/GameImpl.cs
2020-06-01 01:21:54 +02:00

87 lines
3 KiB
C#

using System.Numerics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Cameras;
using MLEM.Extensions;
using MLEM.Font;
using MLEM.Startup;
using ThemeParkClicker.Attractions;
namespace ThemeParkClicker {
public class GameImpl : MlemGame {
public static GameImpl Instance { get; private set; }
private BigInteger tickets;
public BigInteger Tickets {
get => this.tickets;
set {
var diff = value - this.tickets;
if (diff > 0)
this.ticketsPerSecondCounter += (long) diff;
this.tickets = value;
}
}
public ParkMap Map { get; private set; }
public Camera Camera { get; private set; }
public Ui Ui { get; private set; }
public bool DrawMap;
public float TicketsPerSecond { get; private set; }
private long ticketsPerSecondCounter;
private double secondCounter;
public GameImpl() {
Instance = this;
}
protected override void LoadContent() {
base.LoadContent();
this.Ui = new Ui(this.UiSystem);
this.Map = new ParkMap(10, 10);
this.Camera = new Camera(this.GraphicsDevice) {
Scale = 5,
AutoScaleWithScreen = true,
AutoScaleReferenceSize = new Point(720, 1280),
MaxScale = 24,
MinScale = 3
};
#if DEBUG
this.Tickets = 1000;
this.Map.Place(Point.Zero, Attraction.Attractions["Carousel"]());
this.Map.Place(new Point(1, 0), Attraction.Attractions["Carousel"]());
this.Map.Place(new Point(3, 0), Attraction.Attractions["Carousel"]());
this.Map.Place(new Point(1, 2), Attraction.Attractions["FoodCourt"]());
#endif
}
public string DisplayTicketCount() {
return this.Tickets.ToString();
}
protected override void DoUpdate(GameTime gameTime) {
base.DoUpdate(gameTime);
this.Map.Update(gameTime);
this.Ui.Update(gameTime);
// we average tickets per second over two seconds
this.secondCounter += gameTime.ElapsedGameTime.TotalSeconds;
if (this.secondCounter >= 2) {
this.secondCounter -= 2;
this.TicketsPerSecond = this.ticketsPerSecondCounter / 2F;
this.ticketsPerSecondCounter = 0;
}
}
protected override void DoDraw(GameTime gameTime) {
this.GraphicsDevice.Clear(Color.Black);
if (this.DrawMap) {
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, transformMatrix: this.Camera.ViewMatrix);
this.Map.Draw(gameTime, this.SpriteBatch, Vector2.Zero, 1, 1);
this.SpriteBatch.End();
}
base.DoDraw(gameTime);
}
}
}