public void RemoveItem(TrapEntry entry) { bool encountered = false; for (int place = 0; place < filled; place++) { if (encountered |= (entry == entries[place])) { if (place < filled - 1) { entries[place] = entries[place + 1]; StartCoroutine(TweenPos(entries[place], destination(place))); } else { entries[place] = null; } } } filled = Mathf.Max(0, filled - (encountered ? 1 : 0)); }
public void AddItem(TrapEntry entry) { // Cases: // filled = 0: start at -1, drop out of loop and put the item in. This is the first item, so it goes in // filled = TRAP_SIZE, valuable item: walk up the trap, looking for the first item worth more than this one // filled = TRAP_SIZE, worthless item: do nothing int place; for (place = Mathf.Min(TRAP_SIZE - 1, filled - 1); place >= 0 && entry.TrappedItem.Value > entries[place].TrappedItem.Value; place--) { if (place < TRAP_SIZE - 1) { entries[place + 1] = entries[place]; StartCoroutine(TweenPos(entries[place], destination(place + 1))); } } if (++place < TRAP_SIZE) { entries[place] = entry; StartCoroutine(TweenPos(entries[place], destination(place))); } filled = Mathf.Min(TRAP_SIZE, filled + 1); }
public IEnumerator TweenPos(TrapEntry entry, Rect eventual) { float dt = TWEEN_TIME; float totalTime = dt; float pct; float startX = entry.OccupiedRect.x; float startY = entry.OccupiedRect.y; Rect current = entry.OccupiedRect; while (dt > 0f) { yield return 0; dt = Mathf.Max(0f, dt - Time.deltaTime); pct = 1f - (dt / totalTime); current.x = Mathf.Lerp(startX, eventual.x, pct); current.y = Mathf.Lerp(startY, eventual.y, pct); entry.OccupiedRect = current; } }
public void Update() { if (Input.GetMouseButtonDown(0)) { Held = lootTrap.MouseOver(); if (Held != null) { lootTrap.RemoveItem(Held); } } if (Input.GetMouseButtonUp(0)) { Held = null; } if (Held != null) { Rect temp = Held.OccupiedRect; temp.center = InputExtender.MousePos(); Held.OccupiedRect = temp; } }