0

所以基本上我想弄清楚如何在我发射的每个子弹周围的一个类中实现矩形。这是一款导弹指挥型游戏。我想不通的是我会在哪里声明 Rectangle 以及如何将它传递给游戏。

这是我的火箭课程的代码......

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ShootingRocket
{
public class Rocket
{
    public Texture2D DrawTexture { get; set; }
    public Vector2 Position { get; set; }
    public Vector2 Direction { get; set; }
    public float Rotation { get; set; }
    public float Speed { get; set; }
    public bool IsShooting { get; set; }

    int timeBetweenShots = 100;
    int shotTimer = 0;

    public Rocket(Texture2D texture, Vector2 position, Vector2 direction, float rotation, float Speed)
    {
        this.DrawTexture = texture;
        this.Position = position;
        this.Direction = direction;
        this.Rotation = rotation;
        this.Speed = Speed;

        this.IsShooting = false;
    }

    public void Update(GameTime gameTime)
    {
        this.Position += Direction * Speed;

        if (IsShooting)
        {
            shotTimer += gameTime.ElapsedGameTime.Milliseconds;

            if (shotTimer > timeBetweenShots)
            {
                shotTimer = 0;

                ProjectileManager.AddBullet(this.Position, this.Direction, 12, 2000, BulletType.Player);
            }
        }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(
            this.DrawTexture,
            this.Position,
            null,
            Color.White,
            this.Rotation,
            new Vector2(
                this.DrawTexture.Width / 2,
                this.DrawTexture.Height / 2),
            1.0f,
            SpriteEffects.None,
            1.0f);

这是我的子弹类的代码......

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ShootingRocket
{
public enum BulletType { Player, Enemy }

public class Bullet
{
    public BulletType Type { get; set; }
    public Texture2D DrawTexture { get; set; }
    public Vector2 Position { get; set; }
    public Vector2 Direction { get; set; }
    public float Speed { get; set; }
    public int ActiveTime { get; set; }
    public int TotalActiveTime { get; set; }

    public Bullet(Texture2D texture, Vector2 position, Vector2 direction, float speed, int activeTime, BulletType type)
    {
        this.DrawTexture = texture;
        this.Position = position;
        this.Direction = direction;
        this.Speed = speed;
        this.ActiveTime = activeTime;
        this.Type = type;

        this.TotalActiveTime = 0;
    }

    public void Update(GameTime gameTime)
    {
        this.Position += Direction * Speed;

        this.TotalActiveTime += gameTime.ElapsedGameTime.Milliseconds;

    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(
             DrawTexture,
             Position,
             null,
             Color.White,
             0f,
             new Vector2(
                  DrawTexture.Width / 2,
                  DrawTexture.Height / 2),
             1.0f,
             SpriteEffects.None,
             0.8f);
    }
}
}

最后但并非最不重要的是我的 Projectile Manager 课程......

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace ShootingRocket
{
public class ProjectileManager : DrawableGameComponent
{
    static List<Bullet> bullets = new List<Bullet>();
    static Texture2D playerBulletTexture;

    SpriteBatch spriteBatch;

    public ProjectileManager(Game game, SpriteBatch spriteBatch)
        : base(game)
    {
        game.Components.Add(this);
        playerBulletTexture = game.Content.Load<Texture2D>("Bullet");

        this.spriteBatch = spriteBatch;
    }

    public override void Update(GameTime gameTime)
    {
        for(int i = 0; i < bullets.Count; i++)
        {
            bullets[i].Update(gameTime);

            if (bullets[i].TotalActiveTime > bullets[i].ActiveTime)
                bullets.RemoveAt(i);
        }

        base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {
        foreach (Bullet b in bullets)
        {
            b.Draw(spriteBatch);
        }

        base.Draw(gameTime);
    }

    public static void AddBullet(Vector2 position, Vector2 direction, float speed, int activeTime, BulletType type)
    {
        bullets.Add(new Bullet(playerBulletTexture, position, direction, speed,       activeTime, type));
    }
}
}

任何帮助是极大的赞赏!

4

1 回答 1

1
public Rectangle collisionRec {get; private set;}

然后在子弹的更新方法中更新矩形坐标。

collisionRec = new Rectangle(position.X, position.Y, spriteWidth, spriteHeight);

您可能正在维护 game1 或某处中所有这些子弹的列表。

foreach (Bullet b in bulletList)
{
if (b.collisionRec.Intersects(someOtherRec))
    {
     //Do some stuff.
    }
}
于 2013-04-20T14:06:54.687 回答