Пример #1
0
        public void ScreenOfFinalBuilding()
        {
            if (!IsColorArrayProcessed)
            {
                IsColorArrayProcessed = true;

                TimeSpan testTimer = new TimeSpan();
                DateTime curTime   = DateTime.Now;
                DateTime oldTime   = DateTime.Now;
                while (testTimer.TotalMilliseconds < 1000)
                {
                    curTime   = DateTime.Now;
                    testTimer = curTime - oldTime;
                }

                int w = ScreenManager.GraphicsDevice.PresentationParameters.BackBufferWidth;
                int h = ScreenManager.GraphicsDevice.PresentationParameters.BackBufferHeight;

                //pull the picture from the buffer
                int[] backBuffer = new int[w * h];
                ScreenManager.GraphicsDevice.GetBackBufferData(backBuffer);

                //copy into a texture
                Texture2D texture = new Texture2D(
                    ScreenManager.GraphicsDevice,
                    w, h,
                    false,
                    ScreenManager.GraphicsDevice.PresentationParameters.BackBufferFormat);
                texture.SetData(backBuffer);

                int     w1 = (int)((_silhouette._sprite.Size.X) / _silhouette.scaleKoef.X);
                int     h1 = (int)((_silhouette._sprite.Size.Y) / _silhouette.scaleKoef.Y);
                Vector2 positionInPixels      = (_silhouette._body.Position + new Vector2(this._halfWidth, this._halfHeight)) * 24f;
                Vector2 rectBorderOfObjCoords = new Vector2();
                rectBorderOfObjCoords.X = positionInPixels.X - (w1 / 2f);
                rectBorderOfObjCoords.Y = positionInPixels.Y + (h1 / 2f);
                Color[] colors = new Color[w1 * h1];
                texture.GetData <Color>(
                    0,
                    new Rectangle(
                        (int)(rectBorderOfObjCoords.X + 0.7f * (24f)), //0.7f * (24f) - ширина бордюра в пикселях
                        (int)(0.7f * (24f)),
                        w1,
                        h1),
                    colors,
                    0,
                    w1 * h1);


                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(w1, h1);
                Color[,] rawDataAsGrid = new Color[h1, w1];
                for (int row = 0; row < h1; row++)
                {
                    for (int column = 0; column < w1; column++)
                    {
                        // Assumes row major ordering of the array.
                        rawDataAsGrid[row, column] = colors[row * w1 + column];

                        if ((rawDataAsGrid[row, column].R == 61 &&
                             rawDataAsGrid[row, column].G == 96 &&
                             rawDataAsGrid[row, column].B == 119) ||
                            (rawDataAsGrid[row, column].R == 31 &&
                             rawDataAsGrid[row, column].G == 48 &&
                             rawDataAsGrid[row, column].B == 60)) //61 96 119
                        {
                            bitmap.SetPixel(
                                column,
                                row,
                                System.Drawing.Color.FromArgb(0, 255, 255, 255));
                        }
                        else
                        {
                            bitmap.SetPixel(
                                column,
                                row,
                                System.Drawing.Color.FromArgb(255, 0, 0, 0));
                        }

                        //bitmap.SetPixel(
                        //    column,
                        //    row,
                        //    System.Drawing.Color.FromArgb(
                        //    rawDataAsGrid[row, column].A,
                        //    rawDataAsGrid[row, column].R,
                        //    rawDataAsGrid[row, column].G,
                        //    rawDataAsGrid[row, column].B));
                    }
                }

                #region "Тестовый сейв скриншота"
                using (FileStream fs = new FileStream(@"../assets/results/3333.png", FileMode.Create, FileAccess.ReadWrite))
                {
                    bitmap.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
                }
                #endregion

                #region "saveAsFile"
                //System.Drawing.Bitmap bitmap2 = new System.Drawing.Bitmap(bitmap, w1 / 5, h1 / 5);
                //System.Drawing.Bitmap bitmapSource = new System.Drawing.Bitmap(
                //    new System.Drawing.Bitmap("C:\\Users\\space\\Рабочий стол\\TESTTESTTESTASSGDF\\building3.png"),
                //    w1 / 5,
                //    h1 / 5);
                //using (FileStream fs = new FileStream("C:\\Users\\space\\Рабочий стол\\TESTTESTTESTASSGDF\\11.png", FileMode.Create, FileAccess.ReadWrite))
                //{
                //    bitmap2.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
                //}
                //using (FileStream fs = new FileStream("C:\\Users\\space\\Рабочий стол\\TESTTESTTESTASSGDF\\22.png", FileMode.Create, FileAccess.ReadWrite))
                //{
                //    bitmapSource.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
                //}
                #endregion

                System.Drawing.Bitmap bitmapGame   = new System.Drawing.Bitmap(bitmap, w1 / 5, h1 / 5);
                System.Drawing.Bitmap bitmapSource = new System.Drawing.Bitmap(
                    new System.Drawing.Bitmap(this.pathToOriginalSilhouette),
                    w1 / 5,
                    h1 / 5);
                float coincidencesCount          = 0.001f;
                float countOfBlackPixelsInSource = 0.001f;
                for (int i = 0; i < bitmapGame.Height; i++)
                {
                    for (int j = 0; j < bitmapGame.Width; j++)
                    {
                        var c1 = bitmapGame.GetPixel(j, i);
                        var c2 = bitmapSource.GetPixel(j, i);
                        if (c1.R == c2.R && c2.R == (byte)0 &&
                            c1.G == c2.G && c2.G == (byte)0 &&
                            c1.B == c2.B && c2.B == (byte)0 &&
                            c1.A == c2.A && c2.A == (byte)255)
                        {
                            coincidencesCount++;
                        }

                        if (c2.A == 255)
                        {
                            countOfBlackPixelsInSource++;
                        }
                    }
                }

                float result = coincidencesCount / countOfBlackPixelsInSource;
                result = (float)Math.Round((double)result, 2);
                if (result > 0.8f)
                {
                    starCount = 5;
                }
                else if (result > 0.6f && result <= 0.8f)
                {
                    starCount = 4;
                }
                else if (result > 0.4f && result <= 0.6f)
                {
                    starCount = 3;
                }
                else if (result > 0.3f && result <= 0.4f)
                {
                    starCount = 2;
                }
                else if (result > 0.1f && result <= 0.3f)
                {
                    starCount = 1;
                }
                else
                {
                    starCount = 0;
                }

                texture.Dispose();

                resultMessage = new EndGameResultScreen(this.GetTitle(), this.GetDetails(), starCount);
                ScreenManager.AddScreen(resultMessage);

                #region "Сохранение результата"
                string resultFileContent    = Game1.PlayerGamesInfo;
                string resultFileContentUpd = "";
                //using (FileStream fs = new FileStream(
                //    Game1.SavePath,
                //    FileMode.OpenOrCreate,
                //    FileAccess.Read
                //    ))
                //{
                //    using (StreamReader sr = new StreamReader(fs))
                //    {
                //        resultFileContent = sr.ReadToEnd();
                //    }
                //}
                using (FileStream fs = new FileStream(
                           Game1.SavePath,
                           FileMode.Create,
                           FileAccess.Write
                           ))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        Regex regex = new Regex(this.lvl + ":\\d{1,3}\\?\\d{1,3};");
                        Match match = regex.Match(resultFileContent);
                        if (match.Success)
                        {
                            resultFileContentUpd = regex.Replace(resultFileContent, this.lvl + ":" + starCount + "?" + result * 100f + ";");
                        }
                        else
                        {
                            resultFileContentUpd = resultFileContent + (this.lvl + ":" + starCount + "?" + result * 100f + ";");
                        }

                        Game1.PlayerGamesInfo = resultFileContentUpd;
                        sw.WriteLine(resultFileContentUpd);
                    }
                }
                #endregion
            }
        }
