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 @@ -24,10 +24,13 @@ typeof(GameStateMessage), typeof(QuitGameMessage), typeof(SpawnSelection), - typeof(AdjustSelection))] + typeof(AdjustSelection) + )] [Reads(typeof(WindowTypeComponent), typeof(GameStateComponent), - typeof(VisibilityComponent))] + typeof(VisibilityComponent), + typeof(CursorComponent))] + [Writes(typeof(CursorComponent))] public class InputEngine : Engine { private KeyboardState keyboardPrev; @@ -39,15 +42,31 @@ //Area to ignore: private int menuBarHeight; + private int height; + private int width; public InputEngine(int menuBarHeight, Camera camera, - GraphicsDeviceManager gdm) { + GraphicsDeviceManager gdm, int height, int width) { //initialize to blank for now this.keyboardPrev = new KeyboardState(); this.menuBarHeight = menuBarHeight; this.camera = camera; this.gdm = gdm; this.graphicsDevice = gdm.GraphicsDevice; + this.height = height; + this.width = width; + } + + + 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 override void Update(double dt) { @@ -268,6 +287,23 @@ { SendMessage(new AdjustSelection {End = CellMap.calculateMousegrid(original_point)}); } + + var transformedPosition = Vector2.Transform(new Vector2(mouseCur.X, mouseCur.Y), Matrix.Invert(camera.get_transformation(this.graphicsDevice))); + var gridPosition = calculateMousegrid(transformedPosition); + if (MathUtils.Between(gridPosition.X, 0, this.width) + && MathUtils.Between(gridPosition.Y, 0, this.height)) + { + foreach (ref readonly var entity in ReadEntities()) { + + + var cursorComponent = GetComponent(entity); + + SetComponent(entity, new CursorComponent { position = gridPosition, + size = 1 }); + + } + } + } #endregion diff --git a/isometric-park-fna/Engines/Spawners/GameSpawner.cs b/isometric-park-fna/Engines/Spawners/GameSpawner.cs --- a/isometric-park-fna/Engines/Spawners/GameSpawner.cs +++ b/isometric-park-fna/Engines/Spawners/GameSpawner.cs @@ -137,6 +137,15 @@ _ => 1000M }; +#region + + var cursorEntity = CreateEntity(); + AddComponent(cursorEntity, + new CursorComponent { position = new Vector2(20, 20), + size = 1 }); + +#endregion + Logging.Success("Spawned new game."); } } 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 @@ -265,7 +265,8 @@ Logging.Debug(this.Story.ContinueMaximally()); - WorldBuilder.AddEngine(new InputEngine(Menu.MENU_BAR_HEIGHT, this.camera, gdm)); + WorldBuilder.AddEngine(new InputEngine(Menu.MENU_BAR_HEIGHT, this.camera, gdm, + this.simulation.map.MapHeight, this.simulation.map.MapWidth)); WorldBuilder.AddEngine(new UIEngine(this.Story)); WorldBuilder.AddEngine(new DialogEngine(this.Story, this.grammar)); @@ -292,6 +293,7 @@ WorldBuilder.AddGeneralRenderer(new AreaRenderer(this.tileBatch, this.monoFont), 1); WorldBuilder.AddGeneralRenderer(new ImGuiWindowRenderer(this, this.simulation, this.imGuiWindowBridgeEngine, this.gdm), 2); + WorldBuilder.AddGeneralRenderer(new CursorRenderer(this.tileBatch, this.monoFont), 3); var contractWindow = WorldBuilder.CreateEntity(); WorldBuilder.SetComponent(contractWindow, new VisibilityComponent { visible = false }); WorldBuilder.SetComponent(contractWindow, new WindowTypeComponent { type = isometricparkfna.Messages.Window.Contracts }); @@ -733,13 +735,6 @@ drawTileAt(12, 25, 300, 2); #endif - #region draw_cursor - - if (MathUtils.Between(this.mouseGrid.X, 0, this.simulation.map.MapWidth) - && MathUtils.Between(this.mouseGrid.Y, 0, this.simulation.map.MapHeight)) - { - Tile.OutlineSquare(tileBatch, this.mouseGrid.X, this.mouseGrid.Y, Color.Yellow, 1); - } #if DEBUG Tile.OutlineSquare(tileBatch, 1, 1, Color.Red, 2); @@ -763,8 +758,6 @@ Quad.FillSquare2(tileBatch, 8, 7, Color.Teal, .125f, 0.79f); #endif - #endregion draw_cursor - stopWatch2 = new Stopwatch(); stopWatch2.Start(); #region draw_trees 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 @@ -1,4 +1,3 @@ - using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; @@ -15,7 +14,6 @@ private SpriteBatch batch; private SpriteFont font; - public AreaRenderer(SpriteBatch batch, SpriteFont font) { this.batch = batch; diff --git a/isometric-park-fna/Renderers/CursorRenderer.cs b/isometric-park-fna/Renderers/CursorRenderer.cs new file mode 100644 --- /dev/null +++ b/isometric-park-fna/Renderers/CursorRenderer.cs @@ -0,0 +1,36 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +using isometricparkfna.UI; +using isometricparkfna.Components; + +using Encompass; +using SpriteFontPlus; + +namespace isometricparkfna.Renderers +{ + public class CursorRenderer : GeneralRenderer + { + private SpriteBatch batch; + private SpriteFont font; + + public CursorRenderer(SpriteBatch batch, SpriteFont font) + { + this.batch = batch; + this.font = font; + } + + public override void Render() + { + + foreach (ref readonly var entity in ReadEntities()) + { + + var cursorComponent = GetComponent(entity); + Tile.OutlineSquare(batch, cursorComponent.position.X, cursorComponent.position.Y, Color.Yellow); + } + } + + + } +} diff --git a/isometric-park-fna/Tile.cs b/isometric-park-fna/Tile.cs --- a/isometric-park-fna/Tile.cs +++ b/isometric-park-fna/Tile.cs @@ -46,7 +46,7 @@ static public Texture2D TileSetTexture; - + public const float DEPTH = 0.79f; public Tile() @@ -55,7 +55,6 @@ } - static public Rectangle GetSourceRectangle(int tileIndex) { int tileY = tileIndex / (TileSetTexture.Width / TileWidth); @@ -128,26 +127,26 @@ new Vector2(((x - y) * Tile.TileSpriteWidth / 2), (x + y) * Tile.TileSpriteHeight / 2) + adjust2, //new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight), new Vector2(((x - y + size_less_one) * Tile.TileSpriteWidth / 2), (x + y + size_less_one) * Tile.TileSpriteHeight / 2) + adjust2, - color, 0.79f); + color, Tile.DEPTH); //Bottom right Line.drawLine(batch, new Vector2(((x + size - y) * Tile.TileSpriteWidth / 2), (x + size_less_one + y) * Tile.TileSpriteHeight / 2) + adjust2, //new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight), new Vector2(((x + size - (y + size)) * Tile.TileSpriteWidth / 2), (x + size_less_one + (y + size_less_one)) * Tile.TileSpriteHeight / 2) + adjust2, - color, 0.79f); + color, Tile.DEPTH); //Bottom left Line.drawLine(batch, new Vector2(((x - (y + size)) * Tile.TileSpriteWidth / 2), (x + y + size) * Tile.TileSpriteHeight / 2) + adjust2, //new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight), new Vector2(((x + size - (y + size)) * Tile.TileSpriteWidth / 2), (x + size + (y + size)) * Tile.TileSpriteHeight / 2) + adjust2, - color, 0.79f); + color, Tile.DEPTH); //Upper left Line.drawLine(batch, new Vector2(((x - y) * Tile.TileSpriteWidth / 2), (x + y) * Tile.TileSpriteHeight / 2) + adjust2, //new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight), new Vector2(((x - (y + size)) * Tile.TileSpriteWidth / 2), (x + (y + size)) * Tile.TileSpriteHeight / 2) + adjust2, - color, 0.79f); + color, Tile.DEPTH); } public static void drawOutline(SpriteBatch batch, Color color) {