/** * Fills the screen with slots and adds more slots at both ends in order to create * the illusion of an endless stream of slots. The left-hand side has more slots, * because the start animation of the spinner first moves the slots to the right * before spinning at the desired speed to the left. * **/ private void CreateSlots() { m_spinnerSlotsCount = (int)(Screen.width / (Screen.height / 4f)) + 5; // adding enough slots to fill the screen on both sides m_spinnerSlotWidth = (Screen.height / 4f); m_spinnerSlotHeight = m_spinnerSlotWidth; m_deathPosition = new Vector2(((Screen.width / 2f * -1) - 3 * m_spinnerSlotWidth), 0); // higher offset to the left for the start of the spinning animation m_spawnPosition = new Vector2(((Screen.width / 2f * -1) - 3 * m_spinnerSlotWidth) + m_spinnerSlotsCount * m_spinnerSlotWidth, 0); for (int i = 0; i < m_spinnerSlotsCount; i++) { m_gameObjectCache = Instantiate(slotPrefab); m_spinnerSlotCache = m_gameObjectCache.GetComponent <SpinnerSlot> (); m_spinnerSlotCache.name = "SpinnerSlot_" + i; m_spinnerSlotCache.transform.SetParent(transform); m_spinnerSlotCache.SetItemType(GetNextRandomReward()); m_spinnerSlotCache.index = i; m_spinnerSlotCache.SetSize(new Vector2(m_spinnerSlotWidth, m_spinnerSlotHeight)); m_spinnerSlotCache.SetPosition(new Vector2(m_deathPosition.x + i * m_spinnerSlotWidth, 0)); m_spinnerSlots.Add(m_spinnerSlotCache); } m_leftMostSpinnerSlot = m_spinnerSlots [0]; }
void FixedUpdate() { if (m_spinning) { if (!m_braking && iTween.Count(gameObject) == 0) { // spinner finished start animation and is now at desired speed // state of the button is handled spinStopButton.onClick.RemoveListener(Spin); spinStopButton.GetComponentInChildren <Text>().text = "Stop"; spinStopButton.onClick.AddListener(Brake); spinStopButton.interactable = true; } // moving all spinner slots synchronized for (int i = 0; i < m_spinnerSlotsCount; i++) { m_spinnerSlotCache = m_spinnerSlots [i]; m_positionCache = m_spinnerSlotCache.GetPosition(); m_positionCache.x -= m_currentSpeed * Time.fixedDeltaTime; m_spinnerSlotCache.SetPosition(m_positionCache); } // when a spinner has reached the spot marked for death, it will be respawned at the right-most end of the spinner slots // no fixed spawn position is computed, because the offset between the right-most spinner and the spawn point // varies depending on the current speed of the spinner if (m_leftMostSpinnerSlot.GetPosition().x <= m_deathPosition.x) { int leftMostSlotIndex = m_spinnerSlots.IndexOf(m_leftMostSpinnerSlot); int indexOfNextSlot = leftMostSlotIndex + 1; if (indexOfNextSlot == m_spinnerSlotsCount) { indexOfNextSlot = 0; } m_leftMostSpinnerSlot.SetPosition(new Vector2(GetLastSpinnerSlot().GetPosition().x + m_spinnerSlotWidth, 0)); m_leftMostSpinnerSlot.SetItemType(GetNextRandomReward()); m_leftMostSpinnerSlot = m_spinnerSlots [indexOfNextSlot]; } // when the spinner is slowed down to a certain point at which the movement // is not visible to the human eye, it will stop interpolating between values if (m_braking && m_currentSpeed <= BRAKING_SPEED_THRESHOLD) { m_spinning = false; m_braking = false; iTween.Stop(gameObject); m_winningSlot = FindCenterSlot(); m_winningSlot.ShowRewardFX(); m_currentSpeed = 0; // state of the button is handled spinStopButton.onClick.RemoveListener(Brake); spinStopButton.GetComponentInChildren <Text>().text = "Spin"; spinStopButton.onClick.AddListener(Spin); spinStopButton.interactable = true; } } }