Пример #1
0
        public void initialize()
        {
            backgrounds = new Texture2D[1];
            backgrounds[0] = Content.Load<Texture2D>(map + "background");
            foreground = Content.Load<Texture2D>(map + "foreground");

            vertices = new Vertex[13];
            vertices[0] = new Vertex(new Vector2(80, 300));
            vertices[1] = new Vertex(new Vector2(200, 480));
            vertices[2] = new Vertex(new Vector2(400, 480));
            vertices[3] = new Vertex(new Vector2(550, 300));
            vertices[4] = new Vertex(new Vector2(140, 240));
            vertices[5] = new Vertex(new Vector2(180, 240));
            vertices[6] = new Vertex(new Vector2(140, 280));
            vertices[7] = new Vertex(new Vector2(180, 280));
            vertices[8] = new Vertex(new Vector2(420, 280));
            vertices[9] = new Vertex(new Vector2(420, 240));
            vertices[10] = new Vertex(new Vector2(460, 240));
            vertices[11] = new Vertex(new Vector2(460, 280));
            vertices[12] = new Vertex(new Vector2(300, 450));

            vertices[0].createLink(vertices[1]);
            vertices[1].createLink(vertices[2]);
            vertices[2].createLink(vertices[3]);

            vertices[4].createLink(vertices[5]);
            vertices[4].createLink(vertices[6]);
            vertices[5].createLink(vertices[7]);
            vertices[6].createLink(vertices[7]);

            vertices[8].createLink(vertices[9]);
            vertices[8].createLink(vertices[11]);
            vertices[9].createLink(vertices[10]);
            vertices[10].createLink(vertices[11]);

            vertices[1].createLink(vertices[12]);
            vertices[2].createLink(vertices[12]);

            gravity = new Vector2(0, 0.5f);
        }
Пример #2
0
        public void update(KeyboardState keyboardState, Vector2 gravity, Vertex[] vertices)
        {
            velocity += gravity;
            if (keyboardState.IsKeyDown(Keys.A))
            {
                velocity.X -= 1f;
            }
            if (keyboardState.IsKeyDown(Keys.D))
            {
                velocity.X += 1f;
            }
            if (velocity != new Vector2(0, 0))
            {
                velocity /= 1.2f;
            }
            position += velocity;

            #region collisionDetection
            Matrix toonMatrix = Matrix.CreateTranslation(position.X, position.Y, 0);

            for (int i = 0; i < vertices.Length; i++)
            {
                Vector2 startingPosition = vertices[i].getPosition();
                startingPosition.X += 10f;
                startingPosition.Y += 10f;

                for (int j = 0; j < vertices[i].getLinks().Count; j++)
                {
                    float distanceBetweenBothPoints = Vector2.Distance(vertices[i].getPosition(), vertices[i].getLinks()[j].getPosition());

                    #region EfficiencyAlgorithm
                    if ((position.X < (startingPosition.X - distanceBetweenBothPoints)) || (position.X > (startingPosition.X + distanceBetweenBothPoints)))
                    {
                        break;
                    }

                    if ((position.Y < (startingPosition.Y - distanceBetweenBothPoints)) || (position.Y > (startingPosition.Y + distanceBetweenBothPoints)))
                    {
                        break;
                    }
                    #endregion

                    Vector2 slope = new Vector2(vertices[i].getLinks()[j].getPosition().X - vertices[i].getPosition().X, vertices[i].getLinks()[j].getPosition().Y - vertices[i].getPosition().Y);
                    slope /= distanceBetweenBothPoints;
                    for (int k = 0; k < distanceBetweenBothPoints; k++)
                    {
                        Vector2 pixel = startingPosition + slope * k;
                        for (int l = 0; l < 32; l++)
                        {
                            for (int m = 0; m < 32; m++)
                            {
                                Vector2 mockPosition = new Vector2((int)position.X+l, (int)position.Y+m);
                                Vector2 mockPixel = new Vector2((int)pixel.X, (int)pixel.Y);

                                if (mockPosition == mockPixel)
                                {
                                    if (test)
                                    {
                                        Console.WriteLine("collision! - " + position);
                                        test = false;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            #endregion

            #region testShit
            if (keyboardState.IsKeyDown(Keys.Space))
            {
                if (velocity.Y > 0)
                {
                    velocity = new Vector2(0, -25);
                }
                test = true;
            }
            #endregion
        }
Пример #3
0
 public void createLink(Vertex link)
 {
     links.Add(link);
 }