weapon_swing.diff 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. This mod adds horizontal weapon swinging. By drummyfish, released under CC0 1.0,
  2. public domain.
  3. diff --git a/game.h b/game.h
  4. index 991058d..e20f54b 100755
  5. --- a/game.h
  6. +++ b/game.h
  7. @@ -425,6 +425,7 @@ struct
  8. for determining whether player is in the
  9. air. */
  10. uint16_t headBobFrame;
  11. + int16_t weaponSwingPhase;
  12. uint8_t weapon; ///< currently selected weapon
  13. uint8_t health;
  14. uint32_t weaponCooldownFrames; ///< frames left for weapon cooldown
  15. @@ -1396,6 +1397,7 @@ void SFG_initPlayer(void)
  16. SFG_player.previousVerticalSpeed = 0;
  17. SFG_player.headBobFrame = 0;
  18. + SFG_player.weaponSwingPhase = 0;
  19. SFG_player.weapon = SFG_WEAPON_KNIFE;
  20. @@ -3313,36 +3315,56 @@ void SFG_gameStepPlaying(void)
  21. RCL_min(0,SFG_player.camera.shear + SFG_CAMERA_SHEAR_STEP_PER_FRAME);
  22. }
  23. +
  24. #if SFG_HEADBOB_ENABLED && !SFG_PREVIEW_MODE
  25. if (bobbing)
  26. {
  27. SFG_player.headBobFrame += SFG_HEADBOB_FRAME_INCREASE_PER_FRAME;
  28. + SFG_player.weaponSwingPhase =
  29. + (SFG_player.weaponSwingPhase + SFG_WEAPON_SWING_SPEED / SFG_FPS)
  30. + % RCL_UNITS_PER_SQUARE;
  31. }
  32. - else if (SFG_player.headBobFrame != 0)
  33. + else
  34. {
  35. - // smoothly stop bobbing
  36. + if ((SFG_player.weaponSwingPhase % (RCL_UNITS_PER_SQUARE / 4)) == 0)
  37. + SFG_player.weaponSwingPhase = 0;
  38. + else
  39. + {
  40. +#define SMOOTHING 8
  41. + SFG_player.weaponSwingPhase += (SFG_player.weaponSwingPhase /
  42. + (RCL_UNITS_PER_SQUARE / 4) % 2) ? SMOOTHING : (-1 * SMOOTHING);
  43. +
  44. + SFG_player.weaponSwingPhase = (SFG_player.weaponSwingPhase / SMOOTHING)
  45. + * SMOOTHING;
  46. +#undef SMOOTHING
  47. + }
  48. - uint8_t quadrant = (SFG_player.headBobFrame % RCL_UNITS_PER_SQUARE) /
  49. - (RCL_UNITS_PER_SQUARE / 4);
  50. + if (SFG_player.headBobFrame != 0)
  51. + {
  52. + // smoothly stop bobbing
  53. +
  54. + uint8_t quadrant = (SFG_player.headBobFrame % RCL_UNITS_PER_SQUARE) /
  55. + (RCL_UNITS_PER_SQUARE / 4);
  56. +
  57. + /* When in quadrant in which sin is going away from zero, switch to the
  58. + same value of the next quadrant, so that bobbing starts to go towards
  59. + zero immediately. */
  60. - /* When in quadrant in which sin is going away from zero, switch to the
  61. - same value of the next quadrant, so that bobbing starts to go towards
  62. - zero immediately. */
  63. + if (quadrant % 2 == 0)
  64. + SFG_player.headBobFrame =
  65. + ((quadrant + 1) * RCL_UNITS_PER_SQUARE / 4) +
  66. + (RCL_UNITS_PER_SQUARE / 4 - SFG_player.headBobFrame %
  67. + (RCL_UNITS_PER_SQUARE / 4));
  68. - if (quadrant % 2 == 0)
  69. + RCL_Unit currentFrame = SFG_player.headBobFrame;
  70. + RCL_Unit nextFrame = SFG_player.headBobFrame + 16;
  71. +
  72. + // only stop bobbing when we pass a frame at which sin crosses zero
  73. SFG_player.headBobFrame =
  74. - ((quadrant + 1) * RCL_UNITS_PER_SQUARE / 4) +
  75. - (RCL_UNITS_PER_SQUARE / 4 - SFG_player.headBobFrame %
  76. - (RCL_UNITS_PER_SQUARE / 4));
  77. -
  78. - RCL_Unit currentFrame = SFG_player.headBobFrame;
  79. - RCL_Unit nextFrame = SFG_player.headBobFrame + 16;
  80. -
  81. - // only stop bobbing when we pass a frame at which sin crosses zero
  82. - SFG_player.headBobFrame =
  83. - (currentFrame / (RCL_UNITS_PER_SQUARE / 2) ==
  84. - nextFrame / (RCL_UNITS_PER_SQUARE / 2)) ?
  85. - nextFrame : 0;
  86. + (currentFrame / (RCL_UNITS_PER_SQUARE / 2) ==
  87. + nextFrame / (RCL_UNITS_PER_SQUARE / 2)) ?
  88. + nextFrame : 0;
  89. + }
  90. }
  91. #endif
  92. @@ -4374,7 +4396,7 @@ void SFG_drawIndicationBorder(uint16_t width, uint8_t color)
  93. /**
  94. Draws the player weapon, includes handling the shoot animation.
  95. */
  96. -void SFG_drawWeapon(int16_t bobOffset)
  97. +void SFG_drawWeapon(int16_t bobOffset, uint16_t swingPhase)
  98. {
  99. uint32_t animationLength =
  100. RCL_max(SFG_MIN_WEAPON_COOLDOWN_FRAMES,
  101. @@ -4387,6 +4409,9 @@ void SFG_drawWeapon(int16_t bobOffset)
  102. uint8_t fireType = SFG_GET_WEAPON_FIRE_TYPE(SFG_player.weapon);
  103. + uint16_t posX = SFG_WEAPON_IMAGE_POSITION_X + (RCL_sin(swingPhase) *
  104. + (SFG_GAME_RESOLUTION_X / SFG_WEAPON_SWING_WIDTH)) / RCL_UNITS_PER_SQUARE;
  105. +
  106. if (shotAnimationFrame < animationLength)
  107. {
  108. if (fireType == SFG_WEAPON_FIRE_TYPE_MELEE)
  109. @@ -4405,7 +4430,7 @@ void SFG_drawWeapon(int16_t bobOffset)
  110. (fireType == SFG_WEAPON_FIRE_TYPE_BULLET)) &&
  111. shotAnimationFrame < animationLength / 2)
  112. SFG_blitImage(SFG_effectSprites,
  113. - SFG_WEAPON_IMAGE_POSITION_X,
  114. + posX,
  115. SFG_WEAPON_IMAGE_POSITION_Y -
  116. (SFG_TEXTURE_SIZE / 3) * SFG_WEAPON_IMAGE_SCALE + bobOffset,
  117. SFG_WEAPON_IMAGE_SCALE);
  118. @@ -4413,7 +4438,7 @@ void SFG_drawWeapon(int16_t bobOffset)
  119. }
  120. SFG_blitImage(SFG_weaponImages + SFG_player.weapon * SFG_TEXTURE_STORE_SIZE,
  121. - SFG_WEAPON_IMAGE_POSITION_X,
  122. + posX,
  123. SFG_WEAPON_IMAGE_POSITION_Y + bobOffset - 1,
  124. SFG_WEAPON_IMAGE_SCALE);
  125. }
  126. @@ -4871,7 +4896,7 @@ void SFG_draw(void)
  127. #endif // head bob enabled?
  128. #if SFG_PREVIEW_MODE == 0
  129. - SFG_drawWeapon(weaponBobOffset);
  130. + SFG_drawWeapon(weaponBobOffset,SFG_player.weaponSwingPhase);
  131. #endif
  132. // draw HUD:
  133. diff --git a/settings.h b/settings.h
  134. index a0d8c1f..80b5a0c 100644
  135. --- a/settings.h
  136. +++ b/settings.h
  137. @@ -430,6 +430,20 @@
  138. #define SFG_ARDUINO 0
  139. #endif
  140. +/**
  141. + How wide should weapon swinging be, in fractions of horizontal resolution.
  142. +*/
  143. +#ifndef SFG_WEAPON_SWING_WIDTH
  144. + #define SFG_WEAPON_SWING_WIDTH 16
  145. +#endif
  146. +
  147. +/**
  148. + Speed of weapon swinging, in phase increase per second (1024 = full phase).
  149. +*/
  150. +#ifndef SFG_WEAPON_SWING_SPEED
  151. + #define SFG_WEAPON_SWING_SPEED 512
  152. +#endif
  153. +
  154. /**
  155. Whether levels background (in distance or transparent wall textures) should
  156. be drawn. If turned off, the background will be constant color, which can