TouchyTickets/ThemeParkClicker/GameImpl.cs
2020-06-01 15:06:11 +02:00

115 lines
No EOL
3.9 KiB
C#

using System;
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;
public Camera Camera { get; private set; }
public Ui Ui { get; private set; }
public bool DrawMap;
public DateTime LastUpdate;
public float TicketsPerSecond { get; private set; }
private long ticketsPerSecondCounter;
private double secondCounter;
private double saveCounter;
public GameImpl() {
Instance = this;
}
protected override void LoadContent() {
base.LoadContent();
this.Ui = new Ui(this.UiSystem);
this.Camera = new Camera(this.GraphicsDevice) {
Scale = 5,
AutoScaleWithScreen = true,
AutoScaleReferenceSize = new Point(720, 1280),
MaxScale = 24,
MinScale = 3
};
if (!SaveHandler.Load(this))
this.Map = new ParkMap(10, 10);
}
protected override void DoUpdate(GameTime gameTime) {
base.DoUpdate(gameTime);
var now = DateTime.Now;
if (this.LastUpdate != default) {
var passed = now - this.LastUpdate;
// if more than 1 second passed, the app is minimized or a save was loaded, so we penalize
if (passed.TotalSeconds >= 1)
passed = new TimeSpan(passed.Ticks / 2);
this.Map.Update(gameTime, passed);
}
this.LastUpdate = now;
this.Ui.Update(gameTime);
// we average tickets per second over 4 seconds
const float avgTime = 4;
this.secondCounter += gameTime.ElapsedGameTime.TotalSeconds;
if (this.secondCounter >= avgTime) {
this.secondCounter -= avgTime;
this.TicketsPerSecond = this.ticketsPerSecondCounter / avgTime;
this.ticketsPerSecondCounter = 0;
}
// save every 3 seconds
this.saveCounter += gameTime.ElapsedGameTime.TotalSeconds;
if (this.saveCounter >= 3) {
this.saveCounter = 0;
SaveHandler.Save(this);
}
}
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);
}
public string DisplayTicketCount() {
if (this.Tickets < 1000)
return this.Tickets.ToString();
// thousands
if (this.Tickets < 1000000)
return this.Tickets.ToString("0,.##K");
// millions
if (this.Tickets < 1000000000)
return this.Tickets.ToString("0,,.##M");
// billions
if (this.Tickets < 1000000000000)
return this.Tickets.ToString("0,,,.##B");
// trillions
if (this.Tickets < 1000000000000000)
return this.Tickets.ToString("0,,,,.##T");
return this.Tickets.ToString("0,,,,,.##Q");
}
}
}