Exemple #1
0
    /// <summary>
    /// Awake is called when the script instance is being loaded.
    /// </summary>
    void Awake()
    {
        // find all players
        GameObject[] playerGO = GameObject.FindGameObjectsWithTag("PlayerInfo");

        PlayerList = new PlayerInfo[playerGO.Length];

        for (int i = 0; i < playerGO.Length; i++)
        {
            PlayerInfo pInfo = playerGO[i].GetComponent <PlayerInfo>();

            // each player shoudl have their own slot
            Debug.Assert(PlayerList[pInfo.Slot] == null, "Found multiple PlayerInfos with the same slot");

            PlayerList[pInfo.Slot] = pInfo;
        }

        // find all paddles
        GameObject[] paddleGO = GameObject.FindGameObjectsWithTag("Player");

        // Setup player stuff
        foreach (PlayerInfo pInfo in PlayerList)
        {
            if (pInfo.IsAI)
            {
                PlayerPaddle paddle = paddleGO[pInfo.Slot].GetComponent <PlayerPaddle>();

                // paddle.gameObject.AddComponent(typeof(PaddleAI));
                paddle.gameObject.GetComponent <PaddleAI>().enabled = true;
            }
        }

        // subscribe to event
        GameEventManager.OnGoal += SpawnBallOnGoal;
    }
 // Use this for initialization
 void Start()
 {
     //set the paddle script to the PlayerPaddle object for prefab purposes.
     paddle = GameObject.FindObjectOfType <PlayerPaddle>();
     //calculate paddle to ball offset
     paddleToBallVector = this.transform.position - paddle.transform.position;
 }
Exemple #3
0
    public Ball Create(PlayerPaddle paddle, Vector2 position)
    {
        var newBall = Instantiate <Ball>(this, position, transform.rotation);

        newBall.AttachPaddle(paddle);
        newBall.Lock();
        return(newBall);
    }
Exemple #4
0
    private void ResetBall(GameObject ball)
    {
        Destroy(ball);
        GameObject   paddleObj    = GameObject.Find("PlayerPaddle");
        PlayerPaddle playerScript = paddleObj.GetComponent <PlayerPaddle> ();

        playerScript.SpawnBall();
    }
 private void ResetGame()
 {
     paddlePlayer = new PlayerPaddle(40, (int)(SceneManager.WindowHeight * 0.5));
     paddlePlayer.Init();
     paddleAI = new AIPaddle(SceneManager.WindowWidth - 40, (int)(SceneManager.WindowHeight * 0.5));
     paddleAI.Init();
     ball = new Ball((int)(SceneManager.WindowWidth * 0.5), (int)(SceneManager.WindowHeight * 0.5));
     ball.Init();
 }
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         Ball?.Dispose();
         PlayerPaddle?.Dispose();
         AiPaddle?.Dispose();
         ServeBallHandler?.Dispose();
     }
 }
Exemple #7
0
    /// <summary>
    /// Awake is called when the script instance is being loaded.
    /// </summary>
    void Awake()
    {
        // Subscript / register for Event
        GameEventManager.OnNewBall    += AddBall;
        GameEventManager.OnRemoveBall += RemoveBall;

        _paddle = GetComponent <PlayerPaddle>();

        Target = gameObject.transform.position;
    }
Exemple #8
0
    private void manageCollision(Collision2D collision)
    {
        PlayerPaddle script = collision.collider
                              .GetComponent <PlayerPaddle>();

        if (script.isLeft != ownerIsLeft)
        {
            destroyBullet();
            collision.collider
            .gameObject.SendMessage(
                "damageShield", BULLET_DAMAGE, SendMessageOptions.RequireReceiver);
        }
    }
Exemple #9
0
        private void GameLoop(object sender, EventArgs e)
        {
            _currentTick++;
            PlayerPaddle.UpdatePosition(WallSize, GameAreaWidth - WallSize);

            CheckCollectables();
            CheckBalls();

            // Constantly increase speed as the game goes on
            if (_currentTick % 200 == 0)
            {
                IncreaseSpeed(0.1);
            }
        }
    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.GetComponent <Prisoner>())
        {
            //Do nothing if there is no time of confusion
            if (confusionTime <= 0)
            {
                return;
            }

            //Find player paddle
            PlayerPaddle playerPaddle = GameObject.FindGameObjectWithTag("Prisoner Paddle").GetComponent <PlayerPaddle>();
            playerPaddle.SetBeingConfused(confusionTime);
        }
    }
    private void AddBall()
    {
        Scoreboard.text = "Player: " + playerScore + " AI: " + AIScore;

        if (ball != null)
        {
            Destroy(ball.gameObject);
        }

        ball = Instantiate(BallPrefab,
                           PlayerPaddle.transform.position + PlayerPaddle.forward * 0.5f,
                           Quaternion.identity, PlayerPaddle.transform);

        PlayerPaddle.GetComponent <PlayerPaddleController>().Ball = ball;
        AIPaddle.GetComponent <AIPaddleController>().Ball         = ball;
    }
Exemple #12
0
        /// <summary>
        /// Constructor defining the different parameters used in the game loop.
        /// </summary>
        /// <param name="form"></param>
        /// <param name="paddleModel"></param>
        /// <param name="paddleView"></param>
        /// <param name="ballModel"></param>
        /// <param name="ballView"></param>
        /// <param name="targetBlocksCollection"></param>
        /// <param name="messageHandler"></param>
        public GameplayController(GameplayForm form, PlayerPaddle paddleModel, Control paddleView, Ball ballModel,
                                  Control ballView, TargetBlocksCollection targetBlocksCollection, IMessageHandler messageHandler)
        {
            this.form         = form;
            this.paddleModel  = paddleModel;
            this.paddleView   = paddleView;
            this.ballModel    = ballModel;
            this.ballView     = ballView;
            this.targetBlocks = targetBlocksCollection;

            this.targetBlocks.BallSpeedToIncrease += ballModel.OnBallSpeedIncreased;
            this.targetBlocks.EndGame             += messageHandler.OnEndGame;
            this.targetBlocks.EndGame             += form.OnEndGame;
            this.ballModel.EndGame += messageHandler.OnEndGame;
            this.ballModel.EndGame += form.OnEndGame;

            BindModelsToViews();
        }
Exemple #13
0
        private void GameplayForm_Load(object sender, EventArgs e)
        {
            PlayerPaddle paddleModel = new PlayerPaddle(this.Width, this.Height, this.Width, this.Height);
            int          ballX       = this.Width / 2 - 15;
            int          ballY       = this.Height / 2 + 50;
            Ball         ballModel   = new Ball(ballX, ballY, this.Width, this.Height);

            pictureBoxPaddle.Size = new Size(paddleModel.Width, paddleModel.Height);
            pictureBoxBall.Size   = new Size(ballModel.Width, ballModel.Height);

            TargetBlocksCollection targetBlocksCollection = GenerateTargetBlocks();

            SetMessageLocations();
            MessageHandler messageHandler = new MessageHandler(this.labelStatus, this.labelMessage, this.labelScore);

            this.gameplayController = new GameplayController(this, paddleModel, pictureBoxPaddle, ballModel,
                                                             this.pictureBoxBall, targetBlocksCollection, messageHandler);

            Application.Idle += RunLoop;
            loopIsRunning     = true;
        }
Exemple #14
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            _player = new PlayerPaddle(_court);
            _player.Texture = Content.Load<Texture2D>("paddle");
            _player.StartingPosition();

            _ai = new AIPaddle(_court);
            _ai.Texture = Content.Load<Texture2D>("paddle");
            _ai.StartingPosition();

            _ball = new Ball(_court);
            _ball.Texture = Content.Load<Texture2D>("ball");
            _ball.StartingPosition(_court);

            _splash = Content.Load<Texture2D>("splash");
            _font = Content.Load<SpriteFont>("ScoreFont");
            _gameEndHeadingFont = Content.Load<SpriteFont>("GameEndHeading");

            _paddleCollidePlayer = Content.Load<SoundEffect>("paddle_collide_player");
            _paddleCollideAi = Content.Load<SoundEffect>("paddle_collide_ai");
            _wallCollide = Content.Load<SoundEffect>("wall_collide");
            _aiScoreSound = Content.Load<SoundEffect>("disappointed_crowd_idiot");
            _playerScoreSound = Content.Load<SoundEffect>("pleased_crowd_about_time");

            _meanGreen = new Color(45, 198, 13);
            _creditsFont = Content.Load<SpriteFont>("CreditsFont");

            var creditsYSpacing = _creditsFont.MeasureString("X").Y * 0.5f ;
            var creditLineSizes = _creditLines.ConvertAll(x => _creditsFont.MeasureString(x));

            var creditsBaseY = _court.Height;

            for (int i = 0; i < _creditLines.Count; i++)
            {
                _creditsBaseByLine.Add(new Vector2(_court.Width / 2 - creditLineSizes[i].X / 2,
                    creditsBaseY + ((creditLineSizes[i].Y * i) + i * creditsYSpacing)));
            }

            _particleEffect = new ParticleEffect();
            _particleEffect.Texture = Content.Load<Texture2D>("spark02");
            _logger.Initialize(new GameTime(), 1000.0f / 15.0f);
            _gameState = GameState.Playing;
        }
