void updateFlippedThisFrame() { if (flippedThisFrame) { flippedThisFrame = false; } if ((!helicopter.getMovingRight() && targetPosition.x > helicopter.transform.position.x) || (helicopter.getMovingRight() && targetPosition.x < helicopter.transform.position.x)) { flippedThisFrame = true; } }
void updateFlippedThisFrame() { if (flippedThisFrame) { flippedThisFrame = false; } if (((!helicopter.getMovingRight() && targetPosition.x > helicopter.transform.position.x) || (helicopter.getMovingRight() && targetPosition.x < helicopter.transform.position.x)) && (Mathf.Abs(helicopter.transform.position.x - targetPosition.x) > deadZoneForModelFlip)) { flippedThisFrame = true; } }
/** * The default constructor for FlipState. * This makes the assumption that we can finish this FlipState with * the position of the helicopter after the flip as the root, and then * begin the movement pattern from there (starting sine wave at t=0). * Because of this, this shouldn't be called by IdleState, as it can * result in the helicopter repeatedly changing it's anchor height after * every flip, over time making the helicopter descend or ascend significantly */ public FlipState(HeliScript helicopter, float deltaTime, HelicopterState toReturnTo) { //Initializing default, independent vals for behavioral variables this.helicopter = helicopter; timeIntoFlip = 0f; haveFlipped = false; flippedThisFrame = false; rootPosition = toReturnTo.getTargetPosition(); rootPosition.y -= helicopter.yOffsetMovement.indexedValue(); //since we adjust for bob, remove from root position this.toReturnTo = toReturnTo; justSawPlayer = false; initialSpeed = (helicopter.getMovingRight()) ? helicopter.speed : -helicopter.speed; //initalize debug vars //currSpeedDebug = new List<float>(); //targetPositionDebug = new List<float> (); //Shortcuts for accessing frequent variables without reaching back into helicopter each time lengthOfFlipInSeconds = helicopter.flipTime; halfwayPoint = lengthOfFlipInSeconds / 2; spotlightRotationBeforeFlip = helicopter.getSpotlight().transform.rotation; //Only need to calculate new spotlight rotation once, as it is simply the negated spotlight's rotation along the z axis targetSpotlightRotation = calculateNewSpotlightRotation(); //Similarly, only need to calculate spotlight rotation speed once, as it's just the value that is scaled by time //to allow the spotlight to rotate smoothly and end exactly on the rotation point at helicopter.flipTime seconds spotlightRotationSpeed = calculateSpotlightRotationSpeed(targetSpotlightRotation.eulerAngles.z); //Must call this once during the constructor so that we have values initiated for the first frame of it's instantiation updateState(deltaTime); }
//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); }
/** * This is called once per frame and updates the variables necessary for its state * It also checks to see if a new state is required, and if so, returns that new state */ public HelicopterState updateState(float deltaTime) { if (justSawPlayer) { return(new PursuitState(helicopter, deltaTime, this)); } //Logic for checking whether there should be a new state if ((helicopter.getMovingRight() && helicopter.transform.position.x - rootPosition.x > traversalDistance) || (!helicopter.getMovingRight() && rootPosition.x - helicopter.transform.position.x > traversalDistance)) { return(new FlipState(helicopter, deltaTime, this)); } //Update the values for each important field //targetSpotlightRotation and spotlightRotationSpeed never change targetPosition = calculateTargetPosition(deltaTime); currSpeed = calculateNewSpeed(deltaTime); //We haven't changed from idle due to any factors we can calculate within this state, //So we return the current state to be called again next frame return(this); }