Changeset 5581


Ignore:
Timestamp:
2014-07-23 12:43:58 (9 years ago)
Author:
mijoilmo
Message:
 
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  
    1212        public Vector2 offset = Vector2.Zero; 
    1313        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 
    1719        } 
    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)) * 
    2228                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 
    2438        } 
    2539    } 
  • 2014/30/MikkoI/WindowsGame1/WindowsGame1/WindowsGame1/Game1.cs

    r5558 r5581  
    2020        //ContentManager contentManager; 
    2121        SpriteBatch spriteBatch; 
    22         Texture2D texture; 
    2322        Vector2 screenSize = new Vector2(1920, 1080); 
    2423        MouseState currentMouseState; 
     
    2928        MapRenderer mapRenderer; 
    3029 
     30        public Dictionary<string, Texture2D> textures = new Dictionary<string, Texture2D>(); 
     31 
     32        List<Map> allMaps = new List<Map>(); 
     33        int currentMapIndex = 0; 
     34 
    3135        BasicEffect effect; 
    3236 
     37        public static Pikseli Instance; 
     38        public GameTime gameTime; 
     39 
    3340        public Pikseli() 
    3441        { 
     42            Instance = this; 
    3543            graphics = new GraphicsDeviceManager(this); 
    3644            graphics.PreferredBackBufferWidth = (int)screenSize.X; 
     
    5361            random = new Random(); 
    5462            camera = new Camera(); 
    55             //siirrä kamera keskelle 
    56             camera.offset = screenSize / 2; 
    5763            base.Initialize(); 
    5864        } 
     
    6672            // Create a new SpriteBatch, which can be used to draw textures. 
    6773            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 
    6988 
    7089            effect = new BasicEffect(GraphicsDevice); 
    71  
    72             texture = Content.Load<Texture2D>("Graphics/Test/taso5.1"); 
    7390 
    7491            // TODO: use this.Content to load your game content here 
     
    91108        protected override void Update(GameTime gameTime) 
    92109        { 
     110            this.gameTime = gameTime; 
     111 
    93112            // Allows the game to exit 
    94113            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape)) 
     
    110129            previousMouseState = currentMouseState; 
    111130 
    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)) 
    113152            { 
    114153                camera.yaw += 0.6f; 
    115154            } 
    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 
    122182        } 
    123183 
     
    128188        protected override void Draw(GameTime gameTime) 
    129189        { 
    130             GraphicsDevice.Clear(Color.Gray); 
     190            GraphicsDevice.Clear(Color.Black); 
    131191 
    132192 
    133193 
    134194            // 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); 
    147197 
    148198            base.Draw(gameTime); 
  • 2014/30/MikkoI/WindowsGame1/WindowsGame1/WindowsGame1/Render/MapRenderer.cs

    r5558 r5581  
    1010    class MapRenderer 
    1111    { 
     12        //renderöi periaatteessa kaiken paitsi GUI:n. sisältää tekstuurit 
    1213        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        { 
    1420            this.spriteBatch = spriteBatch; 
     21            this.map = map; 
     22            this.screenSize = screenSize; 
    1523        } 
    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); 
    1828        } 
    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(); 
    2178        } 
    2279    } 
  • 2014/30/MikkoI/WindowsGame1/WindowsGame1/WindowsGame1/WindowsGame1.csproj

    r5558 r5581  
    7474  <ItemGroup> 
    7575    <Compile Include="Camera\Camera.cs" /> 
     76    <Compile Include="Map\Map.cs" /> 
     77    <Compile Include="Map\MapObject.cs" /> 
     78    <Compile Include="Map\Tile.cs" /> 
    7679    <Compile Include="Properties\AssemblyInfo.cs" /> 
    7780    <Compile Include="Program.cs" /> 
  • 2014/30/MikkoI/WindowsGame1/WindowsGame1/WindowsGame1/WindowsGame1.csproj.Debug.cachefile

    r5558 r5581  
    11Content\Graphics\Test\taso5.1.xnb 
     2Content\Graphics\Test\Kuusi.xnb 
  • 2014/30/MikkoI/WindowsGame1/WindowsGame1/WindowsGame1/obj/x86/Debug/ContentPipeline-{97A5BC4F-C40A-4D52-BF03-8923DFD60CA0}.xml

    r5558 r5581  
    99      <Options>None</Options> 
    1010      <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> 
    1221    </Item> 
    1322    <BuildSuccessful>true</BuildSuccessful> 
  • 2014/30/MikkoI/WindowsGame1/WindowsGame1/WindowsGame1/obj/x86/Debug/WindowsGame1.csproj.FileListAbsolute.txt

    r5558 r5581  
    66C:\MyTemp\MikkoI\WindowsGame1\WindowsGame1\WindowsGame1\bin\x86\Debug\Content\Graphics\Test\taso5.1.xnb 
    77C:\MyTemp\MikkoI\WindowsGame1\WindowsGame1\WindowsGame1\obj\x86\Debug\WindowsGame1.csprojResolveAssemblyReference.cache 
     8C:\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  
    11Content\Graphics\Test\taso5.1.xnb 
     2Content\Graphics\Test\Kuusi.xnb 
  • 2014/30/MikkoI/WindowsGame1/WindowsGame1/WindowsGame1Content/WindowsGame1Content.contentproj

    r5558 r5581  
    3737    </Compile> 
    3838  </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> 
    3946  <Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\$(XnaFrameworkVersion)\Microsoft.Xna.GameStudio.ContentPipeline.targets" /> 
    4047  <!--  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.