示例#1
0
        public void MoveToPlayer(ObjectNew player)
        {
            Vector2 Player_Position = ConvertUnits.ToDisplayUnits(player.body.Position);

            Vector2 direction = Player_Position - ConvertUnits.ToDisplayUnits(body.Position);

            direction.Normalize();

            rotation = (float)Math.Atan2((double)direction.Y, (double)direction.X);

            if (Math.Abs(rotation - body.Rotation) > MathHelper.ToRadians(180))
            {
                if (rotation > body.Rotation)
                {
                    rotation -= MathHelper.ToRadians(360);
                }
                else
                {
                    rotation += MathHelper.ToRadians(360);
                }
            }

            if (body.Rotation > MathHelper.ToRadians(360))
            {
                body.Rotation -= MathHelper.ToRadians(360);
            }
            if (body.Rotation < MathHelper.ToRadians(0))
            {
                body.Rotation += MathHelper.ToRadians(360);
            }

            if (rotation > body.Rotation)
            {
                body.Rotation += MathHelper.ToRadians(speed_rotation);
            }
            if (rotation < body.Rotation)
            {
                body.Rotation -= MathHelper.ToRadians(speed_rotation);
            }



            // движение к персонажу
            body.ResetDynamics();

            body.ApplyLinearImpulse(new Vector2((float)Math.Sin(MathHelper.ToRadians(90) - body.Rotation) * 0.2f, (float)Math.Cos(MathHelper.ToRadians(90) - body.Rotation) * 0.2f));
        }
