Bassteroids::Bassteroids(int, char **):
game_stage(director.create_stage()),
+ pf_setup({ .size={ 80, 45 }}),
+ playfield(game_stage.get_root(), pf_setup),
cam_setup({ .field_of_view_y=Geometry::Angle<float>::zero(), .size={ 80, 45 }, .near_clip=-30, .far_clip=30, .sequence_name="space.seq" }),
cam_entity(game_stage.get_root(), Game::TransformValues()),
camera(const_cast<const Game::Owned<Game::Entity> &>(cam_entity), cam_setup)
#include <msp/game/resources.h>
#include <msp/gameview/application.h>
#include <msp/gl/resources.h>
+#include "playfield.h"
class BassteroidsResources: public Msp::GL::Resources, public Msp::Game::ApplicationResources
{ };
private:
Msp::Game::Stage &game_stage;
+ PlayfieldSetup pf_setup;
+ Msp::Game::Owned<Playfield> playfield;
+
Msp::Game::CameraSetup cam_setup;
Msp::Game::Owned<Msp::Game::Entity> cam_entity;
Msp::Game::Owned<Msp::Game::Camera> camera;
case LEVEL_START:
defer([this]{
uniform_real_distribution<float> sdist(-1, 1);
+ uniform_real_distribution<float> udist(0, 1);
for(unsigned i=0; i<level+2; ++i)
{
asteroids.emplace_back(stage.get_root(), asteroid_setup);
asteroids.back()->get_transform()->set_position({ sdist(rng)*32, sdist(rng)*18, 0.0f });
- asteroids.back()->get_body()->set_velocity(asteroids.back()->get_transform()->get_position().slice<2>(0)*-0.1f);
+ Geometry::Angle<float> angle = Geometry::Angle<float>::from_turns(udist(rng));
+ float speed = udist(rng)*7;
+ asteroids.back()->get_body()->set_velocity({ cos(angle)*speed, sin(angle)*speed });
}
state = PLAYING;
});
#include "physicalentity.h"
-#include <msp/game/transform.h>
using namespace Msp;
-PhysicalEntity::PhysicalEntity(Game::Handle<Game::Entity> p, const Setup &s):
- Entity(p, Game::TransformValues()),
+PhysicalEntity::PhysicalEntity(Game::Handle<Game::Entity> p, const Setup &s, const Game::TransformValues &tv):
+ Entity(p, tv),
collider(*this, s.collider)
{
if(!s.fixture)
#define PHYSICALENTITY_H_
#include <msp/game/entity.h>
+#include <msp/game/transform.h>
#include "rigidbody.h"
#include "collider.h"
Msp::Game::Owned<Collider> collider;
public:
- PhysicalEntity(Msp::Game::Handle<Msp::Game::Entity>, const Setup &);
+ PhysicalEntity(Msp::Game::Handle<Msp::Game::Entity>, const Setup &, const Msp::Game::TransformValues & = Msp::Game::TransformValues());
bool is_fixture() const { return !body; }
Msp::Game::Handle<RigidBody> get_body() const { return body; }
observer(stage.get_event_bus())
{
observer.observe<Game::Events::EntityCreated>([this](auto &e){ entity_added(e); });
+
+ stage.synthesize_initial_events(observer);
}
void Physics::entity_added(const Game::Events::EntityCreated &e)
--- /dev/null
+#include "playfield.h"
+#include <msp/game/transform.h>
+
+using namespace Msp;
+
+Playfield::Playfield(Game::Handle<Game::Entity> p, const Setup &s):
+ Entity(p, NO_TRANSFORM),
+ setup(s),
+ left(*this, LinAl::Vector<float, 2>(-setup.size.x/2-0.5f, 0.0f), LinAl::Vector<float, 2>(1.0f, setup.size.y+2.0f)),
+ right(*this, LinAl::Vector<float, 2>(setup.size.x/2+0.5f, 0.0f), LinAl::Vector<float, 2>(1.0f, setup.size.y+2.0f)),
+ bottom(*this, LinAl::Vector<float, 2>(0.0f, -setup.size.y/2-0.5f), LinAl::Vector<float, 2>(setup.size.x+2.0f, 1.0f)),
+ top(*this, LinAl::Vector<float, 2>(0.0f, setup.size.y/2+0.5f), LinAl::Vector<float, 2>(setup.size.x+2.0f, 1.0f))
+{ }
+
+
+Playfield::Border::Border(Playfield &f, LinAl::Vector<float, 2> p, LinAl::Vector<float, 2> s):
+ setup{ .fixture=true, .body={}, .collider={ .type=ColliderType::BOX, .size=s }},
+ entity(f, setup, compose(p, 0.0f))
+{ }
--- /dev/null
+#ifndef PLAYFIELD_H_
+#define PLAYFIELD_H_
+
+#include <msp/game/entity.h>
+#include "physicalentity.h"
+
+struct PlayfieldSetup
+{
+ Msp::LinAl::Vector<float, 2> size;
+};
+
+class Playfield: public Msp::Game::Entity
+{
+public:
+ using Setup = PlayfieldSetup;
+
+private:
+ struct Border
+ {
+ PhysicalSetup setup;
+ Msp::Game::Owned<PhysicalEntity> entity;
+
+ Border(Playfield &, Msp::LinAl::Vector<float, 2>, Msp::LinAl::Vector<float, 2>);
+ };
+
+ const Setup &setup;
+ Border left;
+ Border right;
+ Border bottom;
+ Border top;
+
+public:
+ Playfield(Msp::Game::Handle<Msp::Game::Entity>, const Setup &);
+};
+
+#endif