public override void Update(float deltaTime) { foreach (Entity e in getApplicableEntities()) { //Grab needed components SimpleEnemyComponent simpEnemyComp = ( SimpleEnemyComponent )e.getComponent(GlobalVars.SIMPLE_ENEMY_COMPONENT_NAME); VelocityComponent velComp = ( VelocityComponent )e.getComponent(GlobalVars.VELOCITY_COMPONENT_NAME); float speedyBuffer = 0; if (!simpEnemyComp.checkCliff && simpEnemyComp.checkCliffOrig && Math.Abs(velComp.x) <= Math.Abs(simpEnemyComp.mySpeed + speedyBuffer)) { simpEnemyComp.checkCliff = simpEnemyComp.checkCliffOrig; } if (simpEnemyComp.hasRunOnce && !simpEnemyComp.hasLandedOnce && velComp.y <= 0) { simpEnemyComp.hasLandedOnce = true; } if (velComp.x < 0) { simpEnemyComp.movingLeft = true; simpEnemyComp.wasStoppedLastFrame = false; } else if (velComp.x > 0) { simpEnemyComp.movingLeft = false; simpEnemyComp.wasStoppedLastFrame = false; } else if (velComp.x == 0 && velComp.y == 0 && simpEnemyComp.hasLandedOnce) //If it's been stopped for more than one frame, try changing the direction and see if it can move that way instead. { float newVel = simpEnemyComp.mySpeed; if (!simpEnemyComp.movingLeft) { newVel *= -1; } if (simpEnemyComp.wasStoppedLastFrame) { velComp.x = newVel; } simpEnemyComp.wasStoppedLastFrame = true; } //Change position if it's about to fall off a cliff, and checkCliff is true. if (simpEnemyComp.hasLandedOnce && velComp.y == 0 && simpEnemyComp.checkCliff) { PositionComponent posComp = ( PositionComponent )e.getComponent(GlobalVars.POSITION_COMPONENT_NAME); ColliderComponent colComp = ( ColliderComponent )e.getComponent(GlobalVars.COLLIDER_COMPONENT_NAME); //Separated out for easy changing. float e1X = posComp.x; float e1Y = posComp.y; float e1Width = colComp.width; float e1Height = colComp.height; //Center width/height values if (e1Width != posComp.width) { float diff = (posComp.width - e1Width); e1X += diff / 2; } if (e1Height != posComp.height) { float diff = (posComp.height - e1Height); e1Y += diff / 2; } List <Entity> collisionsAheadAndBelow = level.getCollisionSystem().findObjectAtPoint(e1X + getSign(velComp.x) * (e1Width / 2 + 1), e1Y + e1Height / 2 + 1); //Remove spikes from the collisions List <Entity> entsToRemove = new List <Entity>(); foreach (Entity ent in collisionsAheadAndBelow) { if (ent is SpikeEntity) { entsToRemove.Add(ent); } } foreach (Entity ent in entsToRemove) { collisionsAheadAndBelow.Remove(ent); } if (collisionsAheadAndBelow.Count <= 0) { velComp.x = -velComp.x; } //If there are any bounced bullets, decrease their timer - remove if it's been enough time since bounce. if (simpEnemyComp.bouncedBullets.Count > 0) { List <Entity> toRemove = simpEnemyComp.bouncedBullets.Keys.ToList <Entity>(); foreach (Entity bullet in toRemove) { simpEnemyComp.bouncedBullets[bullet] -= deltaTime; if (simpEnemyComp.bouncedBullets[bullet] < 0) { simpEnemyComp.bouncedBullets.Remove(bullet); } } } } //Face the right way if (e is SimpleEnemyEntity) { SimpleEnemyEntity enemy = ( SimpleEnemyEntity )e; if (velComp.x > 0 && !enemy.isLookingRight()) { enemy.faceRight(); } if (velComp.x < 0 && !enemy.isLookingLeft()) { enemy.faceLeft(); } } else if (e is FlyingEnemyEntity) { FlyingEnemyEntity enemy = ( FlyingEnemyEntity )e; if (velComp.x > 0 && !enemy.isLookingRight()) { enemy.faceRight(); } if (velComp.x < 0 && !enemy.isLookingLeft()) { enemy.faceLeft(); } } simpEnemyComp.hasRunOnce = true; } }