Melee dual wield weapon

2020-09-14  本文已影响0人  iqxtreme

TODO:

TO FIX:

Implementation

$Modify the class MeleeWeapon
case WeaponBone.DualHands:
                    // just let the root be the bone for dual weapon shell node.
                    bone = character.transform;
                    break;
if (this.attachment == WeaponBone.DualHands)
                this.ProcessDualHandsWeaponInstance(character, instance);
//< geo for dual wield weapon
        public void ProcessDualHandsWeaponInstance(CharacterAnimator character, GameObject dualWieldWeaponInstance)
        {
            // Get the dual wield component.
            DualWieldBladeComponent dwBlade = dualWieldWeaponInstance.GetComponentInChildren<DualWieldBladeComponent>();
            // for left hand
            this.__AttachWeaponToCharacterBone(dwBlade.weaponLeft, character, HumanBodyBones.LeftHand);
            // for right hand
            this.__AttachWeaponToCharacterBone(dwBlade.weaponRight, character, HumanBodyBones.RightHand);
        }
        private void __AttachWeaponToCharacterBone(Transform weapon, CharacterAnimator character, HumanBodyBones bone)
        {
            Transform node = character.animator.GetBoneTransform(bone);
            if (node)
            {
                weapon.SetParent(node);
                weapon.localPosition = Vector3.zero;
                weapon.localRotation = Quaternion.identity;
                weapon.localScale = Vector3.one;
            }
        }
        //>
$Modify the class BladeComponent
$Write a subclass DualWieldBladeComponent from BladeComponent
using System.Collections.Generic;
using UnityEngine;

using GameCreator.Melee;

public class DualWieldBladeComponent : BladeComponent
{
    [Header("Dual Wield Weapon Setting")]
    [Tooltip("The weapon node of left or right hand.")]
    public Transform weaponLeft, weaponRight;
    [Tooltip("The blade component of left or right hand(optional")]
    public BladeComponent bladeLeft, bladeRight;

    override public void Awake()
    {
        // block BladeComponent.Awake()

        // get bladeLeft and bladeRight
        if (!this.bladeLeft)
            this.bladeLeft = this.weaponLeft.GetComponentInChildren<BladeComponent>();
        if (!this.bladeRight)
            this.bladeRight = this.weaponRight.GetComponentInChildren<BladeComponent>();
    }

    private void OnDestroy()
    {
        // destroy the weapons
        if (this.weaponLeft)
            Destroy(this.weaponLeft.gameObject);
        if (this.weaponRight)
            Destroy(this.weaponRight.gameObject);
    }

    /// <summary>
    /// Override the BladeComponent.Setup().
    /// Call subweapons Setup().
    /// </summary>
    /// <param name="melee"></param>
    override public void Setup(CharacterMelee melee)
    {
        base.Setup(melee);

        if (this.bladeLeft)
            this.bladeLeft.Setup(melee);

        if (this.bladeRight)
            this.bladeRight.Setup(melee);
    }
    /// <summary>
    /// just call sub weapons' CaptureHits().
    /// </summary>
    /// <returns></returns>
    public override GameObject[] CaptureHits()
    {
        GameObject[] candidatesLeft = EMPTY_GO_LIST, candidatesRight = EMPTY_GO_LIST;

        if (this.bladeLeft)
            candidatesLeft = this.bladeLeft.CaptureHits();

        if (this.bladeRight)
            candidatesRight = this.bladeRight.CaptureHits();

        var ret = new List<GameObject>(candidatesLeft.Length + candidatesRight.Length);
        ret.AddRange(candidatesLeft);
        ret.AddRange(candidatesRight);

        return ret.ToArray();
    }
}

Now we have a new melee framework supporting daul wield weapon.

How to use it

$The Weapon Prefab
$The Weapon
上一篇 下一篇

猜你喜欢

热点阅读