From 391f1ab3c7ca52a9990f516f4cdd7094f60350df Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 12 Nov 2022 18:07:07 +0200 Subject: [PATCH] Add playfield boundaries to Bassteroids --- examples/bassteroids/source/bassteroids.cpp | 2 ++ examples/bassteroids/source/bassteroids.h | 4 +++ .../bassteroids/source/gamecontroller.cpp | 5 ++- .../bassteroids/source/physicalentity.cpp | 5 ++- examples/bassteroids/source/physicalentity.h | 3 +- examples/bassteroids/source/physics.cpp | 2 ++ examples/bassteroids/source/playfield.cpp | 19 ++++++++++ examples/bassteroids/source/playfield.h | 36 +++++++++++++++++++ 8 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 examples/bassteroids/source/playfield.cpp create mode 100644 examples/bassteroids/source/playfield.h diff --git a/examples/bassteroids/source/bassteroids.cpp b/examples/bassteroids/source/bassteroids.cpp index 1c0ab66..4ea600f 100644 --- a/examples/bassteroids/source/bassteroids.cpp +++ b/examples/bassteroids/source/bassteroids.cpp @@ -10,6 +10,8 @@ using namespace Msp; 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::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 &>(cam_entity), cam_setup) diff --git a/examples/bassteroids/source/bassteroids.h b/examples/bassteroids/source/bassteroids.h index 9528c31..43a690a 100644 --- a/examples/bassteroids/source/bassteroids.h +++ b/examples/bassteroids/source/bassteroids.h @@ -6,6 +6,7 @@ #include #include #include +#include "playfield.h" class BassteroidsResources: public Msp::GL::Resources, public Msp::Game::ApplicationResources { }; @@ -15,6 +16,9 @@ class Bassteroids: public Msp::GameView::Application playfield; + Msp::Game::CameraSetup cam_setup; Msp::Game::Owned cam_entity; Msp::Game::Owned camera; diff --git a/examples/bassteroids/source/gamecontroller.cpp b/examples/bassteroids/source/gamecontroller.cpp index eae5e2d..d39b343 100644 --- a/examples/bassteroids/source/gamecontroller.cpp +++ b/examples/bassteroids/source/gamecontroller.cpp @@ -19,11 +19,14 @@ void GameController::tick(Time::TimeDelta) case LEVEL_START: defer([this]{ uniform_real_distribution sdist(-1, 1); + uniform_real_distribution udist(0, 1); for(unsigned i=0; iget_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 angle = Geometry::Angle::from_turns(udist(rng)); + float speed = udist(rng)*7; + asteroids.back()->get_body()->set_velocity({ cos(angle)*speed, sin(angle)*speed }); } state = PLAYING; }); diff --git a/examples/bassteroids/source/physicalentity.cpp b/examples/bassteroids/source/physicalentity.cpp index 585eb34..85ff0bd 100644 --- a/examples/bassteroids/source/physicalentity.cpp +++ b/examples/bassteroids/source/physicalentity.cpp @@ -1,10 +1,9 @@ #include "physicalentity.h" -#include using namespace Msp; -PhysicalEntity::PhysicalEntity(Game::Handle p, const Setup &s): - Entity(p, Game::TransformValues()), +PhysicalEntity::PhysicalEntity(Game::Handle p, const Setup &s, const Game::TransformValues &tv): + Entity(p, tv), collider(*this, s.collider) { if(!s.fixture) diff --git a/examples/bassteroids/source/physicalentity.h b/examples/bassteroids/source/physicalentity.h index a0cca37..8f2a57f 100644 --- a/examples/bassteroids/source/physicalentity.h +++ b/examples/bassteroids/source/physicalentity.h @@ -2,6 +2,7 @@ #define PHYSICALENTITY_H_ #include +#include #include "rigidbody.h" #include "collider.h" @@ -22,7 +23,7 @@ protected: Msp::Game::Owned collider; public: - PhysicalEntity(Msp::Game::Handle, const Setup &); + PhysicalEntity(Msp::Game::Handle, const Setup &, const Msp::Game::TransformValues & = Msp::Game::TransformValues()); bool is_fixture() const { return !body; } Msp::Game::Handle get_body() const { return body; } diff --git a/examples/bassteroids/source/physics.cpp b/examples/bassteroids/source/physics.cpp index 5d4c242..db8e494 100644 --- a/examples/bassteroids/source/physics.cpp +++ b/examples/bassteroids/source/physics.cpp @@ -11,6 +11,8 @@ Physics::Physics(Game::Stage &s): observer(stage.get_event_bus()) { observer.observe([this](auto &e){ entity_added(e); }); + + stage.synthesize_initial_events(observer); } void Physics::entity_added(const Game::Events::EntityCreated &e) diff --git a/examples/bassteroids/source/playfield.cpp b/examples/bassteroids/source/playfield.cpp new file mode 100644 index 0000000..8c6cc6c --- /dev/null +++ b/examples/bassteroids/source/playfield.cpp @@ -0,0 +1,19 @@ +#include "playfield.h" +#include + +using namespace Msp; + +Playfield::Playfield(Game::Handle p, const Setup &s): + Entity(p, NO_TRANSFORM), + setup(s), + left(*this, LinAl::Vector(-setup.size.x/2-0.5f, 0.0f), LinAl::Vector(1.0f, setup.size.y+2.0f)), + right(*this, LinAl::Vector(setup.size.x/2+0.5f, 0.0f), LinAl::Vector(1.0f, setup.size.y+2.0f)), + bottom(*this, LinAl::Vector(0.0f, -setup.size.y/2-0.5f), LinAl::Vector(setup.size.x+2.0f, 1.0f)), + top(*this, LinAl::Vector(0.0f, setup.size.y/2+0.5f), LinAl::Vector(setup.size.x+2.0f, 1.0f)) +{ } + + +Playfield::Border::Border(Playfield &f, LinAl::Vector p, LinAl::Vector s): + setup{ .fixture=true, .body={}, .collider={ .type=ColliderType::BOX, .size=s }}, + entity(f, setup, compose(p, 0.0f)) +{ } diff --git a/examples/bassteroids/source/playfield.h b/examples/bassteroids/source/playfield.h new file mode 100644 index 0000000..10596e9 --- /dev/null +++ b/examples/bassteroids/source/playfield.h @@ -0,0 +1,36 @@ +#ifndef PLAYFIELD_H_ +#define PLAYFIELD_H_ + +#include +#include "physicalentity.h" + +struct PlayfieldSetup +{ + Msp::LinAl::Vector size; +}; + +class Playfield: public Msp::Game::Entity +{ +public: + using Setup = PlayfieldSetup; + +private: + struct Border + { + PhysicalSetup setup; + Msp::Game::Owned entity; + + Border(Playfield &, Msp::LinAl::Vector, Msp::LinAl::Vector); + }; + + const Setup &setup; + Border left; + Border right; + Border bottom; + Border top; + +public: + Playfield(Msp::Game::Handle, const Setup &); +}; + +#endif -- 2.43.0