在类中创建一个矩形

时间:2013-04-19 23:13:46

标签: visual-studio-2010 xna xna-4.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));
    }
}
}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分: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.
    }
}