예제 #1
0
        /// <summary>
        /// Creates enemy GameObjects based on spawn count specified in Foe resource (minimum of 1).
        /// Only use this when live enemy is to be first added to scene. Do not use when linking to site or deserializing.
        /// GameObjects created will be disabled, at origin, parentless, and have a new UID for LoadID.
        /// Caller must otherwise complete GameObject setup to suit their needs.
        /// </summary>
        /// <param name="reaction">Foe is hostile by default but can optionally set to passive.</param>
        /// <returns>GameObject[] array of 1-N foes. Array can be null or empty if create fails.</returns>
        public GameObject[] CreateFoeGameObjects(Vector3 position, int spawnOverride = -1, MobileReactions reaction = MobileReactions.Hostile)
        {
            List <GameObject> gameObjects = new List <GameObject>();

            // Get spawn count allowing for caller to override
            int totalSpawns = (spawnOverride >= 1) ? spawnOverride : spawnCount;

            // Generate GameObjects
            for (int i = 0; i < totalSpawns; i++)
            {
                // Generate enemy
                string         name       = string.Format("DaggerfallEnemy [{0}]", foeType.ToString());
                GameObject     go         = GameObjectHelper.InstantiatePrefab(DaggerfallUnity.Instance.Option_EnemyPrefab.gameObject, name, null, position);
                SetupDemoEnemy setupEnemy = go.GetComponent <SetupDemoEnemy>();
                if (setupEnemy != null)
                {
                    // Assign gender randomly
                    MobileGender gender;
                    if (UnityEngine.Random.Range(0f, 1f) < 0.5f)
                    {
                        gender = MobileGender.Male;
                    }
                    else
                    {
                        gender = MobileGender.Female;
                    }

                    // Configure enemy
                    setupEnemy.ApplyEnemySettings(foeType, reaction, gender);

                    // Align non-flying units with ground
                    DaggerfallMobileUnit mobileUnit = setupEnemy.GetMobileBillboardChild();
                    if (mobileUnit.Summary.Enemy.Behaviour != MobileBehaviour.Flying)
                    {
                        GameObjectHelper.AlignControllerToGround(go.GetComponent <CharacterController>());
                    }

                    // Add QuestResourceBehaviour to GameObject
                    QuestResourceBehaviour questResourceBehaviour = go.AddComponent <QuestResourceBehaviour>();
                    questResourceBehaviour.AssignResource(this);
                }

                // Assign load id
                DaggerfallEnemy enemy = go.GetComponent <DaggerfallEnemy>();
                if (enemy)
                {
                    enemy.LoadID     = DaggerfallUnity.NextUID;
                    enemy.QuestSpawn = true;
                }

                // Disable GameObject, caller must set active when ready
                go.SetActive(false);

                // Add to list
                gameObjects.Add(go);
            }

            return(gameObjects.ToArray());
        }
예제 #2
0
        void SetFoeName()
        {
            // Set type name with fallback
            MobileEnemy enemy;

            if (EnemyBasics.GetEnemy(foeType, out enemy))
            {
                typeName = enemy.Name;
            }
            else
            {
                typeName = foeType.ToString();
            }

            // Monster types get a random monster name
            // Always treating monsters as male for now as they don't have any gender in game files
            if ((int)foeType < 128)
            {
                DFRandom.srand(DateTime.Now.Millisecond);
                displayName = DaggerfallUnity.Instance.NameHelper.MonsterName();
                return;
            }

            // Randomly assign a gender for humanoid foes
            humanoidGender = (UnityEngine.Random.Range(0.0f, 1.0f) < 0.5f) ? Genders.Male : Genders.Female;

            // Create a random display name for humanoid foes
            DFRandom.srand(DateTime.Now.Millisecond);
            NameHelper.BankTypes nameBank = GameManager.Instance.PlayerGPS.GetNameBankOfCurrentRegion();
            displayName = DaggerfallUnity.Instance.NameHelper.FullName(nameBank, humanoidGender);
        }
