Use only weapons with ammo

This commit is contained in:
Miloslav Číž 2020-01-13 19:48:40 +01:00
parent 70f7f72a80
commit 4df1c2605b
2 changed files with 122 additions and 89 deletions

View File

@ -136,14 +136,14 @@
#define SFG_AMMO_ROCKETS 1 #define SFG_AMMO_ROCKETS 1
#define SFG_AMMO_PLASMA 2 #define SFG_AMMO_PLASMA 2
#define SFG_AMMO_INCREASE_BULLETS 10
#define SFG_AMMO_INCREASE_ROCKETS 5
#define SFG_AMMO_INCREASE_PLASMA 8
#define SFG_AMMO_TOTAL 3 #define SFG_AMMO_TOTAL 3
#define SFG_AMMO_NONE SFG_AMMO_TOTAL #define SFG_AMMO_NONE SFG_AMMO_TOTAL
#define SFG_AMMO_INCREASE_BULLETS 10
#define SFG_AMMO_INCREASE_ROCKETS 5
#define SFG_AMMO_INCREASE_PLASMA 8
// ---------------------------- // ----------------------------
// derived constants // derived constants

59
main.c
View File

@ -1084,17 +1084,38 @@ void SFG_init()
SFG_lastFrameTimeMs = SFG_getTimeMs(); SFG_lastFrameTimeMs = SFG_getTimeMs();
} }
void SFG_getPlayerWeaponInfo(
uint8_t *ammoType, uint8_t *projectileCount, uint8_t *canShoot)
{
*ammoType = SFG_weaponAmmo(SFG_player.weapon);
*projectileCount = SFG_GET_WEAPON_PROJECTILE_COUNT(SFG_player.weapon);
*canShoot =
(*ammoType == SFG_AMMO_NONE ||
SFG_player.ammo[*ammoType] >= *projectileCount);
}
void SFG_playerRotateWeapon(uint8_t next) void SFG_playerRotateWeapon(uint8_t next)
{ {
SFG_player.weapon += next ? 1 : -1; RCL_Unit initialWeapon = SFG_player.weapon;
RCL_Unit increment = next ? 1 : -1;
if (SFG_player.weapon == SFG_WEAPONS_TOTAL) while (1)
SFG_player.weapon = 0; {
else if (SFG_player.weapon < 0 || SFG_player.weapon > SFG_WEAPONS_TOTAL) SFG_player.weapon =
SFG_player.weapon = SFG_WEAPONS_TOTAL - 1; RCL_wrap(SFG_player.weapon + increment,SFG_WEAPONS_TOTAL);
SFG_player.weaponCooldownStartFrame = SFG_gameFrame - if (SFG_player.weapon == initialWeapon)
SFG_GET_WEAPON_FIRE_COOLDOWN_FRAMES(SFG_player.weapon) / 2; break;
uint8_t ammo, projectileCount, canShoot;
SFG_getPlayerWeaponInfo(&ammo,&projectileCount,&canShoot);
if (canShoot)
break;
}
} }
/** /**
@ -1724,13 +1745,23 @@ void SFG_gameStep()
int8_t shearing = 0; int8_t shearing = 0;
#if SFG_PREVIEW_MODE == 0 #if SFG_PREVIEW_MODE == 0
if ( if (
SFG_keyIsDown(SFG_KEY_B) && SFG_keyIsDown(SFG_KEY_B) &&
!SFG_keyIsDown(SFG_KEY_C) && !SFG_keyIsDown(SFG_KEY_C) &&
(SFG_gameFrame - SFG_player.weaponCooldownStartFrame > (SFG_gameFrame - SFG_player.weaponCooldownStartFrame >
SFG_GET_WEAPON_FIRE_COOLDOWN_FRAMES(SFG_player.weapon))) SFG_GET_WEAPON_FIRE_COOLDOWN_FRAMES(SFG_player.weapon)))
{ {
// attack // player: attack, shoot
uint8_t ammo, projectileCount, canShoot;
SFG_getPlayerWeaponInfo(&ammo,&projectileCount,&canShoot);
if (canShoot)
{
if (ammo != SFG_AMMO_NONE)
SFG_player.ammo[ammo] -= projectileCount;
uint8_t projectile; uint8_t projectile;
@ -1759,9 +1790,6 @@ void SFG_gameStep()
if (projectile != SFG_PROJECTILE_NONE) if (projectile != SFG_PROJECTILE_NONE)
{ {
uint8_t projectileCount =
SFG_GET_WEAPON_PROJECTILE_COUNT(SFG_player.weapon);
uint16_t angleAdd = SFG_PROJECTILE_SPREAD_ANGLE / (projectileCount + 1); uint16_t angleAdd = SFG_PROJECTILE_SPREAD_ANGLE / (projectileCount + 1);
RCL_Unit direction = RCL_Unit direction =
@ -1775,7 +1803,8 @@ void SFG_gameStep()
SFG_player.camera.position, SFG_player.camera.position,
SFG_player.camera.height, SFG_player.camera.height,
RCL_angleToDirection(direction), RCL_angleToDirection(direction),
(SFG_player.camera.shear * SFG_GET_PROJECTILE_SPEED_UPS(projectile)) / (SFG_player.camera.shear *
SFG_GET_PROJECTILE_SPEED_UPS(projectile)) /
SFG_CAMERA_MAX_SHEAR_PIXELS, SFG_CAMERA_MAX_SHEAR_PIXELS,
SFG_ELEMENT_COLLISION_DISTANCE + RCL_CAMERA_COLL_RADIUS SFG_ELEMENT_COLLISION_DISTANCE + RCL_CAMERA_COLL_RADIUS
); );
@ -1785,7 +1814,7 @@ void SFG_gameStep()
} }
else else
{ {
// melee attack // player's melee attack
for (uint16_t i = 0; i < SFG_currentLevel.monsterRecordCount; ++i) for (uint16_t i = 0; i < SFG_currentLevel.monsterRecordCount; ++i)
{ {
@ -1822,6 +1851,10 @@ void SFG_gameStep()
} }
SFG_player.weaponCooldownStartFrame = SFG_gameFrame; SFG_player.weaponCooldownStartFrame = SFG_gameFrame;
if (ammo != SFG_AMMO_NONE && SFG_player.ammo[ammo] == 0)
SFG_playerRotateWeapon(1);
} // endif: has enough ammo?
} // attack } // attack
#endif // SFG_PREVIEW_MODE == 0 #endif // SFG_PREVIEW_MODE == 0