Esempio n. 1
0
        public override void Initialize()
        {
            base.Initialize();

            //var pHost = Game.Host;

            mPlayerLeft = new PaddleInputObj(this)
            {
                Transform = new Transform2d {
                    Position = new Vector2(10, Game.Height / 2 - paddleHeight / 2), Scale = new Vector2(paddleWidth, paddleHeight)
                },
                Color    = "#FFF",
                AxisName = "Vertical1"
            };
            mPlayerRight = new PaddleInputObj(this)
            {
                Transform = new Transform2d {
                    Position = new Vector2(Game.Width - (paddleWidth + 10), Game.Height / 2 - paddleHeight / 2), Scale = new Vector2(paddleWidth, paddleHeight)
                },
                Color    = "#FFF",
                AxisName = "Vertical2"
            };
            mScoreLeft = new ScoreObj(this)
            {
                Transform = new Transform2d {
                    Position = new Vector2(Game.Width / 4, Game.Height / 6)
                },
                Paddle = mPlayerLeft
            };
            mScoreRight = new ScoreObj(this)
            {
                Transform = new Transform2d {
                    Position = new Vector2(3 * Game.Width / 4, Game.Height / 6)
                },
                Paddle = mPlayerRight
            };
            mNetObj = new NetObj(this)
            {
                Transform = new Transform2d {
                    Position = new Vector2(Game.Width / 2 - netWidth / 2, 0), Scale = new Vector2(netWidth, netHeight)
                }
            };
            mBallObj = new BallObj(this)
            {
                Transform = new Transform2d {
                    Position = new Vector2(Game.Width / 2, Game.Height / 2)
                },
                Color    = "#05EDFF",
                Radius   = 7,
                Speed    = 7 * factor,
                Velocity = new Vector2(5 * factor, 5 * factor)
            };
        }
Esempio n. 2
0
        private bool CollisionPlayerBall(PaddleObj argPlayerObj, BallObj argBallObj, bool argPlayerRight)
        {
            var pBallY   = argBallObj.Transform.Position.Y;
            var pPlayerY = argPlayerObj.Transform.Position.Y;
            var pAssets  = Game.GetAssets <PingPongAssets>();

            if
            (
                argPlayerObj.Transform.Position.X <= argBallObj.Transform.Position.X && argPlayerObj.Transform.Position.X + argPlayerObj.Transform.Scale.X >= argBallObj.Transform.Position.X &&
                pPlayerY <= pBallY && pPlayerY + argPlayerObj.Transform.Scale.Y >= pBallY
            )
            {
                var pAng   = 0.0f;
                var pDir   = (argPlayerRight) ? -1.0f : 1.0f;
                var pRad_2 = argBallObj.Radius / 2.0f;
                var pY_2   = pPlayerY / 2.0f;

                if (pBallY < pY_2 - pRad_2)
                {
                    pAng = MathF.PI / -4.0f;
                }
                else if (pBallY > pY_2 + pRad_2)
                {
                    pAng = MathF.PI / 4.0f;
                }

                argBallObj.Velocity = pDir * argBallObj.Speed * new Vector2(MathF.Cos(pAng), MathF.Sin(pAng));

                if (pAssets != null)
                {
                    Game.Host.playSound(pAssets.hitSound);
                }

                return(true);
            }

            return(false);
        }