TouchyTickets/ThemeParkClicker/GameImpl.cs

115 lines
3.9 KiB
C#
Raw Normal View History

2020-06-01 14:45:20 +02:00
using System;
2020-05-30 19:41:49 +02:00
using System.Numerics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
2020-05-31 17:24:10 +02:00
using MLEM.Cameras;
2020-05-30 19:41:49 +02:00
using MLEM.Extensions;
using MLEM.Font;
using MLEM.Startup;
2020-05-31 17:24:10 +02:00
using ThemeParkClicker.Attractions;
2020-05-30 19:41:49 +02:00
namespace ThemeParkClicker {
public class GameImpl : MlemGame {
public static GameImpl Instance { get; private set; }
2020-06-01 01:21:54 +02:00
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;
}
}
2020-06-01 14:45:20 +02:00
public ParkMap Map;
2020-05-31 17:24:10 +02:00
public Camera Camera { get; private set; }
2020-05-30 19:41:49 +02:00
public Ui Ui { get; private set; }
2020-05-31 21:16:50 +02:00
public bool DrawMap;
2020-06-01 14:45:20 +02:00
public DateTime LastUpdate;
2020-06-01 01:21:54 +02:00
public float TicketsPerSecond { get; private set; }
private long ticketsPerSecondCounter;
private double secondCounter;
2020-06-01 14:45:20 +02:00
private double saveCounter;
2020-06-01 01:21:54 +02:00
2020-05-30 19:41:49 +02:00
public GameImpl() {
Instance = this;
}
protected override void LoadContent() {
base.LoadContent();
this.Ui = new Ui(this.UiSystem);
2020-05-31 17:24:10 +02:00
this.Camera = new Camera(this.GraphicsDevice) {
2020-06-01 01:21:54 +02:00
Scale = 5,
2020-05-31 21:16:50 +02:00
AutoScaleWithScreen = true,
AutoScaleReferenceSize = new Point(720, 1280),
MaxScale = 24,
2020-06-01 01:21:54 +02:00
MinScale = 3
2020-05-31 17:24:10 +02:00
};
2020-06-01 14:45:20 +02:00
if (!SaveHandler.Load(this))
this.Map = new ParkMap(10, 10);
2020-05-30 19:41:49 +02:00
}
protected override void DoUpdate(GameTime gameTime) {
base.DoUpdate(gameTime);
2020-06-01 14:45:20 +02:00
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);
2020-06-01 01:21:54 +02:00
2020-06-01 14:45:20 +02:00
// we average tickets per second over 4 seconds
const float avgTime = 4;
2020-06-01 01:21:54 +02:00
this.secondCounter += gameTime.ElapsedGameTime.TotalSeconds;
2020-06-01 14:45:20 +02:00
if (this.secondCounter >= avgTime) {
this.secondCounter -= avgTime;
this.TicketsPerSecond = this.ticketsPerSecondCounter / avgTime;
2020-06-01 01:21:54 +02:00
this.ticketsPerSecondCounter = 0;
}
2020-06-01 14:45:20 +02:00
// save every 3 seconds
this.saveCounter += gameTime.ElapsedGameTime.TotalSeconds;
if (this.saveCounter >= 3) {
this.saveCounter = 0;
SaveHandler.Save(this);
}
}
2020-05-30 19:41:49 +02:00
protected override void DoDraw(GameTime gameTime) {
2020-05-31 21:16:50 +02:00
this.GraphicsDevice.Clear(Color.Black);
if (this.DrawMap) {
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, transformMatrix: this.Camera.ViewMatrix);
2020-06-01 01:21:54 +02:00
this.Map.Draw(gameTime, this.SpriteBatch, Vector2.Zero, 1, 1);
2020-05-31 21:16:50 +02:00
this.SpriteBatch.End();
}
2020-05-30 19:41:49 +02:00
base.DoDraw(gameTime);
}
2020-06-01 14:45:20 +02:00
public string DisplayTicketCount() {
2020-06-01 15:06:11 +02:00
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");
2020-06-01 14:45:20 +02:00
}
2020-05-30 19:41:49 +02:00
}
}