Beispiel #1
0
        /// <summary>
        /// Use for adding an empty inert Agent Component to a gameObject via code.
        /// </summary>
        /// <param name="target"></param>
        /// <param name="isInert"></param>
        /// <returns></returns>
        public static Agent AddAgentComponent(GameObject target, bool isInert)
        {
            var agent = target.AddComponent <Agent>();

            agent._inert            = isInert;
            agent.Resources         = Resources.CreateResources(false, 0, false, 0);
            agent.Resources.Death   = Death.CreateDeath(null);
            agent.Perception        = Perception.CreatePerception(10, 180, 7);
            agent.Perception.Memory = Memory.CreateMemory(10);
            agent.Motion            = Motion.CreateMotion(MotionEngine.Turret, 0, false, false);
            agent.GeneralSettings   = Settings.CreateSettings();
            agent.Inventory         = new Inventory(agent, null);
            agent.AgentAnimation    = new AgentAnimation();
            return(agent);
        }
Beispiel #2
0
        /// <summary>
        /// <para>Create a new Agent with specific components.</para>
        /// </summary>
        /// <param name="agentName"></param>
        /// <param name="resources"></param>
        /// <param name="memory"></param>
        /// <param name="perception"></param>
        /// <param name="motion"></param>
        /// <param name="death"></param>
        /// <param name="settings"></param>
        /// <param name="initSkill"></param>
        /// <param name="graphics"></param>
        /// <returns></returns>
        public static Agent CreateAgent(string agentName,
                                        Resources resources, Memory memory, Perception perception, Motion motion, Death death, Settings settings,
                                        List <Skill> initSkill, GameObject graphics = null)
        {
            // Create all necessary GameObjects
            var newAgent     = new GameObject(agentName);
            var lookPos      = new GameObject("__look__");
            var shootPos     = new GameObject("__shoot__");
            var graphicsPos  = new GameObject("__graphics__");
            var inventoryPos = new GameObject("__inventory__");

            lookPos.transform.parent             = newAgent.transform;
            shootPos.transform.parent            = newAgent.transform;
            graphicsPos.transform.parent         = newAgent.transform;
            inventoryPos.transform.parent        = newAgent.transform;
            lookPos.transform.localPosition      = new Vector3(0, 1, 0);
            shootPos.transform.localPosition     = new Vector3(0, 1, 0);
            graphicsPos.transform.localPosition  = new Vector3(0, 1, 0);
            inventoryPos.transform.localPosition = new Vector3(0, 1, 0);

            var collider = newAgent.AddComponent <CapsuleCollider>();

            collider.center = new Vector3(0, 1, 0);

            // Instantiate graphics
            if (graphics)
            {
                var gr =
                    Instantiate(graphics, graphicsPos.transform.position, graphicsPos.transform.rotation) as GameObject;
                gr.transform.parent = graphicsPos.transform;
            }

            // Add Agent Component and set all his components up
            var agent = newAgent.AddComponent <Agent>();

            agent._resources         = resources;
            agent._resources.Death   = death;
            agent._perception        = perception;
            agent._perception.Origin = lookPos.transform;
            agent._perception.Memory = memory;
            agent._motion            = motion;

            if (motion.Type == MotionEngine.NavMesh)             // No need to add NavMeshAgent if Agent is not a creature
            {
                var navMesh = newAgent.AddComponent <NavMeshAgent>();
                navMesh.acceleration = 100;
                navMesh.angularSpeed = 1000;
            }

#if ASTAR_EXISTS
            else if (motion.Type == MotionEngine.Astar)
            {
                var starAi = agent.gameObject.AddComponent <AIPath>();
                starAi.height = 2f;
                starAi.radius = collider.radius;

                agent.gameObject.AddComponent <Seeker>();
                agent.gameObject.AddComponent <FunnelModifier>();
                var rvoController = agent.gameObject.AddComponent <RVOController>();
                rvoController.agentTimeHorizon    = 0.01f;
                rvoController.obstacleTimeHorizon = 0.01f;

                if (!GameObject.FindObjectOfType <RVOSimulator>())
                {
                    new GameObject("RVOSimulator").AddComponent <RVOSimulator>();
                    Debug.Log("A new GameObject of type RVOSimulator has been created");
                }
            }
#endif

            agent._generalSettings             = settings;
            agent._generalSettings.ShootOrigin = shootPos.transform;
            agent._inventory = new Inventory(agent, inventoryPos.transform);
            agent._skills    = initSkill;

            return(agent);
        }