예제 #3
0
        private static void AddEnemy(
            DFBlock.RdbObject obj,
            MobileTypes type,
            Transform parent = null,
            DFRegion.DungeonTypes dungeonType = DFRegion.DungeonTypes.HumanStronghold)
        {
            // Get default reaction
            MobileReactions reaction = MobileReactions.Hostile;

            if (obj.Resources.FlatResource.FlatData.Reaction == (int)DFBlock.EnemyReactionTypes.Passive)
            {
                reaction = MobileReactions.Passive;
            }

            // Just setup demo enemies at this time
            string         name       = string.Format("DaggerfallEnemy [{0}]", type.ToString());
            Vector3        position   = new Vector3(obj.XPos, -obj.YPos, obj.ZPos) * MeshReader.GlobalScale;
            GameObject     go         = GameObjectHelper.InstantiatePrefab(DaggerfallUnity.Instance.Option_EnemyPrefab.gameObject, name, parent, position);
            SetupDemoEnemy setupEnemy = go.GetComponent <SetupDemoEnemy>();

            if (setupEnemy != null)
            {
                // Configure enemy
                setupEnemy.ApplyEnemySettings(type, reaction);

                // Align non-flying units with ground
                DaggerfallMobileUnit mobileUnit = setupEnemy.GetMobileBillboardChild();
                if (mobileUnit.Summary.Enemy.Behaviour != MobileBehaviour.Flying)
                {
                    GameObjectHelper.AlignControllerToGround(go.GetComponent <CharacterController>());
                }
            }
        }
예제 #4
0
        public override bool ExpandMacro(MacroTypes macro, out string textOut)
        {
            textOut = string.Empty;
            bool result = true;

            switch (macro)
            {
            case MacroTypes.NameMacro1:                 // Display name
                textOut = foeType.ToString();
                break;

            case MacroTypes.DetailsMacro:               // =symbol_ name
                textOut = displayName;
                break;

            default:                                    // Macro not supported
                result = false;
                break;
            }

            return(result);
        }
예제 #5
0
        /// <summary>
        /// Create an enemy in the world and perform common setup tasks.
        /// </summary>
        public static GameObject CreateEnemy(string name, MobileTypes mobileType, Vector3 localPosition, Transform parent = null, MobileReactions mobileReaction = MobileReactions.Hostile)
        {
            // Create target GameObject
            string         displayName = string.Format("{0} [{1}]", name, mobileType.ToString());
            GameObject     go          = InstantiatePrefab(DaggerfallUnity.Instance.Option_EnemyPrefab.gameObject, displayName, parent, Vector3.zero);
            SetupDemoEnemy setupEnemy  = go.GetComponent <SetupDemoEnemy>();

            // Set position
            go.transform.localPosition = localPosition;

            // Assign humanoid gender randomly
            // This does not affect monsters like rats, bats, etc
            MobileGender gender;

            if (UnityEngine.Random.Range(0f, 1f) < 0.5f)
            {
                gender = MobileGender.Male;
            }
            else
            {
                gender = MobileGender.Female;
            }

            // Configure enemy
            setupEnemy.ApplyEnemySettings(mobileType, mobileReaction, gender);

            // Align non-flying units with ground
            DaggerfallMobileUnit mobileUnit = setupEnemy.GetMobileBillboardChild();

            if (mobileUnit.Summary.Enemy.Behaviour != MobileBehaviour.Flying)
            {
                AlignControllerToGround(go.GetComponent <CharacterController>());
            }

            return(go);
        }
