Description:
Move cursor rendering out of FNAGame to separate Renderer.
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
@@ -0,0 +1,36 | |||
|
1 | using Microsoft.Xna.Framework; | |
|
2 | using Microsoft.Xna.Framework.Graphics; | |
|
3 | ||
|
4 | using isometricparkfna.UI; | |
|
5 | using isometricparkfna.Components; | |
|
6 | ||
|
7 | using Encompass; | |
|
8 | using SpriteFontPlus; | |
|
9 | ||
|
10 | namespace isometricparkfna.Renderers | |
|
11 | { | |
|
12 | public class CursorRenderer : GeneralRenderer | |
|
13 | { | |
|
14 | private SpriteBatch batch; | |
|
15 | private SpriteFont font; | |
|
16 | ||
|
17 | public CursorRenderer(SpriteBatch batch, SpriteFont font) | |
|
18 | { | |
|
19 | this.batch = batch; | |
|
20 | this.font = font; | |
|
21 | } | |
|
22 | ||
|
23 | public override void Render() | |
|
24 | { | |
|
25 | ||
|
26 | foreach (ref readonly var entity in ReadEntities<CursorComponent>()) | |
|
27 | { | |
|
28 | ||
|
29 | var cursorComponent = GetComponent<CursorComponent>(entity); | |
|
30 | Tile.OutlineSquare(batch, cursorComponent.position.X, cursorComponent.position.Y, Color.Yellow); | |
|
31 | } | |
|
32 | } | |
|
33 | ||
|
34 | ||
|
35 | } | |
|
36 | } |
@@ -24,10 +24,13 | |||
|
24 | 24 | typeof(GameStateMessage), |
|
25 | 25 | typeof(QuitGameMessage), |
|
26 | 26 | typeof(SpawnSelection), |
|
27 |
typeof(AdjustSelection) |
|
|
27 | typeof(AdjustSelection) | |
|
28 | )] | |
|
28 | 29 | [Reads(typeof(WindowTypeComponent), |
|
29 | 30 | typeof(GameStateComponent), |
|
30 |
typeof(VisibilityComponent) |
|
|
31 | typeof(VisibilityComponent), | |
|
32 | typeof(CursorComponent))] | |
|
33 | [Writes(typeof(CursorComponent))] | |
|
31 | 34 | public class InputEngine : Engine |
|
32 | 35 | { |
|
33 | 36 | private KeyboardState keyboardPrev; |
@@ -39,15 +42,31 | |||
|
39 | 42 | |
|
40 | 43 | //Area to ignore: |
|
41 | 44 | private int menuBarHeight; |
|
45 | private int height; | |
|
46 | private int width; | |
|
42 | 47 | |
|
43 | 48 | public InputEngine(int menuBarHeight, Camera camera, |
|
44 | GraphicsDeviceManager gdm) { | |
|
49 | GraphicsDeviceManager gdm, int height, int width) { | |
|
45 | 50 | //initialize to blank for now |
|
46 | 51 | this.keyboardPrev = new KeyboardState(); |
|
47 | 52 | this.menuBarHeight = menuBarHeight; |
|
48 | 53 | this.camera = camera; |
|
49 | 54 | this.gdm = gdm; |
|
50 | 55 | this.graphicsDevice = gdm.GraphicsDevice; |
|
56 | this.height = height; | |
|
57 | this.width = width; | |
|
58 | } | |
|
59 | ||
|
60 | ||
|
61 | Vector2 calculateMousegrid(Vector2 normalizedMousePos) | |
|
62 | { | |
|
63 | Vector2 adjust = new Vector2(Tile.TileSpriteWidth / 2, Tile.TileSpriteHeight); | |
|
64 | Vector2 adjustedMousePos = normalizedMousePos - adjust; | |
|
65 | ||
|
66 | float boardx = ((adjustedMousePos.X / Tile.TileSpriteWidth) + (adjustedMousePos.Y / Tile.TileSpriteHeight)); | |
|
67 | float boardy = ((adjustedMousePos.Y / Tile.TileSpriteHeight) - (adjustedMousePos.X / Tile.TileSpriteWidth)); | |
|
68 | ||
|
69 | return new Vector2((int)boardx, (int)boardy); | |
|
51 | 70 | } |
|
52 | 71 | |
|
53 | 72 | public override void Update(double dt) { |
@@ -268,6 +287,23 | |||
|
268 | 287 | { |
|
269 | 288 | SendMessage(new AdjustSelection {End = CellMap.calculateMousegrid(original_point)}); |
|
270 | 289 | } |
|
290 | ||
|
291 | var transformedPosition = Vector2.Transform(new Vector2(mouseCur.X, mouseCur.Y), Matrix.Invert(camera.get_transformation(this.graphicsDevice))); | |
|
292 | var gridPosition = calculateMousegrid(transformedPosition); | |
|
293 | if (MathUtils.Between(gridPosition.X, 0, this.width) | |
|
294 | && MathUtils.Between(gridPosition.Y, 0, this.height)) | |
|
295 | { | |
|
296 | foreach (ref readonly var entity in ReadEntities<CursorComponent>()) { | |
|
297 | ||
|
298 | ||
|
299 | var cursorComponent = GetComponent<CursorComponent>(entity); | |
|
300 | ||
|
301 | SetComponent(entity, new CursorComponent { position = gridPosition, | |
|
302 | size = 1 }); | |
|
303 | ||
|
304 | } | |
|
305 | } | |
|
306 | ||
|
271 | 307 | } |
|
272 | 308 | |
|
273 | 309 | #endregion |
@@ -137,6 +137,15 | |||
|
137 | 137 | _ => 1000M |
|
138 | 138 | }; |
|
139 | 139 | |
|
140 | #region | |
|
141 | ||
|
142 | var cursorEntity = CreateEntity(); | |
|
143 | AddComponent(cursorEntity, | |
|
144 | new CursorComponent { position = new Vector2(20, 20), | |
|
145 | size = 1 }); | |
|
146 | ||
|
147 | #endregion | |
|
148 | ||
|
140 | 149 | Logging.Success("Spawned new game."); |
|
141 | 150 | } |
|
142 | 151 | } |
@@ -265,7 +265,8 | |||
|
265 | 265 | |
|
266 | 266 | Logging.Debug(this.Story.ContinueMaximally()); |
|
267 | 267 | |
|
268 |
WorldBuilder.AddEngine(new InputEngine(Menu.MENU_BAR_HEIGHT, this.camera, gdm |
|
|
268 | WorldBuilder.AddEngine(new InputEngine(Menu.MENU_BAR_HEIGHT, this.camera, gdm, | |
|
269 | this.simulation.map.MapHeight, this.simulation.map.MapWidth)); | |
|
269 | 270 | WorldBuilder.AddEngine(new UIEngine(this.Story)); |
|
270 | 271 | WorldBuilder.AddEngine(new DialogEngine(this.Story, this.grammar)); |
|
271 | 272 | |
@@ -292,6 +293,7 | |||
|
292 | 293 | |
|
293 | 294 | WorldBuilder.AddGeneralRenderer(new AreaRenderer(this.tileBatch, this.monoFont), 1); |
|
294 | 295 | WorldBuilder.AddGeneralRenderer(new ImGuiWindowRenderer(this, this.simulation, this.imGuiWindowBridgeEngine, this.gdm), 2); |
|
296 | WorldBuilder.AddGeneralRenderer(new CursorRenderer(this.tileBatch, this.monoFont), 3); | |
|
295 | 297 | var contractWindow = WorldBuilder.CreateEntity(); |
|
296 | 298 | WorldBuilder.SetComponent(contractWindow, new VisibilityComponent { visible = false }); |
|
297 | 299 | WorldBuilder.SetComponent(contractWindow, new WindowTypeComponent { type = isometricparkfna.Messages.Window.Contracts }); |
@@ -733,13 +735,6 | |||
|
733 | 735 | drawTileAt(12, 25, 300, 2); |
|
734 | 736 | #endif |
|
735 | 737 | |
|
736 | #region draw_cursor | |
|
737 | ||
|
738 | if (MathUtils.Between(this.mouseGrid.X, 0, this.simulation.map.MapWidth) | |
|
739 | && MathUtils.Between(this.mouseGrid.Y, 0, this.simulation.map.MapHeight)) | |
|
740 | { | |
|
741 | Tile.OutlineSquare(tileBatch, this.mouseGrid.X, this.mouseGrid.Y, Color.Yellow, 1); | |
|
742 | } | |
|
743 | 738 | |
|
744 | 739 | #if DEBUG |
|
745 | 740 | Tile.OutlineSquare(tileBatch, 1, 1, Color.Red, 2); |
@@ -763,8 +758,6 | |||
|
763 | 758 | Quad.FillSquare2(tileBatch, 8, 7, Color.Teal, .125f, 0.79f); |
|
764 | 759 | #endif |
|
765 | 760 | |
|
766 | #endregion draw_cursor | |
|
767 | ||
|
768 | 761 | stopWatch2 = new Stopwatch(); |
|
769 | 762 | stopWatch2.Start(); |
|
770 | 763 | #region draw_trees |
@@ -1,4 +1,3 | |||
|
1 | ||
|
2 | 1 |
|
|
3 | 2 | using Microsoft.Xna.Framework.Graphics; |
|
4 | 3 | |
@@ -15,7 +14,6 | |||
|
15 | 14 | private SpriteBatch batch; |
|
16 | 15 | private SpriteFont font; |
|
17 | 16 | |
|
18 | ||
|
19 | 17 | public AreaRenderer(SpriteBatch batch, SpriteFont font) |
|
20 | 18 | { |
|
21 | 19 | this.batch = batch; |
@@ -46,7 +46,7 | |||
|
46 | 46 | |
|
47 | 47 | static public Texture2D TileSetTexture; |
|
48 | 48 | |
|
49 | ||
|
49 | public const float DEPTH = 0.79f; | |
|
50 | 50 | |
|
51 | 51 | |
|
52 | 52 | public Tile() |
@@ -55,7 +55,6 | |||
|
55 | 55 | } |
|
56 | 56 | |
|
57 | 57 | |
|
58 | ||
|
59 | 58 | static public Rectangle GetSourceRectangle(int tileIndex) |
|
60 | 59 | { |
|
61 | 60 | int tileY = tileIndex / (TileSetTexture.Width / TileWidth); |
@@ -128,26 +127,26 | |||
|
128 | 127 | new Vector2(((x - y) * Tile.TileSpriteWidth / 2), (x + y) * Tile.TileSpriteHeight / 2) + adjust2, |
|
129 | 128 | //new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight), |
|
130 | 129 | new Vector2(((x - y + size_less_one) * Tile.TileSpriteWidth / 2), (x + y + size_less_one) * Tile.TileSpriteHeight / 2) + adjust2, |
|
131 |
color, |
|
|
130 | color, Tile.DEPTH); | |
|
132 | 131 | |
|
133 | 132 | //Bottom right |
|
134 | 133 | Line.drawLine(batch, |
|
135 | 134 | new Vector2(((x + size - y) * Tile.TileSpriteWidth / 2), (x + size_less_one + y) * Tile.TileSpriteHeight / 2) + adjust2, |
|
136 | 135 | //new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight), |
|
137 | 136 | new Vector2(((x + size - (y + size)) * Tile.TileSpriteWidth / 2), (x + size_less_one + (y + size_less_one)) * Tile.TileSpriteHeight / 2) + adjust2, |
|
138 |
color, |
|
|
137 | color, Tile.DEPTH); | |
|
139 | 138 | //Bottom left |
|
140 | 139 | Line.drawLine(batch, |
|
141 | 140 | new Vector2(((x - (y + size)) * Tile.TileSpriteWidth / 2), (x + y + size) * Tile.TileSpriteHeight / 2) + adjust2, |
|
142 | 141 | //new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight), |
|
143 | 142 | new Vector2(((x + size - (y + size)) * Tile.TileSpriteWidth / 2), (x + size + (y + size)) * Tile.TileSpriteHeight / 2) + adjust2, |
|
144 |
color, |
|
|
143 | color, Tile.DEPTH); | |
|
145 | 144 | //Upper left |
|
146 | 145 | Line.drawLine(batch, |
|
147 | 146 | new Vector2(((x - y) * Tile.TileSpriteWidth / 2), (x + y) * Tile.TileSpriteHeight / 2) + adjust2, |
|
148 | 147 | //new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight), |
|
149 | 148 | new Vector2(((x - (y + size)) * Tile.TileSpriteWidth / 2), (x + (y + size)) * Tile.TileSpriteHeight / 2) + adjust2, |
|
150 |
color, |
|
|
149 | color, Tile.DEPTH); | |
|
151 | 150 | } |
|
152 | 151 | |
|
153 | 152 | public static void drawOutline(SpriteBatch batch, Color color) { |
You need to be logged in to leave comments.
Login now