Skip to content

Commit

Permalink
character: Add start to idle animation sequence
Browse files Browse the repository at this point in the history
If "start" and "start_to_idle" animations are set up, put the character
in "start" until motion controls wake it up and play "start_to_idle"
before transitioning to "idle" and motion controls become available.

Signed-off-by: Alexander Shishkin <[email protected]>
  • Loading branch information
virtuoso committed Oct 7, 2024
1 parent 1ff4a31 commit 48d906b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
21 changes: 21 additions & 0 deletions core/character.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ bool character_is_grounded(struct character *ch, struct scene *s)
return false;
}

static void character_idle(struct scene *s, void *priv)
{
struct character *c = priv;

c->state = CS_AWAKE;
animation_push_by_name(c->entity, s, "idle", true, true);
}

void character_move(struct character *ch, struct scene *s)
{
struct phys_body *body = ch->entity->phys_body;
Expand All @@ -185,6 +193,18 @@ void character_move(struct character *ch, struct scene *s)
}

ch->ragdoll = body ? !phys_body_ground_collide(body) : 0;

if (ch->state == CS_START) {
if (ch->mctl.ls_dx || ch->mctl.ls_dy) {
ch->state = CS_WAKING;
animation_push_by_name(ch->entity, s, "start_to_idle", true, false);
animation_set_end_callback(ch->entity, character_idle, ch);
}
return;
} else if (ch->state < CS_AWAKE) {
return;
}

if (ch->ragdoll) {
dJointSetLMotorParam(body->lmotor, dParamVel1, 0);
dJointSetLMotorParam(body->lmotor, dParamVel2, 0);
Expand Down Expand Up @@ -453,6 +473,7 @@ struct character *character_new(struct model3dtx *txm, struct scene *s)
c->entity->priv = c;
c->orig_update = c->entity->update;
c->entity->update = character_update;
c->state = CS_AWAKE;
list_append(&s->characters, &c->entry);
motion_reset(&c->mctl, s);

Expand Down
7 changes: 7 additions & 0 deletions core/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ struct motionctl {
bool jump;
};

enum character_state {
CS_START = 0,
CS_WAKING,
CS_AWAKE
};

struct character {
struct ref ref;
struct entity3d *entity;
Expand All @@ -55,6 +61,7 @@ struct character {
int stuck;
bool dashing;
bool jumping;
enum character_state state;
};

static inline struct entity3d *character_entity(struct character *c)
Expand Down
8 changes: 8 additions & 0 deletions core/scene.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,14 @@ static int model_new_from_json(struct scene *scene, JsonNode *node)
free(m->anis.x[idx].name);
m->anis.x[idx].name = strdup(p->key);
}

if (c &&
animation_by_name(e->txmodel->model, "start") >= 0 &&
animation_by_name(e->txmodel->model, "start_to_idle") >= 0) {
c->anictl.state = UINT_MAX;
c->state = CS_START;
animation_push_by_name(e, scene, "start", true, true);
}
}
}
} else {
Expand Down

0 comments on commit 48d906b

Please sign in to comment.