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 | |
---|
25 | SpriteBatch spriteBatch; |
---|
26 | |
---|
27 | |
---|
28 | static public int TILE_SIZE = 32; |
---|
29 | static public Texture2D selectionBox; |
---|
30 | static public SpriteFont basicFont; |
---|
31 | Texture2D tileImage; |
---|
32 | MouseState mouseState, previousMouseState; |
---|
33 | KeyboardState keyState, previousKeyState; |
---|
34 | |
---|
35 | Map gameMap; |
---|
36 | |
---|
37 | Button saveButton; |
---|
38 | |
---|
39 | public AdventureGame() |
---|
40 | { |
---|
41 | graphics = new GraphicsDeviceManager(this); |
---|
42 | Content.RootDirectory = "Content"; |
---|
43 | } |
---|
44 | |
---|
45 | /// <summary> |
---|
46 | /// Allows the game to perform any initialization it needs to before starting to run. |
---|
47 | /// This is where it can query for any required services and load any non-graphic |
---|
48 | /// related content. Calling base.Initialize will enumerate through any components |
---|
49 | /// and initialize them as well. |
---|
50 | /// </summary> |
---|
51 | protected override void Initialize() |
---|
52 | { |
---|
53 | // TODO: Add your initialization logic here |
---|
54 | IsMouseVisible = true; |
---|
55 | base.Initialize(); |
---|
56 | } |
---|
57 | |
---|
58 | /// <summary> |
---|
59 | /// LoadContent will be called once per game and is the place to load |
---|
60 | /// all of your content. |
---|
61 | /// </summary> |
---|
62 | protected override void LoadContent() |
---|
63 | { |
---|
64 | // Create a new SpriteBatch, which can be used to draw textures. |
---|
65 | spriteBatch = new SpriteBatch(GraphicsDevice); |
---|
66 | basicFont = Content.Load<SpriteFont>("Basicfont"); |
---|
67 | selectionBox = Content.Load<Texture2D>("selectionbox"); |
---|
68 | tileImage = Content.Load<Texture2D>("tilet"); |
---|
69 | gameMap = new Map(this, tileImage, TILE_SIZE, 16, 8); |
---|
70 | |
---|
71 | saveButton = new Button(new Rectangle(400, 100, 150, 40), "Save", Color.YellowGreen, Color.Red, GraphicsDevice); |
---|
72 | } |
---|
73 | |
---|
74 | /// <summary> |
---|
75 | /// UnloadContent will be called once per game and is the place to unload |
---|
76 | /// all content. |
---|
77 | /// </summary> |
---|
78 | protected override void UnloadContent() |
---|
79 | { |
---|
80 | // TODO: Unload any non ContentManager content here |
---|
81 | } |
---|
82 | |
---|
83 | /// <summary> |
---|
84 | /// Allows the game to run logic such as updating the world, |
---|
85 | /// checking for collisions, gathering input, and playing audio. |
---|
86 | /// </summary> |
---|
87 | /// <param name="gameTime">Provides a snapshot of timing values.</param> |
---|
88 | protected override void Update(GameTime gameTime) |
---|
89 | { |
---|
90 | // Allows the game to exit |
---|
91 | if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) |
---|
92 | this.Exit(); |
---|
93 | |
---|
94 | mouseState = Mouse.GetState(); |
---|
95 | keyState = Keyboard.GetState(); |
---|
96 | |
---|
97 | if (previousMouseState.LeftButton == ButtonState.Released && mouseState.LeftButton == ButtonState.Pressed) |
---|
98 | { |
---|
99 | Rectangle mousePos = new Rectangle(mouseState.X, mouseState.Y, 1, 1); |
---|
100 | foreach (Tile tile in gameMap.Tiles) |
---|
101 | { |
---|
102 | if (mousePos.Intersects(tile.Boundaries)) |
---|
103 | { |
---|
104 | gameMap.UnSelectAll(); |
---|
105 | gameMap.SelectTile(tile); |
---|
106 | } |
---|
107 | } |
---|
108 | if (mousePos.Intersects(saveButton.Boundaries)) |
---|
109 | SaveMap(); |
---|
110 | } |
---|
111 | |
---|
112 | if (keyState.IsKeyDown(Keys.D1) && previousKeyState.IsKeyUp(Keys.D1)) |
---|
113 | gameMap.ChangeTileTo(TileType.Grass); |
---|
114 | if (keyState.IsKeyDown(Keys.D2) && previousKeyState.IsKeyUp(Keys.D2)) |
---|
115 | gameMap.ChangeTileTo(TileType.DarkGrass); |
---|
116 | if (keyState.IsKeyDown(Keys.D3) && previousKeyState.IsKeyUp(Keys.D3)) |
---|
117 | gameMap.ChangeTileTo(TileType.Sand); |
---|
118 | if (keyState.IsKeyDown(Keys.D4) && previousKeyState.IsKeyUp(Keys.D4)) |
---|
119 | gameMap.ChangeTileTo(TileType.Water); |
---|
120 | previousMouseState = mouseState; |
---|
121 | previousKeyState = keyState; |
---|
122 | base.Update(gameTime); |
---|
123 | } |
---|
124 | |
---|
125 | /// <summary> |
---|
126 | /// This is called when the game should draw itself. |
---|
127 | /// </summary> |
---|
128 | /// <param name="gameTime">Provides a snapshot of timing values.</param> |
---|
129 | protected override void Draw(GameTime gameTime) |
---|
130 | { |
---|
131 | GraphicsDevice.Clear(Color.CornflowerBlue); |
---|
132 | spriteBatch.Begin(); |
---|
133 | gameMap.Draw(spriteBatch); |
---|
134 | saveButton.Draw(spriteBatch); |
---|
135 | spriteBatch.End(); |
---|
136 | |
---|
137 | base.Draw(gameTime); |
---|
138 | } |
---|
139 | |
---|
140 | private void SaveMap() |
---|
141 | { |
---|
142 | //FileStream mapFile = File.Create("savedMap,xml"); |
---|
143 | |
---|
144 | XmlWriterSettings settings = new XmlWriterSettings(); |
---|
145 | settings.ConformanceLevel = ConformanceLevel.Document; |
---|
146 | settings.Indent = true; |
---|
147 | settings.Encoding = Encoding.UTF8; |
---|
148 | using (XmlWriter xmlWriter = XmlWriter.Create("savedMap", settings)) |
---|
149 | { |
---|
150 | xmlWriter.WriteStartDocument(); |
---|
151 | xmlWriter.WriteStartElement("Map"); |
---|
152 | foreach (Tile tile in gameMap.Tiles) |
---|
153 | { |
---|
154 | xmlWriter.WriteStartElement("Tile"); |
---|
155 | xmlWriter.WriteStartElement("TileType"); |
---|
156 | xmlWriter.WriteValue(tile.TileType.ToString()); |
---|
157 | xmlWriter.WriteEndElement(); |
---|
158 | //xmlWriter.WriteAttributeString("TileType", tile.TileType.ToString()); |
---|
159 | //xmlWriter.WriteAttributeString("X", tile.Position.X.ToString()); |
---|
160 | //xmlWriter.WriteAttributeString("Y", tile.Position.Y.ToString()); |
---|
161 | xmlWriter.WriteEndElement(); |
---|
162 | } |
---|
163 | xmlWriter.WriteEndElement(); |
---|
164 | xmlWriter.WriteEndDocument(); |
---|
165 | } |
---|
166 | |
---|
167 | } |
---|
168 | } |
---|
169 | } |
---|