Skip to content

Commit

Permalink
Merge pull request #22 from dozingcat/fade-moods
Browse files Browse the repository at this point in the history
Fade mood bubbles in and out
  • Loading branch information
dozingcat authored May 9, 2021
2 parents fcda2f3 + ba2f18f commit ddb2fff
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ enum AnimationMode {
}

final illegalSlapAnimationDuration = Duration(milliseconds: 600);
final moodDuration = Duration(milliseconds: 5000);
final moodFadeMillis = 500;

enum AIMode {human_vs_human, human_vs_ai, ai_vs_ai}

Expand Down Expand Up @@ -97,7 +99,6 @@ class _MyHomePageState extends State<MyHomePage> {
int aiSlapCounter = 0; // Used to check if a previously scheduled AI slap is still valid.
late List<int> catImageNumbers;
List<AIMood> aiMoods = [AIMood.none, AIMood.none];
int aiMoodCounter = 0;
AISlapSpeed aiSlapSpeed = AISlapSpeed.medium;
final numCatImages = 4;

Expand Down Expand Up @@ -244,17 +245,6 @@ class _MyHomePageState extends State<MyHomePage> {
Rank.jack: 12,
};

void _setAiMoods(final List<AIMood> moods) {
aiMoodCounter += 1;
int previousMoodCounter = aiMoodCounter;
setState(() => aiMoods = moods);
Future.delayed(const Duration(milliseconds: 5000), () {
if (previousMoodCounter == aiMoodCounter) {
setState(() => aiMoods = [AIMood.none, AIMood.none]);
}
});
}

// Whether the AI should show a mood after winning or losing a pile, as determined by the number
// and importance of cards in the pile.
bool _aiHasMoodForPile(final List<PileCard> pileCards) {
Expand All @@ -266,6 +256,10 @@ class _MyHomePageState extends State<MyHomePage> {
return total > 16;
}

void _setAiMoods(final List<AIMood> moods) {
setState(() => aiMoods = moods);
}

void _updateAiMoodsForPile(final List<PileCard> pileCards, final int pileWinner) {
if (_aiHasMoodForPile(pileCards)) {
var moods = pileWinner == 0 ? [AIMood.happy, AIMood.angry] : [AIMood.angry, AIMood.happy];
Expand Down Expand Up @@ -415,18 +409,37 @@ class _MyHomePageState extends State<MyHomePage> {
)
),

// Fade mood bubbles in and out.
if (moodImage != null) Positioned.fill(top: 5, bottom: 40, child:
Transform.translate(
offset: Offset(110, 0),
child: Image(
image: AssetImage('assets/cats/$moodImage'),
fit: BoxFit.fitHeight,
alignment: Alignment.center,
child: TweenAnimationBuilder(
tween: Tween(begin: 0.0, end: moodDuration.inMilliseconds.toDouble()),
duration: moodDuration,
onEnd: () => setState(() => aiMoods = [AIMood.none, AIMood.none]),
child: Image(
image: AssetImage('assets/cats/$moodImage'),
fit: BoxFit.fitHeight,
alignment: Alignment.center,
),
builder: (BuildContext context, double animMillis, Widget? child) {
double op = 1.0;
if (animMillis < moodFadeMillis) {
op = animMillis / moodFadeMillis;
}
else if (animMillis > moodDuration.inMilliseconds - moodFadeMillis) {
op = (moodDuration.inMilliseconds - animMillis) / moodFadeMillis;
}
return Opacity(
opacity: op,
child: child,
);
},
)
)
),
),
],
)
),
);
}

Expand Down

0 comments on commit ddb2fff

Please # to comment.