2 using Microsoft.Xna.Framework;
4 using System.Collections.Generic;
62 public AttackResult(List<Affliction> afflictions,
Limb hitLimb, List<DamageModifier> appliedDamageModifiers =
null)
75 public AttackResult(
float damage, List<DamageModifier> appliedDamageModifiers =
null)
110 description:
"Secondary AI behavior after the attack. The character first executes the AfterAttack behavior, then after AfterAttackSecondaryDelay passes, switches to this one. Ignored if AfterAttackSecondaryDelay is 0 or less."),
Editable]
113 [
Serialize(0.0f,
IsPropertySaveable.Yes, description:
"How long the character executes the AfterAttack before switching to AfterAttackSecondary. The secondary behavior is ignored if this value is 0 or less."),
Editable]
122 [
Serialize(
false,
IsPropertySaveable.Yes, description:
"Should the AI try to steer away from the target when aiming with this attack? Best combined with PassiveAggressive behavior."),
Editable]
125 private float _range;
126 [
Serialize(0.0f,
IsPropertySaveable.Yes, description:
"The min distance from the attack limb to the target before the AI tries to attack."),
Editable(MinValueFloat = 0.0f, MaxValueFloat = 10000.0f)]
130 set => _range = value;
133 private float _damageRange;
134 [
Serialize(0.0f,
IsPropertySaveable.Yes, description:
"The min distance from the attack limb to the target to do damage. In distance-based hit detection, the hit will be registered as soon as the target is within the damage range, unless the attack duration has expired."),
Editable(MinValueFloat = 0.0f, MaxValueFloat = 10000.0f)]
138 set => _damageRange = value;
141 [
Serialize(0.0f,
IsPropertySaveable.Yes, description:
"Used by enemy AI to determine the minimum range required for the attack to hit."),
Editable(MinValueFloat = 0.0f, MaxValueFloat = 10000.0f)]
144 [
Serialize(0.25f,
IsPropertySaveable.Yes, description:
"An approximation of the attack duration. Effectively defines the time window in which the hit can be registered. If set to too low value, it's possible that the attack won't hit the target in time."),
Editable(MinValueFloat = 0.0f, MaxValueFloat = 10.0f, DecimalCount = 2)]
147 [
Serialize(5f,
IsPropertySaveable.Yes, description:
"How long the AI must wait before it can use this attack again."),
Editable(MinValueFloat = 0.0f, MaxValueFloat = 100.0f, DecimalCount = 2)]
151 [
Serialize(0f,
IsPropertySaveable.Yes, description:
"When the attack cooldown is running and when there are other valid attacks possible for the character to use, the secondary cooldown is used instead of the regular cooldown. Does not have an effect, if set to 0 or less than the regular cooldown value."),
Editable(MinValueFloat = 0.0f, MaxValueFloat = 100.0f, DecimalCount = 2)]
154 [
Serialize(0f,
IsPropertySaveable.Yes, description:
"A random factor applied to all cooldowns. Example: 0.1 -> adds a random value between -10% and 10% of the cooldown. Min 0 (default), Max 1 (could disable or double the cooldown in extreme cases)."),
Editable(MinValueFloat = 0, MaxValueFloat = 1, DecimalCount = 2)]
157 [
Serialize(
false,
IsPropertySaveable.Yes, description:
"When set to true, causes the enemy AI to use the fast movement animations when the attack is on cooldown."),
Editable]
160 private float _structureDamage;
165 set => _structureDamage = value;
168 [
Serialize(
false,
IsPropertySaveable.Yes, description:
"If the attack causes an explosion of wall damage shrapnel, should some of the shrapnel be launched as projectiles that can go through walls?"),
Editable]
174 private float _itemDamage;
179 set => _itemDamage = value;
182 [
Serialize(0.0f,
IsPropertySaveable.Yes, description:
"Percentage of damage mitigation ignored when hitting armored body parts (deflecting limbs)."),
Editable(MinValueFloat = 0.0f, MaxValueFloat = 1f)]
206 [
Serialize(
false,
IsPropertySaveable.Yes, description:
"When enabled the attack will not be launched when there's a friendly character in the way. Only affects ranged attacks."),
Editable]
209 [
Serialize(20f,
IsPropertySaveable.Yes, description:
"Used by enemy AI to determine how accurately the attack needs to be aimed for the attack to trigger. Only affects ranged attacks."),
Editable]
212 [
Serialize(0f,
IsPropertySaveable.Yes, description:
"By default uses the same value as RequiredAngle. Use if you want to allow selecting the attack but not shooting until the angle is smaller. Only affects ranged attacks."),
Editable]
215 [
Serialize(0f,
IsPropertySaveable.Yes, description:
"How much the attack limb is rotated towards the target. Default 0 = no rotation. Only affects ranged attacks."),
Editable]
218 [
Serialize(-1,
IsPropertySaveable.Yes, description:
"Reference to the limb we apply the aim rotation to. By default same as the attack limb. Only affects ranged attacks."),
Editable]
221 [
Serialize(0f,
IsPropertySaveable.Yes, description:
"How much the held weapon is swayed back and forth while aiming. Only affects monsters using ranged weapons (items). Default 0 means the weapon is not swayed at all."),
Editable]
224 [
Serialize(5f,
IsPropertySaveable.Yes, description:
"How fast the held weapon is swayed back and forth while aiming. Only affects monsters using ranged weapons (items)."),
Editable]
227 [
Serialize(0.0f,
IsPropertySaveable.No, description:
"Legacy functionality. Behaves otherwise the same as stuns defined as afflictions, but explosions only apply the stun once instead of dividing it between the limbs.")]
228 public float Stun {
get;
set; }
243 if (
string.IsNullOrEmpty(value)) {
return; }
244 foreach (
string limbIndexStr
in value.Split(
','))
246 if (
int.TryParse(limbIndexStr.Trim(), out
int limbIndex))
254 [
Serialize(0.0f,
IsPropertySaveable.Yes, description:
"Applied to the attacking limb (or limbs defined using ApplyForceOnLimbs). The direction of the force is towards the target that's being attacked."),
Editable(MinValueFloat = -1000.0f, MaxValueFloat = 1000.0f)]
255 public float Force {
get;
private set; }
257 [
Serialize(
"0.0, 0.0",
IsPropertySaveable.Yes, description:
"Applied to the main limb. In world space coordinates(i.e. 0, 1 pushes the character upwards a bit). The attacker's facing direction is taken into account."),
Editable]
260 [
Serialize(
"0.0, 0.0",
IsPropertySaveable.Yes, description:
"Applied to the main limb. In world space coordinates(i.e. 0, 1 pushes the character upwards a bit). The attacker's facing direction is taken into account."),
Editable]
263 [
Serialize(
"0.0, 0.0",
IsPropertySaveable.Yes, description:
"Applied to the main limb. In world space coordinates(i.e. 0, 1 pushes the character upwards a bit). The attacker's facing direction is taken into account."),
Editable]
269 [
Serialize(0.0f,
IsPropertySaveable.Yes, description:
"Applied to the attacking limb (or limbs defined using ApplyForceOnLimbs)"),
Editable(MinValueFloat = -10000.0f, MaxValueFloat = 10000.0f)]
270 public float Torque {
get;
private set; }
275 [
Serialize(0.0f,
IsPropertySaveable.Yes, description:
"Applied to the target the attack hits. The direction of the impulse is from this limb towards the target (use negative values to pull the target closer)."),
Editable(MinValueFloat = -1000.0f, MaxValueFloat = 1000.0f)]
278 [
Serialize(
"0.0, 0.0",
IsPropertySaveable.Yes, description:
"Applied to the target, in world space coordinates(i.e. 0, -1 pushes the target downwards). The attacker's facing direction is taken into account."),
Editable]
281 [
Serialize(0.0f,
IsPropertySaveable.Yes, description:
"Applied to the target the attack hits. The direction of the force is from this limb towards the target (use negative values to pull the target closer)."),
Editable(-1000.0f, 1000.0f)]
284 [
Serialize(
"0.0, 0.0",
IsPropertySaveable.Yes, description:
"Applied to the target, in world space coordinates(i.e. 0, -1 pushes the target downwards). The attacker's facing direction is taken into account."),
Editable]
287 [
Serialize(1.0f,
IsPropertySaveable.Yes, description:
"Affects the strength of the impact effects the limb causes when it hits a submarine."),
Editable(MinValueFloat = 0.0f, MaxValueFloat = 100.0f)]
298 [
Serialize(0.0f,
IsPropertySaveable.Yes, description:
"Used by enemy AI to determine the priority when selecting attacks. When random attacks are disabled on the character it is multiplied with distance to determine the which attack to use. Only attacks that are currently valid are taken into consideration when making the decision."),
Editable(MinValueFloat = 0.0f, MaxValueFloat = 1.0f)]
301 [
Serialize(
false,
IsPropertySaveable.Yes, description:
"Triggers the 'blink' animation on the attacking limbs when the attack executes. Used e.g. by abyss monsters to make their jaws close when attacking."),
Editable]
302 public bool Blink {
get;
private set; }
306 get {
return statusEffects; }
309 public string Name =>
"Attack";
315 } =
new Dictionary<Identifier, SerializableProperty>();
321 public readonly Dictionary<Affliction, XElement>
Afflictions =
new Dictionary<Affliction, XElement>();
326 public List<PropertyConditional>
Conditionals {
get;
private set; } =
new List<PropertyConditional>();
332 private readonly List<StatusEffect> statusEffects =
new List<StatusEffect>();
336 if (statusEffects ==
null) {
return; }
348 List<Affliction> multipliedAfflictions =
new List<Affliction>();
351 multipliedAfflictions.Add(affliction.
CreateMultiplied(multiplier, affliction));
353 return multipliedAfflictions;
369 return (
Duration == 0.0f) ? dmg : dmg * deltaTime;
382 public Attack(
float damage,
float bleedingDamage,
float burnDamage,
float structureDamage,
float itemDamage,
float range = 0.0f)
408 DebugConsole.ThrowError(
"Error in Attack (" + parentDebugName +
") - Define damage as afflictions instead of using the damage attribute (e.g. <Affliction identifier=\"internaldamage\" strength=\"10\" />).",
419 InitProjSpecific(element);
421 foreach (var subElement
in element.Elements())
423 switch (subElement.Name.ToString().ToLowerInvariant())
430 if (subElement.GetAttribute(
"name") !=
null)
432 DebugConsole.ThrowError(
"Error in Attack (" + parentDebugName +
") - define afflictions using identifiers instead of names.",
433 contentPackage: element.ContentPackage);
434 string afflictionName = subElement.GetAttributeString(
"name",
"").ToLowerInvariant();
435 afflictionPrefab =
AfflictionPrefab.
List.FirstOrDefault(ap => ap.Name.Equals(afflictionName, System.StringComparison.OrdinalIgnoreCase));
436 if (afflictionPrefab ==
null)
438 DebugConsole.ThrowError(
"Error in Attack (" + parentDebugName +
") - Affliction prefab \"" + afflictionName +
"\" not found.",
439 contentPackage: element.ContentPackage);
445 Identifier afflictionIdentifier = subElement.GetAttributeIdentifier(
"identifier",
"");
448 DebugConsole.ThrowError(
"Error in Attack (" + parentDebugName +
") - Affliction prefab \"" + afflictionIdentifier +
"\" not found.",
449 contentPackage: element.ContentPackage);
462 DebugConsole.AddWarning($
"Potentially misconfigured attack in {parentDebugName}. Secondary cooldown should not be longer than the primary cooldown.",
463 contentPackage: element.ContentPackage);
471 foreach (var subElement
in element.GetChildElements(
"affliction"))
474 Identifier afflictionIdentifier = subElement.GetAttributeIdentifier(
"identifier",
"");
477 DebugConsole.ThrowError($
"Error in an Attack defined in \"{parentDebugName}\" - could not find an affliction with the identifier \"{afflictionIdentifier}\".",
478 contentPackage: element.ContentPackage);
481 affliction = afflictionPrefab.Instantiate(0.0f);
493 if (affliction.
Value !=
null)
511 if (targetCharacter !=
null && !targetCharacter.
IsHuman)
519 DamageParticles(deltaTime, worldPosition);
522 var attackResult = target?.
AddDamage(attacker, worldPosition,
this, impulseDirection, deltaTime, playSound) ??
new AttackResult();
523 var conditionalEffectType = attackResult.Damage > 0.0f ?
ActionType.OnSuccess :
ActionType.OnFailure;
525 if (targetCharacter !=
null && targetCharacter.
IsDead)
536 if (additionalEffectType !=
ActionType.OnEating)
538 effect.
Apply(conditionalEffectType, deltaTime, attacker, t, worldPosition);
540 effect.
Apply(additionalEffectType, deltaTime, attacker, t, worldPosition);
544 if (additionalEffectType !=
ActionType.OnEating)
546 effect.
Apply(conditionalEffectType, deltaTime, attacker, attacker);
548 effect.
Apply(additionalEffectType, deltaTime, attacker, attacker);
550 if (targetCharacter !=
null)
554 if (additionalEffectType !=
ActionType.OnEating)
556 effect.
Apply(conditionalEffectType, deltaTime, targetCharacter, attackResult.HitLimb);
558 effect.
Apply(additionalEffectType, deltaTime, targetCharacter, attackResult.HitLimb);
562 var targets = targetCharacter.AnimController.Limbs;
563 if (additionalEffectType !=
ActionType.OnEating)
565 effect.
Apply(conditionalEffectType, deltaTime, targetCharacter, targets);
567 effect.
Apply(additionalEffectType, deltaTime, targetCharacter, targets);
570 if (target is
Entity targetEntity)
577 if (additionalEffectType !=
ActionType.OnEating)
579 effect.
Apply(conditionalEffectType, deltaTime, targetEntity, targets);
581 effect.
Apply(additionalEffectType, deltaTime, targetEntity, targets);
585 if (additionalEffectType !=
ActionType.OnEating)
595 targets.AddRange(attacker.Inventory.AllItems);
596 if (additionalEffectType !=
ActionType.OnEating)
598 effect.
Apply(conditionalEffectType, deltaTime, attacker, targets);
600 effect.
Apply(additionalEffectType, deltaTime, attacker, targets);
607 readonly List<ISerializableEntity> targets =
new List<ISerializableEntity>();
610 if (targetLimb ==
null)
625 DamageParticles(deltaTime, worldPosition);
633 if (penetrationValue.HasValue)
635 penetration += penetrationValue.Value;
638 Vector2 impulseDirection = GetImpulseDirection(targetLimb, worldPosition,
SourceItem);
639 var attackResult = targetLimb.
character.
ApplyAttack(attacker, worldPosition,
this, deltaTime, impulseDirection, playSound, targetLimb, penetration);
652 effect.
Apply(conditionalEffectType, deltaTime, attacker, attacker);
657 effect.
Apply(conditionalEffectType, deltaTime, targetLimb.character, targetLimb.character);
658 effect.
Apply(
ActionType.OnUse, deltaTime, targetLimb.character, targetLimb.character);
662 effect.
Apply(conditionalEffectType, deltaTime, targetLimb.character, targetLimb);
663 effect.
Apply(
ActionType.OnUse, deltaTime, targetLimb.character, targetLimb);
667 var targets = targetLimb.character.AnimController.Limbs;
668 effect.
Apply(conditionalEffectType, deltaTime, targetLimb.character, targets);
669 effect.
Apply(
ActionType.OnUse, deltaTime, targetLimb.character, targets);
676 effect.
Apply(conditionalEffectType, deltaTime, targetLimb.character, targets);
677 effect.
Apply(
ActionType.OnUse, deltaTime, targetLimb.character, targets);
682 targets.AddRange(attacker.Inventory.AllItems);
683 effect.
Apply(conditionalEffectType, deltaTime, attacker, targets);
691 private Vector2 GetImpulseDirection(
ISpatialEntity target, Vector2 sourceWorldPosition,
Item sourceItem)
693 Vector2 impulseDirection = Vector2.Zero;
696 impulseDirection = target.
WorldPosition - sourceWorldPosition;
705 var projectileComponent = sourceItem?.GetComponent<
Projectile>();
706 if (projectileComponent !=
null)
712 if (impulseDirection.LengthSquared() > 0.0001f)
714 impulseDirection = Vector2.Normalize(impulseDirection);
716 return impulseDirection;
778 partial
void DamageParticles(
float deltaTime, Vector2 worldPosition);
784 foreach (var context
in contexts)
void Deserialize(XElement element)
float GetVitalityDecrease(CharacterHealth characterHealth)
Affliction CreateMultiplied(float multiplier, Affliction affliction)
void Serialize(XElement element)
Character Source
Which character gave this affliction
readonly AfflictionPrefab Prefab
AfflictionPrefab is a prefab that defines a type of affliction that can be applied to a character....
Affliction Instantiate(float strength, Character source=null)
static AfflictionPrefab InternalDamage
static IEnumerable< AfflictionPrefab > List
static AfflictionPrefab Burn
static AfflictionPrefab Bleeding
static readonly PrefabCollection< AfflictionPrefab > Prefabs
Attacks are used to deal damage to characters, structures and items. They can be defined in the weapo...
void Deserialize(ContentXElement element, string parentDebugName)
float GetStructureDamage(float deltaTime)
AttackResult DoDamageToLimb(Character attacker, Limb targetLimb, Vector2 worldPosition, float deltaTime, bool playSound=true, PhysicsBody sourceBody=null, Limb sourceLimb=null)
Attack(ContentXElement element, string parentDebugName, Item sourceItem)
float CurrentRandomCoolDown
bool IsValidTarget(Entity target)
void UpdateCoolDown(float deltaTime)
float SecondaryCoolDownTimer
bool IsValidContext(IEnumerable< AttackContext > contexts)
float RangeMultiplier
Used for multiplying all the ranges.
void Serialize(ContentXElement element)
bool EmitStructureDamageParticles
Dictionary< Identifier, SerializableProperty > SerializableProperties
bool IsValidContext(AttackContext context)
float CoolDownRandomFactor
float RequiredAngleToShoot
void SetCoolDown(bool applyRandom)
TransitionMode RootTransitionEasing
AIBehaviorAfterAttack AfterAttackSecondary
float GetLevelWallDamage(float deltaTime)
readonly List< int > ForceOnLimbIndices
Vector2 CalculateAttackPhase(TransitionMode easing=TransitionMode.Linear)
float ImpactMultiplier
Used for multiplying the physics forces.
float GetTotalDamage(bool includeStructureDamage=false)
AIBehaviorAfterAttack AfterAttack
void UpdateAttackTimer(float deltaTime, Character character)
Vector2 RootForceWorldStart
bool IsValidTarget(AttackTarget targetType)
bool FullSpeedAfterAttack
Vector2 TargetImpulseWorld
Vector2 RootForceWorldEnd
Attack(float damage, float bleedingDamage, float burnDamage, float structureDamage, float itemDamage, float range=0.0f)
void ReloadAfflictions(ContentXElement element, string parentDebugName)
float AfterAttackSecondaryDelay
float DamageMultiplier
Used for multiplying all the damage.
bool CreateWallDamageProjectiles
void SetUser(Character user)
AttackResult DoDamage(Character attacker, IDamageable target, Vector2 worldPosition, float deltaTime, bool playSound=true, PhysicsBody sourceBody=null, Limb sourceLimb=null)
Vector2 RootForceWorldMiddle
float SubmarineImpactMultiplier
List< Affliction > GetMultipliedAfflictions(float multiplier)
IEnumerable< StatusEffect > StatusEffects
readonly Dictionary< Affliction, XElement > Afflictions
float GetItemDamage(float deltaTime, float multiplier=1)
HitDetection HitDetectionType
List< PropertyConditional > Conditionals
Only affects ai decision making. All the conditionals has to be met in order to select the attack....
float SeverLimbsProbability
Attack(ContentXElement element, string parentDebugName)
AttackResult ApplyAttack(Character attacker, Vector2 worldPosition, Attack attack, float deltaTime, Vector2 impulseDirection, bool playSound=false, Limb targetLimb=null, float penetration=0f)
Apply the specified attack to this character. If the targetLimb is not specified, the limb closest to...
ContentPackage? ContentPackage
XAttribute? GetAttribute(string name)
readonly Character character
Conditionals are used by some in-game mechanics to require one or more conditions to be met for those...
static IEnumerable< PropertyConditional > FromXElement(ContentXElement element, Predicate< XAttribute >? predicate=null)
static Dictionary< Identifier, SerializableProperty > DeserializeProperties(object obj, XElement element=null)
static void SerializeProperties(ISerializableEntity obj, XElement element, bool saveIfDefault=false, bool ignoreEditable=false)
StatusEffects can be used to execute various kinds of effects: modifying the state of some entity in ...
bool HasTargetType(TargetType targetType)
void SetUser(Character user)
static StatusEffect Load(ContentXElement element, string parentDebugName)
void AddNearbyTargets(Vector2 worldPosition, List< ISerializableEntity > targets)
virtual void Apply(ActionType type, float deltaTime, Entity entity, ISerializableEntity target, Vector2? worldPosition=null)
AttackResult AddDamage(Character attacker, Vector2 worldPosition, Attack attack, Vector2 impulseDirection, float deltaTime, bool playSound=true)
ActionType
ActionTypes define when a StatusEffect is executed.
@ FollowThroughUntilCanAttack
@ FollowThroughWithoutObstacleAvoidance
readonly List< DamageModifier > AppliedDamageModifiers
readonly List< Affliction > Afflictions
AttackResult(float damage, List< DamageModifier > appliedDamageModifiers=null)
AttackResult(List< Affliction > afflictions, Limb hitLimb, List< DamageModifier > appliedDamageModifiers=null)