|
|
using System;
|
|
|
using System.Linq;
|
|
|
using System.Collections.Generic;
|
|
|
using Microsoft.Xna.Framework;
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
|
|
using Encompass;
|
|
|
using JM.LinqFaster;
|
|
|
|
|
|
using isometricparkfna.Messages;
|
|
|
using isometricparkfna.Components;
|
|
|
|
|
|
namespace isometricparkfna.Engines {
|
|
|
|
|
|
[Receives(typeof(SpawnSelection),
|
|
|
typeof(AdjustSelection))]
|
|
|
[Writes(typeof(AreaComponent),
|
|
|
// typeof(SelectedComponent),
|
|
|
typeof(PreserveComponent))]
|
|
|
[Reads(typeof(SelectedComponent),
|
|
|
typeof(ContractStatusComponent),
|
|
|
typeof(AreaComponent),
|
|
|
typeof(PreserveComponent))]
|
|
|
public class BuildToolEngine : Engine {
|
|
|
|
|
|
private CellMap Map;
|
|
|
|
|
|
public BuildToolEngine(CellMap map) {
|
|
|
this.Map = map;
|
|
|
}
|
|
|
|
|
|
private System.Collections.Generic.IEnumerable<float> step_until(float start, float stop) {
|
|
|
if (stop >= start) {
|
|
|
for(float i = start; i <= stop; i++)
|
|
|
{
|
|
|
yield return i;
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
for(float i = start; i >= stop; i--)
|
|
|
{
|
|
|
yield return i;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public override void Update(double dt) {
|
|
|
var occupied = new List<Vector2>();
|
|
|
foreach (var (entity, status) in ReadEntities<AreaComponent>()
|
|
|
.WhereF((e) => HasComponent<ContractStatusComponent>(e))
|
|
|
.SelectWhereF((e) => (e, GetComponent<ContractStatusComponent>(e)),
|
|
|
(e) => ((e.Item2.status != ContractStatus.Broken)
|
|
|
&& (e.Item2.status != ContractStatus.Rejected)
|
|
|
&& (e.Item2.status != ContractStatus.Expired)
|
|
|
&& (e.Item2.status != ContractStatus.Completed)
|
|
|
&& (e.Item2.status != ContractStatus.Proposed))
|
|
|
)) {
|
|
|
var entitySquares = GetComponent<AreaComponent>(entity).squares;
|
|
|
occupied.AddRange(entitySquares);
|
|
|
}
|
|
|
foreach (var entity in ReadEntities<AreaComponent>()
|
|
|
.WhereF((e) => HasComponent<PreserveComponent>(e))) {
|
|
|
var entitySquares = GetComponent<AreaComponent>(entity).squares;
|
|
|
occupied.AddRange(entitySquares);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (ref readonly var message in ReadMessages<SpawnSelection>())
|
|
|
{
|
|
|
if (message.Start.X >= 0 && message.Start.X < this.Map.MapWidth
|
|
|
&& message.Start.Y >= 0 && message.Start.Y < this.Map.MapHeight) {
|
|
|
var entity = CreateEntity();
|
|
|
|
|
|
AddComponent(entity, new AreaComponent { squares = new[] {message.Start} });
|
|
|
AddComponent(entity, new SelectedComponent { selected = true, Type= SelectionType.Area});
|
|
|
}
|
|
|
}
|
|
|
|
|
|
foreach (ref readonly var message in ReadMessages<AdjustSelection>()) {
|
|
|
foreach (ref readonly var entity in ReadEntities<SelectedComponent>()) {
|
|
|
var selection = GetComponent<SelectedComponent>(entity);
|
|
|
if(selection.Type == SelectionType.Area
|
|
|
&& selection.selected) {
|
|
|
if(message.Type == AdjustmentType.Clear) {
|
|
|
Destroy(entity);
|
|
|
}
|
|
|
else if(message.Type == AdjustmentType.Complete) {
|
|
|
SetComponent(entity, new PreserveComponent {});
|
|
|
// SetComponent(entity, new SelectedComponent {selected = false });
|
|
|
}
|
|
|
else {
|
|
|
var area = GetComponent<AreaComponent>(entity);
|
|
|
|
|
|
var newSquares = new List<Vector2>();
|
|
|
|
|
|
var end_x = MathUtils.Clamp(message.End.X, 0.0f, this.Map.MapWidth);
|
|
|
var end_y = MathUtils.Clamp(message.End.Y, 0.0f, this.Map.MapHeight);
|
|
|
|
|
|
foreach (var i in step_until(area.squares[0].X, end_x)) {
|
|
|
foreach (var j in step_until(area.squares[0].Y, end_y)) {
|
|
|
var newSquare = new Vector2(i, j);
|
|
|
if (!occupied.Contains(newSquare)) {
|
|
|
newSquares.Add(newSquare);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
SetComponent(entity, new AreaComponent { squares = newSquares.ToArray() /*( new[]{ area.squares[0], message.End}*/});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|