예제 #6
0
        public static GameObject CreateDaggerfallEnemyGameObject(MobileTypes type, Transform parent, MobileReactions reaction)
        {
            DaggerfallUnity dfUnity = DaggerfallUnity.Instance;

            // Ensure enemy dict is loaded
            if (enemyDict == null)
            {
                enemyDict = EnemyBasics.GetEnemyDict();
            }

            GameObject go = new GameObject(string.Format("DaggerfallEnemy [{0}]", type.ToString()));

            if (parent)
            {
                go.transform.parent = parent;
            }
            go.transform.forward = Vector3.forward;

            // Add custom tag and script
            go.tag = dfUnity.Option_EnemyTag;
#if UNITY_EDITOR
            if (dfUnity.Option_CustomEnemyScript != null)
            {
                go.AddComponent(dfUnity.Option_CustomEnemyScript.GetClass());
            }
#endif

            // Add child object for enemy billboard
            GameObject mobileObject = new GameObject("DaggerfallMobileUnit");
            mobileObject.transform.parent = go.transform;

            // Add mobile enemy
            Vector2 size = Vector2.one;
            DaggerfallMobileUnit dfMobile = mobileObject.AddComponent <DaggerfallMobileUnit>();
            dfMobile.SetEnemy(dfUnity, enemyDict[(int)type], reaction);
            size = dfMobile.Summary.RecordSizes[0];

            // Add character controller
            if (dfUnity.Option_EnemyCharacterController || dfUnity.Option_EnemyExampleAI)
            {
                CharacterController controller = go.AddComponent <CharacterController>();
                controller.radius     = dfUnity.Option_EnemyRadius;
                controller.height     = size.y;
                controller.slopeLimit = dfUnity.Option_EnemySlopeLimit;
                controller.stepOffset = dfUnity.Option_EnemyStepOffset;

                // Reduce height of flying creatures as their wing animation makes them taller than desired
                // This helps them get through doors while aiming for player eye height
                if (dfMobile.Summary.Enemy.Behaviour == MobileBehaviour.Flying)
                {
                    controller.height /= 2f;
                }

                // Limit maximum height to ensure controller can fit through doors
                // For some reason Unity 4.5 doesn't let you set SkinWidth from code >.<
                if (controller.height > 1.9f)
                {
                    controller.height = 1.9f;
                }
            }

            // Add rigidbody
            if (dfUnity.Option_EnemyRigidbody)
            {
                Rigidbody rigidbody = go.AddComponent <Rigidbody>();
                rigidbody.useGravity  = dfUnity.Option_EnemyUseGravity;
                rigidbody.isKinematic = dfUnity.Option_EnemyIsKinematic;
            }

            // Add capsule collider
            if (dfUnity.Option_EnemyCapsuleCollider)
            {
                CapsuleCollider collider = go.AddComponent <CapsuleCollider>();
                collider.radius = dfUnity.Option_EnemyRadius;
                collider.height = size.y;
            }

            // Add navmesh agent
            if (dfUnity.Option_EnemyNavMeshAgent)
            {
                NavMeshAgent agent = go.AddComponent <NavMeshAgent>();
                agent.radius     = dfUnity.Option_EnemyRadius;
                agent.height     = size.y;
                agent.baseOffset = size.y * 0.5f;
            }

            // Add example AI
            if (dfUnity.Option_EnemyExampleAI)
            {
                // EnemyMotor will also add other required components
                go.AddComponent <Demo.EnemyMotor>();

                // Set sounds
                Demo.EnemySounds enemySounds = go.GetComponent <Demo.EnemySounds>();
                if (enemySounds)
                {
                    enemySounds.MoveSound   = (SoundClips)dfMobile.Summary.Enemy.MoveSound;
                    enemySounds.BarkSound   = (SoundClips)dfMobile.Summary.Enemy.BarkSound;
                    enemySounds.AttackSound = (SoundClips)dfMobile.Summary.Enemy.AttackSound;
                }
            }

            return(go);
        }
예제 #7
0
        private static void AddEnemy(
            DFBlock.RdbObject obj,
            MobileTypes type,
            Transform parent = null,
            long loadID = 0)
        {
            // Get default reaction
            MobileReactions reaction = MobileReactions.Hostile;
            if (obj.Resources.FlatResource.Action == (int)DFBlock.EnemyReactionTypes.Passive)
                reaction = MobileReactions.Passive;

            // Just setup demo enemies at this time
            string name = string.Format("DaggerfallEnemy [{0}]", type.ToString());
            Vector3 position = new Vector3(obj.XPos, -obj.YPos, obj.ZPos) * MeshReader.GlobalScale;
            GameObject go = GameObjectHelper.InstantiatePrefab(DaggerfallUnity.Instance.Option_EnemyPrefab.gameObject, name, parent, position);
            SetupDemoEnemy setupEnemy = go.GetComponent<SetupDemoEnemy>();
            if (setupEnemy != null)
            {
                // Configure enemy
                setupEnemy.ApplyEnemySettings(type, reaction);

                // Align non-flying units with ground
                DaggerfallMobileUnit mobileUnit = setupEnemy.GetMobileBillboardChild();
                if (mobileUnit.Summary.Enemy.Behaviour != MobileBehaviour.Flying)
                    GameObjectHelper.AlignControllerToGround(go.GetComponent<CharacterController>());
            }

            DaggerfallEnemy enemy = go.GetComponent<DaggerfallEnemy>();
            if (enemy)
            {
                enemy.LoadID = loadID;
            }
        }
