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);
}
}
}
唯一多出来的是BOSS会显示一个酷炫的血条。而且血条会像街机一样,受到大量伤害的时候,先猛的少一节,然后背景色才跟着慢慢减少的那种。。
管理类,依然简单
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);
}
}
}
接下来实现我们游戏里第一个BOSS
逻辑简单,比较笨弹幕也很弱。。但我能实现的就这么多了
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分为几个阶段,不同阶段会发射不同种类的弹幕(会有组合弹幕)
BOSS是发挥创意最好的地方,通过不同的弹幕可以组合出不同种类的BOSS,最好用脚本语言来控制AI,推荐Lua