예제 #1
0
파일: TankGame.cs 프로젝트: Stran6/TankGame
        private void takeTurn(GameTime time)
        {
            if (currentTank.turnTime > 0 & !shotFired)
            {
                if (currentTank.inventory.getShotCount(currentTank.type) < 1)
                {
                    currentTank.type = currentTank.inventory.findNextShotType();
                }

                if (currentTank.moveLimit > 0)
                {
                    currentTank.move(time);
                }

                if ((currentTank.tankPosition.X > 180 && currentTank.tankPosition.X < 468) || (currentTank.tankPosition.X > 1160 && currentTank.tankPosition.X < 1260))
                {
                    currentTank.rotateTurret(2, -1);
                }
                else if ((currentTank.tankPosition.X > 705 && currentTank.tankPosition.X < 950) || (currentTank.tankPosition.X > 1540 && currentTank.tankPosition.X < 1795))
                {
                    currentTank.rotateTurret(1, -2);
                }
                else
                {
                    currentTank.rotateTurret(Math.PI / 2, -Math.PI / 2);
                }

                if (screenManager.currentState == ScreenManager.GameState.PLAYER1)
                {
                    cameraFollow(player1Tank.getTankPos());
                }
                else if (screenManager.currentState == ScreenManager.GameState.PLAYER2)
                {
                    cameraFollow(player2Tank.getTankPos());
                }

                playerControls();

                currentTank.turnTime -= (float)time.ElapsedGameTime.TotalSeconds;
                screenManager.updateTime((int)currentTank.turnTime, (int)currentTank.moveLimit);

                if (currentTank.turnTime < 0 & !(chargingShot || chargingShot2))
                {
                    turnOver = true;
                }
            }

            else if (chargingShot || chargingShot2)
            {
                currentTank.shoot(power);
                chargingShot  = false;
                chargingShot2 = false;
                shotFired     = true;
                setPreviousShotLine();
            }

            if (shotFired & !shotCollided)
            {
                if (currentTank.firstShot)
                {
                    currentTank.firstShot = false;
                }

                cameraFollow(currentTank.bullet.getBulletPosition());

                if (currentTank.bullet.type == BulletType.ScatterShot)
                {
                    if ((Keyboard.GetState().IsKeyDown(Keys.Space) || controller.IsButtonDown(Buttons.RightTrigger)) & !
                        ((ScatterShot)currentTank.bullet).scattered)
                    {
                        ((ScatterShot)currentTank.bullet).scatter();
                    }
                }

                #region MissileDropCollision

                if (currentTank.bullet.type == BulletType.MissileDrop && ((MissileDrop)currentTank.bullet).targetLockedOn)
                {
                    Missile currentMissile = ((MissileDrop)currentTank.bullet).missile;

                    if (currentMissile.intersectPixels(player1Tank.getTankRect(), player1Tank.tankTextureData) && currentTank != player1Tank ||
                        currentMissile.intersectPixels(player2Tank.getTankRect(), player2Tank.tankTextureData) && currentTank != player2Tank ||
                        currentMissile.intersectPixels(background.layers[1].Sprites[0].Position, background.terrainTextureData) ||
                        currentMissile.outOfBounds)
                    {
                        currentMissile.bulletCollided();

                        shotCollided = true;
                        shotPause    = 2.0f;
                    }

                    if (currentMissile.explosion != null)
                    {
                        if (currentTank != player1Tank)
                        {
                            if (currentMissile.explosion.collisionRectangle.Intersects(player1Tank.collisionRect) & !currentMissile.damageDealt)
                            {
                                player1Tank.takeDamage(currentMissile.damage);
                                GamePad.SetVibration(PlayerIndex.One, 1.0f, 1.0f);
                                uiManager.floatingDamageText(currentMissile.damage.ToString(), currentMissile.getBulletPosition());
                                currentMissile.damageDealt = true;
                            }
                        }

                        if (currentTank != player2Tank)
                        {
                            if (currentMissile.explosion.collisionRectangle.Intersects(player2Tank.collisionRect) & !currentMissile.damageDealt)
                            {
                                player2Tank.takeDamage(currentMissile.damage);
                                GamePad.SetVibration(PlayerIndex.Two, 1.0f, 1.0f);
                                uiManager.floatingDamageText(currentMissile.damage.ToString(), currentMissile.getBulletPosition());
                                currentMissile.damageDealt = true;
                            }
                        }
                    }
                }

                #endregion MissileDropCollision

                #region ScatterShotCollision

                else if (currentTank.bullet.type == BulletType.ScatterShot && ((ScatterShot)currentTank.bullet).scattered)
                {
                    foreach (Shots s in ((ScatterShot)currentTank.bullet).scatterShots)
                    {
                        if (s.intersectPixels(player1Tank.getTankRect(), player1Tank.tankTextureData) && currentTank != player1Tank ||
                            s.intersectPixels(player2Tank.getTankRect(), player2Tank.tankTextureData) && currentTank != player2Tank ||
                            s.intersectPixels(background.layers[1].Sprites[0].Position, background.terrainTextureData) ||
                            s.outOfBounds)
                        {
                            s.bulletCollided();
                        }

                        if (s.explosion != null)
                        {
                            if (currentTank != player1Tank)
                            {
                                if (s.explosion.collisionRectangle.Intersects(player1Tank.collisionRect) & !s.damageDealt)
                                {
                                    player1Tank.takeDamage(s.damage);
                                    GamePad.SetVibration(PlayerIndex.One, 1.0f, 1.0f);
                                    uiManager.floatingDamageText(s.damage.ToString(), s.getBulletPosition());
                                    s.damageDealt = true;
                                }
                            }

                            if (currentTank != player2Tank)
                            {
                                if (s.explosion.collisionRectangle.Intersects(player2Tank.collisionRect) & !s.damageDealt)
                                {
                                    player2Tank.takeDamage(s.damage);
                                    GamePad.SetVibration(PlayerIndex.Two, 1.0f, 1.0f);
                                    uiManager.floatingDamageText(s.damage.ToString(), s.getBulletPosition());
                                    s.damageDealt = true;
                                }
                            }
                        }
                    }

                    bool scatterCollided = false;
                    bool scatterChecked  = false;

                    for (int i = 0; i < ((ScatterShot)currentTank.bullet).scatterShots.Count; i++)
                    {
                        if (((ScatterShot)currentTank.bullet).scatterShots[i].exploded & !scatterChecked)
                        {
                            scatterCollided = true;
                        }
                        else
                        {
                            scatterCollided = false;
                            scatterChecked  = true;
                        }
                    }

                    if (scatterCollided)
                    {
                        shotCollided = true;
                        shotPause    = 2.0f;
                    }
                }

                #endregion ScatterShotCollision

                #region BasicCollision

                else if (currentTank.bullet.intersectPixels(player1Tank.getTankRect(), player1Tank.tankTextureData) && currentTank != player1Tank ||
                         currentTank.bullet.intersectPixels(player2Tank.getTankRect(), player2Tank.tankTextureData) && currentTank != player2Tank)
                {
                    shotCollided = true;
                    shotPause    = 2.0f;
                }
                else if (currentTank.bullet.intersectPixels(background.layers[1].Sprites[0].Position, background.terrainTextureData))
                {
                    if (currentTank.bullet.type == BulletType.TeleportShot)
                    {
                        currentTank.teleportTank(currentTank.bullet.getBulletPosition());
                        currentTank.terrainMovement();
                        currentTank.bullet.damageDealt = true;
                    }

                    shotCollided = true;
                    shotPause    = 1.0f;
                }
                else if (currentTank.bullet.outOfBounds)
                {
                    shotCollided = true;
                    shotPause    = 1.0f;
                }

                #endregion BasicCollision

                if (shotCollided && currentTank.bullet.type == BulletType.MissileDrop & !currentTank.bullet.outOfBounds)
                {
                    MissileDrop drop = ((MissileDrop)currentTank.bullet);

                    if (!drop.targetLockedOn)
                    {
                        if (currentTank != player1Tank)
                        {
                            drop.sendTankData(player1Tank.getTankPos(), player1Tank.getTankRect().Width / 2,
                                              player1Tank.tankPic.Width, player1Tank.tankPic.Height, player1Tank.collisionRect);
                        }
                        else if (currentTank != player2Tank)
                        {
                            drop.sendTankData(player2Tank.getTankPos(), player2Tank.getTankRect().Width / 2,
                                              player2Tank.tankPic.Width, player2Tank.tankPic.Height, player2Tank.collisionRect);
                        }

                        if (drop.scanTarget())
                        {
                            shotCollided = false;
                            shotPause    = 0;
                        }
                    }
                }
                else if (shotCollided)
                {
                    currentTank.bullet.bulletCollided();
                }

                if (currentTank.bullet.explosion != null)
                {
                    if (currentTank != player2Tank)
                    {
                        if (currentTank.bullet.explosion.collisionRectangle.Intersects(player2Tank.collisionRect) & !currentTank.bullet.damageDealt)
                        {
                            player2Tank.takeDamage(currentTank.bullet.damage);
                            GamePad.SetVibration(PlayerIndex.Two, 1.0f, 1.0f);
                            uiManager.floatingDamageText(currentTank.bullet.damage.ToString(), currentTank.bullet.getBulletPosition());
                            currentTank.bullet.damageDealt = true;
                        }
                    }

                    if (currentTank != player1Tank)
                    {
                        if (currentTank.bullet.explosion.collisionRectangle.Intersects(player1Tank.collisionRect) & !currentTank.bullet.damageDealt)
                        {
                            player1Tank.takeDamage(currentTank.bullet.damage);
                            GamePad.SetVibration(PlayerIndex.One, 1.0f, 1.0f);
                            uiManager.floatingDamageText(currentTank.bullet.damage.ToString(), currentTank.bullet.getBulletPosition());
                            currentTank.bullet.damageDealt = true;
                        }
                    }
                }
            }
            else if (shotPause > 0)
            {
                shotPause -= (float)time.ElapsedGameTime.TotalSeconds;
                screenManager.updateTime((int)shotPause, (int)currentTank.moveLimit);

                if (shotPause < 0)
                {
                    turnOver = true;
                }
            }
            else if (turnOver)
            {
                gameRunning = false;
                turnsTaken++;
                GamePad.SetVibration(PlayerIndex.One, 0.0f, 0.0f);
                GamePad.SetVibration(PlayerIndex.Two, 0.0f, 0.0f);
                uiManager.removeText();
                swapPlayers();
            }
        }