TouchyTickets/TouchyTickets/GameImpl.cs

147 lines
5.3 KiB
C#
Raw Normal View History

2020-06-01 14:45:20 +02:00
using System;
2020-06-01 23:02:47 +02:00
using System.Collections.Generic;
2020-05-30 19:41:49 +02:00
using System.Numerics;
2020-06-02 23:23:43 +02:00
using Coroutine;
2020-05-30 19:41:49 +02:00
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.Startup;
2020-07-10 20:45:23 +02:00
using TouchyTickets.Upgrades;
2020-05-30 19:41:49 +02:00
2020-06-01 17:39:57 +02:00
namespace TouchyTickets {
2020-05-30 19:41:49 +02:00
public class GameImpl : MlemGame {
public static GameImpl Instance { get; private set; }
2020-06-01 23:02:47 +02:00
public readonly ISet<Upgrade> AppliedUpgrades = new HashSet<Upgrade>();
public readonly Platform Platform;
2020-06-01 16:18:07 +02:00
public BigInteger Tickets;
2020-06-01 23:02:47 +02:00
public int TimesRestarted;
public int Stars;
2020-06-01 14:45:20 +02:00
public ParkMap Map;
public Tutorial Tutorial { get; private set; }
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-30 19:06:35 +02:00
public TimeSpan PlayTime;
2020-06-01 14:45:20 +02:00
private double saveCounter;
2020-07-21 21:51:30 +02:00
private double achievementCounter;
2020-06-01 01:21:54 +02:00
public GameImpl(Platform platform) {
this.Platform = platform;
2020-05-30 19:41:49 +02:00
Instance = this;
}
protected override void LoadContent() {
base.LoadContent();
Assets.Load();
2020-06-16 23:17:18 +02:00
Options.Load();
2020-06-25 14:02:41 +02:00
// start the load sequence
Ui.SetupUiSystem(this.UiSystem);
CoroutineHandler.Start(Ui.DisplaySplash(this.LoadGame));
}
2020-06-09 19:03:55 +02:00
private void LoadGame() {
2020-07-21 20:26:52 +02:00
// 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"};
2020-06-09 19:03:55 +02:00
// ios comes first, then android. For now they're the same
2020-07-21 20:26:52 +02:00
analytics["GameKey"] = new[] {"cc18de06eebbc5d5e987c384fcd28000", "cc18de06eebbc5d5e987c384fcd28000"};
analytics["SecretKey"] = new[] {"82ca1a930ee38e2383ffb02db7631e16033b511d", "82ca1a930ee38e2383ffb02db7631e16033b511d"};
this.Platform.SetupOnlineInteractions(analytics);
2020-05-30 19:41:49 +02:00
this.Tutorial = new Tutorial();
2020-06-01 16:18:07 +02:00
if (!SaveHandler.Load(this))
this.Map = new ParkMap(20, 20);
// load other stuff
2020-05-30 19:41:49 +02:00
this.Ui = new Ui(this.UiSystem);
2020-05-31 17:24:10 +02:00
this.Camera = new Camera(this.GraphicsDevice) {
2020-06-03 14:19:51 +02:00
Scale = 4,
2020-05-31 21:16:50 +02:00
AutoScaleWithScreen = true,
AutoScaleReferenceSize = new Point(720, 1280),
MaxScale = 24,
2020-06-01 23:02:47 +02:00
MinScale = 2
2020-05-31 17:24:10 +02:00
};
2020-07-08 16:04:44 +02:00
// update the map once to make sure that catching up happens during loading
this.UpdateMapOnce();
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
if (this.Map != null) {
2020-07-08 16:04:44 +02:00
// update the map
this.UpdateMapOnce();
2020-07-21 21:51:30 +02:00
// 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);
}
2020-06-01 14:45:20 +02:00
}
2020-06-01 01:21:54 +02:00
2020-06-30 19:06:35 +02:00
// 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);
}
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);
this.Map.Draw(gameTime, this.SpriteBatch, Vector2.Zero, 1, 1, true, this.Camera.GetVisibleRectangle());
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 23:02:47 +02:00
public BigInteger GetStarPrice() {
2020-07-08 16:16:40 +02:00
return 1000000000 * BigInteger.Pow(100, this.TimesRestarted);
2020-06-05 23:51:23 +02:00
}
public int GetBuyableStars() {
2020-06-25 14:02:41 +02:00
#if DEBUG
return 3;
#endif
return (int) BigInteger.Min(3, this.Tickets / this.GetStarPrice());
2020-06-01 14:45:20 +02:00
}
2020-07-08 16:04:44 +02:00
private void UpdateMapOnce() {
var now = DateTime.Now;
if (this.LastUpdate != default) {
var lastTickets = this.Tickets;
2020-07-08 18:53:48 +02:00
var lastTps = this.Map.TicketsPerSecond;
2020-07-21 21:51:30 +02:00
2020-07-08 16:04:44 +02:00
var passed = now - this.LastUpdate;
this.Map.Update(passed, passed.TotalSeconds >= 1);
// if 10 or more seconds passed, we display a message
2020-07-08 18:53:48 +02:00
if (Options.Instance.WhileYouWereAwayMessage && passed.TotalSeconds >= 10)
Ui.DisplayWhileYouWereAway(passed, this.Tickets - lastTickets, this.Map.TicketsPerSecond - lastTps);
2020-07-08 16:04:44 +02:00
}
this.LastUpdate = now;
}
2020-05-30 19:41:49 +02:00
}
}