GreatSpringGameJam/GreatSpringGameJam/GameImpl.cs

99 lines
4.1 KiB
C#
Raw Normal View History

2021-03-08 19:08:41 +01:00
using System.Collections.Generic;
using Coroutine;
2021-03-07 22:23:51 +01:00
using Microsoft.Xna.Framework;
2021-03-08 19:08:41 +01:00
using Microsoft.Xna.Framework.Graphics;
2021-03-07 22:23:51 +01:00
using MLEM.Cameras;
2021-03-08 19:08:41 +01:00
using MLEM.Font;
2021-03-07 22:23:51 +01:00
using MLEM.Startup;
2021-03-08 19:08:41 +01:00
using MLEM.Ui;
using MLEM.Ui.Elements;
2021-03-07 22:23:51 +01:00
namespace GreatSpringGameJam {
public class GameImpl : MlemGame {
public static GameImpl Instance { get; private set; }
public Map Map { get; private set; }
public Player Player { get; private set; }
2021-03-08 02:37:40 +01:00
public Camera Camera { get; private set; }
2021-03-08 19:08:41 +01:00
public bool IsFinishing { get; private set; }
2021-03-07 22:23:51 +01:00
public GameImpl() {
Instance = this;
2021-03-08 02:37:40 +01:00
this.IsMouseVisible = true;
2021-03-07 22:23:51 +01:00
}
protected override void LoadContent() {
this.GraphicsDeviceManager.PreferredBackBufferWidth = 1280;
this.GraphicsDeviceManager.PreferredBackBufferHeight = 720;
this.GraphicsDeviceManager.ApplyChanges();
base.LoadContent();
2021-03-08 19:08:41 +01:00
this.UiSystem.Style.Font = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/Regular"));
this.UiSystem.Style.TextScale = 0.075F;
this.UiSystem.AutoScaleWithScreen = true;
2021-03-07 22:23:51 +01:00
this.InputHandler.HandleKeyboardRepeats = false;
2021-03-08 02:37:40 +01:00
this.Camera = new Camera(this.GraphicsDevice) {
2021-03-07 22:23:51 +01:00
AutoScaleWithScreen = true,
2021-03-09 02:37:44 +01:00
Scale = 4
2021-03-07 22:23:51 +01:00
};
this.Map = new Map("Level1");
2021-03-09 02:37:44 +01:00
this.Player = new Player(this.Map, this.Map.SpawnPoint.ToVector2());
2021-03-07 22:23:51 +01:00
this.Map.AddEntity(this.Player);
}
protected override void DoUpdate(GameTime gameTime) {
base.DoUpdate(gameTime);
this.Map.Update(gameTime);
2021-03-08 19:08:41 +01:00
if (!this.IsFinishing) {
this.Camera.LookingPosition = Vector2.Lerp(this.Camera.LookingPosition, (this.Player.Position + Vector2.One / 2) * this.Map.TileSize, 0.25F);
this.Camera.ConstrainWorldBounds(Vector2.Zero, this.Map.SizeInPixels.ToVector2());
}
2021-03-07 22:23:51 +01:00
}
protected override void DoDraw(GameTime gameTime) {
this.GraphicsDevice.Clear(Color.CornflowerBlue);
base.DoDraw(gameTime);
2021-03-08 02:37:40 +01:00
this.Map.Draw(gameTime, this.SpriteBatch, this.Camera);
2021-03-07 22:23:51 +01:00
}
2021-03-08 19:08:41 +01:00
public void Finish() {
IEnumerable<Wait> Impl() {
var offset = 0F;
while (offset < 1) {
this.Camera.LookingPosition -= new Vector2(0, offset) * this.Map.TileSize;
offset += 0.01F;
yield return new Wait(CoroutineEvents.Update);
}
var snowRemoved = this.Map.TotalSnow - this.Map.GetTotalTiles("Snow");
var plantsGrown = this.Map.TotalSeeds - this.Map.GetTotalTiles("Seed");
var completion = (snowRemoved + plantsGrown) / (float) (this.Map.TotalSnow + this.Map.TotalSeeds);
var group = new Group(Anchor.Center, new Vector2(0.5F), false);
this.UiSystem.Add("EndCard", group);
group.AddChild(new Paragraph(Anchor.AutoCenter, 1, "Level Completed!", true) {TextScale = 0.125F});
group.AddChild(new VerticalSpace(15));
yield return new Wait(1);
group.AddChild(new Paragraph(Anchor.AutoCenter, 1, $"Snow Removed: {snowRemoved} / {this.Map.TotalSnow}", true));
yield return new Wait(1);
group.AddChild(new Paragraph(Anchor.AutoCenter, 1, $"Plants Watered: {plantsGrown} / {this.Map.TotalSeeds}", true));
yield return new Wait(1);
var completionCounter = 0;
group.AddChild(new VerticalSpace(10));
const string scoreText = "Score: {0}%";
var score = group.AddChild(new Paragraph(Anchor.AutoCenter, 1, string.Format(scoreText, 0), true) {TextScale = 0.1F});
while (completionCounter < completion * 100) {
completionCounter++;
score.Text = string.Format(scoreText, completionCounter);
2021-03-09 02:37:44 +01:00
yield return new Wait(0.03F);
2021-03-08 19:08:41 +01:00
}
}
this.IsFinishing = true;
CoroutineHandler.Start(Impl());
}
2021-03-07 22:23:51 +01:00
}
}