void Start() { currentRoom = Room.BOTTOM_LEFT; state = State.STATIONARY; rb = GetComponent <Rigidbody2D>(); houseController = (HouseController)house.GetComponent <MonoBehaviour>(); cameraZoomController = (CameraZoomController)sceneCamera.GetComponent <MonoBehaviour>(); ahc = (ActivitiesHandlerController)activitiesHandler.GetComponent <MonoBehaviour>(); animator = GetComponent <Animator>(); }
public ActivityController SearchByVariety(Room targetRoom, ActivityVariety targetVariety) { ActivityType[] candidateTypes = activities.Keys.Where( type => activities.GetDictValue(type).GetVariety() == targetVariety && activities.GetDictValue(type).GetRoom() == targetRoom ).ToArray(); return(candidateTypes.Length == 0 ? null : activities.GetDictValue(candidateTypes[0])); }
private void InitiateMove(Room targetRoom) { if (targetRoom == currentRoom) { return; } timeElapsed = 0f; currentPath = houseController.GetPathBetweenRooms(currentRoom, targetRoom); movementDirection = GetMovementDirection( houseController.GetRoomPosition(currentRoom), houseController.GetRoomPosition(currentPath[0]) ); animator.SetBool(movementDirection, true); state = State.MOVING; }
public ActivityController SearchByPosition(Room targetRoom, Vector2 position) { ActivityBounds[] candidateBounds = activityLocations.Keys.Where( bounds => bounds.topLeft.x <= position.x && bounds.topLeft.y >= position.y && bounds.bottomRight.x >= position.x && bounds.bottomRight.y <= position.y && activities.GetDictValue(activityLocations.GetDictValue(bounds)).GetRoom() == targetRoom ).ToArray(); return(candidateBounds.Length == 0 ? null : activities.GetDictValue(activityLocations.GetDictValue(candidateBounds[0]))); }
void Update() { if (!isInitialised) { transform.position = houseController.GetRoomPosition(currentRoom); isInitialised = true; } if (state == State.STATIONARY) { if (Input.GetMouseButtonDown(0)) { Vector3 mp = Input.mousePosition; mp.z = Camera.main.transform.position.z; Vector2 mousePos = Camera.main.ScreenToWorldPoint(mp); float houseX = house.transform.position.x, houseY = house.transform.position.y; Room targetRoom; if (mousePos.x < houseX) { targetRoom = mousePos.y < houseY ? Room.BOTTOM_LEFT : Room.TOP_LEFT; } else { targetRoom = mousePos.y < houseY ? Room.BOTTOM_RIGHT : Room.TOP_RIGHT; } if (targetRoom != currentRoom) { InitiateMove(targetRoom); } else { targetActivity = ahc.SearchByPosition(currentRoom, mousePos); if (targetActivity != null) { InitiateEngagement(); } } } } if (state == State.MOVING) { timeElapsed += Time.deltaTime; int targetIndex = !currentPath.Contains(currentRoom) ? 0 : 1; Vector2 targetPos = houseController.GetRoomPosition(currentPath[targetIndex]); Debug.Log(Vector2.Lerp(houseController.GetRoomPosition(currentRoom), targetPos, timeElapsed / moveTime)); rb.MovePosition(Vector2.Lerp(houseController.GetRoomPosition(currentRoom), targetPos, timeElapsed / moveTime)); if ((Vector2)transform.position == targetPos) { currentRoom = currentPath[targetIndex]; if (targetIndex == currentPath.Count - 1) { state = State.STATIONARY; animator.SetBool(movementDirection, false); } else { timeElapsed = 0f; animator.SetBool(movementDirection, false); movementDirection = GetMovementDirection( houseController.GetRoomPosition(currentRoom), houseController.GetRoomPosition(currentPath[1]) ); animator.SetBool(movementDirection, true); } } } if (state == State.ENGAGING || state == State.DISENGAGING) { bool engaging = state == State.ENGAGING; timeElapsed += Time.deltaTime; Vector2 targetPos = targetActivity.GetEngagementPosition(); Vector2 roomPos = houseController.GetRoomPosition(currentRoom); Vector2 newPos = Vector2.Lerp(roomPos, targetPos, engaging ? timeElapsed / engageTime : 1 - timeElapsed / engageTime); rb.MovePosition(newPos); if ((Vector2)transform.position == (engaging ? targetPos : roomPos)) { animator.SetBool(movementDirection, false); state = engaging ? State.ENGAGED : State.STATIONARY; if (state == State.DISENGAGING) { cameraZoomController.StopZoom(); } } } if (state == State.ENGAGED) { GameObject minigameHandler = ahc.GetActivityHandlerObject(targetActivity.type); if (!minigameHandler.activeSelf) { minigameHandler.SetActive(true); miniGameScreen.SetActive(true); miniGameScreen.transform.position = new Vector3( sceneCamera.transform.position.x, sceneCamera.transform.position.y, miniGameScreen.transform.position.z ); } } }