基于C#彈幕類射擊游戲的實現(xiàn)——(九)BOSS

BOSS類和Enemy差不多,其實貌似因該繼承自GameEnemy才對??

public class GameBoss : GameObject  
    {  
        protected Box[] SubBoxCollider = null; // BoxCollider用作大范圍的檢測,SubBoxCollider是細節(jié)檢測  
          
        protected GameBarrage mBarrage = null; // 彈幕生成器  
          
        protected int BossWidth;  
        protected int BossHeight;  
        protected int HalfBossWidth;  
        protected int HalfBossHeight;  
          
        protected Vector2 Speed;  
          
        protected int Life; // 當前生命值  
        protected int MaxLife; // 最大生命值  
        public bool IsLive; // 是否存活  
          
        protected float ShowLife; // 用于顯示血條  
        protected Rectangle HpRect = new Rectangle(10, 5, Config.ScreenWidth - 20, 8); // 血條顯示位置  
          
        protected int State = 0; // BOSS狀態(tài),也可理解為當前應該發(fā)射的彈幕狀態(tài)  
          
        public GameBoss(GameBarrage barrage)  
        {  
            this.mBarrage = barrage;  
            this.Life = 1;  
            this.MaxLife = 1;  
            this.ShowLife = 1;  
            this.IsLive = true;  
        }  
          
        public bool Collide(Box box)  
        {  
            // 先用大范圍的碰撞盒檢測  
            if ( BoxCollider.Collide(box) == false )  
            {  
                return false;  
            }  
              
            foreach ( Box subBox in SubBoxCollider )  
            {  
                if ( subBox.Collide(box) == true )  
                {  
                    return true;  
                }  
            }  
              
            return false;  
        }  
          
        public void DecLife(int decLife)  
        {  
            Life -= decLife;  
              
            if ( Life <= 0 )  
            {  
                IsLive = false;  
                GameBombManager.AddBomb(Position, true, 1.0f);  
            }  
        }  
          
        protected virtual void MoveLogic(float elapsedTime)  
        {  
              
        }  
          
        protected virtual void ShootLogic(float elapsedTime)  
        {  
              
        }  
          
        public override void Update(float elapsedTime)  
        {  
            float diff = Life - ShowLife;  
              
            ShowLife += diff * 2 * elapsedTime;  
              
            if ( ShowLife < Life )  
            {  
                ShowLife = Life;  
            }  
        }  
          
        public override void Render(Graphics g)  
        {  
            g.DrawRectangle(Pens.Wheat, HpRect);  
            g.FillRectangle(Brushes.Red, new Rectangle(12, 7, (int)((float)(HpRect.Width - 3) * ShowLife / (float)MaxLife), 5));  
            g.FillRectangle(Brushes.Green, new Rectangle(12, 7, (int)((float)(HpRect.Width - 3) * (float)Life / (float)MaxLife), 5));  
              
            if ( Config.IsDebug )  
            {  
                if ( Config.IsShowBox )  
                {  
                    foreach ( Box subBox in SubBoxCollider )  
                    {  
                        g.DrawRectangle(Pens.Green, subBox.ToRectangle());  
                    }  
                }  
                  
                g.DrawString("BossHP:" + Life.ToString(), Data.NormalFont, Brushes.Red, 300, 30);  
            }  
        }  
    }

唯一多出來的是BOSS會顯示一個酷炫的血條。而且血條會像街機一樣,受到大量傷害的時候,先猛的少一節(jié),然后背景色才跟著慢慢減少的那種。。
管理類,依然簡單

public class GameBossManager  
    {  
        private List<GameBoss> mBosss;  
          
        public GameBossManager()  
        {  
            this.mBosss = new List<GameBoss>();  
        }  
          
        public void AddBoss(GameBoss boss)  
        {  
            mBosss.Add(boss);  
        }  
          
        public bool Collide(Box box, int decLife)  
        {  
            for ( int i = 0; i < mBosss.Count; i++ )  
            {  
                if ( mBosss[i].Collide(box) == true )  
                {  
                    mBosss[i].DecLife(decLife);  
                    return true;  
                }  
            }  
              
            return false;  
        }  
          
        public void Update(float elapsedTime)  
        {  
            for ( int i = 0; i < mBosss.Count; i++ )  
            {  
                mBosss[i].Update(elapsedTime);  
                  
                if ( mBosss[i].IsLive == false )  
                {  
                    mBosss.RemoveAt(i);  
                    i--;  
                    continue;  
                }  
            }  
        }  
          
        public void Render(Graphics g)  
        {  
            foreach ( GameBoss boss in mBosss )  
            {  
                boss.Render(g);  
            }  
        }  
    }

接下來實現(xiàn)我們游戲里第一個BOSS
邏輯簡單,比較笨彈幕也很弱。。但我能實現(xiàn)的就這么多了