示例#2
0
        protected override void LoadContent()
        {
            camera              = new Camera(GraphicsDevice.Viewport);
            zombelest           = new List <ObjectNew>();
            spriteBatch         = new SpriteBatch(GraphicsDevice);
            this.IsMouseVisible = true;
            _screenCenter       = new Vector2(graphics.GraphicsDevice.Viewport.Width / 2f, graphics.GraphicsDevice.Viewport.Height / 2f);
            _view           = Matrix.Identity;
            _cameraPosition = Vector2.Zero;
            ConvertUnits.SetDisplayUnitToSimUnitRatio(200f);

            var texture       = Content.Load <Texture2D>("New_1");
            var groundtexture = Content.Load <Texture2D>("grass_tile");
            var textureZ      = Content.Load <Texture2D>("ugnd");

            tiles = new List <Tile>();
            for (int i = -10; i < 10; i++)
            {
                for (int j = -10; j < 10; j++)
                {
                    Tile tmp = new Tile(groundtexture, new Vector2(groundtexture.Width * i, groundtexture.Height * j));
                    // tiles.Add(tmp);
                }
            }
            //отрисовка только одного тайла
            Tile casetile = new Tile(groundtexture, new Vector2(0, 0));

            #region Ящики состоящие из двух 4х угольников с размером в 5 раз больше текстуры
            tiles.Add(casetile);
            test          = new TestS();
            test.Box_6txt = Content.Load <Texture2D>("box_2");
            Vertices vertices1 = new Vertices(4);
            vertices1.Add(ConvertUnits.ToSimUnits(new Vector2(-72, -29)) * 5f);
            vertices1.Add(ConvertUnits.ToSimUnits(new Vector2(-24, -60)) * 5f);
            vertices1.Add(ConvertUnits.ToSimUnits(new Vector2(19, -8)) * 5f);
            vertices1.Add(ConvertUnits.ToSimUnits(new Vector2(-37, 29)) * 5f);
            Vertices vertices2 = new Vertices(4);
            vertices2.Add(ConvertUnits.ToSimUnits(new Vector2(56, -14)) * 5f);
            vertices2.Add(ConvertUnits.ToSimUnits(new Vector2(81, 42)) * 5f);
            vertices2.Add(ConvertUnits.ToSimUnits(new Vector2(22, 64)) * 5f);
            vertices2.Add(ConvertUnits.ToSimUnits(new Vector2(-6, 12)) * 5f);

            List <Vertices> boxlist = new List <Vertices>(2);
            boxlist.Add(vertices1);
            boxlist.Add(vertices2);
            test.Box_6          = BodyFactory.CreateCompoundPolygon(_world, boxlist, 2f);
            test.Box_6.BodyType = BodyType.Static;
            test.Box_6.Position = ConvertUnits.ToSimUnits(new Vector2(600, 300));
            #endregion

            #region Границы по размеру 1 тайла земли
            test.Case = BodyFactory.CreateBody(_world);
            {
                Vertices casevert = new Vertices();
                casevert.Add(ConvertUnits.ToSimUnits(new Vector2(0, 0)));
                casevert.Add(ConvertUnits.ToSimUnits(new Vector2(0, 1190)));
                casevert.Add(ConvertUnits.ToSimUnits(new Vector2(1540, 1190)));
                casevert.Add(ConvertUnits.ToSimUnits(new Vector2(1540, 0)));
                casevert.Add(ConvertUnits.ToSimUnits(new Vector2(0, 0)));

                for (int i = 0; i < casevert.Count - 1; ++i)
                {
                    FixtureFactory.AttachEdge(casevert[i], casevert[i + 1], test.Case);
                }
            }
            test.Case.BodyType = BodyType.Static;
            #endregion

            #region Игрок - 8угольная фигура
            player = new ObjectNew(texture, new Vector2(600, 600), _world)
            {
                _Size = new Vector2(0.5f, 0.5f),
                input = new Input {
                    Left = Keys.A, Right = Keys.D, Up = Keys.W, Down = Keys.S
                },
                centreScreen = new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2),
            };
            Vertices bodyvert = new Vertices(8);
            bodyvert.Add(ConvertUnits.ToSimUnits(new Vector2(-90, 51)));
            bodyvert.Add(ConvertUnits.ToSimUnits(new Vector2(-59, -49)));
            bodyvert.Add(ConvertUnits.ToSimUnits(new Vector2(22, -104)));
            bodyvert.Add(ConvertUnits.ToSimUnits(new Vector2(60, -63)));
            bodyvert.Add(ConvertUnits.ToSimUnits(new Vector2(86, 4)));
            bodyvert.Add(ConvertUnits.ToSimUnits(new Vector2(77, 81)));
            bodyvert.Add(ConvertUnits.ToSimUnits(new Vector2(9, 105)));
            bodyvert.Add(ConvertUnits.ToSimUnits(new Vector2(-44, 109)));

            PolygonShape playershape = new PolygonShape(bodyvert, 1f);

            player.body = BodyFactory.CreateBody(_world);
            player.body.CreateFixture(playershape);

            player.body.BodyType    = BodyType.Dynamic;
            player.body.UserData    = "Player";
            player.body.Restitution = 0.3f;
            player.body.Friction    = 0.5f;
            player.body.Position    = new Vector2(5f, 5f);
            #endregion

            #region Player(old collision)
            //player = new ObjectNew(texture, new Vector2(600, 600), _world)
            //{
            //    _Size = new Vector2(0.3f, 0.3f),
            //    input = new Input { Left = Keys.A, Right = Keys.D, Up = Keys.W, Down = Keys.S },
            //    centreScreen = new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2),

            //};
            //player.body= BodyFactory.CreateCircle(_world, ConvertUnits.ToSimUnits(texture.Width / 2 * player._Size.X), 3f, ConvertUnits.ToSimUnits(new Vector2(600, 600)), BodyType.Dynamic);//)
            //player.body.UserData="Player";
            //player.body.Restitution = 0.3f;
            //player.body.Friction = 0.5f;
            #endregion

            #region Zombe(old collisions)
            for (int i = 0; i < 10; i++)
            {
                Random rnd = new Random();
                Thread.Sleep(20);
                Vector2 _position = new Vector2();
                //_position.X = rnd.Next(-1540*10, 1540 * 10);
                //_position.Y = rnd.Next(-1190*10, 1190 * 10);

                _position.X = rnd.Next(-100, 200);
                _position.Y = rnd.Next(-100, 200);

                zombie                  = new ObjectNew(textureZ, _position, _world);
                zombie.body             = BodyFactory.CreateCircle(_world, ConvertUnits.ToSimUnits(zombie.texture.Width / 2 * zombie._Size.X), 0.2f, ConvertUnits.ToSimUnits(_position), BodyType.Dynamic);
                zombie.body.Restitution = 0.3f;
                zombie.body.Friction    = 0.5f;
                zombelest.Add(zombie);
                zombie.body.UserData = "Zombie";
            }
            #endregion
        }