TouchyTickets/TouchyTickets/GameImpl.cs
2020-07-21 21:51:30 +02:00

147 lines
5.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Numerics;
using Coroutine;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Cameras;
using MLEM.Startup;
using TouchyTickets.Upgrades;
namespace TouchyTickets {
public class GameImpl : MlemGame {
public static GameImpl Instance { get; private set; }
public readonly ISet<Upgrade> AppliedUpgrades = new HashSet<Upgrade>();
public readonly Platform Platform;
public BigInteger Tickets;
public int TimesRestarted;
public int Stars;
public ParkMap Map;
public Tutorial Tutorial { get; private set; }
public Camera Camera { get; private set; }
public Ui Ui { get; private set; }
public bool DrawMap;
public DateTime LastUpdate;
public TimeSpan PlayTime;
private double saveCounter;
private double achievementCounter;
public GameImpl(Platform platform) {
this.Platform = platform;
Instance = this;
}
protected override void LoadContent() {
base.LoadContent();
Assets.Load();
Options.Load();
// start the load sequence
Ui.SetupUiSystem(this.UiSystem);
CoroutineHandler.Start(Ui.DisplaySplash(this.LoadGame));
}
private void LoadGame() {
// set up online stuff
var analytics = new Dictionary<string, object>();
analytics["InfoLog"] = true;
analytics["VerboseLog"] = true;
analytics["ResourceCurrencies"] = new[] {"Tickets", "Stars"};
analytics["ResourceItemTypes"] = new[] {"Attraction", "Restart", "Upgrade", "Modifier"};
// ios comes first, then android. For now they're the same
analytics["GameKey"] = new[] {"cc18de06eebbc5d5e987c384fcd28000", "cc18de06eebbc5d5e987c384fcd28000"};
analytics["SecretKey"] = new[] {"82ca1a930ee38e2383ffb02db7631e16033b511d", "82ca1a930ee38e2383ffb02db7631e16033b511d"};
this.Platform.SetupOnlineInteractions(analytics);
this.Tutorial = new Tutorial();
if (!SaveHandler.Load(this))
this.Map = new ParkMap(20, 20);
// load other stuff
this.Ui = new Ui(this.UiSystem);
this.Camera = new Camera(this.GraphicsDevice) {
Scale = 4,
AutoScaleWithScreen = true,
AutoScaleReferenceSize = new Point(720, 1280),
MaxScale = 24,
MinScale = 2
};
// update the map once to make sure that catching up happens during loading
this.UpdateMapOnce();
}
protected override void DoUpdate(GameTime gameTime) {
base.DoUpdate(gameTime);
if (this.Map != null) {
// update the map
this.UpdateMapOnce();
// achievements
this.achievementCounter += gameTime.ElapsedGameTime.TotalSeconds;
if (this.achievementCounter >= 5) {
this.achievementCounter = 0;
foreach (var achievement in Achievement.Achievements.Values)
achievement.Update();
}
// save every 3 seconds
this.saveCounter += gameTime.ElapsedGameTime.TotalSeconds;
if (this.saveCounter >= 3) {
this.saveCounter = 0;
SaveHandler.Save(this);
}
}
// play time stuff
var lastTime = this.PlayTime;
this.PlayTime += gameTime.ElapsedGameTime;
if (lastTime.TotalHours >= 1 != this.PlayTime.TotalHours >= 1)
Ui.DisplayRatePlease();
this.Ui?.Update(gameTime);
this.Tutorial?.Update(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, true, this.Camera.GetVisibleRectangle());
this.SpriteBatch.End();
}
base.DoDraw(gameTime);
}
public BigInteger GetStarPrice() {
return 1000000000 * BigInteger.Pow(100, this.TimesRestarted);
}
public int GetBuyableStars() {
#if DEBUG
return 3;
#endif
return (int) BigInteger.Min(3, this.Tickets / this.GetStarPrice());
}
private void UpdateMapOnce() {
var now = DateTime.Now;
if (this.LastUpdate != default) {
var lastTickets = this.Tickets;
var lastTps = this.Map.TicketsPerSecond;
var passed = now - this.LastUpdate;
this.Map.Update(passed, passed.TotalSeconds >= 1);
// if 10 or more seconds passed, we display a message
if (Options.Instance.WhileYouWereAwayMessage && passed.TotalSeconds >= 10)
Ui.DisplayWhileYouWereAway(passed, this.Tickets - lastTickets, this.Map.TicketsPerSecond - lastTps);
}
this.LastUpdate = now;
}
}
}