This is probably very simple but for some reason I can’t figure it out.
Trying to make my first game, the easiest I could think of, oldschool drugwars.
So I have maxInventory, cash, price.
How can I calculate the max amount of items I can purchase?
Normally to the inventory is a List or something so you can just check if it has any space left. Depends on how you’re managing inventory size and capacity.
Once you know if there is room just check if there is enough cash and subtract the price from cash.
Okay so now if I hit my “Buy Max” button I want to buy as many items I can afford whils still not overfilling my maxInventory. Do you have an idea I feel so stupid, this should be a very simple math problem but I’m drawing a blank.
//Amount you can afford to buy
var afford = cash / price;
//Clamp to the max amount of avaiable slots
var maxCanBuy = Mathf.Clamp(afford, 0, maxInventory - currentInventory);
//Final cost
var totalCost = maxCanBuy * price;