big map view and movement

This commit is contained in:
Ellpeck 2020-05-31 21:16:50 +02:00
parent 43a1c3c904
commit f485f10c9f
4 changed files with 92 additions and 41 deletions

View file

@ -15,6 +15,7 @@ namespace ThemeParkClicker {
public ParkMap Map { get; private set; } public ParkMap Map { get; private set; }
public Camera Camera { get; private set; } public Camera Camera { get; private set; }
public Ui Ui { get; private set; } public Ui Ui { get; private set; }
public bool DrawMap;
public GameImpl() { public GameImpl() {
Instance = this; Instance = this;
@ -25,14 +26,17 @@ namespace ThemeParkClicker {
this.Ui = new Ui(this.UiSystem); this.Ui = new Ui(this.UiSystem);
this.Map = new ParkMap(10, 10); this.Map = new ParkMap(10, 10);
this.Camera = new Camera(this.GraphicsDevice) { this.Camera = new Camera(this.GraphicsDevice) {
Scale = 4, Scale = 16,
AutoScaleWithScreen = true AutoScaleWithScreen = true,
AutoScaleReferenceSize = new Point(720, 1280),
MaxScale = 24,
MinScale = 4
}; };
this.Map.Place(Point.Zero, Attraction.Attractions["Carousel"]()); this.Map.Place(Point.Zero, Attraction.Attractions["Carousel"]());
this.Map.Place(new Point(1, 0), Attraction.Attractions["Carousel"]()); this.Map.Place(new Point(1, 0), Attraction.Attractions["Carousel"]());
this.Map.Place(new Point(3, 0), Attraction.Attractions["Carousel"]()); this.Map.Place(new Point(3, 0), Attraction.Attractions["Carousel"]());
this.Map.Place(new Point(1,2), Attraction.Attractions["FoodCourt"]()); this.Map.Place(new Point(1, 2), Attraction.Attractions["FoodCourt"]());
} }
public string DisplayTicketCount() { public string DisplayTicketCount() {
@ -46,7 +50,12 @@ namespace ThemeParkClicker {
} }
protected override void DoDraw(GameTime gameTime) { protected override void DoDraw(GameTime gameTime) {
this.GraphicsDevice.Clear(ColorExtensions.FromHex(0x86cfcb)); 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);
this.SpriteBatch.End();
}
base.DoDraw(gameTime); base.DoDraw(gameTime);
} }

View file

@ -1,6 +1,9 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using MLEM.Extensions; using MLEM.Extensions;
using MLEM.Misc; using MLEM.Misc;
using MLEM.Startup; using MLEM.Startup;
@ -25,6 +28,24 @@ namespace ThemeParkClicker {
public void Update(GameTime time) { public void Update(GameTime time) {
foreach (var attraction in this.attractions.Values) foreach (var attraction in this.attractions.Values)
attraction.GainTickets(time); attraction.GainTickets(time);
// map movement
if (GameImpl.Instance.DrawMap) {
var camera = GameImpl.Instance.Camera;
if (MlemGame.Input.GetGesture(GestureType.Pinch, out var pinch)) {
var startDiff = pinch.Position2 - pinch.Position;
var endDiff = pinch.Position2 + pinch.Delta2 - (pinch.Position + pinch.Delta);
if (startDiff.LengthSquared() < endDiff.LengthSquared()) {
// zooming in
camera.Zoom(pinch.Delta.Length() / camera.Scale);
} else {
// zooming out
camera.Zoom(-pinch.Delta.Length() / camera.Scale);
}
} else if (MlemGame.Input.GetGesture(GestureType.FreeDrag, out var drag)) {
camera.Position -= drag.Delta / camera.Scale;
}
}
} }
public void Draw(GameTime time, SpriteBatch batch, Vector2 position, float scale) { public void Draw(GameTime time, SpriteBatch batch, Vector2 position, float scale) {

View file

@ -5,7 +5,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="MLEM.Startup" Version="3.3.3-186" /> <PackageReference Include="MLEM.Startup" Version="3.3.3-189" />
<PackageReference Include="MonoGame.Framework.Portable" Version="3.7.1.189"> <PackageReference Include="MonoGame.Framework.Portable" Version="3.7.1.189">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>

View file

@ -28,7 +28,7 @@ namespace ThemeParkClicker {
private bool finishingSwipe; private bool finishingSwipe;
public Ui(UiSystem uiSystem) { public Ui(UiSystem uiSystem) {
InputHandler.EnableGestures(GestureType.HorizontalDrag, GestureType.Pinch); InputHandler.EnableGestures(GestureType.HorizontalDrag, GestureType.FreeDrag, GestureType.Pinch);
this.uiSystem = uiSystem; this.uiSystem = uiSystem;
this.uiSystem.GlobalScale = 4; this.uiSystem.GlobalScale = 4;
this.uiSystem.AutoScaleWithScreen = true; this.uiSystem.AutoScaleWithScreen = true;
@ -49,6 +49,7 @@ namespace ThemeParkClicker {
rainingTickets.Add(new RainingTicket()); rainingTickets.Add(new RainingTicket());
}, },
OnDrawn = (e, time, batch, alpha) => { OnDrawn = (e, time, batch, alpha) => {
batch.Draw(batch.GetBlankTexture(), e.DisplayArea, ColorExtensions.FromHex(0xff86cfcb));
foreach (var ticket in rainingTickets) foreach (var ticket in rainingTickets)
ticket.Draw(batch, e.DisplayArea.Size, e.Scale); ticket.Draw(batch, e.DisplayArea.Size, e.Scale);
} }
@ -80,7 +81,8 @@ namespace ThemeParkClicker {
var scale = Math.Min(scaleX, scaleY); var scale = Math.Min(scaleX, scaleY);
var pos = e.DisplayArea.Location + (e.DisplayArea.Size - mapSize * scale) / 2; var pos = e.DisplayArea.Location + (e.DisplayArea.Size - mapSize * scale) / 2;
map.Draw(time, batch, pos, scale); map.Draw(time, batch, pos, scale);
} },
OnPressed = e => CoroutineHandler.Start(this.SwipeUi(true))
}); });
this.currentUi = main; this.currentUi = main;
this.uiSystem.Add("Main", main); this.uiSystem.Add("Main", main);
@ -115,6 +117,7 @@ namespace ThemeParkClicker {
public void Update(GameTime time) { public void Update(GameTime time) {
// swiping between tabs // swiping between tabs
if (!this.currentUi.IsHidden) {
if (MlemGame.Input.GetGesture(GestureType.HorizontalDrag, out var gesture)) { if (MlemGame.Input.GetGesture(GestureType.HorizontalDrag, out var gesture)) {
this.swipeProgress -= gesture.Delta.X / this.currentUi.DisplayArea.Width; this.swipeProgress -= gesture.Delta.X / this.currentUi.DisplayArea.Width;
} else if (!this.finishingSwipe && this.swipeProgress != 0 && !MlemGame.Input.TouchState.Any()) { } else if (!this.finishingSwipe && this.swipeProgress != 0 && !MlemGame.Input.TouchState.Any()) {
@ -153,6 +156,7 @@ namespace ThemeParkClicker {
this.swipeRelations[next].Root.Transform = Matrix.CreateTranslation((Math.Sign(this.swipeProgress) - this.swipeProgress) * this.swipeRelations[next].DisplayArea.Width, 0, 0); this.swipeRelations[next].Root.Transform = Matrix.CreateTranslation((Math.Sign(this.swipeProgress) - this.swipeProgress) * this.swipeRelations[next].DisplayArea.Width, 0, 0);
} }
} }
}
private void ResetSwipe() { private void ResetSwipe() {
this.finishingSwipe = false; this.finishingSwipe = false;
@ -163,6 +167,23 @@ namespace ThemeParkClicker {
} }
} }
private IEnumerator<IWait> SwipeUi(bool swipeOut) {
GameImpl.Instance.DrawMap = true;
this.currentUi.IsHidden = false;
var offset = 0F;
while (offset < 1) {
offset += 0.025F;
var trans = (!swipeOut ? 1 - offset : offset) * this.currentUi.DisplayArea.Height;
this.currentUi.Root.Transform = Matrix.CreateTranslation(0, trans, 0);
yield return new WaitEvent(CoroutineEvents.Update);
}
this.currentUi.Root.Transform = Matrix.Identity;
GameImpl.Instance.DrawMap = swipeOut;
this.currentUi.IsHidden = swipeOut;
// disable horizontal and vertical drag on map view to allow free drag to take priority
InputHandler.SetGesturesEnabled(!swipeOut, GestureType.HorizontalDrag, GestureType.VerticalDrag);
}
private static IEnumerator<IWait> WobbleElement(CustomDrawGroup element, float intensity = 0.02F) { private static IEnumerator<IWait> WobbleElement(CustomDrawGroup element, float intensity = 0.02F) {
var sin = 0F; var sin = 0F;
while (sin < MathHelper.Pi) { while (sin < MathHelper.Pi) {