2013-04-19 5 views
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); 
    } 
} 
} 

그리고 마지막으로 내 발사체 관리자 클래스 ..

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
public Rectangle collisionRec {get; private set;} 

그런 다음 글 머리 기호의 업데이트 방법에서 사각형 좌표를 업데이트합니다.

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

아마도 게임 1이나 어딘가에있는 총알 목록을 유지하고있을 것입니다.

foreach (Bullet b in bulletList) 
{ 
if (b.collisionRec.Intersects(someOtherRec)) 
    { 
    //Do some stuff. 
    } 
}