public async void SpawnFire(int initialX, int InitialY) { if (Game1.fireNum <= maxFires) { // PositionComponent ECSComponents.PositionComponent newPos = CalculateFireGridPos(initialX, InitialY); // SpriteComponent ECSComponents.SpriteComponent newSprite = new ECSComponents.SpriteComponent(Game1.SpriteDict["fire_1"], 1); // StatusComponent ECSComponents.StatusComponent newStatus = new ECSComponents.StatusComponent(false, Game1.EntityType.Fire); //hitboxComponet ECSComponents.CollisionBoxComponet newHitBox = new ECSComponents.CollisionBoxComponet(newSprite.Sprite, newPos); if (DetermineSpawnLegality(newPos)) // if this position isn't already occupied by a fireSprite { Entity newDisaster = Game1._world.CreateEntity(); // create the entity int newID = newDisaster.Id; // track the new entity's id newDisaster = Game1._world.GetEntity(newID); // spawn the new fire entity // attach components newDisaster.Attach(newPos); newDisaster.Attach(newSprite); newDisaster.Attach(newStatus); newDisaster.Attach(newHitBox); Game1.fireNum++; await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { Game1.myGamePage.CreateNewFire(newID); }); return; } else { return; // fire already exists in this position so do not spawn it } } else { return; } }
public override void Draw(GameTime gameTime) // overridden draw function that will render all entities with a sprite component every Darw() call { Game1.spriteBatch.Begin(); foreach (var entityID in ActiveEntities) { // Get the entity that the entityID references Entity entity = Game1._world.GetEntity(entityID); // **Get Position** // Pull the PositionComponent from the entity ECSComponents.PositionComponent entityPositionComponent = entity.Get <ECSComponents.PositionComponent>(); // Get the coordinates of the entity from the entity's PositionComponent var entityX = entityPositionComponent.XCoor; var entityY = entityPositionComponent.YCoor; var angle = entityPositionComponent.Angle; // **Get Sprite** // Pull the SpriteComponent from the entity ECSComponents.SpriteComponent entitySpriteComponent = entity.Get <ECSComponents.SpriteComponent>(); // Get the Texture2D of the entity from the entity's SpriteComponent SpriteClass sprite = entitySpriteComponent.Sprite; Microsoft.Xna.Framework.Graphics.Texture2D texture = sprite.texture; float scale = entitySpriteComponent.Scale; // draw the entity Vector2 spritePosition = new Vector2(entityX, entityY); Game1.spriteBatch.Draw(texture, spritePosition, null, Color.White, angle, new Vector2(texture.Width / 2, texture.Height / 2), new Vector2(scale, scale), Microsoft.Xna.Framework.Graphics.SpriteEffects.None, 0f); } Game1.spriteBatch.End(); }