//Constructor run when creating IdleState public ReturnToStateState(HeliScript helicopter, float deltaTime, HelicopterState toReturnTo) { this.helicopter = helicopter; nextState = toReturnTo; targetPosition = nextState.getTargetPosition(); spotlightRotationSpeed = nextState.getSpotlightRotationSpeed(); targetSpotlightRotation = nextState.getTargetSpotlightRotation(); currSpeed = helicopter.speed; if ((helicopter.getMovingRight() && toReturnTo.getTargetPosition().x < helicopter.transform.position.x) || (!helicopter.getMovingRight() && toReturnTo.getTargetPosition().x > helicopter.transform.position.x)) { //We gotta turn around first toDoFirst = new FlipState(helicopter, deltaTime, this); } updateState(deltaTime); }
/** * Each frame, the current state must provide five things: * * Vector3 targetPosition = the position in the world that the helicopter should fly to * float currSpeed = the speed that the helicopter should move towards that position * Quaternion desiredSpotlightRotation = the Quaternion representation of the rotation that the spotlight should rotate towards * float spotlightMoveSpeed = the speed that the spotlight should rotate towards that rotation * bool newDirection = For this frame, newDirection = 1 if movingRight should be changed, 0 if it should stay the same. */ private void updateState() { state = state.updateState(Time.deltaTime); movingRight = (state.newDirection()) ? !movingRight : movingRight; transform.position = Vector3.MoveTowards(transform.position, state.getTargetPosition(), state.getCurrSpeed() * Time.deltaTime); spotlight.transform.rotation = Quaternion.RotateTowards(spotlight.transform.rotation, state.getTargetSpotlightRotation(), state.getSpotlightRotationSpeed() * Time.deltaTime); }