# HG changeset patch # User Alys Brooks # Date 2022-02-13 07:39:10 # Node ID 5c22e0ed170b844bad1098931e9a166176e68405 # Parent 8290bd03f9ac129da15b5733af743f7ce88b73ca Limit to boundaries. 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 @@ -188,16 +188,16 @@ return count; } - public static Vector2 calculateMousegrid(Vector2 normalizedMousePos) - { - Vector2 adjust = new Vector2(Tile.TileSpriteWidth / 2, Tile.TileSpriteHeight); - Vector2 adjustedMousePos = normalizedMousePos - adjust; + 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)); + 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); - } + return new Vector2((int)boardx, (int)boardy); + } public System.Collections.Generic.IEnumerable iterate_cells_with_neighbors(int neighbors) { diff --git a/isometric-park-fna/Engines/BuildToolEngine.cs b/isometric-park-fna/Engines/BuildToolEngine.cs --- a/isometric-park-fna/Engines/BuildToolEngine.cs +++ b/isometric-park-fna/Engines/BuildToolEngine.cs @@ -13,12 +13,18 @@ namespace isometricparkfna.Engines { [Receives(typeof(SpawnSelection), - typeof(AdjustSelection))] + typeof(AdjustSelection))] [Writes(typeof(AreaComponent))] [Reads(typeof(SelectedComponent), - typeof(AreaComponent))] + typeof(AreaComponent))] public class BuildToolEngine : Engine { + private CellMap Map; + + public BuildToolEngine(CellMap map) { + this.Map = map; + } + private System.Collections.Generic.IEnumerable step_until(float start, float stop) { if (stop >= start) { for(float i = start; i <= stop; i++) @@ -60,22 +66,11 @@ newSquares.Add(area.squares[0]); + 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); - // - // for (float i = area.squares[0].X; i >= message.End.X; i--) { - // for (float j = area.squares[0].Y; j >= message.End.Y; j--) { - // newSquares.Add(new Vector2(i, j)); - // } - // } - // - // for (float i = area.squares[0].X; i <= message.End.X; i++) { - // for (float j = area.squares[0].Y; j <= message.End.Y; j++) { - // newSquares.Add(new Vector2(i, j)); - // } - // } - - foreach (var i in step_until(area.squares[0].X, message.End.X)) { - foreach (var j in step_until(area.squares[0].Y, message.End.Y)) { + foreach (var i in step_until(area.squares[0].X, end_x)) { + foreach (var j in step_until(area.squares[0].Y, end_y)) { newSquares.Add(new Vector2(i, j)); } } @@ -83,12 +78,12 @@ newSquares.Add(message.End); SetComponent(entity, new AreaComponent { squares = newSquares.ToArray() /*( new[]{ area.squares[0], message.End}*/}); - } - } - } + } + } + } - } - } - } - } + } + } + } +} 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 @@ -92,22 +92,22 @@ { var areaComponent = GetComponent(entity); - 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; - } - } - } - } + 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 @@ -21,8 +21,8 @@ typeof(GameRateMessage), typeof(GameStateMessage), typeof(QuitGameMessage), - typeof(SpawnSelection), - typeof(AdjustSelection))] + typeof(SpawnSelection), + typeof(AdjustSelection))] [Reads(typeof(WindowTypeComponent), typeof(GameStateComponent), typeof(VisibilityComponent))] @@ -240,21 +240,21 @@ } if (mouseCur.LeftButton == ButtonState.Pressed && mousePrev.LeftButton == ButtonState.Released - && keyboardCur.IsKeyDown(Keys.LeftShift) - ) + && keyboardCur.IsKeyDown(Keys.LeftShift) + ) { SendMessage(new SpawnSelection {Start = CellMap.calculateMousegrid(original_point)}); } - if ( mouseCur.LeftButton == ButtonState.Released && mousePrev.LeftButton == ButtonState.Pressed ) - { + if ( mouseCur.LeftButton == ButtonState.Released && mousePrev.LeftButton == ButtonState.Pressed ) + { SendMessage(new AdjustSelection {Clear = true }); - } + } if (mouseCur.LeftButton == ButtonState.Pressed && mousePrev.LeftButton == ButtonState.Pressed) - { - SendMessage(new AdjustSelection { End = CellMap.calculateMousegrid(original_point)}); - } + { + SendMessage(new AdjustSelection { End = CellMap.calculateMousegrid(original_point)}); + } #endregion this.keyboardPrev = keyboardCur; diff --git a/isometric-park-fna/Engines/UIEngine.cs b/isometric-park-fna/Engines/UIEngine.cs --- a/isometric-park-fna/Engines/UIEngine.cs +++ b/isometric-park-fna/Engines/UIEngine.cs @@ -40,10 +40,10 @@ { foreach (var entity in ReadEntities()) { - var selected = GetComponent(entity); - if (selected.Type == SelectionType.Window) { - SetComponent(entity, new SelectedComponent { selected = false }); - } + var selected = GetComponent(entity); + if (selected.Type == SelectionType.Window) { + SetComponent(entity, new SelectedComponent { selected = false }); + } } foreach (ref readonly var windowMessage in ReadMessages()) 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.AddEngine(new BuildToolEngine(this.simulation.map)); 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/Renderers/AreaRenderer.cs b/isometric-park-fna/Renderers/AreaRenderer.cs --- a/isometric-park-fna/Renderers/AreaRenderer.cs +++ b/isometric-park-fna/Renderers/AreaRenderer.cs @@ -36,25 +36,25 @@ if ((!HasComponent(entity) || GetComponent(entity).status == ContractStatus.Accepted) - && !selected) + && !selected) { Tile.DrawOutlinedSquares(batch, areaComponent.squares, Color.Teal); Quad.FillSquares(batch, areaComponent.squares, Color.Teal, 0.5f, 0.78f); } - else if (HasComponent(entity) - && selected - // && GetComponent(entity).selected - ) + else if (HasComponent(entity) + && selected + // && GetComponent(entity).selected + ) { Tile.DrawOutlinedSquares(batch, areaComponent.squares, Color.Teal); Quad.FillSquares(batch, areaComponent.squares, Color.Gray, 0.5f, 0.78f); } - else if (!HasComponent(entity) ) - { + else if (!HasComponent(entity) ) + { Quad.FillSquares(batch, areaComponent.squares, Color.Red, 0.5f, 0.79f); Tile.DrawOutlinedSquares(batch, areaComponent.squares, Color.Red); - } + } } } } diff --git a/isometric-park-fna/Renderers/ImGuiWindowRenderer.cs b/isometric-park-fna/Renderers/ImGuiWindowRenderer.cs --- a/isometric-park-fna/Renderers/ImGuiWindowRenderer.cs +++ b/isometric-park-fna/Renderers/ImGuiWindowRenderer.cs @@ -116,9 +116,9 @@ foreach (var e in contracts) { - if(HasComponent(e)) { - contract_data.Add(getContractDetails(e)); - } + if(HasComponent(e)) { + contract_data.Add(getContractDetails(e)); + } } ContractsWindow.Render(this.BridgeEngine.font, this.BridgeEngine, contract_data); diff --git a/isometric-park-fna/Utils/MathUtils.cs b/isometric-park-fna/Utils/MathUtils.cs --- a/isometric-park-fna/Utils/MathUtils.cs +++ b/isometric-park-fna/Utils/MathUtils.cs @@ -68,6 +68,22 @@ } } + public static float Clamp(float val, float min, float max) + { + if (val > max) + { + return max; + } + else if (val < min) + { + return min; + } + else + { + return val; + } + } + protected float Decrement(float value, float delta) { float magnitude = Math.Abs(value);