This commit is contained in:
alastaira
2023-09-18 20:15:27 +01:00
parent ee3e6e625c
commit 874f947046
2 changed files with 31 additions and 26 deletions

View File

@@ -34,18 +34,22 @@ void FaceBehavior::Clear() {
} }
} }
// Use roulette wheel to select a new emotion, based on assigned weights
eEmotions FaceBehavior::GetRandomEmotion() { eEmotions FaceBehavior::GetRandomEmotion() {
// Calculate the total sum of all emotional weights
float sum_of_weight = 0; float sum_of_weight = 0;
for (int emotion = 0; emotion < eEmotions::EMOTIONS_COUNT; emotion++) { for (int emotion = 0; emotion < eEmotions::EMOTIONS_COUNT; emotion++) {
sum_of_weight += Emotions[emotion]; sum_of_weight += Emotions[emotion];
} }
// If no weights have been assigned, default to "normal" emotion
if (sum_of_weight == 0) { if (sum_of_weight == 0) {
return eEmotions::Normal; return eEmotions::Normal;
} }
// Now pick a random number that lies somewhere in the range of total weights
float rand = random(0, 1000 * sum_of_weight) / 1000.0; float rand = random(0, 1000 * sum_of_weight) / 1000.0;
// Loop over emotions and select the one whose probabity distribution contains
// the value in which the random number lies
float acc = 0; float acc = 0;
for (int emotion = 0; emotion < eEmotions::EMOTIONS_COUNT; emotion++) { for (int emotion = 0; emotion < eEmotions::EMOTIONS_COUNT; emotion++) {
if (Emotions[emotion] == 0) continue; if (Emotions[emotion] == 0) continue;
@@ -54,7 +58,7 @@ eEmotions FaceBehavior::GetRandomEmotion() {
return (eEmotions)emotion; return (eEmotions)emotion;
} }
} }
// If something goes wrong in the calculation, return "normal"
return eEmotions::Normal; return eEmotions::Normal;
} }
@@ -71,28 +75,29 @@ void FaceBehavior::Update() {
} }
void FaceBehavior::GoToEmotion(eEmotions emotion) { void FaceBehavior::GoToEmotion(eEmotions emotion) {
// Set the currentEmotion to the desired emotion
CurrentEmotion = emotion; CurrentEmotion = emotion;
// Call the appropriate expression transition function
switch (CurrentEmotion) { switch (CurrentEmotion) {
case eEmotions::Normal: _face.Expression.GoTo_Normal(); break; case eEmotions::Normal: _face.Expression.GoTo_Normal(); break;
case eEmotions::Angry: _face.Expression.GoTo_Angry(); break; case eEmotions::Angry: _face.Expression.GoTo_Angry(); break;
case eEmotions::Glee: _face.Expression.GoTo_Glee(); break; case eEmotions::Glee: _face.Expression.GoTo_Glee(); break;
case eEmotions::Happy: _face.Expression.GoTo_Happy(); break; case eEmotions::Happy: _face.Expression.GoTo_Happy(); break;
case eEmotions::Sad: _face.Expression.GoTo_Sad(); break; case eEmotions::Sad: _face.Expression.GoTo_Sad(); break;
case eEmotions::Worried: _face.Expression.GoTo_Worried(); break; case eEmotions::Worried: _face.Expression.GoTo_Worried(); break;
case eEmotions::Focused: _face.Expression.GoTo_Focused(); break; case eEmotions::Focused: _face.Expression.GoTo_Focused(); break;
case eEmotions::Annoyed: _face.Expression.GoTo_Annoyed(); break; case eEmotions::Annoyed: _face.Expression.GoTo_Annoyed(); break;
case eEmotions::Surprised: _face.Expression.GoTo_Surprised(); break; case eEmotions::Surprised: _face.Expression.GoTo_Surprised(); break;
case eEmotions::Skeptic: _face.Expression.GoTo_Skeptic(); break; case eEmotions::Skeptic: _face.Expression.GoTo_Skeptic(); break;
case eEmotions::Frustrated: _face.Expression.GoTo_Frustrated(); break; case eEmotions::Frustrated: _face.Expression.GoTo_Frustrated(); break;
case eEmotions::Unimpressed: _face.Expression.GoTo_Unimpressed(); break; case eEmotions::Unimpressed: _face.Expression.GoTo_Unimpressed(); break;
case eEmotions::Sleepy: _face.Expression.GoTo_Sleepy(); break; case eEmotions::Sleepy: _face.Expression.GoTo_Sleepy(); break;
case eEmotions::Suspicious: _face.Expression.GoTo_Suspicious(); break; case eEmotions::Suspicious: _face.Expression.GoTo_Suspicious(); break;
case eEmotions::Squint: _face.Expression.GoTo_Squint(); break; case eEmotions::Squint: _face.Expression.GoTo_Squint(); break;
case eEmotions::Furious: _face.Expression.GoTo_Furious(); break; case eEmotions::Furious: _face.Expression.GoTo_Furious(); break;
case eEmotions::Scared: _face.Expression.GoTo_Scared(); break; case eEmotions::Scared: _face.Expression.GoTo_Scared(); break;
case eEmotions::Awe: _face.Expression.GoTo_Awe(); break; case eEmotions::Awe: _face.Expression.GoTo_Awe(); break;
default: break;
default: break;
} }
} }

View File

@@ -30,11 +30,11 @@ void setup(void) {
// Assign the current expression // Assign the current expression
face->Expression.GoTo_Normal(); face->Expression.GoTo_Normal();
// Assign a weight to each emotion that can be chosen // Assign a weight to each emotion
face->Behavior.SetEmotion(eEmotions::Normal, 1.0); face->Behavior.SetEmotion(eEmotions::Normal, 1.0);
//face->Behavior.SetEmotion(eEmotions::Angry, 1.0); //face->Behavior.SetEmotion(eEmotions::Angry, 1.0);
//face->Behavior.SetEmotion(eEmotions::Sad, 1.0); //face->Behavior.SetEmotion(eEmotions::Sad, 1.0);
// Automatically switch between possible allowed behaviours (selecting new behaviour randomly based on the weight assigned to each emotion) // Automatically switch between behaviours (selecting new behaviour randomly based on the weight assigned to each emotion)
face->RandomBehavior = true; face->RandomBehavior = true;
// Automatically blink // Automatically blink