using UnityEngine; using System.Collections; using System.Collections.Generic; public class SlotMachine : MonoBehaviour { public Sprite[] slotIcons; // Array to hold slot icons public Vector2 spriteSize = new Vector2(1, 1); // Size of the slot icons public float verticalSpacing = 1.5f; // Vertical distance between sprites public int[] slotValues; // Array to hold the values for each result public float minSpinDuration = 3.0f; // Minimum spin duration public float maxSpinDuration = 5.0f; // Maximum spin duration public float initialSpinSpeed = 5.0f; // Initial speed of the spin animation public float slowDownFactor = 0.98f; // Factor by which the spin speed will slow down each frame private List reelSlots; // List to hold SpriteRenderers for the reel private int[] results; // Array to hold the result values private bool spinning = false; // Flag to track if the machine is spinning void Start() { reelSlots = new List(); results = new int[slotIcons.Length]; // Initialize the reel with slots for (int i = 0; i < slotIcons.Length; i++) { GameObject slot = new GameObject("Slot" + i); slot.transform.SetParent(transform); slot.transform.localPosition = new Vector3(0, i * verticalSpacing, 0); SpriteRenderer renderer = slot.AddComponent(); renderer.sprite = slotIcons[i]; renderer.transform.localScale = new Vector3(spriteSize.x, spriteSize.y, 1); renderer.sortingLayerName = "Foreground"; // Replace with your sorting layer name renderer.sortingOrder = 10; // Adjust the sorting order as needed reelSlots.Add(renderer); } SetSpriteSize(); StartSpin(); // Manually start the spin } public void StartSpin() { if (!spinning) { Debug.Log("Starting spin..."); StartCoroutine(SpinSlots()); } } public void SetSpriteSize() { foreach (var renderer in reelSlots) { if (renderer != null) { renderer.transform.localScale = new Vector3(spriteSize.x, spriteSize.y, 1); } } } public void AddSlotResult(int index, int value) { if (index >= 0 && index < slotIcons.Length) { slotValues[index] = value; } } private IEnumerator SpinSlots() { Debug.Log("Spin coroutine started."); spinning = true; float timeElapsed = 0; float spinDuration = Random.Range(minSpinDuration, maxSpinDuration); float spinSpeed = initialSpinSpeed; while (timeElapsed < spinDuration) { for (int i = 0; i < reelSlots.Count; i++) { var renderer = reelSlots[i]; // Move the sprite downwards to mimic spinning renderer.transform.localPosition -= new Vector3(0, verticalSpacing * Time.deltaTime * spinSpeed, 0); // Wrap the sprite around to the top if (renderer.transform.localPosition.y <= -verticalSpacing) { renderer.transform.localPosition += new Vector3(0, verticalSpacing * reelSlots.Count, 0); renderer.sprite = slotIcons[Random.Range(0, slotIcons.Length)]; Debug.Log("Sprite assigned to renderer: " + renderer.sprite.name); } } timeElapsed += Time.deltaTime; spinSpeed *= slowDownFactor; // Gradually slow down the spin Debug.Log("Spinning... Time elapsed: " + timeElapsed + " Spin Speed: " + spinSpeed); yield return null; // Continue to next frame for smooth animation } // Ensure a winning result SetWinningResult(); CalculateDamage(); spinning = false; Debug.Log("Spin completed."); } private void SetWinningResult() { // Choose a winning result sprite from the slotIcons array Sprite winningSprite = slotIcons[Random.Range(0, slotIcons.Length)]; // Set the middle slot to the winning sprite int middleIndex = reelSlots.Count / 2; reelSlots[middleIndex].sprite = winningSprite; // Adjust the positions so the winning result is in the center float shiftDistance = -reelSlots[middleIndex].transform.localPosition.y; foreach (var renderer in reelSlots) { renderer.transform.localPosition += new Vector3(0, shiftDistance, 0); // Wrap the sprite around to the top if it goes below the bottom position if (renderer.transform.localPosition.y <= -verticalSpacing) { renderer.transform.localPosition += new Vector3(0, verticalSpacing * reelSlots.Count, 0); } else if (renderer.transform.localPosition.y >= verticalSpacing * reelSlots.Count) { renderer.transform.localPosition -= new Vector3(0, verticalSpacing * reelSlots.Count, 0); } } Debug.Log("Winning Sprite: " + winningSprite.name); } private void CalculateDamage() { int totalDamage = 0; // Get results from the middle slot for scoring int middleIndex = reelSlots.Count / 2; SpriteRenderer middleSlot = reelSlots[middleIndex]; for (int j = 0; j < slotIcons.Length; j++) { if (middleSlot.sprite == slotIcons[j]) { results[middleIndex] = slotValues[j]; } } // Implement damage calculation based on single, double, and treble results Dictionary resultCount = new Dictionary(); foreach (int result in results) { if (resultCount.ContainsKey(result)) { resultCount[result]++; } else { resultCount[result] = 1; } } foreach (KeyValuePair entry in resultCount) { if (entry.Value == 1) { totalDamage += entry.Key; } else if (entry.Value == 2) { totalDamage += entry.Key * 2; } else if (entry.Value == 3) { totalDamage += entry.Key * 3; } } Debug.Log("Total Damage: " + totalDamage); } private void UpdateSortingOrder() { foreach (var renderer in reelSlots) { renderer.sortingLayerName = "Foreground"; // Ensure this matches your desired sorting layer renderer.sortingOrder = 10; // Adjust the order to bring elements forward renderer.enabled = false; // Temporarily disable to force update renderer.enabled = true; // Re-enable renderer } } }