예제 #8
0
        public void setupMobile()
        {
            name = string.Format("DaggerfallEnemy [{0}]", creatureType.ToString());

            // Add child object for enemy billboard
            GameObject mobileObject = new GameObject("DaggerfallMobileUnit");

            mobileObject.transform.parent = this.transform;

            // Add mobile enemy
            Vector2 size = Vector2.one;
            DaggerfallMobileUnit dfMobile = mobileObject.AddComponent <DaggerfallMobileUnit>();

            try {
                Dictionary <int, MobileEnemy> enemyDict = EnemyBasics.GetEnemyDict();
                dfMobile.SetEnemy(dfUnity, enemyDict[(int)creatureType], reaction);
                size = dfMobile.Summary.RecordSizes[0];
            } catch (System.Exception e) {
                string message = string.Format("Failed to set enemy type (int)type={0}. '{1}'", (int)creatureType, e.Message);
                // TODO: Change logging
                DaggerfallUnity.LogMessage(message);
                GameObject.DestroyImmediate(dfMobile);
            }

            // Add character controller
            if (dfUnity.Option_EnemyCharacterController)
            {
                CharacterController controller = gameObject.AddComponent <CharacterController>();
                controller.radius     = dfUnity.Option_EnemyRadius;
                controller.height     = size.y;
                controller.slopeLimit = dfUnity.Option_EnemySlopeLimit;
                controller.stepOffset = dfUnity.Option_EnemyStepOffset;

                // Reduce height of flying creatures as their wing animation makes them taller than desired
                // This helps them get through doors while aiming for player eye height
                if (dfMobile.Summary.Enemy.Behaviour == MobileBehaviour.Flying)
                {
                    controller.height /= 2f;
                }

                // Limit maximum height to ensure controller can fit through doors
                // For some reason Unity 4.5 doesn't let you set SkinWidth from code >.<
                if (controller.height > 1.9f)
                {
                    controller.height = 1.9f;
                }
            }

            // Add rigidbody
            if (dfUnity.Option_EnemyRigidbody)
            {
                Rigidbody rigidbody = gameObject.AddComponent <Rigidbody>();
                rigidbody.useGravity  = dfUnity.Option_EnemyUseGravity;
                rigidbody.isKinematic = dfUnity.Option_EnemyIsKinematic;
            }

            // Add capsule collider
            if (dfUnity.Option_EnemyCapsuleCollider)
            {
                CapsuleCollider collider = gameObject.AddComponent <CapsuleCollider>();
                collider.radius = dfUnity.Option_EnemyRadius;
                collider.height = size.y;
            }

            // Add navmesh agent
            if (dfUnity.Option_EnemyNavMeshAgent)
            {
                NavMeshAgent agent = gameObject.AddComponent <NavMeshAgent>();
                agent.radius     = dfUnity.Option_EnemyRadius;
                agent.height     = size.y;
                agent.baseOffset = size.y * 0.5f;
            }

            // Add example AI
            if (dfUnity.Option_EnemyExampleAI)
            {
                // EnemyMotor will also add other required components
                gameObject.AddComponent <DaggerfallWorkshop.Demo.EnemyMotor>();

                // Set sounds
                DaggerfallWorkshop.Demo.EnemySounds enemySounds = gameObject.GetComponent <DaggerfallWorkshop.Demo.EnemySounds>();
                if (enemySounds)
                {
                    enemySounds.MoveSound   = (SoundClips)dfMobile.Summary.Enemy.MoveSound;
                    enemySounds.BarkSound   = (SoundClips)dfMobile.Summary.Enemy.BarkSound;
                    enemySounds.AttackSound = (SoundClips)dfMobile.Summary.Enemy.AttackSound;
                }
            }
        }