コード例 #1
0
ファイル: Player.cs プロジェクト: Lautenschlager-id/HeadWars
        // Regions
        #region Actions
        public void ShootBullet()
        {
            Vector2 dir = Control.getAimCoordinate();

            if (dir.LengthSquared() > 0 && shootCooldown <= 0)
            {
                shootCooldown = (int)shootTimer.Value;
                float aimAngle = dir.Angle();

                // Creates an angle discrepancy for the bullet
                float   angleDiscrepancy = Maths.random.RandomRange(-bulletDiscrepancy, bulletDiscrepancy) + Maths.random.RandomRange(-bulletDiscrepancy, bulletDiscrepancy);
                Vector2 vel = Maths.polarToVector(aimAngle + angleDiscrepancy, 11f);

                // Rotate the initial position of the object in the direction it's moving
                Quaternion eulerAim = Quaternion.CreateFromYawPitchRoll(0, 0, aimAngle);

                // Bullets
                for (int q = 0; q < (int)bulletAmount.Value; q++)
                {
                    int     y      = (q + 1) * (8 + q);
                    Vector2 offset = Vector2.Transform(new Vector2(50, y * alterPosition), eulerAim);
                    EntityManager.New(new Bullet(position + offset, vel));
                    alterPosition *= -1;
                }
            }
        }
コード例 #2
0
        /// Moves randomly
        IEnumerable <int> RandMove()
        {
            // Won't be const as it will change periodically
            // 0 - 360 in rad
            float dir = Maths.random.RandomRange(0, MathHelper.TwoPi);

            Rectangle border = HeadWars.ViewPort.Bounds;

            border.Inflate(-sprite.Width, -sprite.Height);

            while (true)
            {
                dir += Maths.random.RandomRange(-.5f, .5f);
                // MathHelper.WrapAngle normalizes the angle in a range [-π : π]
                dir = MathHelper.WrapAngle(dir);

                for (int i = 0; i < 6; i++)
                {
                    velocity += Maths.polarToVector(dir, .4f);
                    angle    -= .05f;

                    if (!border.Contains(position.asPoint()))
                    {
                        dir = (HeadWars.ScreenDimension / 2 - position).Angle() + Maths.random.RandomRange(-MathHelper.PiOver2, MathHelper.PiOver2);
                    }

                    yield return(0);
                }
            }
        }