Example #1
0
 private void MoveToTarget(NeedAIData data)
 {
     if (!HasMoved && m_PathfindingData.Count > 0)
     {
         Vector2Int    nextPoint     = m_PathfindingData.Peek();
         PhysicsResult physicsResult = PhysicsManager.IsCollision(WorldPosition, nextPoint, MyWorld);
         if (physicsResult != PhysicsResult.EntityCollision)
         {
             m_PathfindingData.Dequeue();
             Move(nextPoint);
             HasMoved = true;
         }
         else if (physicsResult == PhysicsResult.EntityCollision)
         {
             MyWorld.SwapPosition(this, MyWorld.GetEntity(nextPoint));
             m_PathfindingData.Dequeue();
             Move(nextPoint);
             HasMoved = true;
         }
     }
     else if (m_PathfindingData.Count == 0)
     {
         if (data.target != null)
         {
             m_PathfindingData = m_Pathfinder.FindPath(WorldPosition, data.target.WorldPosition, MyWorld);
         }
         else if (data.targetPoint != Vector2Int.zero)
         {
             m_PathfindingData = m_Pathfinder.FindPath(WorldPosition, data.targetPoint, MyWorld);
         }
     }
 }
Example #2
0
        public void UpdateMe()
        {
            if (this.MyWorld.IsDirty)
            {
                FOVBasicBoard board = (FOVBasicBoard)m_FOVHandler.Do(this.WorldPosition, this.MyWorld.Dimensions, this.VisionMod, this.MyWorld.Walls.Keys.ToList());
                m_Vision = board.Vision;
            }
            else
            {
                FOVBasicBoard board = (FOVBasicBoard)m_FOVHandler.Do(this.WorldPosition, this.VisionMod);
                m_Vision = board.Vision;
            }

            HasMoved = false;

            if (!PlayerControlled)
            {
                //Attack immediate threats

                /*
                 * REDO THIS TO BE IN LUA
                 * //List<NeedAIData> targets = MyWorld.SearchForEntities(this, "Any", Intent.Attack, EntityTypeSearch.Any, EntitySentienceSearch.Any);
                 * //List<NeedAIData> validTargets = targets.Where(x => this.HasRelationship(x.target.GUID) < ATTACK_THRESHOLD).ToList();
                 * if(validTargets.Count > 0 && CurrentTarget.target == null)
                 * {
                 *  //TODO: Write a threat assessment system
                 *  //For now, choose a random target and go after them
                 *  int result = RNG.Roll(0, validTargets.Count - 1);
                 *  NeedAIData data = validTargets[result];
                 *
                 *  CurrentTarget = data;
                 *  m_PathfindingData = m_Pathfinder.FindPath(this.WorldPosition, CurrentTarget.target.WorldPosition, this.MyWorld);
                 * }
                 */

                //If you're idle
                if (CurrentTarget.idle == true)
                {
                    //Let's find something to do
                    List <EntityNeed> needs = m_Needs.Values.OrderByDescending(x => x.priority).ToList();
                    //Act on first need

                    bool idle = true;
                    foreach (EntityNeed need in needs)
                    {
                        if (need.contributingHappiness)
                        {
                            continue;
                        }

                        Debug.Log("Running LUA script: FindFulfilmentObject (" + need.name + ") requested by: " + this.JoyName);
                        ScriptingEngine.RunScript(need.InteractionFileContents, need.name, "FindFulfilmentObject", new object[] { new MoonEntity(this) });
                        idle = false;
                        break;
                    }
                    m_CurrentTarget.idle = idle;
                }
                //Otherwise, carry on with what you're doing
                else
                {
                    if (WorldPosition == CurrentTarget.targetPoint || (CurrentTarget.target != null && WorldPosition == CurrentTarget.target.WorldPosition))
                    {
                        //If we have a target
                        if (CurrentTarget.target != null)
                        {
                            //We're interacting with an entity here
                            if (CurrentTarget.intent == Intent.Interact)
                            {
                                //TODO: WRITE AN ENTITY INTERACTION
                                EntityNeed need = this.Needs[CurrentTarget.need];

                                Debug.Log("Running LUA script: FindFulfilmentObject (" + need.name + ") requested by: " + this.JoyName);
                                ScriptingEngine.RunScript(need.InteractionFileContents, need.name, "FindFulfilmentObject", new object[] { new MoonEntity(this) });
                            }
                            else if (CurrentTarget.intent == Intent.Attack)
                            {
                                CombatEngine.SwingWeapon(this, CurrentTarget.target);
                            }
                        }
                    }
                    //If we've not arrived at our target
                    else if (WorldPosition != CurrentTarget.targetPoint || (CurrentTarget.target != null && AdjacencyHelper.IsAdjacent(WorldPosition, CurrentTarget.target.WorldPosition) == false))
                    {
                        //Move to target
                        MoveToTarget(CurrentTarget);
                    }
                }
            }
            else
            {
                if (!HasMoved && m_PathfindingData.Count > 0)
                {
                    Vector2Int    nextPoint     = m_PathfindingData.Peek();
                    PhysicsResult physicsResult = PhysicsManager.IsCollision(WorldPosition, nextPoint, MyWorld);
                    if (physicsResult != PhysicsResult.EntityCollision)
                    {
                        m_PathfindingData.Dequeue();
                        Move(nextPoint);
                        HasMoved = true;
                    }
                    else if (physicsResult == PhysicsResult.EntityCollision)
                    {
                        MyWorld.SwapPosition(this, MyWorld.GetEntity(nextPoint));

                        m_PathfindingData.Dequeue();
                        Move(nextPoint);
                        HasMoved = true;
                    }
                }
            }
        }