fix: roll total not updating properly & die remove not working
This commit is contained in:
parent
dcb8f35d5e
commit
001eeae689
27
src/dice.cpp
27
src/dice.cpp
|
|
@ -5,9 +5,6 @@ static size_t activeDiceCount = 0;
|
||||||
static enum Dice_Die activeDice[MAX_ACTIVE_DICE];
|
static enum Dice_Die activeDice[MAX_ACTIVE_DICE];
|
||||||
|
|
||||||
static struct Dice_ResultType rollResult[MAX_ACTIVE_DICE];
|
static struct Dice_ResultType rollResult[MAX_ACTIVE_DICE];
|
||||||
static struct Dice_ResultType rollTotal = {
|
|
||||||
.roll = 0, .die = NONE
|
|
||||||
};
|
|
||||||
|
|
||||||
int Dice_Roll(enum Dice_Die die) {
|
int Dice_Roll(enum Dice_Die die) {
|
||||||
if (die == COIN) {
|
if (die == COIN) {
|
||||||
|
|
@ -39,34 +36,26 @@ size_t Dice_AddToActiveSet(enum Dice_Die die) {
|
||||||
}
|
}
|
||||||
activeDice[activeDiceCount] = die;
|
activeDice[activeDiceCount] = die;
|
||||||
rollResult[activeDiceCount] = Dice_RollToResultType(die, die);
|
rollResult[activeDiceCount] = Dice_RollToResultType(die, die);
|
||||||
rollTotal.roll += die;
|
|
||||||
rollTotal = Dice_RollToResultType(rollTotal.roll, NONE);
|
|
||||||
return activeDiceCount++;
|
return activeDiceCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dice_RemoveFromActiveSet(size_t index) {
|
void Dice_RemoveFromActiveSet(size_t index) {
|
||||||
if (index >= MAX_ACTIVE_DICE) {
|
if (index >= MAX_ACTIVE_DICE) {
|
||||||
return;
|
return;
|
||||||
|
} else for (size_t i = index; i < activeDiceCount; ++i) {
|
||||||
|
rollResult[i] = rollResult[i+1];
|
||||||
}
|
}
|
||||||
rollTotal.roll -= rollResult[index].roll;
|
|
||||||
rollTotal = Dice_RollToResultType(rollTotal.roll, NONE);
|
|
||||||
memcpy(activeDice + index, activeDice + index + 1, MAX_ACTIVE_DICE - index - 1);
|
|
||||||
--activeDiceCount;
|
--activeDiceCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dice_ClearActiveSet() {
|
void Dice_ClearActiveSet() {
|
||||||
rollTotal.roll = 0;
|
|
||||||
rollTotal = Dice_RollToResultType(rollTotal.roll, NONE);
|
|
||||||
activeDiceCount = 0;
|
activeDiceCount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dice_RollActiveSet() {
|
void Dice_RollActiveSet() {
|
||||||
int total = 0;
|
|
||||||
for (size_t i = 0; i < activeDiceCount; ++i) {
|
for (size_t i = 0; i < activeDiceCount; ++i) {
|
||||||
rollResult[i] = Dice_RollToResultType(Dice_Roll(activeDice[i]), activeDice[i]);
|
rollResult[i] = Dice_RollToResultType(Dice_Roll(activeDice[i]), activeDice[i]);
|
||||||
total += rollResult[i].roll;
|
|
||||||
}
|
}
|
||||||
rollTotal = Dice_RollToResultType(total, NONE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Dice_ResultType *Dice_GetLastResult(size_t *out_length) {
|
struct Dice_ResultType *Dice_GetLastResult(size_t *out_length) {
|
||||||
|
|
@ -76,8 +65,12 @@ struct Dice_ResultType *Dice_GetLastResult(size_t *out_length) {
|
||||||
return rollResult;
|
return rollResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Dice_ResultType Dice_GetLastResultTotal() {
|
int Dice_GetLastResultTotal() {
|
||||||
return rollTotal;
|
int result = 0;
|
||||||
|
for (size_t i = 0; i < activeDiceCount; ++i) {
|
||||||
|
result += rollResult[i].roll;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Clay_String Dice_ToString(enum Dice_Die die) {
|
Clay_String Dice_ToString(enum Dice_Die die) {
|
||||||
|
|
@ -98,9 +91,7 @@ Clay_String Dice_ToString(enum Dice_Die die) {
|
||||||
Clay_String Dice_ResultToString(Dice_ResultType const &result) {
|
Clay_String Dice_ResultToString(Dice_ResultType const &result) {
|
||||||
static char chars[MAX_ROLL_STR_LEN];
|
static char chars[MAX_ROLL_STR_LEN];
|
||||||
Clay_String string = {
|
Clay_String string = {
|
||||||
.isStaticallyAllocated = false,
|
false, 0, chars,
|
||||||
.length = 0,
|
|
||||||
.chars = chars,
|
|
||||||
};
|
};
|
||||||
if (result.die == COIN) {
|
if (result.die == COIN) {
|
||||||
string.length = SDL_snprintf(chars, MAX_ROLL_STR_LEN, result.roll == 1 ? "H" : "T");
|
string.length = SDL_snprintf(chars, MAX_ROLL_STR_LEN, result.roll == 1 ? "H" : "T");
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ extern void Dice_ClearActiveSet();
|
||||||
extern Clay_String Dice_RollToString(Dice_ResultType &type);
|
extern Clay_String Dice_RollToString(Dice_ResultType &type);
|
||||||
|
|
||||||
extern struct Dice_ResultType *Dice_GetLastResult(size_t *out_length);
|
extern struct Dice_ResultType *Dice_GetLastResult(size_t *out_length);
|
||||||
extern struct Dice_ResultType Dice_GetLastResultTotal();
|
extern int Dice_GetLastResultTotal();
|
||||||
|
|
||||||
extern void Dice_RollActiveSet();
|
extern void Dice_RollActiveSet();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -79,28 +79,32 @@ void DiceSelectorContainer() {
|
||||||
static
|
static
|
||||||
void HandleRemoveDieButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t index) {
|
void HandleRemoveDieButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t index) {
|
||||||
if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
|
if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
|
||||||
Dice_RemoveFromActiveSet(index);
|
Dice_RemoveFromActiveSet((size_t)index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline
|
static inline
|
||||||
void RemoveDieButton(enum Dice_Die die, int index) {
|
void RemoveDieButton(enum Dice_Die die, size_t index) {
|
||||||
CLAY(CLAY_IDI("RemoveDieButton", index), {
|
CLAY(CLAY_IDI("RemoveDieButton", index), {
|
||||||
.layout = {
|
.layout = {
|
||||||
.sizing = { CLAY_SIZING_FIXED(200), CLAY_SIZING_FIXED(200) },
|
.sizing = { CLAY_SIZING_FIXED(200), CLAY_SIZING_FIXED(200) },
|
||||||
.childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER },
|
.childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER },
|
||||||
},
|
},
|
||||||
.image = { GetDiceImage(die, Clay_Hovered()) },
|
.image = {
|
||||||
|
GetDiceImage(die, Clay_Hovered())
|
||||||
|
},
|
||||||
}) {
|
}) {
|
||||||
|
Clay_OnHover(&HandleRemoveDieButtonInteraction, (intptr_t)index);
|
||||||
size_t result_length;
|
size_t result_length;
|
||||||
struct Dice_ResultType const *result = Dice_GetLastResult(&result_length);
|
struct Dice_ResultType const *result = Dice_GetLastResult(&result_length);
|
||||||
Clay_OnHover(&HandleRemoveDieButtonInteraction, index);
|
|
||||||
CLAY_TEXT(
|
CLAY_TEXT(
|
||||||
UiData_StoreClayStr(Dice_ResultToString(result[index])),
|
UiData_StoreClayStr(Dice_ResultToString(result[index])),
|
||||||
CLAY_TEXT_CONFIG(Header(1, {
|
CLAY_TEXT_CONFIG(Header(1, {
|
||||||
.textColor = TextColors(0),
|
.textColor = TextColors(0),
|
||||||
.textAlignment = CLAY_TEXT_ALIGN_CENTER,
|
.textAlignment = CLAY_TEXT_ALIGN_CENTER,
|
||||||
})));
|
}
|
||||||
|
))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -140,10 +144,12 @@ void ActiveDiceContainer() {
|
||||||
},
|
},
|
||||||
}) {
|
}) {
|
||||||
TextButton(CLAY_STRING("Roll"), proceedButton, &HandleRollSetButtonInteraction, 0);
|
TextButton(CLAY_STRING("Roll"), proceedButton, &HandleRollSetButtonInteraction, 0);
|
||||||
Dice_ResultType result = Dice_GetLastResultTotal();
|
int result = Dice_GetLastResultTotal();
|
||||||
CLAY_TEXT(UiData_StoreClayStr(Dice_ResultToString(result)), CLAY_TEXT_CONFIG(Header(1, {
|
CLAY_TEXT(UiData_StoreClayStr(Dice_ResultToString({ result, NONE })),
|
||||||
.textColor = TextColors(0),
|
CLAY_TEXT_CONFIG(Header(1, {
|
||||||
})));
|
.textColor = TextColors(0),
|
||||||
|
}))
|
||||||
|
);
|
||||||
TextButton(CLAY_STRING("Clear"), warningButton, &HandleClearSetButtonInteraction, 0);
|
TextButton(CLAY_STRING("Clear"), warningButton, &HandleClearSetButtonInteraction, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue