Esempio n. 1
0
    protected virtual void Start()
    {
        infoModule = GetComponent <EntityInfo>();

        animationModule = new EntityAnimation(this);
        inventoryModule = new EntityInventory(this);
        movementModule  = new EntityMovement(this);
        visualModule    = new EntityVisual(this);

        characterController = GetComponent <CharacterController>();

        Game.OnCreatedEntity(this);

        if (postStartAction != null)
        {
            postStartAction();
        }
    }
        EntityVisual CreateVisual(MARSEntity entity, SceneType sceneType)
        {
            EntityVisual visual = null;

            // Collect all traits from conditions
            var traits     = new HashSet <string>();
            var conditions = entity.GetComponents <ICondition>();

            foreach (var condition in conditions)
            {
                if (!condition.enabled)
                {
                    continue;
                }

                var tagCondition = condition as ISemanticTagCondition; // Skip traits from exclude tag conditions
                if (tagCondition != null && tagCondition.matchRule == SemanticTagMatchRule.Exclude)
                {
                    continue;
                }

                traits.Add(condition.traitName);
            }

            // Also collect traits from multi-conditions
            var multiconditions = entity.GetComponents <MultiConditionBase>();

            foreach (var multicondition in multiconditions)
            {
                if (multicondition.HostedComponents != null)
                {
                    foreach (var subcondition in multicondition.HostedComponents)
                    {
                        var tagCondition = subcondition as ISemanticTagCondition; // Skip traits from exclude tag conditions
                        if (tagCondition != null && tagCondition.matchRule == SemanticTagMatchRule.Exclude)
                        {
                            continue;
                        }

                        traits.Add(subcondition.traitName);
                    }
                }
            }

            // First check mappings, the first matching trait in the mappings list will determine the visual
            foreach (var visualMapping in m_VisualMappings)
            {
                var prefab    = visualMapping.visualPrefab;
                var traitName = visualMapping.trait;
                if (prefab != null && traits.Contains(traitName))
                {
                    visual = CreateVisualFromPrefab(entity, prefab);
                    break;
                }
            }

            // If there is no prefab for the entity's traits, create the default
            if (visual == null)
            {
                visual = CreateVisualFromPrefab(entity, m_DefaultVisual);
            }

            if (visual == null)
            {
                return(null);
            }

#if UNITY_EDITOR
            var selected = Selection.gameObjects.Contains(entity.gameObject);
            visual.InteractionTarget.SetSelected(selected);
#endif

            // Check that the scene type we are using has a visuals root and set the visuals to that root
            switch (sceneType)
            {
            case SceneType.Simulation:
                if (m_SimulationSceneVisualsRoot == null)
                {
                    m_SimulationSceneVisualsRoot = new GameObject(k_VisualsRootName);
                    m_SimulationSceneVisualsRoot.transform.SetParent(simEntitiesRoot);
                    m_SimulationSceneVisualsRoot.hideFlags = k_VisualsRootHideFlags;
                }

                visual.transform.SetParent(m_SimulationSceneVisualsRoot.transform, true);
                visual.gameObject.SetLayerAndHideFlagsRecursively(entity.gameObject.layer, k_VisualHideFlags);
                break;

            case SceneType.Scene:
                if (m_MainSceneVisualsRoot == null)
                {
                    m_MainSceneVisualsRoot           = new GameObject(k_VisualsRootName);
                    m_MainSceneVisualsRoot.hideFlags = k_VisualsRootHideFlags;
                }

                visual.transform.SetParent(m_MainSceneVisualsRoot.transform);
                visual.gameObject.SetLayerAndHideFlagsRecursively(entity.gameObject.layer, k_VisualHideFlags);
                break;

            case SceneType.Prefab:
#if UNITY_EDITOR
                if (m_PrefabStageScene.IsValid())
                {
                    // Prefab stage root needs to be recreated for new prefab stages
                    if (m_PrefabStageVisualsRoot == null)
                    {
                        // Needs different hide flags for proper selection, hiding and not modifying
                        // the prefab scene in prefab mode.
                        m_PrefabStageVisualsRoot = new GameObject(k_VisualsRootName)
                        {
                            hideFlags = k_PrefabStageHideFlags
                        };
                        SceneManager.MoveGameObjectToScene(m_PrefabStageVisualsRoot, m_PrefabStageScene);
                    }

                    visual.transform.SetParent(m_PrefabStageVisualsRoot.transform);
                    visual.gameObject.SetLayerAndHideFlagsRecursively(entity.gameObject.layer, k_PrefabStageHideFlags);
                }
#endif
                break;
            }

            var sizeCondition = entity.GetComponent <PlaneSizeCondition>(); // PlaneSizeConditions will control scale
            if (sizeCondition != null)
            {
                m_SizeConditions.Add(visual, sizeCondition);
            }

            var markerCondition = entity.GetComponent <MarkerCondition>();
            if (markerCondition != null)
            {
                m_ImageMarkerConditions.Add(visual, markerCondition);
            }

            if (traits.Contains(TraitNames.Face))
            {
                m_FaceVisuals.Add(visual.transform);
            }

            return(visual);
        }