Skip to content

Instantly share code, notes, and snippets.

@st4rdog
Created October 27, 2015 04:25
Show Gist options
  • Save st4rdog/40999c442bb83d9aefc1 to your computer and use it in GitHub Desktop.
Save st4rdog/40999c442bb83d9aefc1 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using SpaceTrader.Stations;
using SpaceTrader.Resources;
namespace SpaceTrader.Misc
{
public class Basket : MonoBehaviour
{
public List<BasketItem> _items;
//============================================
// FUNCTIONS (CUSTOM)
//============================================
public bool Contains(Resource resource)
{
foreach (BasketItem i in Items)
{
if (i.Resource == resource)
{
return true;
}
}
return false;
}
public void Add(Resource resource, int amount)
{
BasketItem item = Find(resource);
if (item != null)
{
item.Amount += amount;
}
else
{
BasketItem basketItem = new BasketItem();
basketItem.Resource = resource;
basketItem.Amount = amount;
Items.Add(basketItem);
}
}
public void Remove(Resource resource, int amount)
{
BasketItem item = Find(resource);
if (item != null)
{
// TODO: CHECK IF HAS ENOUGH, SO DOESN'T GO TO NEGATIVE NUMBERS
//
item.Amount -= amount;
if (item.Amount <= 0)
Items.Remove(item);
}
else
{
// COULDN'T FIND IN BASKET
}
}
public BasketItem Find(Resource resource)
{
foreach (BasketItem i in Items)
{
if (i.Resource == resource)
{
return i;
}
}
return null;
}
public int GetPriceTotal(Exporter exporter)
{
int total = 0;
foreach (BasketItem item in Items)
{
total += item.GetPriceFromSeller(exporter) * item.Amount;
}
return total;
}
public void Clear()
{
Items.Clear();
}
//============================================
// PROPERTIES
//============================================
public List<BasketItem> Items
{
get { return _items; }
set { _items = value; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment