/// <summary> /// Setup the boundaries and position of this collider given a sprite /// </summary> /// <param name="theProxy"></param> public void SetupCollisonSpriteProxy(SpriteProxy theProxy) { SpriteBatch colliderBatch = SpriteBatchManager.Active.Find(SpriteBatch.Name.SpriteCollisions); // Remove previous SpriteCollisionProxy if any if (this.collisionSprite != null) { bool oldProxyRemoved = colliderBatch.Detach(collisionSprite.SpriteName, collisionSprite.Id); Debug.Assert(oldProxyRemoved, "The old collision sprite proxy wasn't removed upon setting a new proxy!"); Debug.Assert(this.collisionSprite.SpriteName != Sprite.Name.UNINITIALIZED); SpriteCollisonProxyManager.Active.Recycle(collisionSprite.SpriteName, collisionSprite.Id); this.collisionSprite = null; } // Create a new proxy this.collisionSprite = SpriteCollisonProxyManager.Active.Create(theProxy.SpriteName, theProxy.Id); this.collisionSprite.SetId(theProxy.Id); this.collisionSprite.SetPosition(this.colliderBoundary.x, this.colliderBoundary.y); this.colliderBoundary.w = this.collisionSprite.ModelSprite.Width; this.colliderBoundary.h = this.collisionSprite.ModelSprite.Height; this.collisionSprite.SetColor(this.color); // Attach it to the batch colliderBatch.Attach(this.collisionSprite, this.collisionSprite.Id); }
/// <summary> /// Changes the sprite reference /// </summary> /// <param name="newSprite"></param> public void SetSprite(SpriteBatch.Name newBatchName, SpriteEntity newSprite) { // Remove the old sprite from it's sprite batch SpriteBatch oldBatch = SpriteBatchManager.Active.Find(this.batchName); if (oldBatch != null) { bool su = oldBatch.Detach(this.sprite.SpriteName, this.sprite.Id); Debug.Assert(su, "The old SpriteBatch has to detach a valid (non-null) sprite."); } // Recycle old sprite SpriteProxyManager.Active.Recycle(this.sprite.SpriteName, this.sprite.Id); this.sprite = null; // Get new batch SpriteBatch newBatch = SpriteBatchManager.Active.Find(newBatchName); this.batchName = newBatchName; // If null, get null proxy and leave if (newBatch == null) { this.sprite = SpriteProxyManager.Active.NullSpriteProxy; return; // Leave } // Get new sprite and attach it this.sprite = SpriteProxyManager.Active.Create(newSprite.SpriteName, this.Id); newBatch.Attach(this.sprite, this.sprite.Id); this.collider.SetupCollisonSpriteProxy(this.sprite); }
private void ctor(uint newId) { this.sprite = SpriteProxyManager.Active.NullSpriteProxy; this.collider = new Collider(); this.name = GameObject.Name.UNINITIALIZED; this.batchName = SpriteBatch.Name.UNINITIALIZED; this.color = Colors.White; this.x = 0.0f; this.y = 0.0f; this.instanceId = newId; // All GameObjects have collisions by default // Override by calling Collider.MakeColliderNull() // in the derived class's constructor or Start() method if (this.Collider.IsNull) { this.Collider.SetupCollisonSpriteProxy(5, 5, this.instanceId); } }
/// <summary> /// Clears the data in the GameObject. Base must be called if overriding! /// </summary> public void Reset() { this.wasAlreadyRemoved = true; this.OnDestroy(); // Get the parent of this GameObject GameObject parent = this.Parent as GameObject; this.RemoveThisPCSNode(); // Do post-delete procedure of the parent's collider if (parent != null) { parent.ReevaluateCollider(); } this.ResetAnimator(); this.collider.Reset(); // Remove the old sprite from it's sprite batch SpriteBatch oldBatch = SpriteBatchManager.Active.Find(this.batchName); if (oldBatch != null) { bool su = oldBatch.Detach(this.sprite.SpriteName, this.sprite.Id); Debug.Assert(su, "The old SpriteBatch has to detach a valid (non-null) sprite."); } SpriteProxyManager.Active.Recycle(this.sprite.SpriteName, this.sprite.Id); this.sprite = SpriteProxyManager.Active.NullSpriteProxy; this.name = GameObject.Name.UNINITIALIZED; this.batchName = SpriteBatch.Name.UNINITIALIZED; this.color = Colors.White; this.x = 0.0f; this.y = 0.0f; this.instanceId = 0u; }