diff options
-rw-r--r-- | game/player/PlayerBulletScript.cpp | 2 | ||||
-rw-r--r-- | game/player/PlayerBulletSubScene.cpp | 13 | ||||
-rw-r--r-- | game/player/PlayerScript.cpp | 9 | ||||
-rw-r--r-- | game/player/PlayerScript.h | 5 |
4 files changed, 20 insertions, 9 deletions
diff --git a/game/player/PlayerBulletScript.cpp b/game/player/PlayerBulletScript.cpp index e1637c2..50b6617 100644 --- a/game/player/PlayerBulletScript.cpp +++ b/game/player/PlayerBulletScript.cpp @@ -20,7 +20,7 @@ void PlayerBulletScript::fixed_update(crepe::duration_t dt){ vec2 half_screen = camera.viewport_size / 2; float despawn_location = cam_transform.position.x + half_screen.x + 50; - if(transform.position.x < despawn_location){ + if(transform.position.x > despawn_location){ this->despawn_bullet(); } } diff --git a/game/player/PlayerBulletSubScene.cpp b/game/player/PlayerBulletSubScene.cpp index eb34046..eb89260 100644 --- a/game/player/PlayerBulletSubScene.cpp +++ b/game/player/PlayerBulletSubScene.cpp @@ -1,3 +1,4 @@ +#include <iostream> #include <string> #include <crepe/api/GameObject.h> @@ -19,7 +20,6 @@ using namespace crepe; using namespace std; int PlayerBulletSubScene::create(Scene & scn){ vec2 size = {20, 20}; - static int counter = 0; string unique_name = "playerBullet_" + to_string(counter++); GameObject player_bullet = scn.new_object(unique_name.c_str(),"PlayerBullet",vec2{0,-850},0,1); @@ -27,23 +27,24 @@ int PlayerBulletSubScene::create(Scene & scn){ Rigidbody& player_bullet_body = player_bullet.add_component<Rigidbody>(Rigidbody::Data { .gravity_scale = 0, .body_type = Rigidbody::BodyType::DYNAMIC, - .linear_velocity = vec2{-300,0}, - .collision_layers = {COLL_LAY_PLAYER}, - .collision_layer = COLL_LAY_BULLET, + .linear_velocity = vec2{300,0}, + .angular_velocity = 150, + // .collision_layers = {COLL_LAY_PLAYER}, + // .collision_layer = COLL_LAY_BULLET, }); player_bullet_body.active = false; BoxCollider& player_bullet_collider = player_bullet.add_component<BoxCollider>(vec2(60, 40)); player_bullet_collider.active = false; - Asset player_bullet_asset {"asset/other_effects/effect_smgbullet_x2.png"}; + Asset player_bullet_asset {"asset/other_effects/crepe.png"}; Sprite & player_bullet_sprite = player_bullet.add_component<Sprite>( player_bullet_asset, Sprite::Data { .flip = {true,false}, .sorting_in_layer = SORT_IN_LAY_PLAYER, .order_in_layer = 1, - .size = vec2(60,0), + .size = vec2(30,0), } ); player_bullet.add_component<BehaviorScript>().set_script<PlayerBulletScript>(); diff --git a/game/player/PlayerScript.cpp b/game/player/PlayerScript.cpp index 8372080..96a0552 100644 --- a/game/player/PlayerScript.cpp +++ b/game/player/PlayerScript.cpp @@ -17,6 +17,7 @@ void PlayerScript::init() { subscribe<CollisionEvent>([this](const CollisionEvent & ev) -> bool { return this->on_collision(ev); }); + this->last_fired = std::chrono::steady_clock::now(); } bool PlayerScript::on_collision(const CollisionEvent & ev) { BehaviorScript & play_scr = this->get_components_by_name<BehaviorScript>("player").front(); @@ -86,7 +87,13 @@ void PlayerScript::fixed_update(crepe::duration_t dt) { }); } if(this->get_key_state(Keycode::ENTER)){ - this->shoot(transform.position,0); + + auto now = std::chrono::steady_clock::now(); + std::chrono::duration<float> elapsed = now - last_fired; + if (elapsed > shot_delay) { + this->shoot(transform.position,0); + last_fired = now; + } } if (this->get_key_state(Keycode::SPACE)) { rb.add_force_linear(vec2(0, -PLAYER_GRAVITY_SCALE / 2.5) * dt.count() / 0.02); diff --git a/game/player/PlayerScript.h b/game/player/PlayerScript.h index cdb466f..cec27ce 100644 --- a/game/player/PlayerScript.h +++ b/game/player/PlayerScript.h @@ -2,7 +2,7 @@ #include <crepe/api/Event.h> #include <crepe/api/Script.h> - +#include <chrono> class PlayerScript : public crepe::Script { public: void init(); @@ -14,4 +14,7 @@ private: void shoot(const crepe::vec2& location,float angle); private: int prev_anim = 0; + std::chrono::time_point<std::chrono::steady_clock> last_fired; + std::chrono::duration<float> shot_delay = std::chrono::duration<float>(0.5); + }; |