private int CalculateDisplacementFactor(RunnerLanes destinationLane) { //Lane mask: 111 //Left sample -> 001 ==1<<0 -> displacement factor = -1 int displacementFactor = 0; int laneCount = Enum.GetValues(typeof(RunnerLanes)).Length; int laneShiftFactor = 0; while (laneShiftFactor < laneCount) { RunnerLanes lane = (RunnerLanes)(1 << laneShiftFactor); if (lane == destinationLane) { //-1 is harcoded for 3 lanes. This should be refactore may more lanes be added to the game displacementFactor = laneShiftFactor - 1; break; } laneShiftFactor++; } return(displacementFactor); }
/// <summary> /// Gets a random lane from the RunnerLanes enum, making sure it is not included in the current mask /// </summary> /// <param name="currentMask"></param> /// <returns></returns> private static RunnerLanes GetRandomLane(RunnerLanes currentMask, Array runnerLanes, Random randomGenerator) { RunnerLanes newLane; do { newLane = (RunnerLanes)runnerLanes.GetValue(randomGenerator.Next(runnerLanes.Length)); }while ((currentMask & newLane) != 0); return(newLane); }
/// <summary> /// Generates a valid obstacle mask in raw format /// </summary> /// <returns></returns> public static RunnerLanes GenerateRandomObstacleMask(int activeLaneCount) { Array runnerLanes = Enum.GetValues(typeof(RunnerLanes)); Random randomLaneGenerator = new Random(); RunnerLanes mask = 0; for (int i = 0; i < activeLaneCount; i++) { mask |= GetRandomLane(mask, runnerLanes, randomLaneGenerator); } return(mask); }
/// <summary> /// Moves player one step to the right /// </summary> public void MoveRight() { if (currentLane == RunnerLanes.Right) { return; } RunnerLanes destinationLane = (currentLane == RunnerLanes.Left) ? RunnerLanes.Center : RunnerLanes.Right; if (lateralMovementCoroutine != null) { StopCoroutine(lateralMovementCoroutine); } lateralMovementCoroutine = StartCoroutine(MoveSmooth(transform, destinationLane)); }
private void InitializePlayer() { //Initialize Player position to middle lane: currentLane = RunnerLanes.Center; transform.position = Vector3.zero; currentShapeIndex = 0; //Initialize shapes shapeList = new List <GameObject>(); for (int i = 0; i < shapeParent.childCount; i++) { GameObject shape = shapeParent.GetChild(i).gameObject; shape.SetActive(i == currentShapeIndex); shapeList.Add(shape); } deathParticleSystem.SetActive(false); }
/// <summary> /// Generates a formatted Obstacle maske ready to be used /// </summary> /// <param name="gateCount"></param> /// <returns></returns> public static ObstacleMask GenerateObstacleMask(int gateCount = 1) { RunnerLanes maskSeed = GenerateRandomObstacleMask(gateCount); BitArray b = new BitArray(new byte[] { (byte)maskSeed }); bool[] bits = new bool[b.Count]; b.CopyTo(bits, 0); byte[] bitValues = bits.Select(bit => (byte)(bit ? 1 : 0)).ToArray(); // index 0 = less significant bit. Here I should retrieve indexes 0-2 (both included) //index 0 = Left //index 1 = Center //index 2 = Right ObstacleMask mask = ObstacleMask.CreateInitializedObstacleMask( bitValues[0], bitValues[1], bitValues[2]); return(mask); }
private IEnumerator MoveSmooth(Transform target, RunnerLanes destinationLane) { PlaySound(laneChangeClip); int displacementFactor = CalculateDisplacementFactor(destinationLane); Vector3 destination = transform.position; destination.x = displacementFactor * laneWidth; currentLane = destinationLane; //Set it here so if user swipes again, the player moves accordingly float elapsedTime = 0; Vector3 velocity = Vector3.one; while (elapsedTime <= 1) { transform.position = Vector3.SmoothDamp(target.position, destination, ref velocity, movementDuration); elapsedTime += Time.deltaTime; yield return(null); } lateralMovementCoroutine = null; }