1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using Microsoft.Xna.Framework; |
---|
4 | using Microsoft.Xna.Framework.Graphics; |
---|
5 | using Microsoft.Xna.Framework.Content; |
---|
6 | using Microsoft.Xna.Framework.Input; |
---|
7 | using Microsoft.Xna.Framework.Input.Touch; |
---|
8 | |
---|
9 | namespace Fera_Proelia |
---|
10 | { |
---|
11 | public class GameplayScreen : GameScreen |
---|
12 | { |
---|
13 | ContentManager content; |
---|
14 | |
---|
15 | Tile[,] mapTiles; |
---|
16 | const int TILE_SIZE= 48; |
---|
17 | |
---|
18 | List<GameObject> gameObjects; |
---|
19 | |
---|
20 | Rectangle mapArea, guiArea; |
---|
21 | |
---|
22 | public GameplayScreen() |
---|
23 | { |
---|
24 | EnabledGestures = GestureType.Tap; |
---|
25 | } |
---|
26 | |
---|
27 | public override void LoadContent() |
---|
28 | { |
---|
29 | if (content == null) |
---|
30 | content = new ContentManager(ScreenManager.Game.Services, "Content"); |
---|
31 | |
---|
32 | mapArea = new Rectangle(0, 0, ScreenManager.GraphicsDevice.Viewport.Width, 500); |
---|
33 | guiArea = new Rectangle(0, 520, ScreenManager.GraphicsDevice.Viewport.Width, ScreenManager.GraphicsDevice.Viewport.Height - 520); |
---|
34 | |
---|
35 | int mapWidth = mapArea.Width / TILE_SIZE; |
---|
36 | int mapHeight = mapArea.Height / TILE_SIZE; |
---|
37 | mapTiles = new Tile[mapWidth, mapHeight]; |
---|
38 | |
---|
39 | for (int x = 0; x < mapWidth; x++) |
---|
40 | { |
---|
41 | for (int y = 0; y < mapHeight; y++) |
---|
42 | { |
---|
43 | Tile tile = new Tile(new Vector2(x * TILE_SIZE, y * TILE_SIZE), TILE_SIZE, TILE_SIZE); |
---|
44 | tile.TileType = TileType.Ground; |
---|
45 | tile.Selected += TileSelectedEvent; |
---|
46 | mapTiles[x, y] = tile; |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | Tile.Textures[0] = content.Load<Texture2D>("groundTexture"); |
---|
51 | Texture2D testTex = content.Load<Texture2D>("debugImage"); |
---|
52 | |
---|
53 | gameObjects = new List<GameObject>(); |
---|
54 | |
---|
55 | for (int i = 0; i < 10; i++) |
---|
56 | { |
---|
57 | for (int j = 0; j < 10; j++) |
---|
58 | { |
---|
59 | Beast test = new Beast(new Vector2(i * 50, j * 50), 50, 50); |
---|
60 | test.Texture = testTex; |
---|
61 | gameObjects.Add(test); |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | ScreenManager.Game.ResetElapsedTime(); |
---|
67 | } |
---|
68 | |
---|
69 | void TileSelectedEvent(object sender, SelectedEventArgs<Tile> e) |
---|
70 | { |
---|
71 | e.Selection.IsSelected = true; |
---|
72 | } |
---|
73 | |
---|
74 | public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) |
---|
75 | { |
---|
76 | base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen); |
---|
77 | |
---|
78 | if (IsActive) |
---|
79 | { |
---|
80 | foreach (GameObject o in gameObjects) |
---|
81 | { |
---|
82 | o.Update(gameTime); |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | public override void HandleInput(InputState input) |
---|
88 | { |
---|
89 | foreach (GestureSample gesture in input.Gestures) |
---|
90 | { |
---|
91 | if (gesture.GestureType == GestureType.Tap) |
---|
92 | { |
---|
93 | Point tapLocation = new Point((int)gesture.Position.X, (int)gesture.Position.Y); |
---|
94 | for (int x = 0; x < mapTiles.GetLength(0); x++) |
---|
95 | { |
---|
96 | for (int y = 0; y < mapTiles.GetLength(1); y++) |
---|
97 | { |
---|
98 | Tile tile = mapTiles[x, y]; |
---|
99 | if (tile.HitBounds.Contains(tapLocation)) |
---|
100 | { |
---|
101 | OnSelectedTile(tile); |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | protected virtual void OnSelectedTile(Tile tile) |
---|
110 | { |
---|
111 | for (int x = 0; x < mapTiles.GetLength(0); x++) |
---|
112 | for (int y = 0; y < mapTiles.GetLength(1); y++) |
---|
113 | mapTiles[x, y].IsSelected = false; |
---|
114 | tile.OnSelectTile(tile); |
---|
115 | } |
---|
116 | |
---|
117 | public override void Draw(GameTime gameTime) |
---|
118 | { |
---|
119 | ScreenManager.GraphicsDevice.Clear(ClearOptions.Target, Color.Violet, 0, 0); |
---|
120 | SpriteBatch sb = ScreenManager.SpriteBatch; |
---|
121 | |
---|
122 | sb.Begin(); |
---|
123 | |
---|
124 | for (int x = 0; x < mapTiles.GetLength(0); x++) |
---|
125 | { |
---|
126 | for (int y = 0; y < mapTiles.GetLength(1); y++) |
---|
127 | { |
---|
128 | Color drawColor = Color.White; |
---|
129 | if (mapTiles[x, y].IsSelected) |
---|
130 | drawColor = Color.Red; |
---|
131 | sb.Draw(Tile.Textures[(int)(mapTiles[x, y].TileType)], mapTiles[x, y].Position, drawColor); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | foreach (GameObject o in gameObjects) |
---|
136 | { |
---|
137 | sb.Draw(o.Texture, o.Position, Color.White); |
---|
138 | } |
---|
139 | sb.End(); |
---|
140 | base.Draw(gameTime); |
---|
141 | } |
---|
142 | } |
---|
143 | } |
---|