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 | Texture2D texture; |
---|
23 | Vector2 screenSize = new Vector2(1920, 1080); |
---|
24 | MouseState currentMouseState; |
---|
25 | MouseState previousMouseState; |
---|
26 | Vector2 mapSize = new Vector2(40, 40); |
---|
27 | Random random; |
---|
28 | Camera camera; |
---|
29 | MapRenderer mapRenderer; |
---|
30 | |
---|
31 | BasicEffect effect; |
---|
32 | |
---|
33 | public Pikseli() |
---|
34 | { |
---|
35 | graphics = new GraphicsDeviceManager(this); |
---|
36 | graphics.PreferredBackBufferWidth = (int)screenSize.X; |
---|
37 | graphics.PreferredBackBufferHeight = (int)screenSize.Y; |
---|
38 | //graphics.IsFullScreen = true; |
---|
39 | //contentManager = new ContentManager(); |
---|
40 | Content.RootDirectory = "Content"; |
---|
41 | currentMouseState = new MouseState(); |
---|
42 | } |
---|
43 | |
---|
44 | /// <summary> |
---|
45 | /// Allows the game to perform any initialization it needs to before starting to run. |
---|
46 | /// This is where it can query for any required services and load any non-graphic |
---|
47 | /// related content. Calling base.Initialize will enumerate through any components |
---|
48 | /// and initialize them as well. |
---|
49 | /// </summary> |
---|
50 | protected override void Initialize() |
---|
51 | { |
---|
52 | // TODO: Add your initialization logic here |
---|
53 | random = new Random(); |
---|
54 | camera = new Camera(); |
---|
55 | //siirrä kamera keskelle |
---|
56 | camera.offset = screenSize / 2; |
---|
57 | base.Initialize(); |
---|
58 | } |
---|
59 | |
---|
60 | /// <summary> |
---|
61 | /// LoadContent will be called once per game and is the place to load |
---|
62 | /// all of your content. |
---|
63 | /// </summary> |
---|
64 | protected override void LoadContent() |
---|
65 | { |
---|
66 | // Create a new SpriteBatch, which can be used to draw textures. |
---|
67 | spriteBatch = new SpriteBatch(GraphicsDevice); |
---|
68 | mapRenderer = new MapRenderer(spriteBatch); |
---|
69 | |
---|
70 | effect = new BasicEffect(GraphicsDevice); |
---|
71 | |
---|
72 | texture = Content.Load<Texture2D>("Graphics/Test/taso5.1"); |
---|
73 | |
---|
74 | // TODO: use this.Content to load your game content here |
---|
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 (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape)) |
---|
95 | this.Exit(); |
---|
96 | |
---|
97 | // TODO: Add your update logic here |
---|
98 | |
---|
99 | currentMouseState = Mouse.GetState(); |
---|
100 | |
---|
101 | int erotus = currentMouseState.ScrollWheelValue - previousMouseState.ScrollWheelValue; |
---|
102 | if (erotus > 0) |
---|
103 | { |
---|
104 | camera.zoomFactor *= 1.2f; |
---|
105 | } |
---|
106 | else if (erotus < 0) |
---|
107 | { |
---|
108 | camera.zoomFactor /= 1.2f; |
---|
109 | } |
---|
110 | previousMouseState = currentMouseState; |
---|
111 | |
---|
112 | if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Down)) |
---|
113 | { |
---|
114 | camera.yaw += 0.6f; |
---|
115 | } |
---|
116 | else if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Up)) |
---|
117 | { |
---|
118 | camera.yaw -= 0.6f; |
---|
119 | } |
---|
120 | |
---|
121 | base.Update(gameTime); |
---|
122 | } |
---|
123 | |
---|
124 | /// <summary> |
---|
125 | /// This is called when the game should draw itself. |
---|
126 | /// </summary> |
---|
127 | /// <param name="gameTime">Provides a snapshot of timing values.</param> |
---|
128 | protected override void Draw(GameTime gameTime) |
---|
129 | { |
---|
130 | GraphicsDevice.Clear(Color.Gray); |
---|
131 | |
---|
132 | |
---|
133 | |
---|
134 | // TODO: Add your drawing code here |
---|
135 | Matrix matrix = camera.getMatrix(new Vector2(texture.Width, texture.Height), mapSize); |
---|
136 | Vector2 size = new Vector2(texture.Width, texture.Height) * camera.zoomFactor; |
---|
137 | |
---|
138 | spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, matrix); |
---|
139 | for (var x = 0; x < mapSize.X; x++) |
---|
140 | { |
---|
141 | for (var y = 0; y < mapSize.Y; y++) |
---|
142 | { |
---|
143 | mapRenderer.renderGroundTile(texture,x,y); |
---|
144 | } |
---|
145 | } |
---|
146 | spriteBatch.End(); |
---|
147 | |
---|
148 | base.Draw(gameTime); |
---|
149 | } |
---|
150 | } |
---|
151 | } |
---|