Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum e_anum
- {
- e_anim_idle,
- e_anim_attack,
- e_anim_count,
- };
- struct s_anim_data
- {
- b8 loop;
- int sprite_count;
- float duration;
- s_v2i sprite_indices[16];
- };
- local_persist s_anim_data g_anim_data[] = {
- {
- .loop = true,
- .sprite_count = 5,
- .duration = 0.5f,
- .sprite_indices = {
- v2i(6, 4), v2i(7, 4), v2i(8, 4), v2i(9, 4), v2i(10, 4)
- },
- },
- {
- .sprite_count = 4,
- // .duration = 0.5f,
- .sprite_indices = {
- v2i(6, 5), v2i(7, 5), v2i(8, 5), v2i(9, 5)
- },
- },
- };
- struct s_anim
- {
- float time;
- int get_index(int anim)
- {
- float percent = time / g_anim_data[anim].duration;
- int index = roundfi(percent * (g_anim_data[anim].sprite_count - 1));
- return index;
- }
- void update(int anim)
- {
- time += delta;
- if(time > g_anim_data[anim].duration)
- {
- if(g_anim_data[anim].loop) { time = 0; }
- else { time = 1; }
- }
- }
- };
- struct s_hero
- {
- float attack_time;
- int anim;
- s_anim anims[e_anim_count];
- };
- local_persist s_hero hero = zero;
- constexpr float attack_delay = 0.2f;
- delta = 1.0f/60.0f;
- hero.attack_time = at_most(attack_delay + delta, hero.attack_time + delta);
- local_persist s_v2 proj = zero;
- local_persist s_v2 proj_dir = v2(1, 0);
- local_persist s_v2 hero_pos = c_world_center;
- while(v2_distance(input->mouse, c_world_center) < 600 && hero.attack_time >= attack_delay)
- {
- hero.attack_time -= attack_delay;
- proj = hero_pos;
- proj_dir = v2_normalized(input->mouse - proj);
- // attack
- hero.anim = e_anim_attack;
- }
- hero.anims[hero.anim].update(hero.anim);
- int index;
- if(hero.anim == e_anim_attack)
- {
- ui_label("ATTACK", v2(c_world_center.x, 300));
- float percent = hero.attack_time / attack_delay;
- index = roundfi(percent * (g_anim_data[hero.anim].sprite_count - 1));
- if(percent >= 1)
- {
- hero.anim = e_anim_idle;
- index = 0;
- }
- }
- else
- {
- ui_label("IDLE", v2(c_world_center.x, 300));
- index = hero.anims[hero.anim].get_index(hero.anim);
- }
- draw_texture_ui(
- e_texture_atlas,
- hero_pos,
- e_z_index_ui0,
- v2(256, 256),
- color(1).rgb,
- g_anim_data[hero.anim].sprite_indices[index],
- origin_center
- );
- draw_texture_ui(
- e_texture_atlas,
- proj,
- e_z_index_ui0,
- v2(128, 128),
- color(1).rgb,
- v2i(7, 8),
- origin_center
- );
- proj += proj_dir * delta * 800;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement