-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented a fire rate in projectile spawner
- Loading branch information
Showing
3 changed files
with
74 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,47 @@ | ||
#include "nodes/projectile_spawner.hpp" | ||
|
||
#include "util/bind.hpp" | ||
|
||
namespace rl::inline node | ||
{ | ||
void ProjectileSpawner::_ready() | ||
[[nodiscard]] | ||
Projectile* ProjectileSpawner::spawn_projectile() | ||
{ | ||
m_scene.set_path(path::scene::Bullet); | ||
auto elapsed{ clock_t::now() - m_prev_spawn_time }; | ||
if (elapsed < m_spawn_delay) | ||
return nullptr; | ||
else | ||
{ | ||
Projectile* projectile{ m_scene.instantiate() }; | ||
m_prev_spawn_time = clock_t::now(); | ||
return projectile; | ||
} | ||
} | ||
|
||
[[nodiscard]] | ||
Projectile* ProjectileSpawner::spawn_projectile() | ||
[[property]] | ||
double ProjectileSpawner::get_fire_rate() const | ||
{ | ||
return m_fire_rate; | ||
} | ||
|
||
[[property]] | ||
void ProjectileSpawner::set_fire_rate(double fire_rate) | ||
{ | ||
m_fire_rate = fire_rate; | ||
m_spawn_delay = ProjectileSpawner::calculate_spawn_delay(m_fire_rate); | ||
} | ||
|
||
ProjectileSpawner::millisec_t ProjectileSpawner::calculate_spawn_delay(double fire_rate) | ||
{ | ||
// converts fire rate (shots per second) to the time delay between shots in ms. | ||
// the multiplication by 100 is just to offset the rounding errors by shifting | ||
// the decimal place to the right a few places before dividing. | ||
return (1000ms * 100) / static_cast<uint64_t>(fire_rate * 100); | ||
} | ||
|
||
void ProjectileSpawner::_bind_methods() | ||
{ | ||
Projectile* projectile{ m_scene.instantiate() }; | ||
return projectile; | ||
bind_member_function(ProjectileSpawner, get_fire_rate); | ||
bind_member_function(ProjectileSpawner, set_fire_rate); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters