Advertisement
Tkap1

Untitled

Sep 27th, 2022 (edited)
1,052
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. enum e_anum
  2. {
  3.     e_anim_idle,
  4.     e_anim_attack,
  5.     e_anim_count,
  6. };
  7.  
  8. struct s_anim_data
  9. {
  10.     b8 loop;
  11.     int sprite_count;
  12.     float duration;
  13.     s_v2i sprite_indices[16];
  14. };
  15.  
  16. local_persist s_anim_data g_anim_data[] =  {
  17.     {
  18.         .loop = true,
  19.         .sprite_count = 5,
  20.         .duration = 0.5f,
  21.         .sprite_indices = {
  22.             v2i(6, 4), v2i(7, 4), v2i(8, 4), v2i(9, 4), v2i(10, 4)
  23.         },
  24.     },
  25.     {
  26.         .sprite_count = 4,
  27.         // .duration = 0.5f,
  28.         .sprite_indices = {
  29.             v2i(6, 5), v2i(7, 5), v2i(8, 5), v2i(9, 5)
  30.         },
  31.     },
  32. };
  33.  
  34. struct s_anim
  35. {
  36.     float time;
  37.  
  38.     int get_index(int anim)
  39.     {
  40.         float percent = time / g_anim_data[anim].duration;
  41.         int index = roundfi(percent * (g_anim_data[anim].sprite_count - 1));
  42.         return index;
  43.     }
  44.  
  45.     void update(int anim)
  46.     {
  47.         time += delta;
  48.         if(time > g_anim_data[anim].duration)
  49.         {
  50.             if(g_anim_data[anim].loop) { time = 0; }
  51.             else { time = 1; }
  52.         }
  53.     }
  54. };
  55.  
  56. struct s_hero
  57. {
  58.     float attack_time;
  59.     int anim;
  60.     s_anim anims[e_anim_count];
  61. };
  62.  
  63. local_persist s_hero hero = zero;
  64.  
  65. constexpr float attack_delay = 0.2f;
  66.  
  67. delta = 1.0f/60.0f;
  68.  
  69. hero.attack_time = at_most(attack_delay + delta, hero.attack_time + delta);
  70.  
  71. local_persist s_v2 proj = zero;
  72. local_persist s_v2 proj_dir = v2(1, 0);
  73. local_persist s_v2 hero_pos = c_world_center;
  74.  
  75.  
  76. while(v2_distance(input->mouse, c_world_center) < 600 && hero.attack_time >= attack_delay)
  77. {
  78.     hero.attack_time -= attack_delay;
  79.  
  80.     proj = hero_pos;
  81.     proj_dir = v2_normalized(input->mouse - proj);
  82.  
  83.     // attack
  84.     hero.anim = e_anim_attack;
  85. }
  86.  
  87. hero.anims[hero.anim].update(hero.anim);
  88.  
  89. int index;
  90. if(hero.anim == e_anim_attack)
  91. {
  92.     ui_label("ATTACK", v2(c_world_center.x, 300));
  93.     float percent = hero.attack_time / attack_delay;
  94.     index = roundfi(percent * (g_anim_data[hero.anim].sprite_count - 1));
  95.     if(percent >= 1)
  96.     {
  97.         hero.anim = e_anim_idle;
  98.         index = 0;
  99.     }
  100. }
  101. else
  102. {
  103.     ui_label("IDLE", v2(c_world_center.x, 300));
  104.     index = hero.anims[hero.anim].get_index(hero.anim);
  105. }
  106.  
  107. draw_texture_ui(
  108.     e_texture_atlas,
  109.     hero_pos,
  110.     e_z_index_ui0,
  111.     v2(256, 256),
  112.     color(1).rgb,
  113.     g_anim_data[hero.anim].sprite_indices[index],
  114.     origin_center
  115. );
  116.  
  117. draw_texture_ui(
  118.     e_texture_atlas,
  119.     proj,
  120.     e_z_index_ui0,
  121.     v2(128, 128),
  122.     color(1).rgb,
  123.     v2i(7, 8),
  124.     origin_center
  125. );
  126.  
  127. proj += proj_dir * delta * 800;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement