1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using Microsoft.Xna.Framework; |
---|
5 | using Microsoft.Xna.Framework.Audio; |
---|
6 | using Microsoft.Xna.Framework.Content; |
---|
7 | using Microsoft.Xna.Framework.GamerServices; |
---|
8 | using Microsoft.Xna.Framework.Graphics; |
---|
9 | using Microsoft.Xna.Framework.Input; |
---|
10 | using Microsoft.Xna.Framework.Media; |
---|
11 | |
---|
12 | namespace 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 | Random random; |
---|
27 | Camera camera; |
---|
28 | MapRenderer mapRenderer; |
---|
29 | |
---|
30 | public Dictionary<string, Texture2D> textures = new Dictionary<string, Texture2D>(); |
---|
31 | |
---|
32 | List<Map> allMaps = new List<Map>(); |
---|
33 | int currentMapIndex = 0; |
---|
34 | |
---|
35 | BasicEffect effect; |
---|
36 | |
---|
37 | public static Pikseli Instance; |
---|
38 | public GameTime gameTime; |
---|
39 | |
---|
40 | public Pikseli() |
---|
41 | { |
---|
42 | Instance = this; |
---|
43 | graphics = new GraphicsDeviceManager(this); |
---|
44 | graphics.PreferredBackBufferWidth = (int)screenSize.X; |
---|
45 | graphics.PreferredBackBufferHeight = (int)screenSize.Y; |
---|
46 | //graphics.IsFullScreen = true; |
---|
47 | //contentManager = new ContentManager(); |
---|
48 | Content.RootDirectory = "Content"; |
---|
49 | currentMouseState = new MouseState(); |
---|
50 | } |
---|
51 | |
---|
52 | /// <summary> |
---|
53 | /// Allows the game to perform any initialization it needs to before starting to run. |
---|
54 | /// This is where it can query for any required services and load any non-graphic |
---|
55 | /// related content. Calling base.Initialize will enumerate through any components |
---|
56 | /// and initialize them as well. |
---|
57 | /// </summary> |
---|
58 | protected override void Initialize() |
---|
59 | { |
---|
60 | // TODO: Add your initialization logic here |
---|
61 | random = new Random(); |
---|
62 | camera = new Camera(); |
---|
63 | base.Initialize(); |
---|
64 | } |
---|
65 | |
---|
66 | /// <summary> |
---|
67 | /// LoadContent will be called once per game and is the place to load |
---|
68 | /// all of your content. |
---|
69 | /// </summary> |
---|
70 | protected override void LoadContent() |
---|
71 | { |
---|
72 | // Create a new SpriteBatch, which can be used to draw textures. |
---|
73 | spriteBatch = new SpriteBatch(GraphicsDevice); |
---|
74 | |
---|
75 | allMaps.Add(new Map(new Texture2D(GraphicsDevice, 1, 1), 10, 10)); |
---|
76 | |
---|
77 | mapRenderer = new MapRenderer(spriteBatch, allMaps[currentMapIndex], screenSize); |
---|
78 | |
---|
79 | /*LOAD ALL TEXTURES*/ |
---|
80 | textures.Add("testitile", Content.Load<Texture2D>("Graphics/Test/taso5.1")); |
---|
81 | textures.Add("testipuu", Content.Load<Texture2D>("Graphics/Test/kuusi")); |
---|
82 | |
---|
83 | |
---|
84 | |
---|
85 | |
---|
86 | |
---|
87 | |
---|
88 | |
---|
89 | effect = new BasicEffect(GraphicsDevice); |
---|
90 | |
---|
91 | // TODO: use this.Content to load your game content here |
---|
92 | } |
---|
93 | |
---|
94 | /// <summary> |
---|
95 | /// UnloadContent will be called once per game and is the place to unload |
---|
96 | /// all content. |
---|
97 | /// </summary> |
---|
98 | protected override void UnloadContent() |
---|
99 | { |
---|
100 | // TODO: Unload any non ContentManager content here |
---|
101 | } |
---|
102 | |
---|
103 | /// <summary> |
---|
104 | /// Allows the game to run logic such as updating the world, |
---|
105 | /// checking for collisions, gathering input, and playing audio. |
---|
106 | /// </summary> |
---|
107 | /// <param name="gameTime">Provides a snapshot of timing values.</param> |
---|
108 | protected override void Update(GameTime gameTime) |
---|
109 | { |
---|
110 | this.gameTime = gameTime; |
---|
111 | |
---|
112 | // Allows the game to exit |
---|
113 | if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape)) |
---|
114 | this.Exit(); |
---|
115 | |
---|
116 | // TODO: Add your update logic here |
---|
117 | |
---|
118 | currentMouseState = Mouse.GetState(); |
---|
119 | |
---|
120 | int erotus = currentMouseState.ScrollWheelValue - previousMouseState.ScrollWheelValue; |
---|
121 | if (erotus > 0) |
---|
122 | { |
---|
123 | camera.zoomFactor *= 1.2f; |
---|
124 | } |
---|
125 | else if (erotus < 0) |
---|
126 | { |
---|
127 | camera.zoomFactor /= 1.2f; |
---|
128 | } |
---|
129 | previousMouseState = currentMouseState; |
---|
130 | |
---|
131 | ListenKeyBoard(); |
---|
132 | |
---|
133 | base.Update(gameTime); |
---|
134 | } |
---|
135 | |
---|
136 | void ListenKeyBoard() |
---|
137 | { |
---|
138 | if (Keyboard.GetState().IsKeyDown(Keys.Down)) |
---|
139 | { |
---|
140 | camera.pitch += 0.6f; |
---|
141 | } |
---|
142 | else if (Keyboard.GetState().IsKeyDown(Keys.Up)) |
---|
143 | { |
---|
144 | camera.pitch -= 0.6f; |
---|
145 | } |
---|
146 | |
---|
147 | if (Keyboard.GetState().IsKeyDown(Keys.Right)) |
---|
148 | { |
---|
149 | camera.yaw -= 0.6f; |
---|
150 | } |
---|
151 | else if (Keyboard.GetState().IsKeyDown(Keys.Left)) |
---|
152 | { |
---|
153 | camera.yaw += 0.6f; |
---|
154 | } |
---|
155 | |
---|
156 | float scrollSpeed = 9; |
---|
157 | |
---|
158 | if (Keyboard.GetState().IsKeyDown(Keys.W)) |
---|
159 | { |
---|
160 | //lisätään molempia koska tämä muokkaa translaatiota |
---|
161 | camera.offset.X += scrollSpeed / camera.zoomFactor; |
---|
162 | camera.offset.Y += scrollSpeed / camera.zoomFactor; |
---|
163 | } |
---|
164 | else if (Keyboard.GetState().IsKeyDown(Keys.S)) |
---|
165 | { |
---|
166 | camera.offset.Y -= scrollSpeed / camera.zoomFactor; |
---|
167 | camera.offset.X -= scrollSpeed / camera.zoomFactor; |
---|
168 | } |
---|
169 | |
---|
170 | if (Keyboard.GetState().IsKeyDown(Keys.A)) |
---|
171 | { |
---|
172 | camera.offset.X += scrollSpeed / camera.zoomFactor; |
---|
173 | camera.offset.Y -= scrollSpeed / camera.zoomFactor; |
---|
174 | } |
---|
175 | else if (Keyboard.GetState().IsKeyDown(Keys.D)) |
---|
176 | { |
---|
177 | camera.offset.X -= scrollSpeed / camera.zoomFactor; |
---|
178 | camera.offset.Y += scrollSpeed / camera.zoomFactor; |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | } |
---|
183 | |
---|
184 | /// <summary> |
---|
185 | /// This is called when the game should draw itself. |
---|
186 | /// </summary> |
---|
187 | /// <param name="gameTime">Provides a snapshot of timing values.</param> |
---|
188 | protected override void Draw(GameTime gameTime) |
---|
189 | { |
---|
190 | GraphicsDevice.Clear(Color.Black); |
---|
191 | |
---|
192 | |
---|
193 | |
---|
194 | // TODO: Add your drawing code here |
---|
195 | |
---|
196 | mapRenderer.RenderMap(camera); |
---|
197 | |
---|
198 | base.Draw(gameTime); |
---|
199 | } |
---|
200 | } |
---|
201 | } |
---|