Exemple #15
0
        /// <summary>
        /// Checks interactions between balls and other game instances (death triggers, player paddle, walls, bricks).
        /// TODO: Could be optimized - when ball is moving from a certain position in a certain direction,
        /// not all bricks have to be checked for collision.
        /// </summary>
        private void CheckBalls()
        {
            for (int i = 0; i < Balls.Count; i++)
            {
                var ball = Balls[i];
                if (ball.IsPinned)
                {
                    Vector pinnedPosition = PlayerPaddle.GetPinnedPosition();
                    ball.X = pinnedPosition.X - ball.Width / 2;
                    ball.Y = pinnedPosition.Y - ball.Height;
                    continue;
                }
                ball.UpdatePosition();

                Vector hitDirection = new Vector();

                // Interaction with player paddle
                if (ball.Intersect(PlayerPaddle, ref hitDirection))
                {
                    ball.Bounce(ref hitDirection);
                    if (PlayerPaddle.CurrentPlayerDirection == Paddle.PlayerDirection.left)
                    {
                        ball.DeflectX(-0.85);
                    }
                    else if (PlayerPaddle.CurrentPlayerDirection == Paddle.PlayerDirection.right)
                    {
                        ball.DeflectX(0.85);
                    }
                }

                // Interaction with walls
                foreach (var wall in CurrentLevel.Walls)
                {
                    if (ball.Intersect(wall, ref hitDirection))
                    {
                        ball.Bounce(ref hitDirection);
                    }
                }

                // Interaction with death triggers
                foreach (var trigger in CurrentLevel.DeathTriggers)
                {
                    if (ball.Intersect(trigger, ref hitDirection))
                    {
                        Balls.Remove(ball);
                        if (Balls.Count < 1)
                        {
                            for (int j = Collectables.Count - 1; j >= 0; j--)
                            {
                                Collectables.RemoveAt(i);
                            }
                            RemoveLife();
                        }
                        continue;
                    }
                }

                // Interaction with bricks
                foreach (var brickRow in CurrentLevel.BrickRows)
                {
                    for (int j = brickRow.Count - 1; j >= 0; j--)
                    {
                        Brick brick = brickRow[j];

                        if (ball.Intersect(brick, ref hitDirection))
                        {
                            brick.Hit(ball.Damage);
                            if (!brick.Alive())
                            {
                                CurrentPlayerStats.Score += brick.BrickPoints;

                                int rnd = _randomGenerator.Next(0, 100);
                                if (rnd < 10)
                                {
                                    Collectables.Add(new Collectable(Collectable.CollectableType.clone_all, brick.X, brick.Y));
                                }
                                else if (rnd > 10 && rnd < 15)
                                {
                                    Collectables.Add(new Collectable(Collectable.CollectableType.increase_width, brick.X, brick.Y));
                                }
                                else if (rnd > 15 && rnd < 20)
                                {
                                    Collectables.Add(new Collectable(Collectable.CollectableType.decrease_width, brick.X, brick.Y));
                                }
                                else if (rnd > 20 && rnd < 25)
                                {
                                    Collectables.Add(new Collectable(Collectable.CollectableType.decrease_speed, brick.X, brick.Y));
                                }
                                else if (rnd > 25 && rnd < 30)
                                {
                                    Collectables.Add(new Collectable(Collectable.CollectableType.add_life, brick.X, brick.Y));
                                }
                                else if (rnd > 30 && rnd < 35)
                                {
                                    Collectables.Add(new Collectable(Collectable.CollectableType.increase_damage, brick.X, brick.Y));
                                }


                                brickRow.Remove(brick);

                                if (CurrentLevel.IsAllEmpty())
                                {
                                    NextLevel();
                                }
                            }
                            ball.Bounce(ref hitDirection);
                        }
                    }
                }
            }
        }
Exemple #16
0
 public void AttachPaddle(PlayerPaddle paddle)
 {
     this.paddle = paddle;
 }
 public void MovePaddle(float direction, PlayerPaddle paddle)
 {
     paddle.SetDirection(direction);
 }