TouchyTickets/TouchyTickets/GameImpl.cs

95 lines
3.1 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-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>();
2020-06-02 23:58:46 +02:00
public readonly Tutorial Tutorial = new Tutorial();
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;
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;
private double saveCounter;
2020-06-01 01:21:54 +02:00
public GameImpl() {
2020-05-30 19:41:49 +02:00
Instance = this;
}
protected override void LoadContent() {
base.LoadContent();
2020-06-01 16:18:07 +02:00
if (!SaveHandler.Load(this))
this.Map = new ParkMap(20, 20);
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-06-02 23:23:43 +02:00
CoroutineHandler.Start(this.Ui.DisplaySplash());
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-02 23:58:46 +02:00
this.Tutorial.Update(this);
2020-06-01 01:21:54 +02:00
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);
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-06-08 20:47:40 +02:00
return 1000000000 * BigInteger.Pow(10, this.TimesRestarted);
2020-06-05 23:51:23 +02:00
}
public int GetMaxStars() {
return 3 * (this.TimesRestarted + 1);
}
public int GetBuyableStars() {
return Math.Min(this.GetMaxStars(), (int) (this.Tickets / this.GetStarPrice()));
2020-06-01 14:45:20 +02:00
}
2020-05-30 19:41:49 +02:00
}
}