XNA - 检测与数组对象的冲突

时间:2014-12-30 21:48:28

标签: c# xna

所以我在XNA中制作了一个Space Invaders克隆。我需要做碰撞检测逻辑。我的入侵者被保存在一个数组中。每个入侵者都有自己的矩形边界框。因此,如果我的船接触到一个入侵者,它应该是松散的或类似的东西。这是我的入侵者类:

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

namespace SpaceInvaders
{
    public class botInvaders
    {
        public botInvaders()
        {

        }

        public static Texture2D botInvaderTex;
        public static Rectangle botInvaderHitBox;
        public static Vector2 botInvaderOrigin;
        public int botInvaderCurrentFrame = 1, botInvaderFrameWidth = 52, botInvaderFrameHeight = 90, invaderRows = 3, invaderCollumns = 10; // invaderRows = 5 // For 50 invaders
        float botInvadersTimer = 0f, botInvadersInterval = 100;
        public static Rectangle[,] botInvadersRect;
        String botInvadersDirection = "RIGHT";
        public static Color invadersColor = Color.White;
        SoundEffect invaderTeletransportation;
        SoundEffectInstance invaderTeletransportationInstance;

        public void LoadContent(ContentManager Content)
        {
            botInvaderTex = Content.Load<Texture2D>(".\\gameGraphics\\gameSprites\\botInvaders\\normalInvaders\\invaderShip1");
            invaderTeletransportation = Content.Load<SoundEffect>(".\\gameSounds\\teletransportation");
            invaderTeletransportationInstance = invaderTeletransportation.CreateInstance();
            invaderTeletransportationInstance.IsLooped = false;
            botInvadersRect = new Rectangle[invaderRows, invaderCollumns];
            for (int r = 0; r < invaderRows; r++)
            {
                for (int c = 0; c < invaderCollumns; c++)
                {
                    botInvadersRect[r, c].Width = botInvaderFrameWidth;
                    botInvadersRect[r, c].Height = botInvaderTex.Height;
                    botInvadersRect[r, c].X = 70 * c;
                    botInvadersRect[r, c].Y = (70 * r) + 22;
                }
            }
        }

        public void Update(GameTime gameTime)
        {
            botInvaderHitBox = new Rectangle(botInvaderCurrentFrame * botInvaderFrameWidth, 0, botInvaderFrameWidth, botInvaderFrameHeight);
            botInvaderOrigin = new Vector2(botInvaderHitBox.X / 2, botInvaderHitBox.Y / 2);

            botInvadersTimer += (float)gameTime.ElapsedGameTime.Milliseconds;

            if (botInvadersTimer > botInvadersInterval)
            {
                botInvaderCurrentFrame++;
                botInvadersTimer = 0f;
            }

            if (botInvaderCurrentFrame == 2)
            {
                botInvaderCurrentFrame = 0;
            }

            if (Game1.gameStart == 2)
            {
                invaderTeletransportationInstance.Play();
            }

            botInvaderHitBox = new Rectangle(botInvaderCurrentFrame * botInvaderFrameWidth, 0, botInvaderFrameWidth, botInvaderFrameHeight);
            botInvaderOrigin = new Vector2(botInvaderHitBox.Width / 2, botInvaderHitBox.Height / 2);
        }

        public void Draw(Texture2D invadersTex, Rectangle[,] invadersDestinationRect, Nullable<Rectangle> invadersSourceRect, Color invadersColor, float invadersRotation, Vector2 invadersOrigin, SpriteEffects invadersEffects, float invadersScale, SpriteBatch spriteBatch)
        {
            for (int r = 0; r < invaderRows; r++)
            {
                for (int c = 0; c < invaderCollumns; c++)
                {
                    spriteBatch.Draw(botInvaderTex, botInvadersRect[r, c], botInvaderHitBox, Color.White);
                }
            }
        }
    }
}

没有数组(只有一个),ship类基本上是相同的东西: 所以它也有一个playerShipHitBox

1 个答案:

答案 0 :(得分:1)

Update()方法中,你可以做一个简单的矩形交叉检查:

for (int r = 0; r < invaderRows; r++)
    for (int c = 0; c < invaderColumns; c++)
        if (botInvadersRect[r, c].Intersects(playerShipHitBox))
            // BAM! Collision!