Skip to content

Commit

Permalink
model: Implement sprinting using animation speedup
Browse files Browse the repository at this point in the history
Replace dashing with sprinting, use sped up motion animation to visually
reflect it, and rename "dash" model parameter to "can_sprint".

Signed-off-by: Alexander Shishkin <[email protected]>
  • Loading branch information
virtuoso committed Oct 9, 2024
1 parent 08f1cfa commit 24fe55a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
15 changes: 13 additions & 2 deletions core/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,16 @@ void animation_set_end_callback(struct entity3d *e, void (*end)(struct scene *,
e->aniq.x[nr_qas - 1].end_priv = priv;
}

void animation_set_speed(struct entity3d *e, float speed)
{
struct queued_animation *qa = ani_current(e);

if (!qa)
return;

qa->speed = speed;
}

void animation_push_by_name(struct entity3d *e, struct scene *s, const char *name,
bool clear, bool repeat)
{
Expand All @@ -1401,6 +1411,7 @@ void animation_push_by_name(struct entity3d *e, struct scene *s, const char *nam
qa = darray_add(&e->aniq.da);
qa->animation = id;
qa->repeat = repeat;
qa->speed = 1.0;
if (clear) {
animation_start(e, s->frames_total, id);
e->animation = 0;
Expand All @@ -1420,10 +1431,10 @@ static void animated_update(struct entity3d *e, struct scene *s)
animation_next(e, s);
qa = ani_current(e);
an = &model->anis.x[qa->animation];
channels_transform(e, an, (float)(s->frames_total - e->ani_frame) / framerate);
channels_transform(e, an, (float)(s->frames_total - e->ani_frame) / framerate * qa->speed);
one_joint_transform(e, 0, -1);

if (s->frames_total - e->ani_frame >= an->time_end * framerate)
if ((float)(s->frames_total - e->ani_frame) * qa->speed >= an->time_end * framerate)
animation_next(e, s);
}

Expand Down
2 changes: 2 additions & 0 deletions core/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ void animation_push_by_name(struct entity3d *e, struct scene *s, const char *nam
bool clear, bool repeat);
int animation_by_name(struct model3d *m, const char *name);
void animation_set_end_callback(struct entity3d *e, void (*end)(struct scene *, void *), void *priv);
void animation_set_speed(struct entity3d *e, float speed);

#define LOD_MAX 4
struct model3d {
Expand Down Expand Up @@ -218,6 +219,7 @@ struct queued_animation {
unsigned long delay;
void (*end)(struct scene *s, void *end_priv);
void *end_priv;
float speed;
};

struct entity3d {
Expand Down
8 changes: 4 additions & 4 deletions core/scene.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ static int model_new_from_json(struct scene *scene, JsonNode *node)
{
double mass = 1.0, bounce = 0.0, bounce_vel = dInfinity, geom_off = 0.0, geom_radius = 1.0, geom_length = 1.0, speed = 0.75;
char *name = NULL, *obj = NULL, *binvec = NULL, *gltf = NULL, *tex = NULL;
bool terrain_clamp = false, cull_face = true, alpha_blend = false, jump = false, dash = false;
bool terrain_clamp = false, cull_face = true, alpha_blend = false, jump = false, can_sprint = false;
JsonNode *p, *ent = NULL, *ch = NULL, *phys = NULL, *anis = NULL, *light = NULL;
int class = dSphereClass, collision = -1, ptype = PHYS_BODY;
struct gltf_data *gd = NULL;
Expand Down Expand Up @@ -414,8 +414,8 @@ static int model_new_from_json(struct scene *scene, JsonNode *node)
cull_face = p->bool_;
else if (p->tag == JSON_BOOL && !strcmp(p->key, "alpha_blend"))
alpha_blend = p->bool_;
else if (p->tag == JSON_BOOL && !strcmp(p->key, "dash"))
dash = p->bool_;
else if (p->tag == JSON_BOOL && !strcmp(p->key, "can_sprint"))
can_sprint = p->bool_;
else if (p->tag == JSON_BOOL && !strcmp(p->key, "jump"))
jump = p->bool_;
else if (p->tag == JSON_ARRAY && !strcmp(p->key, "entity"))
Expand Down Expand Up @@ -540,7 +540,7 @@ static int model_new_from_json(struct scene *scene, JsonNode *node)
// scene->control = c;
e = c->entity;
e->skip_culling = true;
c->dashing = dash;
c->can_sprint = can_sprint;
c->jumping = jump;
} else {
e = entity3d_new(txm);
Expand Down

0 comments on commit 24fe55a

Please sign in to comment.