source: 2014/30/MikkoI/WindowsGame1/WindowsGame1/WindowsGame1/Game1.cs @ 5626

Revision 5626, 8.2 KB checked in by mijoilmo, 9 years ago (diff)

varjosimulaattori

Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using Microsoft.Xna.Framework;
5using Microsoft.Xna.Framework.Audio;
6using Microsoft.Xna.Framework.Content;
7using Microsoft.Xna.Framework.GamerServices;
8using Microsoft.Xna.Framework.Graphics;
9using Microsoft.Xna.Framework.Input;
10using Microsoft.Xna.Framework.Media;
11
12namespace WindowsGame1
13{
14    /// <summary>
15    /// This is the main type for your game
16    /// </summary>
17    public class Pikseli : Microsoft.Xna.Framework.Game
18    {
19        GraphicsDeviceManager graphics;
20        //ContentManager contentManager;
21        SpriteBatch spriteBatch;
22        Vector2 screenSize = new Vector2(1920, 1080);
23        MouseState currentMouseState;
24        MouseState previousMouseState;
25        Vector2 mapSize = new Vector2(40, 40);
26        public Vector2 mousePos = new Vector2(0, 0);
27        public Random random;
28        Camera camera;
29        MasterRenderer masterRenderer;
30
31        public Dictionary<string, Texture2D> textures = new Dictionary<string, Texture2D>();
32
33        List<Map> allMaps = new List<Map>();
34        int currentMapIndex = 0;
35
36        BasicEffect effect;
37
38        public static Pikseli Instance;
39        public GameTime gameTime;
40
41        public Pikseli()
42        {
43            Instance = this;
44            graphics = new GraphicsDeviceManager(this);
45            graphics.PreferredBackBufferWidth = (int)screenSize.X;
46            graphics.PreferredBackBufferHeight = (int)screenSize.Y;
47
48            //graphics.IsFullScreen = true;
49            //contentManager = new ContentManager();
50            Content.RootDirectory = "Content";
51            currentMouseState = new MouseState();
52        }
53
54        /// <summary>
55        /// Allows the game to perform any initialization it needs to before starting to run.
56        /// This is where it can query for any required services and load any non-graphic
57        /// related content.  Calling base.Initialize will enumerate through any components
58        /// and initialize them as well.
59        /// </summary>
60        protected override void Initialize()
61        {
62            // TODO: Add your initialization logic here
63            random = new Random();
64            camera = new Camera(screenSize);
65            base.Initialize();
66        }
67
68        /// <summary>
69        /// LoadContent will be called once per game and is the place to load
70        /// all of your content.
71        /// </summary>
72        void LoadTextures()
73        {
74            /*LOAD ALL TEXTURES*/
75            textures.Add("testitile", Content.Load<Texture2D>("Graphics/Test/taso5.1"));
76            textures.Add("test/select", Content.Load<Texture2D>("Graphics/Test/Select"));
77            textures.Add("test/kuutio", Content.Load<Texture2D>("Graphics/Test/Kuutio"));
78
79            //KÄYTETTÄVÄT PUUT
80            textures.Add("puut/kuusi", Content.Load<Texture2D>("Graphics/Puut/kuusi"));
81            textures.Add("puut/mänty", Content.Load<Texture2D>("Graphics/Puut/mänty"));
82            textures.Add("puut/ohut", Content.Load<Texture2D>("Graphics/Puut/ohut"));
83            textures.Add("puut/kuollut", Content.Load<Texture2D>("Graphics/Puut/kuollut"));
84
85            //TAIVAS
86            textures.Add("taivas/aurinko", Content.Load<Texture2D>("Graphics/Taivas/Aurinko"));
87            textures.Add("taivas/tähdet", Content.Load<Texture2D>("Graphics/Taivas/Tähdet"));
88
89            //OLENNOT
90            textures.Add("olennot/ukko", Content.Load<Texture2D>("Graphics/Olennot/Ukko"));
91
92            //RAKENNUKSET
93            textures.Add("rakennukset/kerrostalo", Content.Load<Texture2D>("Graphics/Rakennukset/isotalo"));
94
95            //GUI
96            textures.Add("gui/cursor", Content.Load<Texture2D>("Graphics/Gui/Cursor"));
97        }
98        protected override void LoadContent()
99        {
100            // Create a new SpriteBatch, which can be used to draw textures.
101            spriteBatch = new SpriteBatch(GraphicsDevice);
102
103            allMaps.Add(new Map(new Texture2D(GraphicsDevice, 1, 1), 20, 20));
104
105            masterRenderer = new MasterRenderer(spriteBatch, allMaps[currentMapIndex], camera);
106
107            LoadTextures();
108
109            effect = new BasicEffect(GraphicsDevice);
110
111            // TODO: use this.Content to load your game content here
112        }
113
114        /// <summary>
115        /// UnloadContent will be called once per game and is the place to unload
116        /// all content.
117        /// </summary>
118        protected override void UnloadContent()
119        {
120            // TODO: Unload any non ContentManager content here
121        }
122
123        /// <summary>
124        /// Allows the game to run logic such as updating the world,
125        /// checking for collisions, gathering input, and playing audio.
126        /// </summary>
127        /// <param name="gameTime">Provides a snapshot of timing values.</param>
128        protected override void Update(GameTime gameTime)
129        {
130            this.gameTime = gameTime;
131
132            masterRenderer.Update();
133
134            // Allows the game to exit
135            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape))
136                this.Exit();
137
138            // TODO: Add your update logic here
139
140            currentMouseState = Mouse.GetState();
141
142            int erotus = currentMouseState.ScrollWheelValue - previousMouseState.ScrollWheelValue;
143            if (erotus > 0)
144            {
145                camera.zoomFactor *= 1.2f;
146            }
147            else if (erotus < 0)
148            {
149                camera.zoomFactor /= 1.2f;
150            }
151
152            mousePos = new Vector2(currentMouseState.X,currentMouseState.Y);
153           
154            previousMouseState = currentMouseState;
155
156            ListenKeyBoard();
157
158            base.Update(gameTime);
159        }
160
161        void ListenKeyBoard()
162        {
163            if (Keyboard.GetState().IsKeyDown(Keys.Down))
164            {
165                camera.pitch += 0.6f;
166            }
167            else if (Keyboard.GetState().IsKeyDown(Keys.Up))
168            {
169                camera.pitch -= 0.6f;
170            }
171
172            if (Keyboard.GetState().IsKeyDown(Keys.Right))
173            {
174                camera.yaw -= 0.6f;
175            }
176            else if (Keyboard.GetState().IsKeyDown(Keys.Left))
177            {
178                camera.yaw += 0.6f;
179            }
180
181            float scrollSpeed = 9;
182
183            if (Keyboard.GetState().IsKeyDown(Keys.W))
184            {
185                //lisätään molempia koska tämä muokkaa translaatiota
186                camera.offset.X += (scrollSpeed / camera.zoomFactor) * (float)Math.Sin(MathHelper.ToRadians(camera.yaw));
187                camera.offset.Y += (scrollSpeed / camera.zoomFactor) * (float)Math.Cos(MathHelper.ToRadians(camera.yaw));
188            }
189            else if (Keyboard.GetState().IsKeyDown(Keys.S))
190            {
191                camera.offset.X -= (scrollSpeed / camera.zoomFactor) * (float)Math.Sin(MathHelper.ToRadians(camera.yaw));
192                camera.offset.Y -= (scrollSpeed / camera.zoomFactor) * (float)Math.Cos(MathHelper.ToRadians(camera.yaw));
193            }
194
195            if (Keyboard.GetState().IsKeyDown(Keys.A))
196            {
197                camera.offset.X += (scrollSpeed / camera.zoomFactor) * (float)Math.Cos(MathHelper.ToRadians(camera.yaw));
198                camera.offset.Y -= (scrollSpeed / camera.zoomFactor) * (float)Math.Sin(MathHelper.ToRadians(camera.yaw));
199            }
200            else if (Keyboard.GetState().IsKeyDown(Keys.D))
201            {
202                camera.offset.X -= (scrollSpeed / camera.zoomFactor) * (float)Math.Cos(MathHelper.ToRadians(camera.yaw));
203                camera.offset.Y += (scrollSpeed / camera.zoomFactor) * (float)Math.Sin(MathHelper.ToRadians(camera.yaw));
204            }
205
206
207        }
208
209        /// <summary>
210        /// This is called when the game should draw itself.
211        /// </summary>
212        /// <param name="gameTime">Provides a snapshot of timing values.</param>
213        protected override void Draw(GameTime gameTime)
214        {
215            GraphicsDevice.Clear(Color.Black);
216
217            // TODO: Add your drawing code here
218
219            masterRenderer.RenderAll();
220
221            base.Draw(gameTime);
222        }
223    }
224}
Note: See TracBrowser for help on using the repository browser.