private void _GenerateItems(int lastItemGeneratedSectionIndex) { int currentItemSectionIndex = Mathf.FloorToInt(_runMiles / ITEM_SECTION_DISTANCE); if (currentItemSectionIndex >= lastItemGeneratedSectionIndex - ITEM_SECTION_DIFF) { for (int i = lastItemGeneratedSectionIndex + 1; i <= currentItemSectionIndex + ITEM_SECTION_DIFF; ++i) { var itemIndex = _SelectItemGeneratedIndex(_items.Select(item => item.GeneratedProbability).Concat(new float[] { EMPTY_PROBABILITY }).ToArray()); if (itemIndex >= _items.Length) { continue; } var generatedItem = _items[itemIndex]; Rect itemRange = RectExtension.ToRect(new Vector2(0, i * ITEM_SECTION_DISTANCE), new Vector2(10, 12)); var itemInstance = new ItemInstance { Data = generatedItem, Position = new Vector2(Random.Range(itemRange.xMin, itemRange.xMax), Random.Range(itemRange.yMin, itemRange.yMax)), IsUsed = false, }; _itemInstances.Add(itemInstance); _lastItemGeneratedSectionIndex = i; } } }
private void _TouchItems(Vector2 characterPosition, float characterSize, IEnumerable <ItemInstance> itemInstances) { var characterSizeVector = new Vector2(characterSize, characterSize); Rect characterRect = RectExtension.ToRect(characterPosition, characterSizeVector); foreach (var item in itemInstances) { if (item.IsUsed) { continue; } Rect itemRect = RectExtension.ToRect(item.Position, item.Data.Size); if (itemRect.Overlaps(characterRect)) { item.Data.GetEffect(this); item.IsUsed = true; } } }