Exemple #1
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();

            //set timestep
            IsFixedTimeStep = true;
            TargetElapsedTime = TimeSpan.FromSeconds(1.0 / 60.0);

            //create initial objects
            tanks[0] = new Tank(this, 1);
            tanks[1] = new Tank(this, 2);

            tanks[0].Position = new Vector2(128, 128);
            tanks[1].Position = new Vector2(512, 128);

            Components.Add(tanks[0]);
            Components.Add(tanks[1]);
        }
Exemple #2
0
        private void CollisionCheck(GameTime gameTime)
        {
            // Tạm thời lưu lại trạng thái góc quay và vị trí của AI, nếu
            // trạng thái, hành vi mới đc thiết lập bởi hàm Action gây ra
            // va chạm thì trả lại trạng thái này.
            float   tempRotationAngle = rotationAngle;
            Vector2 tempPosition      = position;

            // AI tiến hành vài hành động thay đổi vị trí, góc quay
            Action(gameTime);

            // Sau đó, check vị trí, góc quay mới. Nếu vị trí, góc quay mới
            // gây ra va chạm với tường hay các Tanks khác thì đưa ra xử lý
            // thích hợp. Cách xử lý hiện tại là khá đơn giản: Trả về trạng
            // thái chưa có va chạm và tự động đổi chiều di chuyển từ đi tới
            // sang đi lùi.

            // Tiến hành check va chạm với tất cả các ô xung quanh
            for (int i = -1; i < 2; i++)
            {
                for (int j = -1; j < 2; j++)
                {
                    for (int k = 0; k < 4; k++)
                    {
                        // Không check với những ô ko nằm trong bản đồ
                        if ((int)currentMapPosition.X + i < 0
                            ||
                            (int)currentMapPosition.Y + j < 0
                            ||
                            (int)currentMapPosition.X + i >= TileMap.MapWidth
                            ||
                            (int)currentMapPosition.Y + j >= TileMap.MapHeight)
                        {
                            break;
                        }

                        // Lấy loại đối tượng (Là địa hình hay Tank, Wall,...)
                        int tempType =
                            TileMap.MapSquares[(int)currentMapPosition.X + i,
                                               (int)currentMapPosition.Y + j][k, 0];

                        // Lấy Index của đối tượng trong mảng chứa nó
                        int tempIndex =
                            TileMap.MapSquares[(int)currentMapPosition.X + i,
                                               (int)currentMapPosition.Y + j][k, 1];

                        // Va chạm với Wall
                        if (tempType >= 0 && tempType <= TileMap.WallTileEnd)
                        {
                            Wall wall = GameplayScreen.WallList[tempIndex];
                            // Nếu va chạm
                            if (this.CollideWith(wall))
                            {
                                // Trả lại trạng thái trước đó
                                position      = tempPosition;
                                rotationAngle = tempRotationAngle;

                                // Đổi chiều di chuyển
                                // Trong ví dụ này thì đổi movingSpeed từ (1, 0)
                                // là đi tới sang (0, 1) là đi lùi
                                float t = movingSpeed.X;
                                movingSpeed.X = movingSpeed.Y;
                                movingSpeed.Y = t;
                            }
                        }

                        // Va chạm với Enemy
                        else if (tempType == 89)
                        {
                            // Do Tank bị hủy diệt trong quá trình chơi,
                            // đôi khi gây mất đồng bộ trong quá trình
                            // update, dẫn đến tempIndex >= EnemyList.Count.
                            // Khi đó ta sẽ không check va chạm.
                            // Trong trường hợp this is Enemy, thì ko check
                            // với bản thân.
                            if (tempIndex >= GameplayScreen.EnemyList.Count
                                ||
                                this == GameplayScreen.EnemyList[tempIndex])
                            {
                                break;
                            }

                            Tank enemy = GameplayScreen.EnemyList[tempIndex];
                            // Nếu va chạm
                            if (this.CollideWith(enemy))
                            {
                                // Trả lại trạng thái trước đó
                                position      = tempPosition;
                                rotationAngle = tempRotationAngle;

                                // Đổi chiều di chuyển
                                // Trong ví dụ này thì đổi movingSpeed từ (1, 0)
                                // là đi tới sang (0, 1) là đi lùi
                                float t = movingSpeed.X;
                                movingSpeed.X = movingSpeed.Y;
                                movingSpeed.Y = t;
                            }
                        }

                        // Va chạm với Player
                        else if (tempType == 88)
                        {
                            // Nếu va chạm
                            if (this.CollideWith(GameplayScreen.Player))
                            {
                                // Trả lại trạng thái trước đó
                                position      = tempPosition;
                                rotationAngle = tempRotationAngle;

                                // Đổi chiều di chuyển
                                // Trong ví dụ này thì đổi movingSpeed từ (1, 0)
                                // là đi tới sang (0, 1) là đi lùi
                                float t = movingSpeed.X;
                                movingSpeed.X = movingSpeed.Y;
                                movingSpeed.Y = t;
                            }
                        }
                    }
                }
            }
        }
Exemple #3
0
        private float RotateTo(float originalAngle, Tank targetTank, float rotatingSpeed)
        {
            float targetAngle = GetAngle(this, targetTank);

            return(Rotate(originalAngle, targetAngle, rotatingSpeed));
        }