Example #1
0
    /// <summary>
    /// Creates the sub-objects that display the individual units in a squad.
    /// </summary>
    /// <param name="units"></param>
    /// <param name="flipHorizontally"></param>
    /// <param name="offset"></param>
    private void createUnits(IEnumerable <UnitData> units, bool flipHorizontally, float offset)
    {
        if (fontMaterial == null || font == null)
        {
            Debug.LogWarning("Font material is not set on the Combat System Behavior! Ensure you are using the prefab to create the combat system!");
        }

        // Create a base object
        GameObject unitBase = new GameObject();

        unitBase.name                    = "__UNITBASE__";
        unitBase.transform.parent        = transform;
        unitBase.transform.localPosition = Vector3.zero;
        unitBase.AddComponent <MonoBehaviour>();

        foreach (UnitData data in units)
        {
            float x = (flipHorizontally ? (-1.0f + (0.33f * (1 - data.Position.Row))) : 1.0f - (0.33f * (1 - data.Position.Row))) + (data.Position.Column % 2 == 0 ? 0.1f : 0.0f);
            float y = 0.7f - (0.33f * data.Position.Column);
            float z = 0.9f - (0.05f * data.Position.Column);

            NodeSkeletonBehavior skele = (NodeSkeletonBehavior)Instantiate(unitSkeleton);
            skele.gameObject.AddComponent <UnitIdleAnimationBehavior>();

            GameObject obj = new GameObject();
            obj.AddComponent <MeshRenderer>();
            obj.transform.parent = skele.transform;

            CombatSystemUnitBehavior unitBehavior = obj.gameObject.AddComponent <CombatSystemUnitBehavior>();
            unitBehavior.unit = data.Unit;
            unitBehavior.transform.localPosition = Vector3.zero;
            unitBehavior.transform.localScale    = Vector3.one / 5.0f;
            if (font != null)
            {
                unitBehavior.SetFont(font);
            }

            if (fontMaterial != null)
            {
                unitBehavior.renderer.material = fontMaterial;
            }

            // Load body parts for the unit.
            foreach (NSSNode node in skele.SkeletonStructure.Nodes)
            {
                UnitAssetBehavior prefab;
                if (node.Name == "Body" && (unitBehavior.unit.BodyOverride != 0))
                {
                    prefab = UnitAssetRepository.Instance.getAssetGroupByName("Body").getPrefabByIndex(unitBehavior.unit.BodyOverride);
                }
                else
                {
                    prefab = (UnitAssetRepository.Instance.getAssetGroupByName(node.Name).getPrefabByName(node.Name == "Weapon" ? data.Unit.Weapon.ToString() : data.Unit.Name));
                }


                if (prefab == null)
                {
                    Debug.LogWarning(string.Format("Could not find prefab for 'Prefabs/UnitParts/{0}/001'", node.Name));
                    continue;
                }

                skele.AttachToNode(node.Name, prefab.gameObject);
            }

            skele.transform.parent = unitBase.transform;
            Vector3 scale = (Vector3.one / 2.0f);
            if (flipHorizontally)
            {
                scale.x *= -1.0f;
                Vector3 lScale = unitBehavior.transform.localScale;
                lScale.x *= -1.0f;
                unitBehavior.transform.localScale = lScale;
            }
            skele.transform.localScale    = scale;
            skele.transform.localPosition = Vector3.zero;

            skele.transform.Translate(x, y, z);
        }
    }
Example #2
0
    /// <summary>
    /// Updates the overworld visuals for the squad.
    /// </summary>
    private void updateSquadVisuals()
    {
        // Retrieve the actor to determine whether or not to flip the object.
        ActorBehavior actor = GetComponent <ActorBehavior>();

        if (actor == null)
        {
            return;
        }

        bool flipHorizontally = (actor == null ? false : actor.theSide == GameControllerBehaviour.UnitSide.player);

        // Hide any mesh renderer on this object.
        gameObject.renderer.enabled = false;

        // Clear the squad's sub units.
        List <MonoBehaviour> children = new List <MonoBehaviour>();

        children.AddRange(GetComponentsInChildren <MonoBehaviour>());
        for (int _i = (children.Count - 1); _i >= 0; _i--)
        {
            if (children[_i] == null || children[_i].gameObject == gameObject)
            {
                continue;
            }

            DestroyImmediate(children[_i].gameObject);
            children.RemoveAt(_i);
        }

        // Add the selector
        GameObject selNode = (GameObject)Instantiate(selectionNode);

        selNode.name                    = "Selection Node";
        selNode.transform.parent        = transform;
        selNode.transform.localPosition = Vector3.zero;
        this.selNode                    = selNode;

        // Determine the side of the material based on its Actor component.
        baseColor = (actor.theSide == GameControllerBehaviour.UnitSide.player ? Color.blue : Color.red);

        // Create the new sub-units.
        unitCount = squad.Units.Count;

        foreach (UnitData data in squad.Units)
        {
            float x = (flipHorizontally ? (-0.1f + (0.2f * (1 - data.Position.Row))) : 0.1f - (0.2f * (1 - data.Position.Row))) + (data.Position.Column % 2 == 0 ? 0.05f : 0.0f);
            float z = 0.25f - (0.1f * data.Position.Column);
            float y = 0.5f;

            NodeSkeletonBehavior      skele = (NodeSkeletonBehavior)Instantiate(unitSkeleton);
            UnitIdleAnimationBehavior idle  = skele.gameObject.AddComponent <UnitIdleAnimationBehavior>();
            idle.bobDistance = 0.1f;
            idle.Active      = false;

            // Load body parts for the unit.
            foreach (NSSNode node in skele.SkeletonStructure.Nodes)
            {
                UnitAssetBehavior prefab;
                if (node.Name == "Body" && (data.Unit.BodyOverride != 0))
                {
                    prefab = UnitAssetRepository.Instance.getAssetGroupByName("Body").getPrefabByIndex(data.Unit.BodyOverride);
                }
                else
                {
                    prefab = (UnitAssetRepository.Instance.getAssetGroupByName(node.Name).getPrefabByName(node.Name == "Weapon" ? data.Unit.Weapon.ToString() : data.Unit.Name));
                }

                if (prefab == null)
                {
                    Debug.LogWarning(string.Format("Could not find prefab for 'Prefabs/UnitParts/{0}/001'", node.Name));
                    continue;
                }

                skele.AttachToNode(node.Name, prefab.gameObject);
            }

            skele.transform.parent = transform;

            Vector3 scale = Vector3.one;
            if (flipHorizontally)
            {
                scale.x = -1.0f;
            }
            scale.y = 0.5f;
            skele.transform.localScale    = scale;
            skele.transform.localPosition = Vector3.zero;

            skele.transform.Translate(x, y, z);
        }
    }