Exemple #1
0
        public override void Locomotion(IEntity vehicle)
        {
            this.Initialise();

            //Don't move if you're currently busy
            if (vehicle.NeedFulfillmentData.Name.IsNullOrEmpty() == false &&
                vehicle.NeedFulfillmentData.Counter > 0)
            {
                return;
            }

            //If you're idle
            if (vehicle.CurrentTarget.Idle)
            {
                //Let's find something to do
                List <INeed> needs = vehicle.Needs.Values.OrderByDescending(x => x.Priority).ToList();
                //Act on first need

                bool idle   = true;
                bool wander = false;
                foreach (INeed need in needs)
                {
                    if (need.ContributingHappiness)
                    {
                        continue;
                    }

                    idle &= need.FindFulfilmentObject(vehicle);
                    break;
                }

                if (idle)
                {
                    int result = this.Roller.Roll(0, 10);
                    if (result < 1)
                    {
                        wander = true;
                    }
                }

                if (wander)
                {
                    this.WanderAction.Execute(
                        new IJoyObject[] { vehicle },
                        new[] { "wander", "idle" });
                }
            }

            //If we have somewhere to be, move there
            if (vehicle.WorldPosition != vehicle.CurrentTarget.TargetPoint ||
                vehicle.CurrentTarget.Target != null)
            {
                if (vehicle.CurrentTarget.Target is IItemInstance &&
                    vehicle.WorldPosition != vehicle.CurrentTarget.Target.WorldPosition)
                {
                    if (vehicle.CurrentTarget.TargetPoint.Equals(GlobalConstants.NO_TARGET))
                    {
                        NeedAIData newData = new NeedAIData
                        {
                            Idle        = vehicle.CurrentTarget.Idle,
                            Intent      = vehicle.CurrentTarget.Intent,
                            Need        = vehicle.CurrentTarget.Need,
                            Searching   = vehicle.CurrentTarget.Searching,
                            Target      = vehicle.CurrentTarget.Target,
                            TargetPoint = vehicle.CurrentTarget.Target.WorldPosition
                        };
                        vehicle.CurrentTarget = newData;
                    }

                    this.MoveToTarget(vehicle);
                }
                else if (vehicle.CurrentTarget.Target is IEntity &&
                         AdjacencyHelper.IsAdjacent(vehicle.WorldPosition, vehicle.CurrentTarget.Target.WorldPosition) == false)
                {
                    this.MoveToTarget(vehicle);
                }
                else if (vehicle.CurrentTarget.Target is null &&
                         vehicle.CurrentTarget.TargetPoint.Equals(GlobalConstants.NO_TARGET) == false)
                {
                    this.MoveToTarget(vehicle);
                }
            }

            if (vehicle.MyWorld.GetEntity(vehicle.CurrentTarget.TargetPoint) is null == false ||
                vehicle.MyWorld.GetObject(vehicle.CurrentTarget.TargetPoint) is null == false)
            {
                //If we've arrived at our destination, then we do our thing
                if ((vehicle.WorldPosition == vehicle.CurrentTarget.TargetPoint &&
                     (vehicle.CurrentTarget.Target is IItemInstance || vehicle.CurrentTarget.Target is null) ||
                     (vehicle.CurrentTarget.Target is IEntity &&
                      AdjacencyHelper.IsAdjacent(vehicle.WorldPosition, vehicle.CurrentTarget.Target.WorldPosition))))
                {
                    //If we have a target
                    if (vehicle.CurrentTarget.Target is null == false)
                    {
                        if (vehicle.CurrentTarget.Intent == Intent.Attack)
                        {
                            //CombatEngine.SwingWeapon(this, CurrentTarget.target);
                        }
                        else if (vehicle.CurrentTarget.Intent == Intent.Interact)
                        {
                            INeed need = vehicle.Needs[vehicle.CurrentTarget.Need];

                            need.Interact(vehicle, vehicle.CurrentTarget.Target);
                            vehicle.CurrentTarget = NeedAIData.IdleState();
                        }
                    }
Exemple #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;
                    }
                }
            }
        }
