]> git.tdb.fi Git - libs/game.git/blobdiff - examples/bassteroids/source/playercontroller.cpp
Make asteroids take damage when shot and eventually be destroyed
[libs/game.git] / examples / bassteroids / source / playercontroller.cpp
index 65a879d2447727277ce4da16c4b4e1f822f57aef..46f9c1f85e30dfe710c96411a041c73455b22029 100644 (file)
@@ -12,6 +12,7 @@ PlayerController::PlayerController(Game::Stage &s):
                .mesh={ .object_name="Bass guitar.object" },
                .speed=12.0f, .turn_rate=4.71f },
        bullet_setup{ .physical={ .body={ .mass=0.05f, .moment_of_inertia=0.04f }, .collider={ .type=ColliderType::CIRCLE, .radius=0.2f }},
+               .hittable={ .damaging=true, .hits={ .max_hits=1 }, .damage={ .amount=1, .type=0 }},
                .mesh={ .object_name="Quaver.object" }}
 { }
 
@@ -55,16 +56,22 @@ void PlayerController::tick(Time::TimeDelta dt)
        controls->reset_edges();
 }
 
+void PlayerController::deferred_tick()
+{
+       System::deferred_tick();
+       erase_if(bullets, [](Game::Handle<Bullet> b){ return !b->get_hitpoints()->is_alive(); });
+}
+
 void PlayerController::fire()
 {
        Game::Handle<Game::Transform> player_tf = player_ship->get_transform();
        Game::Handle<RigidBody> player_body = player_ship->get_body();
 
-       bullets.emplace_back(stage.get_root(), bullet_setup);
+       Game::TransformValues tv;
+       tv.position = (player_tf->get_world_matrix()*LinAl::Vector<float, 4>(2.0f, 0.0f, 0.0f, 1.0f)).slice<3>(0);
+       tv.rotation = player_tf->get_rotation()*Geometry::Quaternion<float>::rotation(Geometry::Angle<float>::from_degrees(10), LinAl::Vector<float, 3>(0.0f, 0.0f, 1.0f));
+       bullets.emplace_back(stage.get_root(), bullet_setup, tv);
        Game::Handle<Bullet> bullet = bullets.back();
-       Game::Handle<Game::Transform> bullet_tf = bullet->get_transform();
-       bullet_tf->set_position((player_tf->get_world_matrix()*LinAl::Vector<float, 4>(2.0f, 0.0f, 0.0f, 1.0f)).slice<3>(0));
-       bullet_tf->set_rotation(player_tf->get_rotation()*Geometry::Quaternion<float>::rotation(Geometry::Angle<float>::from_degrees(10), LinAl::Vector<float, 3>(0.0f, 0.0f, 1.0f)));
        Game::Handle<RigidBody> bullet_body = bullet->get_body();
        bullet_body->set_velocity(player_body->get_velocity()+(player_tf->get_world_matrix()*LinAl::Vector<float, 4>(20.0f, 0.0f, 0.0f, 0.0f)).slice<2>(0));
 }