Changeset 5581
- Timestamp:
- 2014-07-23 12:43:58 (9 years ago)
- Location:
- 2014/30/MikkoI/WindowsGame1
- Files:
-
- 7 added
- 16 edited
Legend:
- Unmodified
- Added
- Removed
-
2014/30/MikkoI/WindowsGame1/WindowsGame1/WindowsGame1/Camera/Camera.cs
r5558 r5581 12 12 public Vector2 offset = Vector2.Zero; 13 13 public float zoomFactor = 1; 14 public float yaw = -45; 15 public Camera() { 16 14 public float pitch = -45; 15 public float yaw = 0; 16 public Camera() 17 { 18 17 19 } 18 public Matrix getMatrix(Vector2 gridSize,Vector2 mapSize) { 19 return Matrix.CreateTranslation(new Vector3(-(gridSize.X * mapSize.X / 2), -(gridSize.Y * mapSize.Y / 2), 0)) * 20 Matrix.CreateRotationZ(MathHelper.ToRadians(45)) * 21 Matrix.CreateRotationX(MathHelper.ToRadians(yaw)) * 20 public Matrix getMatrix(Vector2 gridSize, Vector2 mapSize, Vector2 screenSize) 21 { 22 viewPortBounds(); 23 24 Vector3 pixelMapSize = new Vector3((gridSize.X * mapSize.X / 2), (gridSize.Y * mapSize.Y / 2), 0); 25 return Matrix.CreateTranslation(new Vector3(-pixelMapSize.X + offset.X, -pixelMapSize.Y + offset.Y, 0)) * 26 Matrix.CreateRotationZ(MathHelper.ToRadians(45 + yaw)) * 27 Matrix.CreateRotationX(MathHelper.ToRadians(pitch)) * 22 28 Matrix.CreateScale(zoomFactor, zoomFactor, 0) * 23 Matrix.CreateTranslation(new Vector3(offset.X, offset.Y, 0)); 29 Matrix.CreateTranslation(new Vector3(screenSize.X / 2, screenSize.Y / 2, 0)); 30 } 31 void viewPortBounds() 32 { 33 //rajoittaa viewporttia 34 35 //if (yaw < -70) { yaw = -70; } else if (yaw > 70) { yaw = 70; }//rajoita pyörittämistä (-70 - 70) 36 if (pitch < -88) { pitch = -88; } else if (pitch > -45) { pitch = -45; }//rajoittaa ylös-alas-liikettä 37 if (zoomFactor < 0.4f) { zoomFactor = 0.4f; } else if (zoomFactor > 3f) { zoomFactor = 3f; }//rajoittaa zoomia 24 38 } 25 39 } -
2014/30/MikkoI/WindowsGame1/WindowsGame1/WindowsGame1/Game1.cs
r5558 r5581 20 20 //ContentManager contentManager; 21 21 SpriteBatch spriteBatch; 22 Texture2D texture;23 22 Vector2 screenSize = new Vector2(1920, 1080); 24 23 MouseState currentMouseState; … … 29 28 MapRenderer mapRenderer; 30 29 30 public Dictionary<string, Texture2D> textures = new Dictionary<string, Texture2D>(); 31 32 List<Map> allMaps = new List<Map>(); 33 int currentMapIndex = 0; 34 31 35 BasicEffect effect; 32 36 37 public static Pikseli Instance; 38 public GameTime gameTime; 39 33 40 public Pikseli() 34 41 { 42 Instance = this; 35 43 graphics = new GraphicsDeviceManager(this); 36 44 graphics.PreferredBackBufferWidth = (int)screenSize.X; … … 53 61 random = new Random(); 54 62 camera = new Camera(); 55 //siirrä kamera keskelle56 camera.offset = screenSize / 2;57 63 base.Initialize(); 58 64 } … … 66 72 // Create a new SpriteBatch, which can be used to draw textures. 67 73 spriteBatch = new SpriteBatch(GraphicsDevice); 68 mapRenderer = new MapRenderer(spriteBatch); 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 69 88 70 89 effect = new BasicEffect(GraphicsDevice); 71 72 texture = Content.Load<Texture2D>("Graphics/Test/taso5.1");73 90 74 91 // TODO: use this.Content to load your game content here … … 91 108 protected override void Update(GameTime gameTime) 92 109 { 110 this.gameTime = gameTime; 111 93 112 // Allows the game to exit 94 113 if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape)) … … 110 129 previousMouseState = currentMouseState; 111 130 112 if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Down)) 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)) 113 152 { 114 153 camera.yaw += 0.6f; 115 154 } 116 else if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Up)) 117 { 118 camera.yaw -= 0.6f; 119 } 120 121 base.Update(gameTime); 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 122 182 } 123 183 … … 128 188 protected override void Draw(GameTime gameTime) 129 189 { 130 GraphicsDevice.Clear(Color. Gray);190 GraphicsDevice.Clear(Color.Black); 131 191 132 192 133 193 134 194 // 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(); 195 196 mapRenderer.RenderMap(camera); 147 197 148 198 base.Draw(gameTime); -
2014/30/MikkoI/WindowsGame1/WindowsGame1/WindowsGame1/Render/MapRenderer.cs
r5558 r5581 10 10 class MapRenderer 11 11 { 12 //renderöi periaatteessa kaiken paitsi GUI:n. sisältää tekstuurit 12 13 SpriteBatch spriteBatch; 13 public MapRenderer(SpriteBatch spriteBatch) { 14 Map map; 15 Vector2 screenSize; 16 Vector3 sunPosition = new Vector3(0,0,15); 17 18 public MapRenderer(SpriteBatch spriteBatch, Map map, Vector2 screenSize) 19 { 14 20 this.spriteBatch = spriteBatch; 21 this.map = map; 22 this.screenSize = screenSize; 15 23 } 16 public void renderGroundTile(Texture2D texture, int x, int y){//renderöi maata 17 spriteBatch.Draw(texture, new Rectangle(x * texture.Width, y * texture.Height, texture.Width, texture.Height), null, Color.White, 0f, Vector2.Zero, SpriteEffects.None, 0f); 24 void renderGroundTile(Texture2D texture, int x, int y) 25 {//renderöi maata 26 //maatilen koko on 64x64. 27 spriteBatch.Draw(texture, new Rectangle(x * 64, y * 64, texture.Width, texture.Height), null, Color.White, 0f, Vector2.Zero, SpriteEffects.None, 0f); 18 28 } 19 public void renderGroundObject(Texture2D texture, int x, int y, Vector2 size) {//renderöi maassa olevan asian (ei liiku) 20 spriteBatch.Draw(texture, new Rectangle(x * texture.Width, y * texture.Height, (int)size.X, (int)size.Y), null, Color.Red, 0, Vector2.Zero, SpriteEffects.None, 0f); 29 void renderGroundObject(Texture2D texture, int x, int y, Vector2 size, Matrix matrix) 30 {//renderöi maassa olevan asian (ei liiku) 31 Vector2 paikka = Vector2.Transform(new Vector2((x + 0.5f) * 64, (y + 0.5f) * 64), matrix) - new Vector2(size.X / 2, size.Y); 32 spriteBatch.Draw(texture, new Rectangle((int)paikka.X, (int)paikka.Y, (int)size.X, (int)size.Y), null, Color.White, 0, Vector2.Zero, SpriteEffects.None, (paikka.Y + size.Y) / (screenSize.Y + size.Y)); 33 } 34 35 public void RenderMap(Camera camera) 36 {//renderöi koko mapin, kutsu ulkopuolelta 37 Matrix matrix = camera.getMatrix(new Vector2(64, 64), map.Size, screenSize);//mapin tilen koko 64x64 38 spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, matrix); 39 for (var x = 0; x < map.Size.X; x++) 40 { 41 for (var y = 0; y < map.Size.Y; y++) 42 { 43 renderGroundTile(Pikseli.Instance.textures[map.Tiles[x, y].textureId], x, y); 44 } 45 } 46 47 //varjot testi 48 49 sunPosition = new Vector3((float)Pikseli.Instance.gameTime.TotalGameTime.TotalSeconds, (float)Pikseli.Instance.gameTime.TotalGameTime.TotalSeconds, 0); 50 51 foreach (MapObject obj in map.Objects) 52 { 53 Texture2D texture = Pikseli.Instance.textures[obj.textureId]; 54 Vector2 size = new Vector2(texture.Width, texture.Height) * camera.zoomFactor;//ditto 55 56 Vector2 erotus = new Vector2(sunPosition.X, sunPosition.Y) - obj.position; 57 58 59 float shadowAngle = (float)(Math.Atan2(erotus.Y, erotus.X) - Math.PI/2); 60 double shadowOffsetX = Math.Cos(shadowAngle + Math.PI / 2) * 5, 61 shadowOffsetY = Math.Sin(shadowAngle + Math.PI / 2) * 5; 62 63 spriteBatch.Draw(texture, new Rectangle((int)((obj.position.X + 0.5) * 64 + shadowOffsetX), (int)((obj.position.Y + 0.5) * 64 + shadowOffsetY), texture.Width, texture.Height), null, new Color(0f,0f,0f,0.3f), shadowAngle, new Vector2(texture.Width / 2, texture.Height), SpriteEffects.None, 0f); 64 //spriteBatch.Draw(obj.texture, new Rectangle((int)(obj.position.X + 0.5) * 64, (int)(obj.position.Y + 0.5) * 64, (int)obj.texture.Width, (int)obj.texture.Height), null, Color.Gray, MathHelper.ToRadians(30), new Vector2(obj.texture.Width / 2, obj.texture.Height), SpriteEffects.None, 0f); 65 } 66 spriteBatch.End(); 67 68 69 70 spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend); 71 foreach (MapObject obj in map.Objects) 72 { 73 Texture2D texture = Pikseli.Instance.textures[obj.textureId]; 74 Vector2 size = new Vector2(texture.Width, texture.Height) * camera.zoomFactor;//ditto 75 renderGroundObject(texture, (int)obj.position.X, (int)obj.position.Y, size, matrix); 76 } 77 spriteBatch.End(); 21 78 } 22 79 } -
2014/30/MikkoI/WindowsGame1/WindowsGame1/WindowsGame1/WindowsGame1.csproj
r5558 r5581 74 74 <ItemGroup> 75 75 <Compile Include="Camera\Camera.cs" /> 76 <Compile Include="Map\Map.cs" /> 77 <Compile Include="Map\MapObject.cs" /> 78 <Compile Include="Map\Tile.cs" /> 76 79 <Compile Include="Properties\AssemblyInfo.cs" /> 77 80 <Compile Include="Program.cs" /> -
2014/30/MikkoI/WindowsGame1/WindowsGame1/WindowsGame1/WindowsGame1.csproj.Debug.cachefile
r5558 r5581 1 1 Content\Graphics\Test\taso5.1.xnb 2 Content\Graphics\Test\Kuusi.xnb -
2014/30/MikkoI/WindowsGame1/WindowsGame1/WindowsGame1/obj/x86/Debug/ContentPipeline-{97A5BC4F-C40A-4D52-BF03-8923DFD60CA0}.xml
r5558 r5581 9 9 <Options>None</Options> 10 10 <Output>C:\MyTemp\MikkoI\WindowsGame1\WindowsGame1\WindowsGame1\bin\x86\Debug\Content\Graphics\Test\taso5.1.xnb</Output> 11 <Time>2014-07-21T10:21:08.8802747+03:00</Time> 11 <Time>2014-07-23T10:55:33.930918+03:00</Time> 12 </Item> 13 <Item> 14 <Source>Graphics\Test\Kuusi.png</Source> 15 <Name>Graphics\Test\Kuusi</Name> 16 <Importer>TextureImporter</Importer> 17 <Processor>TextureProcessor</Processor> 18 <Options>None</Options> 19 <Output>C:\MyTemp\MikkoI\WindowsGame1\WindowsGame1\WindowsGame1\bin\x86\Debug\Content\Graphics\Test\Kuusi.xnb</Output> 20 <Time>2014-07-23T11:18:07.466918+03:00</Time> 12 21 </Item> 13 22 <BuildSuccessful>true</BuildSuccessful> -
2014/30/MikkoI/WindowsGame1/WindowsGame1/WindowsGame1/obj/x86/Debug/WindowsGame1.csproj.FileListAbsolute.txt
r5558 r5581 6 6 C:\MyTemp\MikkoI\WindowsGame1\WindowsGame1\WindowsGame1\bin\x86\Debug\Content\Graphics\Test\taso5.1.xnb 7 7 C:\MyTemp\MikkoI\WindowsGame1\WindowsGame1\WindowsGame1\obj\x86\Debug\WindowsGame1.csprojResolveAssemblyReference.cache 8 C:\MyTemp\MikkoI\WindowsGame1\WindowsGame1\WindowsGame1\bin\x86\Debug\Content\Graphics\Test\Kuusi.xnb -
2014/30/MikkoI/WindowsGame1/WindowsGame1/WindowsGame1/obj/x86/Debug/cachefile-{97A5BC4F-C40A-4D52-BF03-8923DFD60CA0}-targetpath.txt
r5558 r5581 1 1 Content\Graphics\Test\taso5.1.xnb 2 Content\Graphics\Test\Kuusi.xnb -
2014/30/MikkoI/WindowsGame1/WindowsGame1/WindowsGame1Content/WindowsGame1Content.contentproj
r5558 r5581 37 37 </Compile> 38 38 </ItemGroup> 39 <ItemGroup> 40 <Compile Include="Graphics\Test\Kuusi.png"> 41 <Name>Kuusi</Name> 42 <Importer>TextureImporter</Importer> 43 <Processor>TextureProcessor</Processor> 44 </Compile> 45 </ItemGroup> 39 46 <Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\$(XnaFrameworkVersion)\Microsoft.Xna.GameStudio.ContentPipeline.targets" /> 40 47 <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Note: See TracChangeset
for help on using the changeset viewer.