// Adds an Entity to the Entity List public void AddEntity(BaseCharacter entity) { if (entity != null) { m_entityList.Add(entity); } }
public virtual void HandleCollision(BaseCharacter entity) { //if (this.Bounds.Intersects(entity.Bounds)) //{ // entity.m_tintColor = Color.Red; //} //else //{ // entity.m_tintColor = Color.White; //} Player CroPlayer = null; if (this.Name == "Cro") { CroPlayer = EntityManager.m_croPlayer; } if (CroPlayer != null) { // Check to see if the bounding rectangles of both entities are intersecting with the bottom animation. if (this.Bounds.Intersects(entity.Bounds)) { // Creating a Spritebatch and RenderTarget2D to facilitate perpixel collision for the animation. SpriteBatch spriteBatch = EntityManager.m_spriteBatch; RenderTarget2D theRenderTargetBottom = null; RenderTarget2D theRenderTargetTop = null; theRenderTargetBottom = new RenderTarget2D(EntityManager.m_graphics, CroPlayer.BottomAnimation.Animation.FrameWidth, CroPlayer.BottomAnimation.Animation.FrameHeight); theRenderTargetTop = new RenderTarget2D(EntityManager.m_graphics, CroPlayer.TopAnimation.Animation.FrameWidth, CroPlayer.TopAnimation.Animation.FrameHeight); EntityManager.m_graphics.SetRenderTarget(theRenderTargetBottom); EntityManager.m_graphics.SetRenderTarget(theRenderTargetTop); EntityManager.m_graphics.Clear(Color.Transparent); spriteBatch.Begin(); Rectangle source = new Rectangle(CroPlayer.BottomAnimation.FrameIndex * CroPlayer.BottomAnimation.Animation.FrameWidth, 0, CroPlayer.BottomAnimation.Animation.FrameWidth, CroPlayer.BottomAnimation.Animation.Texture.Height); spriteBatch.Draw(CroPlayer.BottomAnimation.Animation.Texture, Vector2.Zero, source, Color.White, 0.0f, Vector2.Zero, 1.0f, CroPlayer.Flip, 0.0f); spriteBatch.Draw(CroPlayer.TopAnimation.Animation.Texture, Vector2.Zero, source, Color.White, 0.0f, Vector2.Zero, 1.0f, CroPlayer.Flip, 0.0f); spriteBatch.End(); EntityManager.m_graphics.SetRenderTarget(null); // Take the texture data for both entities and put them into colour arrays. Color[] entityData = new Color[theRenderTargetBottom.Width * theRenderTargetBottom.Height]; theRenderTargetBottom.GetData(entityData); Color[] entityData2 = new Color[entity.Texture.Width * entity.Texture.Height]; entity.Texture.GetData(entityData2); // Pass in the texture data and bounds of both entites in collision. if (IntersectPixels(this.Bounds, entityData, entity.Bounds, entityData2) == true) { // Change colour to red if there is a hit. entity.m_tintColor = Color.Red; } else { // Leave it as/change it back to white if there is no collision. entity.m_tintColor = Color.White; } } } }
// Removes an Entity from the Entity List public void RemoveEntity(BaseCharacter entity) { if (entity != null) { m_entityList.Remove(entity); } }