]> git.tdb.fi Git - libs/game.git/blob - examples/bassteroids/source/gamecontroller.cpp
Implement a simple physics system in Bassteroids
[libs/game.git] / examples / bassteroids / source / gamecontroller.cpp
1 #include "gamecontroller.h"
2 #include <stdexcept>
3 #include <msp/game/root.h>
4 #include <msp/game/transform.h>
5
6 using namespace std;
7 using namespace Msp;
8
9 GameController::GameController(Game::Stage &s):
10         System(s),
11         asteroid_setup{ .physical = { .body = { .mass = 200 }, .collider = { .type = ColliderType::CIRCLE, .radius = 3.0f } },
12                 .mesh = { .object_name = "Asteroid 1.object" }}
13 { }
14
15 void GameController::tick(Time::TimeDelta)
16 {
17         switch(state)
18         {
19         case LEVEL_START:
20                 defer([this]{
21                         uniform_real_distribution<float> sdist(-1, 1);
22                         for(unsigned i=0; i<level+2; ++i)
23                         {
24                                 asteroids.emplace_back(stage.get_root(), asteroid_setup);
25                                 asteroids.back()->get_transform()->set_position({ sdist(rng)*32, sdist(rng)*18, 0.0f });
26                                 asteroids.back()->get_body()->set_velocity(asteroids.back()->get_transform()->get_position().slice<2>(0)*-0.1f);
27                         }
28                         state = PLAYING;
29                 });
30                 break;
31         case PLAYING:
32                 break;
33         default:
34                 throw logic_error("Unimplemented state");
35         }
36 }