protected override void OnEntityAdded(Entity entity, int liveId) { Placement placement = placementComponents.GetComponentFor(entity); Physics physics = physicsComponents.GetComponentFor(entity); // Give it an initial position (after this, the collision engine will drive the position) Vector3 initialPosition = placement.Position; Body body = GetBodyFromBoundingVolume(placement, physics, initialPosition.XY()); // REVIEW: Might want to set this prior to getting the body? // body.CollidesWith = placement.Visible ? Category.All : Category.None; // Update categories body.IsSensor = physics.IsSensor; //Debug.Assert(body.OnSeparation == null); if (physics.IsSensor) { body.OnSeparation += body_OnSeparation; } body.CollidesWith = (Category)physics.CollidesWidth; body.CollisionCategories = (Category)physics.CollisionCategories; Debug.Assert(bodySlots[liveId] == null); bodySlots[liveId] = body; Debug.Assert(!bodyIdToLiveId.ContainsKey(body.BodyId)); bodyIdToLiveId[body.BodyId] = liveId; }
protected override void OnEntityAdded(Entity entity, int liveId) { base.OnEntityAdded(entity, liveId); SoundLoop soundLoop = soundLoopComponents.GetComponentFor(entity); soundEffectInstance[liveId] = soundEffects[soundLoop.CueNameId].CreateInstance(); soundEffectInstance[liveId].IsLooped = true; // REVIEW: support non-looped. UpdateSound(liveId, soundLoop); soundEffectInstance[liveId].Play(); }
protected override void OnDraw(GameTime gameTime, IEnumerable <int> entityIdCollection) { float ellapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; foreach (int liveId in entityIdCollection) { FrameAnimation animation = animationComponents.GetComponentFor(liveId); Aspect aspect = aspectComponents.GetComponentFor(liveId); int frameCount = spriteMapper.GetSpriteForId(aspect.ModelNameId).FrameCount; if (animation != null) { float period = 1f / animation.FrameRate; animation.EllapsedTime += ellapsed; if (animation.EllapsedTime >= period) { animation.EllapsedTime -= period; ChooseNextFrame(animation, aspect, frameCount); } } else { ChooseNextFrame(animation, aspect, frameCount); } } }
protected override void OnProcessEntities(GameTime gameTime, IEnumerable <int> entityIdCollection) { currentKeyboardState = Keyboard.GetState(); currentGamePadState = GamePad.GetState(PlayerIndex.One); // REVIEW: We invoke the handlers as we're enumerating through components. // This will be a problem if the components are added/remove during the enumeration in response // to a handler. float ellapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; foreach (int liveId in entityIdCollection) { statesWorker.Clear(); actionsWorker.Clear(); InputMap inputMap = inputMapComponents.GetComponentFor(liveId); foreach (KeyValuePair <Keys, int> pair in inputMap.KeyToAction) { if (!previousKeyboardState.IsKeyDown(pair.Key) && currentKeyboardState.IsKeyDown(pair.Key)) { actionsWorker.Add(pair.Value); } } foreach (KeyValuePair <Keys, int> pair in inputMap.KeyToState) { if (currentKeyboardState.IsKeyDown(pair.Key)) { statesWorker.Add(pair.Value); } } foreach (KeyValuePair <Buttons, int> pair in inputMap.ButtonToAction) { if (!previousGamePadState.IsButtonDown(pair.Key) && currentGamePadState.IsButtonDown(pair.Key)) { actionsWorker.Add(pair.Value); } } foreach (KeyValuePair <Buttons, int> pair in inputMap.ButtonToState) { if (currentGamePadState.IsButtonDown(pair.Key)) { statesWorker.Add(pair.Value); } } InputHandlers inputHandlers = inputHandlersComponents.GetComponentFor(liveId); foreach (int callbackId in inputHandlers.InputHandlerIds) { Callbacks.GetCallback(callbackId)(EntityManager, liveId, actionsWorker, statesWorker); } } previousKeyboardState = currentKeyboardState; previousGamePadState = currentGamePadState; }
protected override void OnProcessEntities(GameTime gameTime, IEnumerable <int> entityIdCollection) { removeIdWorker.Clear(); float ellapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; foreach (int liveId in entityIdCollection) { Placement placement = placementComponents.GetComponentFor(liveId); Bomb bomb = bombComponents.GetComponentFor(liveId); bomb.Countdown -= ellapsed; if (bomb.Countdown <= 0f) { removeIdWorker.Add(liveId); TriggerBomb(placementComponents.GetComponentFor(liveId), bomb); } } EntityManager.DelayFreeByLiveId(removeIdWorker, EntityFreeOptions.Deep); }
protected override void OnProcessEntities(GameTime gameTime, IEnumerable <int> entityIdCollection) { float ellapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; foreach (int liveId in entityIdCollection) { GameState gameState = gameStateComponents.GetComponentFor(liveId); entityGrid.Bounds = gameState.Bounds; gameState.TimeRemaining = Math.Max(0, gameState.TimeRemaining - ellapsed);; if (cachedBounds != gameState.Bounds) { cachedBounds = gameState.Bounds; Util.Helpers.CalculateDeathSpiralPoints(cachedBounds, deathBlockSpiralPoints); } MaybeSendDeathBlock(gameState); // Let's do some other tasks. if (!gameState.IsGameOver) { MessageData data = new MessageData(); EntityManager.SendMessage(Messages.QueryWinningPlayer, ref data, null); if (data.Handled) { gameState.WinningPlayerUniqueId = data.Int32; // The game is over. This is the uniqueId of the player that won. if (gameState.WinningPlayerUniqueId != EntityManager.InvalidEntityUniqueId) { gameState.IsGameOver = true; bool allocatedNew; InputHandlers ih = (InputHandlers)EntityManager.AddComponentToEntity(EntityManager.GetEntityByLiveId(liveId), ComponentTypeIds.InputHandlers, out allocatedNew); ih.InputHandlerIds.Add("RestartGame".CRC32Hash()); } } } } }
protected override void OnProcessEntities(GameTime gameTime, IEnumerable <int> entityIdCollection) { EntityManager em = this.EntityManager; for (int i = 0; i < textures.Length; i++) { textures[i].Clear(); } foreach (int liveId in entityIdCollection) { Placement placement = placementComponents.GetComponentFor(liveId); Aspect aspect = aspectComponents.GetComponentFor(liveId); if (placement.Visible) { int frameIndex = aspect.FrameIndex; int varietyIndex = aspect.VarietyIndex; Sprite sprite = spriteMapper.GetSpriteForId(aspect.ModelNameId); Texture2D texture = sprite.Texture; Rectangle rect; sprite.GetBounds(varietyIndex, frameIndex, out rect); int framePixelWidth = rect.Width; int framePixelHeight = rect.Height; int startPixelX = rect.X; int startPixelY = rect.Y; Color leColor = aspect.Tint; TexAndPos texAndPos = new TexAndPos() { Size = aspect.Size / framePixelWidth, Texture = texture, Tint = leColor, Rotation = MathHelper.ToRadians(placement.OrientationAngle + placement.AdditionalVisualOrientation), StartEndSourceRectH = new Point(startPixelX, framePixelWidth), StartEndSourceRectV = new Point(startPixelY, framePixelHeight), SpriteEffects = aspect.SpriteEffects }; texAndPos.Position = placement.Position; texAndPos.Position += placement.AdditionalVisualPosition; if (placement.Layer < textures.Length) { textures[placement.Layer].Add(texAndPos); } } } }
protected override void OnProcessEntities(GameTime gameTime, IEnumerable <int> entityIdCollection) { foreach (int liveId in entityIdCollection) { ScriptContainer scriptContainer = scriptsComponents.GetComponentFor(liveId); // Make a temp copy of things incase stuff is removed. scriptIdsWorker.Clear(); scriptIdsWorker.AddRange(scriptContainer.GetScriptIds()); foreach (int scriptId in scriptIdsWorker) { // Call each script Scripts.Scripts.GetScript(scriptId)(EntityManager, scriptContainer, liveId, gameTime); } } }
protected override void OnProcessEntities(GameTime gameTime, IEnumerable <int> entityIdCollection) { entityGrid.Clear(); float ellapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; foreach (int liveId in entityIdCollection) { Placement placement = placementComponents.GetComponentFor(liveId); int x = (int)Math.Round(placement.Position.X); int y = (int)Math.Round(placement.Position.Y); entityGrid.Assign(x, y, EntityManager.GetEntityByLiveId(liveId).UniqueId); } }
protected override void OnProcessEntities(Microsoft.Xna.Framework.GameTime gameTime, IEnumerable <int> entityIdCollection) { liveIdsRemoveWorker.Clear(); explosionTemplateWorker.Clear(); propagateListWorker.Clear(); propagateListWorkerPlacement.Clear(); float ellapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; foreach (int liveId in entityIdCollection) { Explosion explosion = explosionComponents.GetComponentFor(liveId); explosion.Countdown -= ellapsed; explosion.PropagationCountDown -= ellapsed; // Apply any impacts. These may affect propagation, so do it first. Placement placement = placementComponents.GetComponentFor(liveId); int x = (int)Math.Round(placement.Position.X); int y = (int)Math.Round(placement.Position.Y); ApplyImpacts(x, y, explosion); // Check to see if it's time to propagate. Propagation creates new entities, which we can't do inside the enumeration loop if ((explosion.PropagationCountDown <= 0f) && (explosion.State.PropagationDirection != PropagationDirection.None) && (explosion.State.Range > 0)) { explosionTemplateWorker.Add(EntityManager.GetEntityByLiveId(liveId).TemplateId); propagateListWorker.Add(explosion); propagateListWorkerPlacement.Add(placement); } // Or expire... if (explosion.Countdown <= 0f) { liveIdsRemoveWorker.Add(liveId); } } PropagateExplosions(explosionTemplateWorker, propagateListWorker, propagateListWorkerPlacement); // Remove the expired explosions // REVIEW: Since removing something after a timer is a frequent operation, we could generalize this too. EntityManager.DelayFreeByLiveId(liveIdsRemoveWorker, EntityFreeOptions.Deep); }
protected override void OnProcessEntities(GameTime gameTime, IEnumerable <int> entityIdCollection) { // We could use the physics system to handle grabbing powerups, but it is fairly straightforward to just do it here with our grid. foreach (int liveId in entityIdCollection) { Placement placement = placementComponents.GetComponentFor(liveId); entityGrid.QueryUniqueIdsAt(placement.Position.ToInteger(), uniqueIdWorker); foreach (int uniqueIdOverlap in uniqueIdWorker) { Entity possiblePlayerEntity = EntityManager.GetEntityByUniqueId(uniqueIdOverlap); PlayerInfo player = (PlayerInfo)EntityManager.GetComponent(possiblePlayerEntity, ComponentTypeIds.Player); if (player != null) { // A player will pick this up! PickUpPowerUp(possiblePlayerEntity.LiveId, player, powerUpComponents.GetComponentFor(liveId)); // Delete this powerup. EntityManager.DelayFreeByLiveId(liveId, EntityFreeOptions.Deep); break; } } } }