Exemple #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);
        }
Exemple #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);
        }
        /// <summary>
        /// Implementation of finding the target.
        /// </summary>
        /// <returns></returns>
        private IEnumerator INTERNAL_ApplyEffectEnum()
        {
            // Create the loading special effects.
            OnApplyEffectOnInit(_sourceObject);

            // Cast the ray to find the target
            if (_initSkillBy == InitSkillBy.Ray)
            {
                //Try find target agent
                var targetDir = _agent.Target
                                        ? _agent.Target.position - _agent.transform.position
                                        : Perception.InitRay(_range, 0, 90, _agent.Perception.Origin);
                var fov = _fieldOfView > 0 ? _fieldOfView : _agent.GeneralSettings.AimFieldOfView;
                if (Vector3.Angle(targetDir, _agent.Perception.Origin.forward) >= fov)
                {
                    yield break;
                }
                RaycastHit hit;
                if (!Physics.Raycast(_agent.GeneralSettings.ShootOrigin.position, targetDir, out hit, _range))
                {
                    yield break;
                }
                var agent = hit.transform.gameObject.GetComponent <Agent>();

                var unit = hit.transform.gameObject.GetComponent <Unit>();
                if (unit)
                {
                    var targetIsFriend = _agent.Unit.IsFriend(unit);
                    if (targetIsFriend && !_canAffectFriends)
                    {
                        yield break;
                    }
                    if (!targetIsFriend && !_canAffectEnemies)
                    {
                        yield break;
                    }
                }

                //Apply effects to him
                if (agent)
                {
                    agent.AddEffect(this, _agent);
                }
                else
                {
                    INTERNAL_ApplyEffectNonAgent(hit.transform.gameObject);
                }
            }

            // Create a Projectile and pass the skill to it.
            else if (_initSkillBy == InitSkillBy.Projectile)
            {
                var origin = _agent.GeneralSettings.ShootOrigin ?? _agent.Perception.Origin;
                var pjtl   = Instantiate(_projectilePrefab, origin.position, origin.rotation) as Projectile;
                //pjtl.GetComponent<Projectile>().Init(_agent, _agent.Target, this, _minPower, _maxPower, _canAffectEnemies, _canAffectFriends);
                pjtl.Init(_agent, _agent.Target, this, _minPower, _maxPower, _canAffectEnemies, _canAffectFriends);
            }

            // Apply the skill to oneself.
            else if (_initSkillBy == InitSkillBy.Self)
            {
                _agent.AddEffect(this, _agent);
            }

            // Apply effects to a target directly by a reference to the GameObject.
            else if (_initSkillBy == InitSkillBy.Direct)
            {
                if (!_agent.Target)
                {
                    yield return(null);
                }
                var agent = _agent.Target.GetComponent <Agent>();
                if (agent)
                {
                    var unit = _agent.Target.GetComponent <Unit>();
                    if (unit)
                    {
                        var targetIsFriend = _agent.Unit.IsFriend(unit);
                        if (targetIsFriend && !_canAffectFriends)
                        {
                            yield break;
                        }
                        if (!targetIsFriend && !_canAffectEnemies)
                        {
                            yield break;
                        }
                    }

                    agent.AddEffect(this, _agent);
                }
                else
                {
                    INTERNAL_ApplyEffectNonAgent(_agent.Target.gameObject);
                }
            }

            // Find all appropriate targets in cpecified radius and apply effects to them.
            else if (_initSkillBy == InitSkillBy.Radius)
            {
                var agents = FindObjectsOfType <Agent>();
                foreach (var agent in agents)
                {
                    var targetIsFriend = _agent.Unit.IsFriend(agent.Unit);
                    if (targetIsFriend && !_canAffectFriends)
                    {
                        continue;
                    }
                    if (!targetIsFriend && !_canAffectEnemies)
                    {
                        continue;
                    }

                    if (Vector3.Distance(agent.transform.position, _agent.transform.position) <= _range &&
                        agent != _agent)
                    {
                        agent.AddEffect(this, _agent);
                    }
                }
            }

            // Create special effects on target after applying the effects.
            if (_agent.Target)
            {
                var agent = _agent.Target.GetComponent <Agent>();
                if (agent)
                {
                    var targetIsFriend = _agent.Unit.IsFriend(agent.Unit);
                    if (targetIsFriend && !_canAffectFriends)
                    {
                        yield break;
                    }
                    if (!targetIsFriend && !_canAffectEnemies)
                    {
                        yield break;
                    }
                }

                OnApplyEffect(_agent.Target.gameObject);
            }
        }
Exemple #4
0
 public bool ObstaclesBeforeTarget()
 {
     return(Perception.ObstaclesBetweenMeAndTarget(_agent.Perception.Origin, _agent.Target));
 }
 /// <summary>
 /// Initialize the interface.
 /// </summary>
 /// <param name="agent"></param>
 public PerceptionConditionInterface(Agent agent)
 {
     _agent      = agent;
     _perception = agent.Perception;
 }