Skip to content

Commit

Permalink
added happy animation
Browse files Browse the repository at this point in the history
  • Loading branch information
cheniel committed Sep 3, 2014
1 parent dd1fc9b commit 1acd173
Showing 1 changed file with 59 additions and 12 deletions.
71 changes: 59 additions & 12 deletions food-baby/src/sprite.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ static GBitmap *contentPreJump;
static GBitmap *contentNormal;
static GBitmap *contentJump;

static PropertyAnimation *happyUp;
static PropertyAnimation *happyDown;
static GBitmap *happyPreJump;
static GBitmap *happyNormal;
static GBitmap *happyJump;
Expand All @@ -98,7 +100,6 @@ static void startSadAnimation();
static void sadAnimationStarted(Animation *animation, void *data);
static void sadAnimationStopped(Animation *animation, bool finished, void *data);

static void upAnimationStarted(Animation *animation, void *data);
static void upAnimationStopped(Animation *animation, bool finished, void *data);
static void downAnimationStopped(Animation *animation, bool finished, void *data);

Expand All @@ -108,6 +109,9 @@ static void contentUpStopped(Animation *animation, bool finished, void *data);
static void contentDownStopped(Animation *animation, bool finished, void *data);

static void startHappyAnimation();
static void happyUpStarted(Animation *animation, void *data);
static void happyUpStopped(Animation *animation, bool finished, void *data);
static void happyDownStopped(Animation *animation, bool finished, void *data);

// ----------------- Animation Structures
static const AnimationImplementation sleepAnimImpl = {
Expand All @@ -122,7 +126,7 @@ static const AnimationHandlers sadAnimationHandlers = {
};

static const AnimationHandlers upAnimationHandlers = {
.started = upAnimationStarted,
.started = happyUpStarted,
.stopped = upAnimationStopped,
};

Expand All @@ -140,6 +144,15 @@ static const AnimationHandlers contentDownHandlers = {
.stopped = contentDownStopped,
};

static const AnimationHandlers happyUpHandlers = {
.started = happyUpStarted,
.stopped = happyUpStopped,
};

static const AnimationHandlers happyDownHandlers = {
.stopped = happyDownStopped,
};

/* ========================================================================== */

void initSprite(Layer* windowLayer) {
Expand Down Expand Up @@ -370,9 +383,50 @@ static void contentDownStopped(Animation *animation, bool finished, void *data)
if (continueAnimation) { startAnimation(); }
}


static void startHappyAnimation() {
if (happyUp) { property_animation_destroy(happyUp); }
if (happyDown) { property_animation_destroy(happyDown); }

moveTo = getNextLocation();

GRect peak = GRect((moveTo.origin.x - baby.x) / 2 + baby.x,
HAPPY_JUMP_CEILING, SPRITE_WIDTH, SPRITE_HEIGHT);
happyUp = property_animation_create_layer_frame(
bitmap_layer_get_layer(spriteLayer), NULL, &peak);
animation_set_duration((Animation*) happyUp, JUMP_AIR_TIME / 2);
animation_set_curve((Animation*) happyUp, AnimationCurveEaseIn);
animation_set_handlers((Animation*) happyUp, happyUpHandlers, NULL);

GRect base = GRect(moveTo.origin.x, SPRITE_FLOOR, SPRITE_WIDTH,
SPRITE_HEIGHT);
happyDown = property_animation_create_layer_frame(
bitmap_layer_get_layer(spriteLayer), &peak, &base);
animation_set_duration((Animation*) happyDown, JUMP_AIR_TIME / 2);
animation_set_curve((Animation*) happyDown, AnimationCurveEaseOut);
animation_set_handlers((Animation*) happyDown, happyDownHandlers, NULL);

animation_schedule((Animation*) happyUp);
}

static void happyUpStarted(Animation *animation, void *data) {
// set up pre jump
bitmap_layer_set_bitmap(spriteLayer, happyPreJump);
psleep(100);
bitmap_layer_set_bitmap(spriteLayer, happyNormal);
psleep(100);
bitmap_layer_set_bitmap(spriteLayer, happyJump);
}

static void happyUpStopped(Animation *animation, bool finished, void *data) {
animation_schedule((Animation*) happyDown);
}

static void happyDownStopped(Animation *animation, bool finished, void *data) {
bitmap_layer_set_bitmap(spriteLayer, happyNormal);

updateLocation();

if (continueAnimation) { startAnimation(); }
}

void stopAnimation() {
Expand Down Expand Up @@ -410,15 +464,6 @@ void happyJumps() {
}
}

static void upAnimationStarted(Animation *animation, void *data) {
// set up pre jump
bitmap_layer_set_bitmap(spriteLayer, happyPreJump);
psleep(100);
bitmap_layer_set_bitmap(spriteLayer, happyNormal);
psleep(100);
bitmap_layer_set_bitmap(spriteLayer, happyJump);
}

static void upAnimationStopped(Animation *animation, bool finished, void *data) {
animation_schedule((Animation*) down);
}
Expand Down Expand Up @@ -463,6 +508,8 @@ void deinitSprite() {
if (down) { property_animation_destroy(down); }
if (contentUp) { property_animation_destroy(contentUp); }
if (contentDown) { property_animation_destroy(contentDown); }
if (happyUp) { property_animation_destroy(happyUp); }
if (happyDown) { property_animation_destroy(happyDown); }
}

static void updateLocation() {
Expand Down

0 comments on commit 1acd173

Please sign in to comment.