public void Update(GameTime gameTime) { ComponentManager cm = ComponentManager.GetInstance(); foreach (var entity in cm.GetComponentsOfType <KnockbackComponent>()) { KnockbackComponent knockbackComponent = (KnockbackComponent)entity.Value; MoveComponent moveComponent = cm.GetComponentForEntity <MoveComponent>(entity.Key); if (knockbackComponent.KnockbackActive) { moveComponent.Velocity = knockbackComponent.KnockbackDir; knockbackComponent.Cooldown -= (float)gameTime.ElapsedGameTime.TotalSeconds; if (cm.HasEntityComponent <SoundComponent>(entity.Key)) { cm.GetComponentForEntity <SoundComponent>(entity.Key).Sounds["Walk"].Action = SoundAction.Pause; } if (knockbackComponent.Cooldown <= 0.0f) { knockbackComponent.KnockbackActive = false; moveComponent.Velocity = new Vector2(0, 0); moveComponent.Direction = knockbackComponent.prevDir.ToPoint(); } } } }
// Knockback private void ApplyKnockbackToEntity(int entityHit, int attacker, int damage, GameTime gameTime) { ComponentManager cm = ComponentManager.GetInstance(); KnockbackComponent knockbackComponent = cm.GetComponentForEntity <KnockbackComponent>(entityHit); PositionComponent posComp = cm.GetComponentForEntity <PositionComponent>(entityHit); MoveComponent moveComp = cm.GetComponentForEntity <MoveComponent>(entityHit); Vector2 posCompAttacker = cm.GetComponentForEntity <PositionComponent>(attacker).Position; int attackDmg = damage; Vector2 newDir = new Vector2(posComp.Position.X - posCompAttacker.X, posComp.Position.Y - posCompAttacker.Y); knockbackComponent.prevDir = moveComp.Direction.ToVector2(); knockbackComponent.KnockbackDir = Vector2.Normalize(newDir * attackDmg + new Vector2(5, 5)); knockbackComponent.Cooldown = attackDmg / 40.0f; knockbackComponent.KnockbackActive = true; }
public void Update(GameTime gameTime) { ComponentManager cm = ComponentManager.GetInstance(); foreach (var entity in cm.GetComponentsOfType <ArmComponent>()) { ArmComponent armComp = (ArmComponent)entity.Value; MoveComponent moveComp = cm.GetComponentForEntity <MoveComponent>(armComp.playerID); AnimationGroupComponent armAnimation = cm.GetComponentForEntity <AnimationGroupComponent>(entity.Key); KnockbackComponent knockbackComp = cm.GetComponentForEntity <KnockbackComponent>(armComp.playerID); if (knockbackComp != null && knockbackComp.KnockbackActive) { return; } if (!cm.HasEntityComponent <AnimationGroupComponent>(armComp.playerID)) { return; } AnimationGroupComponent playerAnimation = cm.GetComponentForEntity <AnimationGroupComponent>(armComp.playerID); int animation = GetAnimationRow(moveComp.Direction); int walking = cm.GetComponentForEntity <MoveComponent>(armComp.playerID).Velocity != new Vector2(0.0f, 0.0f) ? 4 : 0; int attacking = cm.GetComponentForEntity <AttackComponent>(armComp.playerID).AttackCooldown > 0.0f ? 4 : 0; if (playerAnimation.ActiveAnimation != animation + walking) { playerAnimation.ActiveAnimation = animation + walking; } if (armAnimation.ActiveAnimation != animation + attacking) { armAnimation.ActiveAnimation = animation + attacking; } if (cm.HasEntityComponent <InventoryComponent>(armComp.playerID)) { InventoryComponent invenComp = cm.GetComponentForEntity <InventoryComponent>(armComp.playerID); ChangeEquipmentDirection(cm, ref invenComp, 0, animation + attacking); ChangeEquipmentDirection(cm, ref invenComp, 1, animation + walking); ChangeEquipmentDirection(cm, ref invenComp, 2, animation + walking); } } }