source: 2011/23/sijoseha/AdventureGame/AdventureGame/AdventureGame.cs @ 1729

Revision 1729, 6.3 KB checked in by tekrjant, 9 years ago (diff)

Sumthing

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