// This one is fascinating. We add this to our entitys animation component instance which // contains a list of GeneralAnimations, this method is the action that is stored in the // general animation. This action will then be performed in another system and can do it's // own stuff independently of this system later on. Good for when we want to delete the sprite // later on. public void AttachNewDeathFadeAwayAnimation(GeneralAnimation animation, int entityKey) { animation.Animation = delegate(double currentTime) { // if more time has passed since the start of the animation // then the specified lenght that we want it to run var elapsedTime = currentTime - animation.StartOfAnimation; var sprite = ComponentManager.Instance.GetEntityComponentOrDefault <SpriteComponent>(entityKey); if (sprite == null) { return; } // This will make the blood disappear in a fading fashion. if (animation.Length < elapsedTime) { if (sprite.Alpha > 0) { // fading rate can be adjusted here. sprite.Alpha -= 0.0001f; return; } // ComponentManager.Instance.RemoveComponentFromEntity(typeof(SpriteAnimationComponent), entityKey); // ComponentManager.Instance.RemoveComponentFromEntity(typeof(HealthComponent), entityKey); // ComponentManager.Instance.RemoveComponentFromEntity(typeof(SpriteComponent), entityKey); animation.IsDone = true; } }; }
private void HandleTurnAroundEvent(InputEvent moveEvent) { if (moveEvent.KeyEvent != ActionBindings.KeyEvent.KeyPressed) { return; } var moveComponent = ComponentManager.GetEntityComponentOrDefault <MoveComponent>(moveEvent.EntityId); if (moveComponent == null) { return; } var animationComponent = GetOrCreateDefault(moveEvent.EntityId); var animation = new GeneralAnimation { AnimationType = TurnAroundEventName, StartOfAnimation = moveEvent.EventTime, Unique = true, Length = 220 }; NewTurningAnimation(moveEvent.EntityId, animation, moveComponent); animationComponent.Animations.Add(animation); }
public void NewWanderAnimation(int entityId, GeneralAnimation generalAnimation) { var moveComponent = ComponentManager.GetEntityComponentOrDefault <MoveComponent>(entityId); var aiComponent = ComponentManager.GetEntityComponentOrDefault <AIComponent>(entityId); double start = moveComponent.Direction; double target = Random.NextDouble() * MathHelper.TwoPi % MathHelper.TwoPi; generalAnimation.Animation = delegate(double currentTime) { if (!aiComponent.Wander) { generalAnimation.IsDone = true; return; } var elapsedTime = currentTime - generalAnimation.StartOfAnimation; if (elapsedTime > generalAnimation.Length) { target = Random.NextDouble() * MathHelper.TwoPi % MathHelper.TwoPi; start = moveComponent.Direction; generalAnimation.StartOfAnimation = currentTime; } //Algorithm for turning stepwise on each iteration //Modulus is for when the direction makes a whole turn moveComponent.Direction = (float) ((start + (target - start) / generalAnimation.Length * elapsedTime) % MathHelper.TwoPi); }; }
// This method is called in the handleFireWeapon method to create the bullet // that is fired from the entity. It will give the bullet all the necessary components. public void CreateBullet(InputEvent inputEvent, SpriteComponent bulletSpriteComponent, WeaponComponent weaponComponent, MoveComponent moveComponent, RenderComponent renderComponent, PositionComponent positionComponent) { // We create an new position instance for the bullet that starts from the player but should // not be the same as the players, as we found out when we did our test, otherwise the player // will follow the same way ass the bullet. var bulletPositionComponent = new PositionComponent() { Position = new Vector2(positionComponent.Position.X, positionComponent.Position.Y), ZIndex = positionComponent.ZIndex }; int bulletEntityId = EntityManager.GetEntityManager().NewEntity(); var bulletRenderComponent = new RenderComponent() { DimensionsComponent = new DimensionsComponent() { Height = 10, Width = 10 } }; var bulletMoveComponent = new MoveComponent() { AccelerationSpeed = 0, Speed = 1000, MaxVelocitySpeed = 1000, Direction = moveComponent.Direction }; var bulletComponent = new BulletComponent() { Damage = weaponComponent.Damage, ShooterEntityId = inputEvent.EntityId }; var bulletCollisionComponent = new CollisionComponent(); var animationComponent = new AnimationComponent(); ComponentManager.AddComponentToEntity(bulletPositionComponent, bulletEntityId); ComponentManager.AddComponentToEntity(bulletComponent, bulletEntityId); ComponentManager.AddComponentToEntity(bulletSpriteComponent, bulletEntityId); ComponentManager.AddComponentToEntity(bulletMoveComponent, bulletEntityId); ComponentManager.AddComponentToEntity(bulletRenderComponent, bulletEntityId); ComponentManager.AddComponentToEntity(bulletCollisionComponent, bulletEntityId); ComponentManager.AddComponentToEntity(animationComponent, bulletEntityId); var animation = new GeneralAnimation() { AnimationType = "BulletAnimation", StartOfAnimation = inputEvent.EventTime, Length = 2000, Unique = true }; NewBulletAnimation(animation, bulletEntityId); animationComponent.Animations.Add(animation); }
// Used for animating blood when entities die. // They will ned to have the SpriteAnimationComponent. private void DeadEntities(GameTime gameTime) { var healthEntities = ComponentManager.Instance.GetEntitiesWithComponent(typeof(HealthComponent)); foreach (var entity in healthEntities) { // Better yet would be to use a component to determine if they should be deleted // then when they should be deleted, and be able to get the associated components. var healthComponent = entity.Value as HealthComponent; if (!healthComponent.Alive) { // We want to remove all the components for the entity except for the // spriteComponent and health, we need them still. ComponentManager.Instance.RemoveComponentFromEntity(typeof(CameraFollowComponent), entity.Key); ComponentManager.Instance.RemoveComponentFromEntity(typeof(PlayerComponent), entity.Key); ComponentManager.Instance.RemoveComponentFromEntity(typeof(SoundComponent), entity.Key); ComponentManager.Instance.RemoveComponentFromEntity(typeof(WeaponComponent), entity.Key); ComponentManager.Instance.RemoveComponentFromEntity(typeof(ActionBindings), entity.Key); ComponentManager.Instance.RemoveComponentFromEntity(typeof(CollisionComponent), entity.Key); ComponentManager.Instance.RemoveComponentFromEntity(typeof(MoveComponent), entity.Key); ComponentManager.Instance.RemoveComponentFromEntity(typeof(AIComponent), entity.Key); //TODO reinsert removals var lightComponent = ComponentManager.Instance.GetEntityComponentOrDefault <LightComponent>(entity.Key); if (lightComponent != null) { lightComponent.Light.Enabled = false; } var animationComponent = ComponentManager.Instance.GetEntityComponentOrDefault <AnimationComponent>(entity.Key); if (animationComponent == null) { animationComponent = new AnimationComponent(); ComponentManager.Instance.AddComponentToEntity(animationComponent, entity.Key); } // For this animation we need the lenght (to when we'll start fading the blood) // this animation also needs to be unique, which means we won't create it // every time we come to this system. Also the gametime is needed. var animation = new GeneralAnimation() { AnimationType = "BloodPool", StartOfAnimation = gameTime.TotalGameTime.TotalMilliseconds, Length = 6000, Unique = true }; // Now we add it. AttachNewDeathFadeAwayAnimation(animation, entity.Key); animationComponent.Animations.Add(animation); } } }
public void BeginWander(int entityId, double startTime) { var animationComponent = GetOrCreateDefault(entityId); var animation = new GeneralAnimation { AnimationType = "AiWander", StartOfAnimation = startTime, Unique = true, Length = 5000 }; NewWanderAnimation(entityId, animation); animationComponent.Animations.Add(animation); }
public void NewTurningAnimation(int entityId, GeneralAnimation generalAnimation, MoveComponent moveComponent) { double start = moveComponent.Direction; double target = start + Math.PI; generalAnimation.Animation = delegate(double currentTime) { var elapsedTime = currentTime - generalAnimation.StartOfAnimation; if (elapsedTime > generalAnimation.Length) { generalAnimation.IsDone = true; } //Algorithm for turning stepwise on each iteration //Modulus is for when the direction makes a whole turn moveComponent.Direction = (float) ((start + (target - start) / generalAnimation.Length * elapsedTime) % MathHelper.TwoPi); }; }
// Animation for when the bullet should be deleted. public void NewBulletAnimation(GeneralAnimation generalAnimation, int entityId) { generalAnimation.Animation = delegate(double currentTimeInMilliseconds) { Debug.WriteLine("Bullet " + entityId + " currTime:" + currentTimeInMilliseconds + ", startTime:" + generalAnimation.StartOfAnimation + ", length:" + generalAnimation.Length); if (currentTimeInMilliseconds - generalAnimation.StartOfAnimation > generalAnimation.Length) { Debug.WriteLine("Remove bullet."); var tagComponent = ComponentManager.GetEntityComponentOrDefault <TagComponent>(entityId); if (tagComponent == null) { throw new Exception( "Entity does not have a tag component which is needed to remove the entity."); } tagComponent.Tags.Add(Tag.Delete); generalAnimation.IsDone = true; } }; }