阅读背景:

基于C#弹幕类射击游戏的实现——(九)BOSS

来源:互联网 

BOSS类和Enemy差不多,其实貌似因该继承自GameEnemy才对??

public class GameBoss : GameObject
	{
		protected Box[] SubBoxCollider = null; // BoxCollider用作大范围的检测,SubBoxCollider是细节检测
		
		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状态,也可理解为当前应该发射的弹幕状态
		
		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);
			}
		}
	}publ



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: