1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.IO; |
---|
5 | using System.Text; |
---|
6 | using System.Xml; |
---|
7 | using Microsoft.Xna.Framework; |
---|
8 | using Microsoft.Xna.Framework.Audio; |
---|
9 | using Microsoft.Xna.Framework.Content; |
---|
10 | using Microsoft.Xna.Framework.GamerServices; |
---|
11 | using Microsoft.Xna.Framework.Graphics; |
---|
12 | using Microsoft.Xna.Framework.Input; |
---|
13 | using Microsoft.Xna.Framework.Media; |
---|
14 | |
---|
15 | namespace AdventureGame |
---|
16 | { |
---|
17 | /// <summary> |
---|
18 | /// This is the main type for your game |
---|
19 | /// </summary> |
---|
20 | public class AdventureGame : Microsoft.Xna.Framework.Game |
---|
21 | { |
---|
22 | GraphicsDeviceManager graphics; |
---|
23 | |
---|
24 | SpriteBatch spriteBatch; |
---|
25 | |
---|
26 | static public int TILE_SIZE = 32; |
---|
27 | static public Texture2D selectionBox; |
---|
28 | static public SpriteFont basicFont; |
---|
29 | Texture2D tileImage; |
---|
30 | Texture2D miniMapBorders; |
---|
31 | MouseState mouseState, previousMouseState; |
---|
32 | KeyboardState keyState, previousKeyState; |
---|
33 | |
---|
34 | Map gameMap; |
---|
35 | Camera gameCamera; |
---|
36 | HUD gameHUD; |
---|
37 | |
---|
38 | Viewport mapView, hudView, miniMapView; |
---|
39 | |
---|
40 | Button saveButton; |
---|
41 | |
---|
42 | public AdventureGame() |
---|
43 | { |
---|
44 | graphics = new GraphicsDeviceManager(this); |
---|
45 | Content.RootDirectory = "Content"; |
---|
46 | } |
---|
47 | |
---|
48 | /// <summary> |
---|
49 | /// Allows the game to perform any initialization it needs to before starting to run. |
---|
50 | /// This is where it can query for any required services and load any non-graphic |
---|
51 | /// related content. Calling base.Initialize will enumerate through any components |
---|
52 | /// and initialize them as well. |
---|
53 | /// </summary> |
---|
54 | protected override void Initialize() |
---|
55 | { |
---|
56 | |
---|
57 | |
---|
58 | //graphics.IsFullScreen = true; |
---|
59 | //graphics.ApplyChanges(); |
---|
60 | IsMouseVisible = true; |
---|
61 | base.Initialize(); |
---|
62 | } |
---|
63 | |
---|
64 | /// <summary> |
---|
65 | /// LoadContent will be called once per game and is the place to load |
---|
66 | /// all of your content. |
---|
67 | /// </summary> |
---|
68 | protected override void LoadContent() |
---|
69 | { |
---|
70 | // Create a new SpriteBatch, which can be used to draw textures. |
---|
71 | spriteBatch = new SpriteBatch(GraphicsDevice); |
---|
72 | basicFont = Content.Load<SpriteFont>("Basicfont"); |
---|
73 | selectionBox = Content.Load<Texture2D>("selectionbox"); |
---|
74 | tileImage = Content.Load<Texture2D>("tilet"); |
---|
75 | miniMapBorders = Content.Load<Texture2D>("miniMapBorders"); |
---|
76 | |
---|
77 | mapView = new Viewport(0, 0, |
---|
78 | graphics.PreferredBackBufferWidth - 200, |
---|
79 | graphics.PreferredBackBufferHeight); |
---|
80 | hudView = new Viewport(graphics.PreferredBackBufferWidth - 200, |
---|
81 | 0, 200, graphics.PreferredBackBufferHeight); |
---|
82 | miniMapView = new Viewport(graphics.PreferredBackBufferWidth - 200, |
---|
83 | graphics.PreferredBackBufferHeight - 200, 200, 200); |
---|
84 | |
---|
85 | gameHUD = new HUD(); |
---|
86 | gameHUD.ViewPort = hudView; |
---|
87 | gameHUD.AddButton(new Button(new Rectangle(10, 10, 150, 40), "Save", Color.YellowGreen, Color.Red, GraphicsDevice)); |
---|
88 | |
---|
89 | gameHUD.Buttons[0].Clicked += new ButtonClickedHandler(SaveMap); |
---|
90 | |
---|
91 | gameMap = new Map(this, tileImage, TILE_SIZE, 200, 200); |
---|
92 | gameHUD.AddMiniMap(new MiniMap(gameMap, miniMapView, miniMapBorders)); |
---|
93 | gameCamera = new Camera(mapView); |
---|
94 | } |
---|
95 | |
---|
96 | /// <summary> |
---|
97 | /// UnloadContent will be called once per game and is the place to unload |
---|
98 | /// all content. |
---|
99 | /// </summary> |
---|
100 | protected override void UnloadContent() |
---|
101 | { |
---|
102 | // TODO: Unload any non ContentManager content here |
---|
103 | } |
---|
104 | |
---|
105 | /// <summary> |
---|
106 | /// Allows the game to run logic such as updating the world, |
---|
107 | /// checking for collisions, gathering input, and playing audio. |
---|
108 | /// </summary> |
---|
109 | /// <param name="gameTime">Provides a snapshot of timing values.</param> |
---|
110 | protected override void Update(GameTime gameTime) |
---|
111 | { |
---|
112 | // Allows the game to exit |
---|
113 | if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) |
---|
114 | this.Exit(); |
---|
115 | |
---|
116 | mouseState = Mouse.GetState(); |
---|
117 | keyState = Keyboard.GetState(); |
---|
118 | |
---|
119 | if (previousMouseState.LeftButton == ButtonState.Released && mouseState.LeftButton == ButtonState.Pressed) |
---|
120 | { |
---|
121 | if (mouseState.X < gameHUD.ViewPort.X) |
---|
122 | { |
---|
123 | Vector2 mouseWorldPos = gameCamera.getMouseWorldPos(new Vector2(mouseState.X, mouseState.Y), mapView); |
---|
124 | Rectangle mousePos = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 1, 1); |
---|
125 | foreach (Tile tile in gameMap.Tiles) |
---|
126 | { |
---|
127 | if (mousePos.Intersects(tile.Boundaries)) |
---|
128 | { |
---|
129 | gameMap.UnSelectAll(); |
---|
130 | gameMap.SelectTile(tile); |
---|
131 | } |
---|
132 | } |
---|
133 | } |
---|
134 | else |
---|
135 | { |
---|
136 | Vector2 mouseWorldPos = gameCamera.getMouseWorldPos(new Vector2(mouseState.X - gameHUD.ViewPort.X, mouseState.Y), gameHUD.ViewPort); |
---|
137 | Rectangle mousePos = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 1, 1); |
---|
138 | foreach (Button button in gameHUD.Buttons) |
---|
139 | { |
---|
140 | if (mousePos.Intersects(button.Boundaries)) |
---|
141 | button.Pressed(); |
---|
142 | } |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | if (keyState.IsKeyDown(Keys.D1) && previousKeyState.IsKeyUp(Keys.D1)) |
---|
147 | gameMap.ChangeTileTo(TileType.Grass); |
---|
148 | if (keyState.IsKeyDown(Keys.D2) && previousKeyState.IsKeyUp(Keys.D2)) |
---|
149 | gameMap.ChangeTileTo(TileType.DarkGrass); |
---|
150 | if (keyState.IsKeyDown(Keys.D3) && previousKeyState.IsKeyUp(Keys.D3)) |
---|
151 | gameMap.ChangeTileTo(TileType.Sand); |
---|
152 | if (keyState.IsKeyDown(Keys.D4) && previousKeyState.IsKeyUp(Keys.D4)) |
---|
153 | gameMap.ChangeTileTo(TileType.Water); |
---|
154 | |
---|
155 | if (keyState.IsKeyDown(Keys.Left)) |
---|
156 | gameCamera.Move(new Vector2(-5, 0)); |
---|
157 | if (keyState.IsKeyDown(Keys.Right)) |
---|
158 | gameCamera.Move(new Vector2(5, 0)); |
---|
159 | if (keyState.IsKeyDown(Keys.Up)) |
---|
160 | gameCamera.Move(new Vector2(0, -5)); |
---|
161 | if (keyState.IsKeyDown(Keys.Down)) |
---|
162 | gameCamera.Move(new Vector2(0, 5)); |
---|
163 | previousMouseState = mouseState; |
---|
164 | previousKeyState = keyState; |
---|
165 | base.Update(gameTime); |
---|
166 | } |
---|
167 | |
---|
168 | /// <summary> |
---|
169 | /// This is called when the game should draw itself. |
---|
170 | /// </summary> |
---|
171 | /// <param name="gameTime">Provides a snapshot of timing values.</param> |
---|
172 | protected override void Draw(GameTime gameTime) |
---|
173 | { |
---|
174 | GraphicsDevice.Clear(Color.CornflowerBlue); |
---|
175 | GraphicsDevice.Viewport = mapView; |
---|
176 | spriteBatch.Begin(SpriteSortMode.Deferred, |
---|
177 | BlendState.AlphaBlend, |
---|
178 | null,null,null,null,gameCamera.getTransformation(mapView)); |
---|
179 | gameMap.Draw(spriteBatch, gameCamera); |
---|
180 | spriteBatch.End(); |
---|
181 | GraphicsDevice.Viewport = gameHUD.ViewPort; |
---|
182 | spriteBatch.Begin(); |
---|
183 | gameHUD.Draw(spriteBatch); |
---|
184 | spriteBatch.End(); |
---|
185 | GraphicsDevice.Viewport = miniMapView; |
---|
186 | spriteBatch.Begin(SpriteSortMode.Deferred, |
---|
187 | BlendState.AlphaBlend, |
---|
188 | null, null, null, null, gameCamera.getTransformation(miniMapView, true, gameMap.Size)); |
---|
189 | gameHUD.DrawMinimap(spriteBatch); |
---|
190 | spriteBatch.End(); |
---|
191 | spriteBatch.Begin(); |
---|
192 | spriteBatch.Draw(gameHUD.MiniMap.Borders, new Rectangle(0,0,200,200), Color.White); |
---|
193 | spriteBatch.End(); |
---|
194 | base.Draw(gameTime); |
---|
195 | } |
---|
196 | |
---|
197 | private void SaveMap(object sender, EventArgs e) |
---|
198 | { |
---|
199 | //FileStream mapFile = File.Create("savedMap,xml"); |
---|
200 | using (FileStream mapSave = new FileStream("savedMap", FileMode.Create, FileAccess.Write)) |
---|
201 | { |
---|
202 | using (BinaryWriter w = new BinaryWriter(mapSave)) |
---|
203 | { |
---|
204 | w.Write(gameMap.Tiles.Count); |
---|
205 | foreach (Tile tile in gameMap.Tiles) |
---|
206 | { |
---|
207 | w.Write((int)tile.TileType); |
---|
208 | w.Write((int)tile.Position.X); |
---|
209 | w.Write((int)tile.Position.Y); |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | using (FileStream mapLoad = new FileStream("savedMap", FileMode.Open, FileAccess.Read)) |
---|
215 | { |
---|
216 | using (BinaryReader r = new BinaryReader(mapLoad)) |
---|
217 | { |
---|
218 | using (StreamWriter random = new StreamWriter("clear.txt")) |
---|
219 | { |
---|
220 | int amount = r.ReadInt32(); |
---|
221 | for (int i = 0; i < amount; i++) |
---|
222 | { |
---|
223 | int tile = r.ReadInt32(); |
---|
224 | int x = r.ReadInt32(); |
---|
225 | int y = r.ReadInt32(); |
---|
226 | random.WriteLine(tile.ToString() + ' ' + x.ToString() + ' ' + y.ToString()); |
---|
227 | } |
---|
228 | } |
---|
229 | } |
---|
230 | } |
---|
231 | } |
---|
232 | } |
---|
233 | } |
---|