public class Boss1 : GameBoss  
    {  
        private float WaitTime;  
        private float ShootWaitTime;  
          
        private float MoveDistance;  
        private bool IsLeft;  
          
        public Boss1(GameBarrage barrage, Vector2 position)  
            : base(barrage)  
        {  
            this.Position = position;  
              
            this.BoxCollider = new Box((int)position.X, (int)position.Y, 144, 156);  
              
            this.SubBoxCollider = new Box[3];  
            this.SubBoxCollider[0] = new Box(4 + 34 / 2, 0 + 147 / 2, 34, 147);  
            this.SubBoxCollider[1] = new Box(106 + 34 / 2, 0 + 147 / 2, 34, 147);  
            this.SubBoxCollider[2] = new Box(38 + 68 / 2, 0 + 90 / 2, 68, 90);  
              
            this.Life = 100;  
            this.MaxLife = 100;  
            this.ShowLife = 100;  
            this.Speed = new Vector2(50, 100);  
            this.IsLeft = false;  
              
            this.WaitTime = 0.5f;  
            this.ShootWaitTime = 1.0f;  
            this.MoveDistance = 0;  
              
            this.BossWidth = 144;  
            this.BossHeight = 156;  
            this.HalfBossWidth = BossWidth / 2;  
            this.HalfBossHeight = BossHeight / 2;  
        }  
          
        protected override void MoveLogic(float elapsedTime)  
        {  
            if (Position.Y < 120 )  
            {  
                Position.Y += Speed.Y * elapsedTime;  
                return;  
            }  
              
            if ( WaitTime > 0 )  
            {  
                WaitTime -= elapsedTime;  
                return;  
            }  
              
            if ( MoveDistance <= 0 )  
            {  
                MoveDistance = Helper.GetRandomFloat(50, 100);  
                  
                IsLeft = (Position.X - GameScene.MainScene.Player1Position.X) < 0 ? false : true;  
                  
                WaitTime = 2.0f;  
            }  
            else  
            {  
                if ( IsLeft )  
                {  
                    Position.X -= Speed.X * elapsedTime;  
                }  
                else  
                {  
                    Position.X += Speed.X * elapsedTime;  
                }  
                MoveDistance -= Speed.X * elapsedTime;  
            }  
        }  
          
        protected override void ShootLogic(float elapsedTime)  
        {  
            if ( ShootWaitTime > 0 )  
            {  
                ShootWaitTime -= elapsedTime;  
                return;  
            }  
              
            int time = 1;  
              
            switch ( State )  
            {  
                case 0:  
                    State = 1;  
                    time = BossBarrage.TwoFans(mBarrage, Position);  
                    break;  
                case 1:  
                    State = 2;  
                    time = BossBarrage.ThreeCircle(mBarrage, Position);  
                    break;  
                case 2:  
                    State = 3;  
                    time = BossBarrage.TwoReverseRotate(mBarrage, Position);  
                    break;  
                case 3:  
                    State = 4;  
                    time = BossBarrage.Line(mBarrage);  
                    time += BossBarrage.ThreeCircle(mBarrage, Position);  
                    break;  
                case 4:  
                    State = 5;  
                    time = BossBarrage.TwoFans(mBarrage, Position);  
                    time += BossBarrage.TwoReverseRotate(mBarrage, Position);  
                    time += BossBarrage.ThreeCircle(mBarrage, Position);  
                    break;  
                case 5:  
                    State = 6;  
                    time = BossBarrage.Line(mBarrage);  
                    time += BossBarrage.Petal(mBarrage, Position);  
                    break;  
                case 6:  
                    State = 7;  
                    time = BossBarrage.Line(mBarrage);  
                    time += BossBarrage.Petal(mBarrage, Position);  
                    time += BossBarrage.TwoFans(mBarrage, Position);  
                    break;  
                case 7:  
                    State = 0;  
                    time = BossBarrage.Line(mBarrage);  
                    time += BossBarrage.Petal(mBarrage, Position);  
                    time += BossBarrage.TwoFans(mBarrage, Position);  
                    time += BossBarrage.TwoReverseRotate(mBarrage, Position);  
                    break;  
                default:  
                    break;  
            }  
              
            WaitTime = 2;  
            ShootWaitTime = time + 2;  
              
            //BossBarrage.Petal(mBarrage, Position);  
        }  
          
        public override void Update(float elapsedTime)  
        {  
            MoveLogic(elapsedTime);  
            ShootLogic(elapsedTime);  
              
            SubBoxCollider[0].X = (int)Position.X + 4 + 34 / 2 - HalfBossWidth;  
            SubBoxCollider[0].Y = (int)Position.Y + 147 / 2 - HalfBossHeight;  
            SubBoxCollider[1].X = (int)Position.X + 106 + 34 / 2 - HalfBossWidth;  
            SubBoxCollider[1].Y = (int)Position.Y + 147 / 2 - HalfBossHeight;  
            SubBoxCollider[2].X = (int)Position.X + 38 + 68 / 2 - HalfBossWidth;  
            SubBoxCollider[2].Y = (int)Position.Y + 90 / 2 - HalfBossHeight;  
              
            base.Update(elapsedTime);  
        }  
          
        public override void Render(Graphics g)  
        {  
            g.DrawImage(Data.Boss1Source,  
                        new Rectangle((int)Position.X - HalfBossWidth, (int)Position.Y - HalfBossHeight, BossWidth, BossHeight),  
                        new Rectangle(0, 0, BossWidth, BossHeight),  
                        GraphicsUnit.Pixel);  
              
            base.Render(g);  
        }  
    }

可以看出,BOSS分為幾個階段,不同階段會發(fā)射不同種類的彈幕(會有組合彈幕)
BOSS是發(fā)揮創(chuàng)意最好的地方,通過不同的彈幕可以組合出不同種類的BOSS,最好用腳本語言來控制AI,推薦Lua

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容