Пример #2
0
        protected override void HandleCursor(InputHelper input)
        {
            #region "Манипуляции с блоками"
            Vector2 position = Camera.ConvertScreenToWorld(input.Cursor);//пиксели -> местные координаты???

            //захватить блок
            if ((/*input.IsNewButtonPress(Buttons.A) || */ input.IsNewMouseButtonPress(MouseButtons.LeftButton)) && _fixedMouseJoint == null)
            {
                tBlock = null;
                Fixture savedFixture = World.TestPoint(position);
                if (savedFixture != null)
                {
                    bodyToThrow = savedFixture.Body;
                    if ((tBlock = _blocks.FirstOrDefault(a => a.body == bodyToThrow)) != null)
                    {
                        //_throwTrajectory.SetBodyToThrow(tBlock);
                        //bodyToThrow.BodyType = BodyType.Dynamic;
                        _fixedMouseJoint          = new FixedMouseJoint(bodyToThrow, position);
                        _fixedMouseJoint.MaxForce = 1000.0f * bodyToThrow.Mass;
                        World.Add(_fixedMouseJoint);
                        bodyToThrow.Awake = true;

                        tBlock._blockState   = BlockState.Griped;
                        tBlock.fixedRotation = tBlock.body.Rotation;
                    }
                }
                else
                {
                    savedFixture = null;
                }
            }

            //перетаскивать блок
            if ((/*input.IsNewButtonRelease(Buttons.A) || */ input.IsNewMouseButtonRelease(MouseButtons.LeftButton)) && _fixedMouseJoint != null)
            {
                if (bodyToThrow == tBlock.body)
                {
                    if (tBlock._blockState == BlockState.Griped)
                    {
                        tBlock.body.AngularVelocity = 0f;
                    }

                    tBlock._blockState = BlockState.Throwed;
                }

                World.Remove(_fixedMouseJoint);
                _fixedMouseJoint = null;
            }

            //заморозить блок
            if (tBlock != null)
            {
                if (input.IsNewKeyPress(Keys.Space) && (tBlock._blockState == BlockState.Throwed || tBlock._blockState == BlockState.Griped))
                {
                    tBlock.body.BodyType = BodyType.Static;
                    //tBlock.body.SetCollisionCategories(Category.None);
                    //tBlock.body.SetCollidesWith(Category.None);

                    tBlock._blockState = BlockState.Throwed;
                }
            }

            //повернуть блок
            if (tBlock != null)
            {
                if ((input.KeyboardState.IsKeyDown(Keys.Z)) && tBlock._blockState == BlockState.Griped)
                {
                    tBlock.fixedRotation += 0.01f;
                }
                if ((input.KeyboardState.IsKeyDown(Keys.C)) && tBlock._blockState == BlockState.Griped)
                {
                    tBlock.fixedRotation -= 0.01f;
                }
            }

            //разморозить блоки
            if (input.IsNewKeyPress(Keys.F))
            {
                foreach (var e in _blocks)
                {
                    e.body.BodyType = BodyType.Dynamic;
                    e.body.SetCollisionCategories(Category.Cat1);
                    e.body.SetCollidesWith(Category.Cat1);
                }
            }

            ////создать блок
            //if (input.IsNewMouseButtonPress(MouseButtons.RightButton))
            //{
            //    string blockPath = "";
            //    Vector2 sizeScale = new Vector2(0.005f, 0.005f);
            //    float massKoef = 10f;
            //    var blockNum = rnd.Next(1, 100);
            //    if (/*blockNum <= 99 && blockNum > 70*/input.IsNewKeyPress(Keys.Y))
            //    {
            //        blockPath = "gameObjs\\wood-plank";
            //        var tempRnd = (float)(rnd.Next(1, 5));
            //        sizeScale *= new Vector2(tempRnd, 1f);
            //    }
            //    else if (blockNum <= 70 && blockNum > 65)
            //    {
            //        blockPath = "gameObjs\\wood-circle";
            //        //var tempRnd = (float)(rnd.Next(2, 3));
            //        //sizeScale = new Vector2(0.4f, 0.25f);
            //    }
            //    else if (blockNum <= 65 && blockNum > 50)
            //    {
            //        blockPath = "gameObjs\\wood-corner";
            //        //sizeScale = new Vector2(0.4f, 0.1f);
            //    }
            //    else if (blockNum <= 50 && blockNum > 40)
            //    {
            //        blockPath = "gameObjs\\wood-corner2";
            //        //sizeScale = new Vector2(0.4f, 0.1f);
            //    }
            //    else if (blockNum <= 40 && blockNum > 10)
            //    {
            //        blockPath = "gameObjs\\wood-halfPlank";
            //        //sizeScale = new Vector2(0.4f, 0.05f);
            //    }
            //    else
            //    {
            //        blockPath = "gameObjs\\bootilka1";
            //        //sizeScale = new Vector2(0.1f, 0.3f);
            //        massKoef = 1f;
            //    }

            //    _blocks.Add(new WoodBlock(
            //        World,
            //        ScreenManager,
            //        new Vector2(-15f, -5f),
            //        Camera,
            //        blockPath,
            //        TriangulationAlgorithm.Bayazit,
            //        sizeScale,
            //        50f,
            //        massKoef));
            //    _blocks.Last()._blockState = BlockState.Created;
            //}

            //////TEST
            if (input.IsNewKeyPress(Keys.Y))
            {
                string  blockPath = "";
                Vector2 sizeScale = new Vector2(0.005f, 0.005f);
                float   massKoef  = 10f;
                var     blockNum  = rnd.Next(1, 100);

                blockPath = "gameObjs\\wood-plank";
                var tempRnd = (float)(rnd.Next(1, 5));
                sizeScale *= new Vector2(tempRnd, 1f);

                _blocks.Add(new WoodBlock(
                                World,
                                ScreenManager,
                                new Vector2(-15f, -5f),
                                Camera,
                                blockPath,
                                TriangulationAlgorithm.Bayazit,
                                sizeScale,
                                50f,
                                massKoef));
                _blocks.Last()._blockState = BlockState.Created;
            }
            else if (input.IsNewKeyPress(Keys.U))
            {
                string  blockPath = "";
                Vector2 sizeScale = new Vector2(0.005f, 0.005f);
                float   massKoef  = 10f;
                var     blockNum  = rnd.Next(1, 100);

                blockPath = "gameObjs\\wood-circle";
                //var tempRnd = (float)(rnd.Next(2, 3));
                //sizeScale = new Vector2(0.4f, 0.25f);

                _blocks.Add(new WoodBlock(
                                World,
                                ScreenManager,
                                new Vector2(-15f, -5f),
                                Camera,
                                blockPath,
                                TriangulationAlgorithm.Bayazit,
                                sizeScale,
                                50f,
                                massKoef));
                _blocks.Last()._blockState = BlockState.Created;
            }
            else if (input.IsNewKeyPress(Keys.I))
            {
                string  blockPath = "";
                Vector2 sizeScale = new Vector2(0.005f, 0.005f);
                float   massKoef  = 10f;
                var     blockNum  = rnd.Next(1, 100);

                blockPath = "gameObjs\\wood-corner";
                //sizeScale = new Vector2(0.4f, 0.1f);

                _blocks.Add(new WoodBlock(
                                World,
                                ScreenManager,
                                new Vector2(-15f, -5f),
                                Camera,
                                blockPath,
                                TriangulationAlgorithm.Bayazit,
                                sizeScale,
                                50f,
                                massKoef));
                _blocks.Last()._blockState = BlockState.Created;
            }
            else if (input.IsNewKeyPress(Keys.O))
            {
                string  blockPath = "";
                Vector2 sizeScale = new Vector2(0.005f, 0.005f);
                float   massKoef  = 10f;
                var     blockNum  = rnd.Next(1, 100);

                blockPath = "gameObjs\\wood-corner2";
                //sizeScale = new Vector2(0.4f, 0.1f);

                _blocks.Add(new WoodBlock(
                                World,
                                ScreenManager,
                                new Vector2(-15f, -5f),
                                Camera,
                                blockPath,
                                TriangulationAlgorithm.Bayazit,
                                sizeScale,
                                50f,
                                massKoef));
                _blocks.Last()._blockState = BlockState.Created;
            }
            else if (input.IsNewKeyPress(Keys.P))
            {
                string  blockPath = "";
                Vector2 sizeScale = new Vector2(0.005f, 0.005f);
                float   massKoef  = 10f;
                var     blockNum  = rnd.Next(1, 100);

                blockPath = "gameObjs\\wood-halfPlank";
                //sizeScale = new Vector2(0.4f, 0.05f);

                _blocks.Add(new WoodBlock(
                                World,
                                ScreenManager,
                                new Vector2(-15f, -5f),
                                Camera,
                                blockPath,
                                TriangulationAlgorithm.Bayazit,
                                sizeScale,
                                50f,
                                massKoef));
                _blocks.Last()._blockState = BlockState.Created;
            }
            else if (input.IsNewKeyPress(Keys.T))
            {
                string  blockPath = "";
                Vector2 sizeScale = new Vector2(0.005f, 0.005f);
                float   massKoef  = 10f;
                var     blockNum  = rnd.Next(1, 100);

                blockPath = "gameObjs\\bootilka1";
                //sizeScale = new Vector2(0.1f, 0.3f);
                massKoef = 1f;

                _blocks.Add(new WoodBlock(
                                World,
                                ScreenManager,
                                new Vector2(-15f, -5f),
                                Camera,
                                blockPath,
                                TriangulationAlgorithm.Bayazit,
                                sizeScale,
                                50f,
                                massKoef));
                _blocks.Last()._blockState = BlockState.Created;
            }
            //////////////

            if (_fixedMouseJoint != null)
            {
                _fixedMouseJoint.WorldAnchorB = position;
            }
            #endregion

            #region "Снимок и обработка"
            if (input.IsNewKeyPress(Keys.Q))
            {
                HasCursor = false;
                IsGameEnd = true;
            }

            //вывести звезды
            if (input.IsNewKeyPress(Keys.E))
            {
                resultMessage = new EndGameResultScreen(this.GetTitle(), this.GetDetails(), starCount);

                ScreenManager.AddScreen(resultMessage);
            }
            #endregion
        }