Exemple #3
0
        public override void HandleInput()
        {
            base.HandleInput();

            bool hasMoved = false;

            Entity player = m_ActiveWorld.Player;

            /*
             * if (m_Input.currentMouseState.ScrollWheelValue > m_Input.lastMouseState.ScrollWheelValue)
             * {
             *  m_Camera.zoom += 0.05f;
             * }
             *
             * if (m_Input.currentMouseState.ScrollWheelValue < m_Input.lastMouseState.ScrollWheelValue)
             * {
             *  m_Camera.zoom -= 0.05f;
             * }
             *
             * if(m_Input.IsOldPress(MouseButtons.RightButton))
             * {
             *  m_GUIManager.Screen.Desktop.Children.Remove(m_ContextMenu);
             *  m_ContextMenu.MoveTo(m_Input.currentMouseState.Position);
             *  m_GUIManager.Screen.Desktop.Children.Add(m_ContextMenu);
             *
             *  Vector2 mouseVector = (m_Input.currentMouseState.Position - (new Point(m_Renderer.m_ScreenWidth / 2, m_Renderer.m_ScreenHeight / 2))).ToVector2();
             *  m_MenuTile = new Point((int)Math.Floor(mouseVector.X / (ObjectIcons.SPRITE_SIZE * m_Camera.zoom)), (int)Math.Floor(mouseVector.Y / (ObjectIcons.SPRITE_SIZE * m_Camera.zoom))) + s_ActiveWorld.player.position;
             * }
             *
             * if(m_Input.IsOldPress(Keys.Space))
             * {
             *  autoTurn = !autoTurn;
             * }
             *
             * if(m_Input.IsOldPress(Keys.L))
             * {
             *  s_ActiveWorld.player.LevelUp();
             * }
             *
             */

            /*
             * if(Input.GetMouseButtonDown(0))
             * {
             *  Vector3 mouseWorld = m_Camera.ScreenToWorldPoint(Input.mousePosition);
             *  int x = (int)mouseWorld.x;
             *  int y = (int)mouseWorld.y;
             *
             *  Pathfinder pathfinder = new Pathfinder();
             *  Queue<Vector2Int> path = pathfinder.FindPath(player.WorldPosition, new Vector2Int(x, y), m_ActiveWorld);
             *  player.SetPath(path);
             *  autoTurn = true;
             * }
             */

            if (Input.GetKeyDown(KeyCode.I))
            {
                m_InventoryOpen = !m_InventoryOpen;
                if (m_InventoryOpen == false)
                {
                    s_GUIManager.OpenGUI("NeedsPanel");
                }
                else
                {
                    s_GUIManager.OpenGUI("GUIInventory");
                }
            }

            if (s_GUIManager.RemovesControl())
            {
                return;
            }

            if (Input.GetKeyDown(KeyCode.Return))
            {
                //Going up a level
                if (m_ActiveWorld.Parent != null && player.WorldPosition == m_ActiveWorld.SpawnPoint && !player.HasMoved)
                {
                    ChangeWorld(m_ActiveWorld.Parent, m_ActiveWorld.GetTransitionPointForParent());
                    return;
                }

                //Going down a level
                else if (m_ActiveWorld.Areas.ContainsKey(player.WorldPosition) && !player.HasMoved)
                {
                    ChangeWorld(m_ActiveWorld.Areas[player.WorldPosition], m_ActiveWorld.Areas[player.WorldPosition].SpawnPoint);
                    return;
                }

                PhysicsResult physicsResult = PhysicsManager.IsCollision(player.WorldPosition, player.WorldPosition, m_ActiveWorld);
                if (physicsResult == PhysicsResult.ObjectCollision)
                {
                    //Get the item picked up
                    ItemInstance pickUp = m_ActiveWorld.PickUpObject(player);

                    //And try to destroy the corresponding GameObject
                    if (pickUp != null)
                    {
                        GameObject.Destroy(GameObject.Find(pickUp.JoyName + ":" + pickUp.GUID));
                    }
                }
            }
            Vector2Int newPlayerPoint = m_ActiveWorld.Player.WorldPosition;

            //North
            if (Input.GetKeyDown(KeyCode.Keypad8))
            {
                if (m_GameplayFlags == GameplayFlags.Targeting)
                {
                    player.TargetPoint = new Vector2Int(player.TargetPoint.x, player.TargetPoint.y - 1);
                }
                else
                {
                    newPlayerPoint.y += 1;
                    hasMoved          = true;
                }
            }
            //North east
            else if (Input.GetKeyDown(KeyCode.Keypad9))
            {
                if (m_GameplayFlags == GameplayFlags.Targeting)
                {
                    player.TargetPoint = new Vector2Int(player.TargetPoint.x + 1, player.TargetPoint.y - 1);
                }
                else
                {
                    newPlayerPoint.x += 1;
                    newPlayerPoint.y += 1;
                    hasMoved          = true;
                }
            }
            //East
            else if (Input.GetKeyDown(KeyCode.Keypad6))
            {
                if (m_GameplayFlags == GameplayFlags.Targeting)
                {
                    player.TargetPoint = new Vector2Int(player.TargetPoint.x + 1, player.TargetPoint.y);
                }
                else
                {
                    newPlayerPoint.x += 1;
                    hasMoved          = true;
                }
            }
            //South east
            else if (Input.GetKeyDown(KeyCode.Keypad3))
            {
                if (m_GameplayFlags == GameplayFlags.Targeting)
                {
                    player.TargetPoint = new Vector2Int(player.TargetPoint.x + 1, player.TargetPoint.y + 1);
                }
                else
                {
                    newPlayerPoint.x += 1;
                    newPlayerPoint.y -= 1;
                    hasMoved          = true;
                }
            }
            //South
            else if (Input.GetKeyDown(KeyCode.Keypad2))
            {
                if (m_GameplayFlags == GameplayFlags.Targeting)
                {
                    player.TargetPoint = new Vector2Int(player.TargetPoint.x, player.TargetPoint.y + 1);
                }
                else
                {
                    newPlayerPoint.y -= 1;
                    hasMoved          = true;
                }
            }
            //South west
            else if (Input.GetKeyDown(KeyCode.Keypad1))
            {
                if (m_GameplayFlags == GameplayFlags.Targeting)
                {
                    player.TargetPoint = new Vector2Int(player.TargetPoint.x - 1, player.TargetPoint.y + 1);
                }
                else
                {
                    newPlayerPoint.x -= 1;
                    newPlayerPoint.y -= 1;
                    hasMoved          = true;
                }
            }
            //West
            else if (Input.GetKeyDown(KeyCode.Keypad4))
            {
                if (m_GameplayFlags == GameplayFlags.Targeting)
                {
                    player.TargetPoint = new Vector2Int(player.TargetPoint.x - 1, player.TargetPoint.y);
                }
                else
                {
                    newPlayerPoint.x -= 1;
                    hasMoved          = true;
                }
            }
            //North west
            else if (Input.GetKeyDown(KeyCode.Keypad7))
            {
                if (m_GameplayFlags == GameplayFlags.Targeting)
                {
                    player.TargetPoint = new Vector2Int(player.TargetPoint.x - 1, player.TargetPoint.y - 1);
                }
                else
                {
                    newPlayerPoint.x -= 1;
                    newPlayerPoint.y += 1;
                    hasMoved          = true;
                }
            }
            else if (Input.GetKeyDown(KeyCode.Keypad5))
            {
                Tick();
                return;
            }

            if (hasMoved)
            {
                PhysicsResult physicsResult = PhysicsManager.IsCollision(player.WorldPosition, newPlayerPoint, m_ActiveWorld);

                if (physicsResult == PhysicsResult.EntityCollision)
                {
                    Entity tempEntity = m_ActiveWorld.GetEntity(newPlayerPoint);
                    if (m_GameplayFlags == GameplayFlags.Interacting)
                    {
                        if (tempEntity.Sentient)
                        {
                            TalkToPlayer(tempEntity);
                        }
                    }
                    else if (m_GameplayFlags == GameplayFlags.Giving)
                    {
                    }
                    else if (m_GameplayFlags == GameplayFlags.Moving)
                    {
                        playerWorld.SwapPosition(player, tempEntity);
                        Tick();
                    }
                    else if (m_GameplayFlags == GameplayFlags.Attacking)
                    {
                        if (tempEntity.GUID != player.GUID)
                        {
                            CombatEngine.SwingWeapon(player, tempEntity);
                            tempEntity.InfluenceMe(player.GUID, -50);
                            if (!tempEntity.Alive)
                            {
                                m_ActiveWorld.RemoveEntity(newPlayerPoint);

                                //Find a way to remove the GameObject
                                for (int i = 0; i < m_EntitiesHolder.transform.childCount; i++)
                                {
                                    if (m_EntitiesHolder.transform.GetChild(i).name.Contains(tempEntity.GUID.ToString()))
                                    {
                                        GameObject.Destroy(m_EntitiesHolder.transform.GetChild(i).gameObject);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    Tick();
                }
                else if (physicsResult == PhysicsResult.WallCollision)
                {
                    //Do nothing!
                }
                else
                {
                    if (newPlayerPoint.x >= 0 && newPlayerPoint.x < m_ActiveWorld.Tiles.GetLength(0) && newPlayerPoint.y >= 0 && newPlayerPoint.y < m_ActiveWorld.Tiles.GetLength(1))
                    {
                        player.Move(newPlayerPoint);
                        Tick();
                    }
                }
            }
            else if (m_GameplayFlags == GameplayFlags.Targeting)
            {
                if (player.TargetingAbility.targetType == AbilityTarget.Adjacent)
                {
                    if (AdjacencyHelper.IsAdjacent(player.WorldPosition, player.TargetPoint))
                    {
                        Entity tempEntity = m_ActiveWorld.GetEntity(player.TargetPoint);
                        if (tempEntity != null && Input.GetKeyDown(KeyCode.Return))
                        {
                            player.TargetingAbility.Use(player, tempEntity);
                            Tick();
                            m_GameplayFlags = GameplayFlags.Moving;
                        }
                    }
                }
                else if (player.TargetingAbility.targetType == AbilityTarget.Ranged)
                {
                    Entity tempEntity = m_ActiveWorld.GetEntity(player.TargetPoint);
                    if (tempEntity != null && Input.GetKeyDown(KeyCode.Return))
                    {
                        player.TargetingAbility.Use(player, tempEntity);
                        Tick();
                        m_GameplayFlags = GameplayFlags.Moving;
                    }
                }
            }

            if (autoTurn)
            {
                Tick();
            }
            m_Camera.transform.position = new Vector3(player.WorldPosition.x, player.WorldPosition.y, m_Camera.transform.position.z);
        }