在 Ship.Render 中加入護盾繪製邏輯:
if (inShield && evolved)
{
var shieldTex = SpacewarGame.ContentManager.Load<Texture2D>(
SpacewarGame.Settings.MediaPath + @"textures\circle");
batch.Begin(SpriteBlendMode.AlphaBlend);
// 3D 座標投影至 2D 螢幕空間
Vector3 loc = device.Viewport.Project(position,
SpacewarGame.Camera.Projection, SpacewarGame.Camera.View, Matrix.Identity);
batch.Draw(shieldTex,
new Vector2(loc.X - shieldTex.Width / 2f, loc.Y - shieldTex.Height / 2f),
new Rectangle(0, 0, shieldTex.Width, shieldTex.Height),
new Color(new Vector4(0f, 1f, 0f, 0.5f)), // 半透明綠
0f,
new Vector2(shieldTex.Width / 2f, shieldTex.Height / 2f),
(3f * (float)Math.Abs(Math.Cos(exitShieldTime))) + 2f, // 脈動縮放
SpriteEffects.None, 0f);
batch.End();
}
Viewport.Project:將 3D 世界座標轉為 2D 螢幕座標。Vector4(0,1,0,0.5):RGBA,Alpha=0.5 達成半透明。Cos(exitShieldTime):隨時間脈動縮放,增強視覺效果。層深 0f:確保繪製在飛船之上。修改 SpacewarScreen.cs 的 HitPlayer1/2:
// HitPlayer2 開頭
if (ship2.Shield)
{
Sound.PlayCue(Sounds.WeaponPickup); // 彈跳音效
return; // 忽略傷害
}
// HitPlayer1 同理