]> git.tdb.fi Git - libs/game.git/blob - examples/bassteroids/source/playercontroller.cpp
Pass bullet's initial transform values directly to the constructor
[libs/game.git] / examples / bassteroids / source / playercontroller.cpp
1 #include "playercontroller.h"
2 #include <msp/game/root.h>
3 #include <msp/game/stage.h>
4 #include "controls.h"
5
6 using namespace std;
7 using namespace Msp;
8
9 PlayerController::PlayerController(Game::Stage &s):
10         System(s),
11         player_setup{ .physical={ .body={ .mass=1, .moment_of_inertia=0.8f }, .collider={ .type=ColliderType::CIRCLE, .radius=0.8f }},
12                 .mesh={ .object_name="Bass guitar.object" },
13                 .speed=12.0f, .turn_rate=4.71f },
14         bullet_setup{ .physical={ .body={ .mass=0.05f, .moment_of_inertia=0.04f }, .collider={ .type=ColliderType::CIRCLE, .radius=0.2f }},
15                 .mesh={ .object_name="Quaver.object" }}
16 { }
17
18 void PlayerController::set_controls(Controls *c)
19 {
20         controls = c;
21         if(!player_ship)
22                 player_ship = Game::Owned<Ship>(stage.get_root(), player_setup);
23 }
24
25 void PlayerController::tick(Time::TimeDelta dt)
26 {
27         if(!controls)
28                 return;
29
30         float dt_secs = dt/Time::sec;
31
32         const Ship::Setup &setup = player_ship->get_setup();
33         float thrust = setup.speed*setup.speed;
34         float torque = setup.turn_rate*setup.turn_rate;
35
36         Game::Handle<Game::Transform> transform = player_ship->get_transform();
37         LinAl::Vector<float, 2> fwd_dir = normalize(transform->get_world_matrix().column(0).slice<2>(0));
38         Game::Handle<RigidBody> body = player_ship->get_body();
39
40         body->add_force(fwd_dir*(controls->forward.get_value()*thrust));
41         const LinAl::Vector<float, 2> &velocity = body->get_velocity();
42         float speed = velocity.norm();
43         if(speed>1e-5)
44                 body->add_force(velocity*-min(speed+setup.speed/speed, 1.0f/dt_secs));
45
46         body->add_torque(controls->turn_left.get_value()*torque);
47         Geometry::Angle<float> angular_vel = body->get_angular_velocity();
48         Geometry::Angle<float> angular_speed = abs(angular_vel);
49         if(abs(angular_speed.radians())>1e-5)
50                 body->add_torque(angular_vel.radians()*-min(angular_speed.radians()+setup.turn_rate/angular_speed.radians(), 1.0f/dt_secs));
51
52         if(controls->fire.was_pressed())
53                 fire();
54
55         controls->reset_edges();
56 }
57
58 void PlayerController::fire()
59 {
60         Game::Handle<Game::Transform> player_tf = player_ship->get_transform();
61         Game::Handle<RigidBody> player_body = player_ship->get_body();
62
63         Game::TransformValues tv;
64         tv.position = (player_tf->get_world_matrix()*LinAl::Vector<float, 4>(2.0f, 0.0f, 0.0f, 1.0f)).slice<3>(0);
65         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));
66         bullets.emplace_back(stage.get_root(), bullet_setup, tv);
67         Game::Handle<Bullet> bullet = bullets.back();
68         Game::Handle<RigidBody> bullet_body = bullet->get_body();
69         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));
70 }