예제 #1
0
파일: MainView.cs 프로젝트: BaltaX/MegaJump
        internal void Draw(Model.MainModel a_mainModel, double a_elapsedTimeTotalSeconds)
        {
            //Variables used for drawing game
                //Vector2 modelPosition = a_mainModel.getPlayerPosition();

                m_tiles = a_mainModel.getTiles();

                //Destination rectangle for Megaman

                //Get model coordinates for Megaman
                Vector2 megamanModelPos = a_mainModel.getPlayerPosition();

                //Model height of viewPort
                float modelViewPortY = (float)m_windowHeight / 64f;

                //Calculate model displacement for y
                float modelDisplacementY = megamanModelPos.Y - (modelViewPortY / 2.0f);

                //Calculate view DisplacementY for Y
                int viewDisplacementY = (int)(modelDisplacementY * 64f);

                //Translate to view coordinates for megaman
                Vector2 megamanViewPos = megamanModelPos * 64f;

                //Create destination rectangle for megaman
                Rectangle destRectMegaman = new Rectangle((int)megamanViewPos.X, (int)megamanViewPos.Y - viewDisplacementY, 64, 64);

                //Create destination rectangle for background
                Rectangle sourceBackgroundRectangle = new Rectangle(0, 100, 64, 64);

                Rectangle destBackgroundRectangle = new Rectangle(0, -(int)((float)viewDisplacementY / 27f), 640, 1920);

                //Create destination rectangle for scoreboard
                Rectangle destScoreBoard = new Rectangle(0, 10, 640, 60);

                m_spriteBatch.Begin();

                //Draw background
                switch (m_currentLevel)
                {
                    case 0: m_spriteBatch.Draw(m_background, destBackgroundRectangle, Color.White);
                        break;

                    case 1: m_spriteBatch.Draw(m_backGroundSky, destBackgroundRectangle, Color.White);
                        break;

                    case 2: m_spriteBatch.Draw(m_backGroundSpace, destBackgroundRectangle, Color.White);
                        break;

                }

                //Draw level
                for (int x = 0; x < a_mainModel.getlevelWidth(); x++)
                {
                    for (int y = 0; y < a_mainModel.getlevelHeight(); y++)
                    {
                        //Source rectangle
                        Rectangle sourceRectangle = new Rectangle((int)m_tiles[x, y] * m_textureTileSize, 0, m_viewscale, m_viewscale);

                        //Destination rectangle
                        //y ska ändras här för att passa spelarens position! Dvs istället för 100 ska vi ha displacement
                        //Med 100 har allting "flyttats ned" 100 pixlar
                        Rectangle destRect = new Rectangle((x * m_viewscale), (y * m_viewscale) - viewDisplacementY, m_viewscale, m_viewscale);

                        m_spriteBatch.Draw(m_texture, destRect, sourceRectangle, Color.White);

                    }
                }

                //Draw Megaman if not dead
                if (!gameOver)
                    m_spriteBatch.Draw(m_megaMan, destRectMegaman, Color.White);

                else if (gameOver&&!m_clearedAllLevels)
                {
                    m_spriteBatch.Draw(m_megaManDead, destRectMegaman, Color.White);
                    Rectangle gameOverDestRect = new Rectangle(90, 200, 477, 93);
                    m_spriteBatch.Draw(m_gameOverSign, gameOverDestRect, Color.White);
                }

                //Draw scoreboard
                m_spriteBatch.Draw(m_scoreBoard, destScoreBoard, Color.White);

                //Draw height score
                m_spriteBatch.DrawString(font, "Height: " + ((int)((400 - a_mainModel.getPlayerPosition().Y+a_mainModel.getAccumulatedHeight()))).ToString(), new Vector2(20, 30), Color.White);

                //Draw elapsed time
                m_spriteBatch.DrawString(font, "Time: " + String.Format("{0:0.00}", a_mainModel.getElapsedTime()), new Vector2(160, 30), Color.White);

                //Draw number of coins
                m_spriteBatch.DrawString(font, "Coins: " + a_mainModel.getNumberOfCoins().ToString(), new Vector2(300, 30), Color.White);

                //Draw score
                m_spriteBatch.DrawString(font, "Score: " + a_mainModel.getScore().ToString(), new Vector2(440, 30), Color.White);

                if (m_showAnnouncement)
                {
                    Rectangle levelDestRect = new Rectangle(90, 280, 477, 93);
                    m_spriteBatch.Draw(m_nextLevelSign, levelDestRect, Color.White);

                }

                if(m_clearedAllLevels)
                {

                    Rectangle gameOverDestRect = new Rectangle(90, 200, 477, 93);
                    m_spriteBatch.Draw(m_gameOverSign, gameOverDestRect, Color.White);
                }

                foreach (ParticleSystem particleSystem in m_particleSystems)
                {
                    //Only update if particle system is still alive
                    if (particleSystem.getIsAlive())
                    {
                        particleSystem.UpdateAndDraw(a_elapsedTimeTotalSeconds, m_spriteBatch, m_camera, modelDisplacementY);
                    }

                }

                m_spriteBatch.End();
        }