public bool Navigate(PixelPose pose) { // Find a list of doors to navigate to List <PixelDoor> path = FindPathToRoom(pose.pixelRoom); if (path != null) { foreach (PixelDoor door in path) { Debug.Log("Take " + door.name + " to " + door.destination); } } // Enqueue walkto tasks foreach (PixelDoor door in path) { CharacterTask walkToDoorTask = new CharacterTask(GameTask.TaskType.WALKTO, door.dropInWorldLocation); CharacterTask enterDoorTask = new CharacterTask(GameTask.TaskType.ENTERDOOR, door); characterTasks.Enqueue(walkToDoorTask); characterTasks.Enqueue(enterDoorTask); } Vector2 lastPosition; if (path.Count == 0) { lastPosition = transform.position; } else { lastPosition = path.Last().dropOffWorldLocation; } WalkInRoom(pose.pixelRoom, lastPosition, pose.position); CharacterTask faceTask = new CharacterTask(GameTask.TaskType.FACEDIRECTION, pose.direction); faceTask.character = this; characterTasks.Enqueue(faceTask); return(true); }
IEnumerator UpdateCharacterAction() { while (true) { if (characterTasks.Count == 0) { yield return(new WaitForSeconds(0.1f)); continue; } GameTask t = characterTasks.Peek(); Debug.Assert(t != null); if (t.taskType == GameTask.TaskType.NAVIGATE) { Debug.Assert(t.arguments.Count() >= 1 && t.arguments.Count() <= 3); bool completed; positionLocked = true; if (t.arguments.Count() == 1) { PixelPose pixelPose = (PixelPose)t.arguments[0]; completed = Navigate(pixelPose); } else { Direction direction = Direction.All; if (t.arguments.Count() == 3) { direction = (Direction)t.arguments[2]; } completed = NavigateObject((PixelRoom)t.arguments[0], (PixelCollider)t.arguments[1], direction); } if (completed) { positionLocked = false; characterTasks.Dequeue(); } } else if (t.taskType == GameTask.TaskType.WALKTO) { Debug.Assert(t.arguments.Count() == 1); positionLocked = true; bool completed = WalkTo((Vector2)t.arguments[0]); if (completed) { positionLocked = false; characterTasks.Dequeue(); } } else if (t.taskType == GameTask.TaskType.ENTERDOOR) { Debug.Assert(t.arguments.Count() == 1); EnterDoor((PixelDoor)t.arguments[0]); characterTasks.Dequeue(); } else if (t.taskType == GameTask.TaskType.INSPECT) { Debug.Assert(t.arguments.Count() == 1); InspectObject((PixelCollision)t.arguments[0]); characterTasks.Dequeue(); } else if (t.taskType == GameTask.TaskType.CREATE) { Debug.Assert(t.arguments.Count() == 2); CreateItem((GameObject)t.arguments[1], (int)t.arguments[0]); characterTasks.Dequeue(); } else if (t.taskType == GameTask.TaskType.PUTS) { Debug.Assert(t.arguments.Count() == 3); Puts((int)t.arguments[0], (string)t.arguments[1], (PixelStorage)t.arguments[2]); characterTasks.Dequeue(); } else if (t.taskType == GameTask.TaskType.TAKES) { Debug.Assert(t.arguments.Count() == 3); Takes((int)t.arguments[0], (string)t.arguments[1], (PixelStorage)t.arguments[2]); characterTasks.Dequeue(); } else if (t.taskType == GameTask.TaskType.GIVES) { Debug.Assert(t.arguments.Count() == 3); Gives((int)t.arguments[0], (string)t.arguments[1], (Character)t.arguments[2]); characterTasks.Dequeue(); } else if (t.taskType == GameTask.TaskType.STEALS) { Debug.Assert(t.arguments.Count() == 3); Steals((int)t.arguments[0], (string)t.arguments[1], (Character)t.arguments[2]); characterTasks.Dequeue(); } else if (t.taskType == GameTask.TaskType.FACEDIRECTION) { Debug.Assert(t.arguments.Count() == 1); FaceDirection((Direction)t.arguments[0]); characterTasks.Dequeue(); } else if (t.taskType == GameTask.TaskType.MOUNT) { // TODO 2. Create a function here that calls the mount function } yield return(new WaitForFixedUpdate()); } }
public bool NavigateObject(PixelRoom room, PixelCollider pixelCollider, Direction direction = Direction.All) { PixelRoom pixelRoom = pixelCollider.GetPixelRoom(); PixelCollider playerCollider = GetComponentInChildren <PixelCollider>(); // Find last location // Closest from the door or the players position List <PixelDoor> path = FindPathToRoom(room); Vector2 startPosition; if (path.Count != 0) { room.gameObject.SetActive(true); room.GetNavigationalMesh(playerCollider, path.Last().dropOffWorldLocation); room.gameObject.SetActive(false); startPosition = path.Last().dropOffWorldLocation; } else { startPosition = transform.position; } // Navigate from the last location to find the pixel pose PixelPose pixelPose; if (pixelCollider != null) { pixelRoom.GetNavigationalMesh(playerCollider, startPosition); // Object is movable if (pixelCollider.transform.parent.GetComponent <MovableObject>()) { if (direction != Direction.All) { WayPoint wayPoint = pixelCollider.FindWayPointInDirection(direction); pixelPose = new PixelPose(pixelRoom, direction, wayPoint.position); } else { // Player moves to the characters position first KeyValuePair <PixelPose, float> bestPlayerMovementWayPoint = pixelCollider.FindBestWayPoint(); pixelPose = bestPlayerMovementWayPoint.Key; Debug.DrawLine(transform.position, pixelPose.position, Color.red, 10.0f); // Character enqueues a movement to the best place for that position Character character = pixelCollider.GetComponentInParent <Character>(); if (character != null) { PixelPose translatedPose = pixelPose.TranslatePose(2 * (pixelCollider.navigationMargin + playerCollider.navigationMargin)); PixelPose flippedPose = translatedPose.Flip(); GameTask characterNavTask = new GameTask(GameTask.TaskType.NAVIGATE); characterNavTask.character = character; characterNavTask.arguments.Add(flippedPose); character.characterTasks.Enqueue(characterNavTask); } } } else { if (direction != Direction.All) { WayPoint wayPoint = pixelCollider.FindWayPointInDirection(direction); pixelPose = new PixelPose(pixelRoom, direction, wayPoint.position); } else { PixelCollider characterCollider = GetComponentInChildren <PixelCollider>(); pixelPose = pixelCollider.FindBestWayPointPosition(startPosition); } } Navigate(pixelPose); } return(true); }