Changeset 6252 for 2014/30/MitjaK/Attack to Agora/Attack to Agora/Attack to Agora/Attack to Agora/ChromaCase.cs
- Timestamp:
- 2015-06-24 14:09:25 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
2014/30/MitjaK/Attack to Agora/Attack to Agora/Attack to Agora/Attack to Agora/ChromaCase.cs
r6250 r6252 7 7 public class ChromaCase 8 8 { 9 public GameObject OpeningScreenFront; 10 private Vector ShownItemPosition = Vector.Zero; 11 private int CurrentItem = 0; 9 12 13 private List<GameObject> PossibleItems = new List<GameObject>(); 14 private List<GameObject> ShownItems = new List<GameObject>(); 15 16 private GameObject FinalItem; 17 18 public Vector Position = Vector.Zero; 19 20 public const double START_SPEED = 0.1; 21 public const double STOP_SPEED = 2.0; 22 private Timer changeTimer = new Timer(); 23 24 private int layer; 25 26 public delegate void CaseHandler(GameObject item); 27 public event CaseHandler ItemReceived; 28 29 /// <summary> 30 /// Luo ChromaCase-olion. Lisää olio ruudulle Add-metodilla. 31 /// Todennäköisyydet ovat esineiden tageissa doublena mielivaltaisella välillä, 32 /// esim 0.0 - 100.0 33 /// </summary> 34 /// <param name="possibleItems"></param> 35 /// <param name="finalItem"></param> 36 public ChromaCase(List<GameObject> possibleItems, GameObject finalItem) 37 { 38 this.PossibleItems.AddRange(possibleItems); 39 if (finalItem != null) 40 FinalItem = finalItem; 41 } 42 43 public void Add(Vector position, int layer = 0) 44 { 45 Game.Instance.Add(OpeningScreenFront, layer); 46 47 this.Position = position; 48 OpeningScreenFront.Position = this.Position; 49 this.layer = layer; 50 } 51 52 public void Destroy() 53 { 54 OpeningScreenFront.Destroy(); 55 for (int i = 0; i < ShownItems.Count; i++) 56 { 57 ShownItems[i].Destroy(); 58 } 59 } 60 61 public void Start() 62 { 63 if (ShownItems.Count == 0) 64 return; 65 66 double speedIncrease = (STOP_SPEED - START_SPEED) / ShownItems.Count; 67 68 changeTimer.Interval = START_SPEED; 69 changeTimer.Timeout += delegate { ChangeItem(); }; 70 changeTimer.Timeout += delegate 71 { 72 changeTimer.Interval += speedIncrease; 73 if (changeTimer.Interval >= STOP_SPEED) 74 { 75 changeTimer.Timeout += delegate 76 { 77 StopTimer(); 78 GiveItem(); 79 Destroy(); 80 }; 81 } 82 }; 83 changeTimer.Start(); 84 85 // eka esine näkyville 86 ChangeItem(); 87 } 88 89 private void ChangeItem(bool destroyOld = true) 90 { 91 if (destroyOld) 92 { 93 ShownItems[CurrentItem].Destroy(); 94 CurrentItem++; 95 } 96 97 ShownItems[CurrentItem].Position = ShownItemPosition; 98 if (layer > -3) 99 Game.Instance.Add(ShownItems[CurrentItem], layer - 1); 100 else 101 Game.Instance.Add(ShownItems[CurrentItem], layer); 102 } 103 104 private void StopTimer() 105 { 106 changeTimer.Stop(); 107 } 108 109 private void GiveItem() 110 { 111 if (ItemReceived != null) 112 ItemReceived(ShownItems[ShownItems.Count - 1]); 113 } 114 115 private void GenerateItemList(int itemAmount) 116 { 117 for (int i = 0; i < itemAmount; i++) 118 { 119 if (i == itemAmount - 1 && FinalItem != null) 120 { 121 ShownItems.Add(FinalItem); 122 break; 123 } 124 125 AddItemWithProbabilities(); 126 } 127 } 128 129 /// <summary> 130 /// Lisää ShownItems-listaan esineen PossibleItems-listasta. 131 /// Todennäköisyydet ovat esineiden tageissa doublena mielivaltaisella välillä, 132 /// esim 0.0 - 100.0 133 /// </summary> 134 private void AddItemWithProbabilities() 135 { 136 double total = 0; 137 List<double> limits = new List<double>(); 138 limits.Add(0.0); 139 140 for (int i = 0; i < PossibleItems.Count; i++) 141 { 142 double current = (double)PossibleItems[i].Tag; 143 total += current; 144 145 limits.Add(total); 146 } 147 148 double rNumber = RandomGen.NextDouble(0, total); 149 150 for (int i = 0; i < limits.Count - 1; i++) 151 { 152 if (rNumber >= limits[i] && rNumber < limits[i + 1]) 153 { 154 ShownItems.Add(PossibleItems[i]); 155 } 156 } 157 } 10 158 }
Note: See TracChangeset
for help on using the changeset viewer.