TouhouSortSortable[] LoadTouhous(Category category, int amount) { List <Style> styles = new List <Style>(); foreach (Sprite sprite in category.leftPool) { Style style = new Style(); style.name = category.name; style.sprite = sprite; styles.Add(style); } foreach (Sprite sprite in category.rightPool) { Style style = new Style(); style.sprite = sprite; styles.Add(style); } // Scoop <amount> random touhous from the category if (amount > styles.Count) { amount = styles.Count; } MouseGrabbableGroup grabGroup = stagingArea.GetComponent <MouseGrabbableGroup>(); TouhouSortSortable[] randomTouhous = new TouhouSortSortable[amount]; UnityEvent dudEvent = new UnityEvent(); UnityEvent sortEvent = new UnityEvent(); sortEvent.AddListener(CheckSort); for (int i = 0; i < amount; i++) { Style style = styles[Random.Range(0, styles.Count)]; // Build a new touhou instance TouhouSortSortable touhou = Instantiate(touhouTemplate, transform.position, transform.rotation); touhou.GetComponent <SpriteRenderer>().sprite = style.sprite; touhou.gameObject.AddComponent <PolygonCollider2D>(); touhou.SetStyle(style.name); MouseGrabbable grab = touhou.gameObject.AddComponent <MouseGrabbable>(); grab.onGrab = dudEvent; grab.onRelease = sortEvent; grab.disableOnVictory = true; styles.Remove(style); touhou.transform.parent = stagingArea; grabGroup.addGrabbable(grab, true); randomTouhous[i] = touhou; } return(randomTouhous); }
TouhouSortSortable[] LoadTouhous(Category category, int amount) { List <Style> leftStyles = new List <Style>(); List <Style> rightStyles = new List <Style>(); foreach (Sprite sprite in category.leftPool) { Style style = new Style(); style.name = category.name; style.sprite = sprite; leftStyles.Add(style); } foreach (Sprite sprite in category.rightPool) { Style style = new Style(); style.sprite = sprite; rightStyles.Add(style); } MouseGrabbableGroup grabGroup = stagingArea.GetComponent <MouseGrabbableGroup>(); TouhouSortSortable[] randomTouhous = new TouhouSortSortable[amount]; int lastPickedSide = 0; // 0 for left, 1 for right for (int i = 0; i < amount; i++) { Style style; if (leftStyles.Count == 0) { style = rightStyles[Random.Range(0, rightStyles.Count)]; lastPickedSide = 0; rightStyles.Remove(style); } else if (rightStyles.Count == 0) { style = leftStyles[Random.Range(0, leftStyles.Count)]; lastPickedSide = 1; leftStyles.Remove(style); } else if (i == 1) // Ensure one of each type { if (lastPickedSide == 1) { style = leftStyles[Random.Range(0, leftStyles.Count)]; leftStyles.Remove(style); } else { style = rightStyles[Random.Range(0, leftStyles.Count)]; rightStyles.Remove(style); } lastPickedSide = 1 - lastPickedSide; } else { int coin = Random.Range(0, 2); if (coin == 0) { style = leftStyles[Random.Range(0, leftStyles.Count)]; lastPickedSide = 0; leftStyles.Remove(style); } else { style = rightStyles[Random.Range(0, rightStyles.Count)]; lastPickedSide = 1; rightStyles.Remove(style); } } // Build a new touhou instance TouhouSortSortable touhou = Instantiate(touhouTemplate, transform.position, transform.rotation); touhou.GetComponent <SpriteRenderer>().sprite = style.sprite; touhou.gameObject.AddComponent <PolygonCollider2D>(); touhou.SetStyle(style.name); MouseGrabbable grab = touhou.gameObject.AddComponent <MouseGrabbable>(); UnityEvent grabEvent = new UnityEvent(); grabEvent.AddListener(touhou.OnGrab); UnityEvent releaseEvent = new UnityEvent(); releaseEvent.AddListener(touhou.OnRelease); releaseEvent.AddListener(CheckSort); grab.onGrab = grabEvent; grab.onRelease = releaseEvent; grab.disableOnVictory = true; touhou.transform.parent = stagingArea; grabGroup.addGrabbable(grab, true); randomTouhous[i] = touhou; } randomTouhous.Shuffle(); return(randomTouhous); }