use balance to purchase

This commit is contained in:
ntr 2019-03-14 23:40:06 +11:00
parent 2c89db4f01
commit c2fe8f779e
2 changed files with 50 additions and 7 deletions

View File

@ -152,9 +152,9 @@ class ItemList extends Phaser.Scene {
drawInventory(graphics);
drawVbox(graphics);
this.add.text(X + WIDTH * 0.1, Y, 'Inventory', TEXT.HEADER);
this.add.text(X + WIDTH * 0.47, Y, 'Combiner', TEXT.HEADER);
this.add.text(X + WIDTH * 0.35, Y + HEIGHT / 2, 'Varibox', TEXT.HEADER);
this.add.text(X + WIDTH * 0.1, Y, 'inventory', TEXT.HEADER);
this.add.text(X + WIDTH * 0.47, Y, 'iCombinator', TEXT.HEADER);
this.add.text(X + WIDTH * 0.35, Y + HEIGHT / 2, `vBox - ${vbox.bits}b`, TEXT.HEADER);
const reroll = this.add
.rectangle(WIDTH * 0.01, Y + HEIGHT * 0.775, ITEM_WIDTH * 1.25, ITEM_HEIGHT * 1.25, 0x222222)

View File

@ -96,6 +96,26 @@ impl Var {
}
}
fn cost(&self) -> Result<u16, Error> {
match self {
Var::Red => Ok(1),
Var::Green => Ok(1),
Var::Blue => Ok(1),
Var::Attack => Ok(2),
Var::Block => Ok(2),
Var::Buff => Ok(2),
Var::Debuff => Ok(2),
Var::Stun => Ok(2),
Var::Damage => Ok(3),
Var::Hp => Ok(3),
Var::Speed => Ok(3),
_ => Err(err_msg("var not purchasable")),
}
}
fn effect(&self) -> Option<VarEffect> {
if let Some(_skill) = self.skill() {
return Some(VarEffect::Skill);
@ -170,7 +190,7 @@ enum ColourCode {
#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct Vbox {
pub id: Uuid,
pub balance: u16,
pub bits: u16,
pub free: Vec<Vec<Var>>,
pub bound: Vec<Var>,
pub instance: Uuid,
@ -197,7 +217,7 @@ impl Vbox {
instance: instance_id,
free: vec![],
bound: starting_items,
balance: 9,
bits: 9,
};
vbox.fill();
@ -205,6 +225,21 @@ impl Vbox {
return vbox;
}
pub fn balance_sub(&mut self, amount: u16) -> Result<&mut Vbox, Error> {
let new_balance = self.bits
.checked_sub(amount)
.ok_or(format_err!("insufficient balance: {:?}", self.bits))?;
self.bits = new_balance;
Ok(self)
}
pub fn balance_add(&mut self, amount: u16) -> &mut Vbox {
self.bits = self.bits.saturating_add(amount);
self
}
pub fn fill(&mut self) -> &mut Vbox {
let colours = vec![
(Var::Red, 1),
@ -248,10 +283,16 @@ impl Vbox {
return Err(err_msg("too many vars bound"));
}
// check item exists
self.free
.get(i).ok_or(format_err!("no var group at index {:?}", i))?
.get(j).ok_or(format_err!("no var at index {:?}", j))?;
// check can purchase
let cost = self.free[i][j].cost()?;
self.balance_sub(cost)?;
// actually move
self.bound.push(self.free[i].remove(j));
Ok(self)
@ -259,8 +300,10 @@ impl Vbox {
pub fn drop(&mut self, i: usize) -> Result<&mut Vbox, Error> {
self.bound.get(i).ok_or(format_err!("no var at index {:?}", i))?;
self.bound.remove(i);
// balance update
let dropped = self.bound.remove(i);
// calculate total cost
// self.balance_add(dropped.cost()?);
Ok(self)
}