# HG changeset patch # User Alys Brooks # Date 2022-02-12 11:06:30 # Node ID e3be69aada5b611d8fe1a3fe1e3641a5793e5154 # Parent 0d56136b7883838dd37f2b7560ce6c0e9c4ad1c7 Draw areas when clicked. diff --git a/isometric-park-fna/CellMap.cs b/isometric-park-fna/CellMap.cs --- a/isometric-park-fna/CellMap.cs +++ b/isometric-park-fna/CellMap.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using Microsoft.Xna.Framework; + namespace isometricparkfna { @@ -186,6 +188,17 @@ return count; } + public static Vector2 calculateMousegrid(Vector2 normalizedMousePos) + { + Vector2 adjust = new Vector2(Tile.TileSpriteWidth / 2, Tile.TileSpriteHeight); + Vector2 adjustedMousePos = normalizedMousePos - adjust; + + float boardx = ((adjustedMousePos.X / Tile.TileSpriteWidth) + (adjustedMousePos.Y / Tile.TileSpriteHeight)); + float boardy = ((adjustedMousePos.Y / Tile.TileSpriteHeight) - (adjustedMousePos.X / Tile.TileSpriteWidth)); + + return new Vector2((int)boardx, (int)boardy); + } + public System.Collections.Generic.IEnumerable iterate_cells_with_neighbors(int neighbors) { for (int i = 0; i < MapHeight; i++) diff --git a/isometric-park-fna/Engines/BuildToolEngine.cs b/isometric-park-fna/Engines/BuildToolEngine.cs new file mode 100644 --- /dev/null +++ b/isometric-park-fna/Engines/BuildToolEngine.cs @@ -0,0 +1,29 @@ +using System; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; + +using Encompass; + +using isometricparkfna.Messages; +using isometricparkfna.Components; + +namespace isometricparkfna.Engines { + + [Receives(typeof(SpawnSelection), + typeof(AdjustSelection))] + [Writes(typeof(AreaComponent))] + public class BuildToolEngine : Engine { + public override void Update(double dt) { + + foreach (ref readonly var message in ReadMessages()) + { + var entity = CreateEntity(); + + AddComponent(entity, new AreaComponent { squares = new[] {message.Start} }); + AddComponent(entity, new SelectedComponent { selected = false}); + } + } + } +} + diff --git a/isometric-park-fna/Engines/GameBridgeEngine.cs b/isometric-park-fna/Engines/GameBridgeEngine.cs --- a/isometric-park-fna/Engines/GameBridgeEngine.cs +++ b/isometric-park-fna/Engines/GameBridgeEngine.cs @@ -91,19 +91,23 @@ foreach (ref readonly var entity in ReadEntities()) { var areaComponent = GetComponent(entity); - var contractStatusComponent = GetComponent(entity); - foreach (var square in areaComponent.squares) - { - if (game.mouseGrid == square) - { - game.in_zone = true; - if ((contractStatusComponent.status == ContractStatus.Active) - || (contractStatusComponent.status == ContractStatus.Accepted)) - { - game.in_active_zone = true; - } - } - } + + if (HasComponent(entity)) + { + var contractStatusComponent = GetComponent(entity); + foreach (var square in areaComponent.squares) + { + if (game.mouseGrid == square) + { + game.in_zone = true; + if ((contractStatusComponent.status == ContractStatus.Active) + || (contractStatusComponent.status == ContractStatus.Accepted)) + { + game.in_active_zone = true; + } + } + } + } } foreach (ref readonly var message in ReadMessages()) diff --git a/isometric-park-fna/Engines/InputEngine.cs b/isometric-park-fna/Engines/InputEngine.cs --- a/isometric-park-fna/Engines/InputEngine.cs +++ b/isometric-park-fna/Engines/InputEngine.cs @@ -20,7 +20,9 @@ typeof(TogglePauseMessage), typeof(GameRateMessage), typeof(GameStateMessage), - typeof(QuitGameMessage))] + typeof(QuitGameMessage), + typeof(SpawnSelection), + typeof(AdjustSelection))] [Reads(typeof(WindowTypeComponent), typeof(GameStateComponent), typeof(VisibilityComponent))] @@ -52,7 +54,6 @@ var mouseCur = Mouse.GetState(); var original_point = Vector2.Transform(new Vector2(mouseCur.X, mouseCur.Y), Matrix.Invert(this.camera.get_transformation(this.graphicsDevice))); - bool isPlaying = false; var viewWidth = gdm.PreferredBackBufferWidth; @@ -238,6 +239,13 @@ SendMessage(new JumpCameraMessage {Movement = original_point}); } + if (mouseCur.LeftButton == ButtonState.Pressed && mousePrev.LeftButton == ButtonState.Released + && keyboardCur.IsKeyDown(Keys.LeftShift) + ) + { + SendMessage(new SpawnSelection {Start = CellMap.calculateMousegrid(original_point)}); + } + #endregion this.keyboardPrev = keyboardCur; this.mousePrev = mouseCur; diff --git a/isometric-park-fna/FNAGame.cs b/isometric-park-fna/FNAGame.cs --- a/isometric-park-fna/FNAGame.cs +++ b/isometric-park-fna/FNAGame.cs @@ -288,7 +288,7 @@ WorldBuilder.AddEngine(new PolicyEngine()); WorldBuilder.AddEngine(new TraceryBridgeEngine(this.grammar)); WorldBuilder.AddEngine(new SimulationGameRateBridgeEngine(this.simulation)); - + WorldBuilder.AddEngine(new BuildToolEngine()); WorldBuilder.AddGeneralRenderer(new AreaRenderer(this.tileBatch, this.monoFont), 1); WorldBuilder.AddGeneralRenderer(new ImGuiWindowRenderer(this, this.simulation, this.imGuiWindowBridgeEngine, this.gdm), 2); diff --git a/isometric-park-fna/Messages/AdjustSelection.cs b/isometric-park-fna/Messages/AdjustSelection.cs new file mode 100644 --- /dev/null +++ b/isometric-park-fna/Messages/AdjustSelection.cs @@ -0,0 +1,9 @@ +using Microsoft.Xna.Framework; + +using Encompass; + +namespace isometricparkfna.Messages { + public struct AdjustSelection : IMessage { + public Vector2 End; + } +} diff --git a/isometric-park-fna/Messages/SpawnSelection.cs b/isometric-park-fna/Messages/SpawnSelection.cs new file mode 100644 --- /dev/null +++ b/isometric-park-fna/Messages/SpawnSelection.cs @@ -0,0 +1,9 @@ +using Microsoft.Xna.Framework; + +using Encompass; + +namespace isometricparkfna.Messages { + public struct SpawnSelection : IMessage { + public Vector2 Start; + } +} diff --git a/isometric-park-fna/Renderers/AreaRenderer.cs b/isometric-park-fna/Renderers/AreaRenderer.cs --- a/isometric-park-fna/Renderers/AreaRenderer.cs +++ b/isometric-park-fna/Renderers/AreaRenderer.cs @@ -32,6 +32,7 @@ { var areaComponent = GetComponent(entity); // var SelectedComponent = GetComponent(entity); + var selected = GetComponent(entity).selected; if (!HasComponent(entity) || GetComponent(entity).status == ContractStatus.Accepted @@ -41,9 +42,7 @@ Quad.FillSquares(batch, areaComponent.squares, Color.Teal, 0.5f, 0.79f); } - var selected = GetComponent(entity).selected; - - if (HasComponent(entity) + else if (HasComponent(entity) && selected // && GetComponent(entity).selected ) @@ -51,6 +50,11 @@ Tile.DrawOutlinedSquares(batch, areaComponent.squares, Color.Teal); Quad.FillSquares(batch, areaComponent.squares, Color.Gray, 0.5f, 0.80f); } + else if (!HasComponent(entity) ) + { + Quad.FillSquares(batch, areaComponent.squares, Color.Red, 0.5f, 0.80f); + Tile.DrawOutlinedSquares(batch, areaComponent.squares, Color.Red); + } } } }