using System.Collections.Generic; using Coroutine; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using MLEM.Cameras; using MLEM.Font; using MLEM.Startup; using MLEM.Ui; using MLEM.Ui.Elements; 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; } public Camera Camera { get; private set; } public bool IsFinishing { get; private set; } public GameImpl() { Instance = this; this.IsMouseVisible = true; } protected override void LoadContent() { this.GraphicsDeviceManager.PreferredBackBufferWidth = 1280; this.GraphicsDeviceManager.PreferredBackBufferHeight = 720; this.GraphicsDeviceManager.ApplyChanges(); base.LoadContent(); this.UiSystem.Style.Font = new GenericSpriteFont(LoadContent("Fonts/Regular")); this.UiSystem.Style.TextScale = 0.075F; this.UiSystem.AutoScaleWithScreen = true; this.InputHandler.HandleKeyboardRepeats = false; this.Camera = new Camera(this.GraphicsDevice) { AutoScaleWithScreen = true, Scale = 4, Position = new Vector2(0, float.MaxValue) }; this.Map = new Map("Level1"); this.Player = new Player(this.Map, new Vector2(70, 20)); this.Map.AddEntity(this.Player); } protected override void DoUpdate(GameTime gameTime) { base.DoUpdate(gameTime); this.Map.Update(gameTime); 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()); } } protected override void DoDraw(GameTime gameTime) { this.GraphicsDevice.Clear(Color.CornflowerBlue); base.DoDraw(gameTime); this.Map.Draw(gameTime, this.SpriteBatch, this.Camera); } public void Finish() { IEnumerable 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); yield return new Wait(0.05F); } } this.IsFinishing = true; CoroutineHandler.Start(Impl()); } } }