public void AddBodyParts(List <BodyPart> bodyParts, ref BodyNode parent) { foreach (var bodyPart in bodyParts) { AddBodyPart(bodyPart, ref parent); } }
// TODO 1: test this function more // TODO 2: balance damages // will recursively add events to BodyDamageEvent event list (per body part) private void TakeDamage(BodyNode node, Damage damage) { // Debug.Log("TAKE DAMAGE: index " + node.Index); float r1 = UnityEngine.Random.value; // unused float r2 = UnityEngine.Random.value; if (0 <= node.Index) { var bodyPart = _bodyParts[node.Index]; // get damage event from body part hp system var dmgEvent = bodyPart.TakeDamage(damage) as HpSystemDamageEvent; var bodyPartHpEvent = new BodyPartHpEvent(bodyPart, dmgEvent.HpSystemEvent); // add current event to the body event system this._eventSystem.OnEvent(bodyPartHpEvent); } // Debug.Log($"bodyPart [{node.Index}] {bodyPart.NameCustom}, children: " + node.ChildrenCount); // apply damage to node's children if (node.HasChildren) { var children = node.Children.ToArray(); var sb = new StringBuilder(); foreach (var i in children) { sb.Append(i + ", "); } // Debug.Log("children: " + sb.ToString()); // sample indices of children to pick which children will be applied damage to Vector2Int indices = Samplers.SampleFromPdf( r2, GetBodyPartSizes(new IndexedEnumerator <BodyPart>(this._bodyParts, children)), damage.Dispersion); // get the children var damagedNodes = new IndexedEnumerator <BodyNode>( this._bodyNodes, new SliceEnumerator <int>(children, indices[0], indices[1] + 1).ToArray() ); // Debug.Log("damaged nodes count " + damagedNodes.Count()); damagedNodes.Reset(); // need to reset since its an iterator // compute the damage amount passed on to children of current body part var damageAfterMultiplier = damage * damage.Penetration; foreach (var damagedNode in damagedNodes) { TakeDamage(damagedNode, damageAfterMultiplier); } } }
public Body(EBodyType bodyType) { this.BodyPartCount = 0; this.BodyType = bodyType; this.Name = BodyTypes.BodyType2String(BodyType); this._bodyParts = new BodyPart[MaxBodyPartCount]; this._bodyNodes = new BodyNode[MaxBodyPartCount]; this._rootNode = new BodyNode(-1); this.HealingAmount = 0; this.HealingAmountMultiplier = 1f; this.HealingPeriod = 0; this.TimeSinceHeal = float.MaxValue; this._eventSystem = new BodyEventSystem(this); }
public BodyNode AddBodyPart(BodyPart bodyPart, ref BodyNode parent) { if (BodyPartCount >= MaxBodyPartCount) { return(BodyNode.NullNode); } var node = new BodyNode(BodyPartCount, parent.Index); this._bodyNodes[BodyPartCount] = node; this._bodyParts[BodyPartCount] = bodyPart; parent.AddChild(BodyPartCount); BodyPartCount++; return(node); }
public Body(Body body) : this(body.BodyType) { this.BodyPartCount = body.BodyPartCount; for (int i = 0; i < this.BodyPartCount; i++) { // deep copy this._rootNode = new BodyNode(body._rootNode); this._bodyNodes[i] = new BodyNode(body._bodyNodes[i]); this._bodyParts[i] = new BodyPart(body._bodyParts[i]); } this.HealingAmount = body.HealingAmount; this.HealingAmountMultiplier = body.HealingAmountMultiplier; this.HealingPeriod = body.HealingPeriod; this.TimeSinceHeal = body.TimeSinceHeal; }
public void SetNode(BodyNode node) => this._bodyNodes[node.Index] = node;