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