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

本文关键字:对象 冲突 数组 检测 XNA | 更新日期: 2023-09-27 18:35:39

所以我正在XNA中制作太空侵略者克隆。我需要执行碰撞检测逻辑。我的入侵者被控制在一个阵列中。每个入侵者都有自己的矩形边界框。因此,如果我的船碰到一个入侵者,它应该会松动哈尔特或类似的东西。这是我的入侵者类:

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);
                }
            }
        }
    }
}

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

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

Update() 方法中,您可以只执行一个简单的矩形相交检查:

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