TouchyTickets/ThemeParkClicker/ParkMap.cs
2020-05-31 21:16:50 +02:00

79 lines
No EOL
3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using MLEM.Extensions;
using MLEM.Misc;
using MLEM.Startup;
using MLEM.Textures;
using ThemeParkClicker.Attractions;
namespace ThemeParkClicker {
public class ParkMap {
public readonly int Width;
public readonly int Height;
private readonly Attraction[,] grid;
private readonly Dictionary<Point, Attraction> attractions = new Dictionary<Point, Attraction>();
public ParkMap(int width, int height) {
this.Width = width;
this.Height = height;
this.grid = new Attraction[width, height];
}
public void Update(GameTime time) {
foreach (var attraction in this.attractions.Values)
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) {
var tileSize = Attraction.TileSize * scale;
// draw ground
batch.Draw(batch.GetBlankTexture(), new RectangleF(position, new Vector2(this.Width, this.Height) * tileSize), ColorExtensions.FromHex(0xff53a662));
// draw attractions
foreach (var kv in this.attractions)
batch.Draw(kv.Value.TextureRegion, position + kv.Key.ToVector2() * tileSize, Color.White);
}
public bool CanPlace(Point position, Attraction attraction) {
for (var x = 0; x < attraction.Width; x++) {
for (var y = 0; y < attraction.Height; y++) {
if (this.grid[position.X + x, position.Y + y] != null)
return false;
}
}
return true;
}
public void Place(Point position, Attraction attraction) {
for (var x = 0; x < attraction.Width; x++) {
for (var y = 0; y < attraction.Height; y++)
this.grid[position.X + x, position.Y + y] = attraction;
}
this.attractions[position] = attraction;
}
}
}