TouchyTickets/TouchyTickets/ParkMap.cs

284 lines
14 KiB
C#
Raw Normal View History

2020-05-31 21:16:50 +02:00
using System;
2020-05-31 17:24:10 +02:00
using System.Collections.Generic;
2020-05-31 21:16:50 +02:00
using System.Linq;
2020-06-01 14:45:20 +02:00
using System.Runtime.Serialization;
2020-05-31 17:24:10 +02:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
2020-05-31 21:16:50 +02:00
using Microsoft.Xna.Framework.Input.Touch;
2020-05-31 17:24:10 +02:00
using MLEM.Extensions;
using MLEM.Misc;
using MLEM.Startup;
using MLEM.Textures;
2020-06-01 17:39:57 +02:00
using TouchyTickets.Attractions;
2020-07-10 20:45:23 +02:00
using TouchyTickets.Upgrades;
2020-05-31 17:24:10 +02:00
2020-06-01 17:39:57 +02:00
namespace TouchyTickets {
2020-06-01 14:45:20 +02:00
[DataContract]
2020-05-31 17:24:10 +02:00
public class ParkMap {
2020-06-01 23:02:47 +02:00
private const int AdditionalRadius = 15;
2020-07-09 00:25:44 +02:00
private const int AutoBuyIntervalSecs = 30;
2020-06-01 14:45:20 +02:00
[DataMember]
2020-05-31 17:24:10 +02:00
public readonly int Width;
2020-06-01 14:45:20 +02:00
[DataMember]
2020-05-31 17:24:10 +02:00
public readonly int Height;
2020-06-01 14:45:20 +02:00
[DataMember]
private readonly List<(Point, Attraction)> attractions = new List<(Point, Attraction)>();
2020-06-01 16:18:07 +02:00
private readonly Dictionary<Point, int> treePositions = new Dictionary<Point, int>();
private readonly Dictionary<Point, int> fencePositions = new Dictionary<Point, int>();
2020-06-02 12:20:16 +02:00
private readonly Attraction[,] attractionGrid;
2020-05-31 17:24:10 +02:00
2020-07-08 18:53:48 +02:00
[DataMember]
public double TicketsPerSecond { get; private set; }
2020-06-01 01:21:54 +02:00
public Attraction PlacingAttraction;
2020-06-08 18:01:10 +02:00
public AttractionModifier PlacingModifier;
2020-06-01 01:21:54 +02:00
public Point PlacingPosition;
2020-06-05 20:49:42 +02:00
public Point? SelectedPosition;
2020-06-01 01:21:54 +02:00
private bool draggingAttraction;
2020-07-09 00:25:44 +02:00
private double autoBuyCounter;
2020-06-01 01:21:54 +02:00
2020-05-31 17:24:10 +02:00
public ParkMap(int width, int height) {
this.Width = width;
this.Height = height;
2020-06-02 12:20:16 +02:00
this.attractionGrid = new Attraction[width, height];
2020-06-01 16:18:07 +02:00
// set up trees
var random = new Random();
2020-06-01 23:02:47 +02:00
for (var x = -AdditionalRadius; x < this.Width + AdditionalRadius; x++) {
for (var y = -AdditionalRadius; y < this.Height + AdditionalRadius; y++) {
var pos = new Point(x, y);
if (this.IsInBounds(pos))
2020-06-01 23:02:47 +02:00
continue;
if (random.Next(15) != 0)
continue;
var type = random.Next(3);
this.treePositions[pos] = type;
2020-06-01 23:02:47 +02:00
}
2020-06-01 16:18:07 +02:00
}
// set up fences
this.fencePositions[new Point(-1, -1)] = 2;
this.fencePositions[new Point(this.Width, -1)] = 3;
this.fencePositions[new Point(-1, this.Height)] = 4;
this.fencePositions[new Point(this.Width, this.Height)] = 5;
for (var x = 0; x < this.Width; x++) {
this.fencePositions[new Point(x, -1)] = 0;
this.fencePositions[new Point(x, this.Height)] = 0;
}
for (var y = 0; y < this.Height; y++) {
this.fencePositions[new Point(-1, y)] = 1;
this.fencePositions[new Point(this.Width, y)] = 1;
}
2020-05-31 17:24:10 +02:00
}
2020-07-08 16:04:44 +02:00
public void Update(TimeSpan passed, bool wasAway) {
var toSimulate = wasAway ? new TimeSpan(passed.Ticks / 2) : passed;
// handle auto-buying
2020-07-09 00:25:44 +02:00
this.autoBuyCounter += toSimulate.TotalSeconds;
this.TryAutoBuy();
var autoBuysPerAttraction = ((float) this.autoBuyCounter / AutoBuyIntervalSecs / this.attractions.Count).Ceil();
2020-07-08 16:04:44 +02:00
// update tickets
2020-07-08 14:36:26 +02:00
this.TicketsPerSecond = 0;
2020-06-02 12:20:16 +02:00
foreach (var (pos, attraction) in this.attractions) {
2020-07-08 16:04:44 +02:00
var genPerSecond = attraction.Update(toSimulate, this, pos);
2020-07-08 14:36:26 +02:00
this.TicketsPerSecond += genPerSecond;
2020-07-08 16:04:44 +02:00
2020-07-09 00:25:44 +02:00
// if we were away, we have to catch up with auto-buys while also taking into account
// the amount of tickets that each ride generates. The easiest way we can do this is
// to progress, between updating each ride, by a percentage of the total update amount
if (wasAway) {
for (var i = autoBuysPerAttraction; i > 0; i--) {
if (!this.TryAutoBuy())
break;
}
}
2020-06-01 16:18:07 +02:00
}
2020-05-31 21:16:50 +02:00
// map movement
2020-06-02 16:46:44 +02:00
if (GameImpl.Instance.DrawMap && GameImpl.Instance.UiSystem.Controls.HandleTouch) {
2020-05-31 21:16:50 +02:00
var camera = GameImpl.Instance.Camera;
if (MlemGame.Input.GetGesture(GestureType.Pinch, out var pinch)) {
2020-06-15 16:49:13 +02:00
// pinch zoom
var center = (pinch.Position + pinch.Position2) / 2;
var newDist = Vector2.Distance(pinch.Position + pinch.Delta, pinch.Position2 + pinch.Delta2);
var oldDist = Vector2.Distance(pinch.Position, pinch.Position2);
var newScale = newDist / oldDist * camera.Scale;
camera.Zoom(newScale - camera.Scale, center);
2020-05-31 21:16:50 +02:00
} else if (MlemGame.Input.GetGesture(GestureType.FreeDrag, out var drag)) {
2020-06-01 01:21:54 +02:00
if (this.draggingAttraction) {
// move the current placing position
var nextPos = (camera.ToWorldPos(drag.Position + drag.Delta) / Assets.TileSize).ToPoint();
2020-06-02 15:49:46 +02:00
// drag the center of the attraction
nextPos -= new Point(this.PlacingAttraction.Type.Width / 2, this.PlacingAttraction.Type.Height / 2);
if (this.PlacingAttraction.Type.GetCoveredTiles().Select(p => nextPos + p).All(this.IsInBounds))
2020-06-01 01:21:54 +02:00
this.PlacingPosition = nextPos;
} else {
// move the camera
2020-06-15 16:49:13 +02:00
camera.Position -= drag.Delta / camera.ActualScale;
2020-06-01 01:21:54 +02:00
}
} else if (this.PlacingAttraction != null) {
foreach (var touch in MlemGame.Input.TouchState) {
if (touch.State != TouchLocationState.Pressed)
continue;
// when first pressing down, go into attraction drag mode if we're touching the place location
var offset = (camera.ToWorldPos(touch.Position) / Assets.TileSize).ToPoint();
2020-06-02 15:49:46 +02:00
this.draggingAttraction = this.PlacingAttraction.Type.GetCoveredTiles()
2020-06-01 01:21:54 +02:00
.Any(p => this.PlacingPosition + p == offset);
}
2020-06-05 20:49:42 +02:00
} else {
// we're not placing an attraction, so we're in remove and move mode
if (MlemGame.Input.GetGesture(GestureType.Tap, out var tap) && GameImpl.Instance.UiSystem.Controls.GetElementUnderPos(tap.Position) == null) {
var pos = (camera.ToWorldPos(tap.Position) / Assets.TileSize).ToPoint();
2020-06-05 20:49:42 +02:00
var attraction = this.GetAttractionAt(pos);
2020-06-08 22:55:33 +02:00
if (attraction != null && (this.PlacingModifier == null || this.PlacingModifier.IsAffected(attraction))) {
2020-06-05 20:49:42 +02:00
// actually select the top left for easy usage later
this.SelectedPosition = this.attractions.First(kv => kv.Item2 == attraction).Item1;
} else {
2020-06-05 20:49:42 +02:00
this.SelectedPosition = null;
}
}
2020-05-31 21:16:50 +02:00
}
camera.ConstrainWorldBounds(new Vector2(-AdditionalRadius) * Assets.TileSize, new Vector2(this.Width + AdditionalRadius, this.Height + AdditionalRadius) * Assets.TileSize);
2020-05-31 21:16:50 +02:00
}
2020-05-31 17:24:10 +02:00
}
public void Draw(GameTime time, SpriteBatch batch, Vector2 position, float scale, float alpha, bool showSurroundings, RectangleF visibleArea) {
var tileSize = Assets.TileSize * scale;
2020-05-31 17:24:10 +02:00
// draw ground
var additionalRadius = showSurroundings ? AdditionalRadius : 0;
var minX = Math.Max(-additionalRadius, visibleArea.Left / tileSize.X).Floor();
var minY = Math.Max(-additionalRadius, visibleArea.Top / tileSize.Y).Floor();
var maxX = Math.Min(this.Width + additionalRadius, visibleArea.Right / tileSize.X).Ceil();
var maxY = Math.Min(this.Height + additionalRadius, visibleArea.Bottom / tileSize.Y).Ceil();
for (var x = minX; x < maxX; x++) {
for (var y = minY; y < maxY; y++) {
2020-06-01 16:18:07 +02:00
var pos = new Vector2(x, y);
var drawPos = position + pos * tileSize;
batch.Draw(Assets.TilesTexture[0, 0], drawPos, Color.White * alpha, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
2020-06-01 16:18:07 +02:00
if (this.fencePositions.TryGetValue(pos.ToPoint(), out var fenceType)) {
batch.Draw(Assets.TilesTexture[fenceType, 1], drawPos, Color.White * alpha, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
2020-06-01 16:18:07 +02:00
} else if (this.treePositions.TryGetValue(pos.ToPoint(), out var treeType)) {
batch.Draw(Assets.TilesTexture[1 + treeType, 0], drawPos, Color.White * alpha, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
2020-06-01 16:18:07 +02:00
}
}
}
2020-06-05 20:49:42 +02:00
// selected attraction
if (this.SelectedPosition != null) {
var selected = this.SelectedPosition.Value;
var attr = this.GetAttractionAt(selected);
foreach (var pos in attr.Type.GetCoveredTiles())
2020-06-08 22:55:33 +02:00
batch.Draw(batch.GetBlankTexture(), new RectangleF(position + (selected + pos).ToVector2() * tileSize, tileSize), Color.Black * 0.25F * alpha);
2020-06-05 20:49:42 +02:00
}
2020-05-31 17:24:10 +02:00
// draw attractions
2020-06-08 22:55:33 +02:00
foreach (var (pos, attraction) in this.attractions) {
if (this.PlacingModifier != null && this.PlacingModifier.IsAffected(attraction)) {
var color = GameImpl.Instance.Tickets >= attraction.GetModifierPrice(this.PlacingModifier) ? Color.Yellow : Color.Red;
2020-06-08 22:55:33 +02:00
foreach (var offset in attraction.Type.GetCoveredTiles())
batch.Draw(batch.GetBlankTexture(), new RectangleF(position + (pos + offset).ToVector2() * tileSize, tileSize), color * 0.25F * alpha);
2020-06-08 22:55:33 +02:00
}
2020-06-16 17:52:29 +02:00
attraction.Draw(batch, position + pos.ToVector2() * tileSize, alpha, scale);
2020-06-08 22:55:33 +02:00
}
2020-06-01 01:21:54 +02:00
// placing attraction
if (this.PlacingAttraction != null) {
var placingPos = position + this.PlacingPosition.ToVector2() * tileSize;
var color = this.CanPlace(this.PlacingPosition, this.PlacingAttraction) ? Color.Yellow : Color.Red;
2020-06-02 15:49:46 +02:00
foreach (var pos in this.PlacingAttraction.Type.GetCoveredTiles())
batch.Draw(batch.GetBlankTexture(), new RectangleF(placingPos + pos.ToVector2() * tileSize, tileSize), color * 0.25F * alpha);
2020-06-16 17:52:29 +02:00
this.PlacingAttraction.Draw(batch, placingPos, alpha * 0.5F, scale);
2020-06-01 01:21:54 +02:00
}
2020-05-31 17:24:10 +02:00
}
public bool CanPlace(Point position, Attraction attraction) {
2020-06-02 15:49:46 +02:00
foreach (var offset in attraction.Type.GetCoveredTiles()) {
if (!this.IsInBounds(position + offset))
return false;
2020-06-01 14:45:20 +02:00
if (this.GetAttractionAt(position + offset) != null)
2020-06-01 01:21:54 +02:00
return false;
2020-05-31 17:24:10 +02:00
}
return true;
}
public void Place(Point position, Attraction attraction) {
2020-06-02 15:49:46 +02:00
foreach (var (x, y) in attraction.Type.GetCoveredTiles())
2020-06-02 12:20:16 +02:00
this.attractionGrid[position.X + x, position.Y + y] = attraction;
2020-06-01 14:45:20 +02:00
this.attractions.Add((position, attraction));
}
2020-06-05 20:49:42 +02:00
public Attraction Remove(Point position) {
var attraction = this.GetAttractionAt(position);
if (attraction != null) {
foreach (var (x, y) in attraction.Type.GetCoveredTiles())
this.attractionGrid[position.X + x, position.Y + y] = null;
this.attractions.Remove((position, attraction));
}
return attraction;
}
2020-06-01 14:45:20 +02:00
public Attraction GetAttractionAt(Point position) {
return !this.IsInBounds(position) ? null : this.attractionGrid[position.X, position.Y];
2020-05-31 17:24:10 +02:00
}
2020-06-01 15:18:20 +02:00
public int GetAttractionAmount(AttractionType type) {
2020-06-08 22:55:33 +02:00
return this.attractions.Count(a => type == null || a.Item2.Type == type);
2020-06-01 15:18:20 +02:00
}
2020-06-08 18:01:10 +02:00
public int GetModifierAmount(AttractionModifier modifier) {
return this.attractions.Sum(a => a.Item2.GetModifierAmount(modifier));
}
2020-06-17 01:48:35 +02:00
public bool IsAnyAttractionAffected(AttractionModifier modifier) {
return this.attractions.Any(a => modifier.IsAffected(a.Item2));
}
2020-07-21 21:51:30 +02:00
public IEnumerable<(Point, Attraction)> GetAttractions() {
foreach (var attraction in this.attractions)
yield return attraction;
}
public bool IsInBounds(Point pos) {
return pos.X >= 0 && pos.Y >= 0 && pos.X < this.Width && pos.Y < this.Height;
}
2020-06-02 12:20:16 +02:00
public ParkMap Copy(int? newWidth = null, int? newHeight = null) {
var newMap = new ParkMap(newWidth ?? this.Width, newHeight ?? this.Height);
foreach (var (pos, attraction) in this.attractions) {
if (newMap.CanPlace(pos, attraction))
newMap.Place(pos, attraction);
}
2020-07-08 18:53:48 +02:00
newMap.TicketsPerSecond = this.TicketsPerSecond;
2020-06-02 12:20:16 +02:00
return newMap;
2020-06-01 23:02:47 +02:00
}
2020-07-09 00:25:44 +02:00
private bool TryAutoBuy() {
2020-07-08 16:04:44 +02:00
if (!Options.Instance.AutoBuyEnabled)
2020-07-09 00:25:44 +02:00
return false;
if (GameImpl.Instance.Tickets < Options.Instance.MinTicketsForAutoBuy)
return false;
if (this.autoBuyCounter < AutoBuyIntervalSecs)
return false;
this.autoBuyCounter -= AutoBuyIntervalSecs;
2020-07-08 16:04:44 +02:00
2020-07-09 00:25:44 +02:00
var success = false;
// auto-buy modifiers
if (Upgrade.AutoPlaceModifiers[0].IsActive()) {
// loop through all attractions, but look at attractions with fewer applied modifiers first
foreach (var attraction in this.attractions.Select(kv => kv.Item2).OrderBy(a => a.GetModifierAmount(null))) {
var match = AttractionModifier.Modifiers.Values.Where(m => m.IsAffected(attraction));
// if we don't have level 2, we only want to increase existing modifiers
if (!Upgrade.AutoPlaceModifiers[1].IsActive())
match = match.Where(m => attraction.GetModifierAmount(m) > 0);
// we want to apply the least applied modifier on this attraction
var modifier = match.OrderBy(m => attraction.GetModifierAmount(m)).FirstOrDefault();
if (modifier != null && modifier.Buy(attraction))
success = true;
2020-07-08 16:04:44 +02:00
}
}
2020-07-09 00:25:44 +02:00
return success;
2020-07-08 16:04:44 +02:00
}
2020-05-31 17:24:10 +02:00
}
}