]> git.tdb.fi Git - libs/game.git/commitdiff
Add playfield boundaries to Bassteroids
authorMikko Rasa <tdb@tdb.fi>
Sat, 12 Nov 2022 16:07:07 +0000 (18:07 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 12 Nov 2022 16:13:04 +0000 (18:13 +0200)
examples/bassteroids/source/bassteroids.cpp
examples/bassteroids/source/bassteroids.h
examples/bassteroids/source/gamecontroller.cpp
examples/bassteroids/source/physicalentity.cpp
examples/bassteroids/source/physicalentity.h
examples/bassteroids/source/physics.cpp
examples/bassteroids/source/playfield.cpp [new file with mode: 0644]
examples/bassteroids/source/playfield.h [new file with mode: 0644]

index 1c0ab66249fa365f5d6e9b38e665c06c3d028bd1..4ea600fce7543135d5643a63dbd2606acc11048f 100644 (file)
@@ -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<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)
index 9528c31d1a058007ed48b9f7bb935f58c9bf0353..43a690ac56b726c33d0063621d8b22cd67738bc0 100644 (file)
@@ -6,6 +6,7 @@
 #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
 { };
@@ -15,6 +16,9 @@ class Bassteroids: public Msp::GameView::Application<Bassteroids, BassteroidsRes
 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;
index eae5e2d3cf27e8560c187c38cb227cd773f24d2c..d39b3434e702a4290ef2f8c6563d754f79c3ba20 100644 (file)
@@ -19,11 +19,14 @@ void GameController::tick(Time::TimeDelta)
        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;
                });
index 585eb349e683116f560d98cf91fd1de3840a38ab..85ff0bdea390ad2f31a0a2ab2e1abdf291097680 100644 (file)
@@ -1,10 +1,9 @@
 #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)
index a0cca3768a0308cfe35ba7321d11e32057ec00b1..8f2a57fa07fae65308947a12908fbfc50eb38f1b 100644 (file)
@@ -2,6 +2,7 @@
 #define PHYSICALENTITY_H_
 
 #include <msp/game/entity.h>
+#include <msp/game/transform.h>
 #include "rigidbody.h"
 #include "collider.h"
 
@@ -22,7 +23,7 @@ protected:
        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; }
index 5d4c242233da509062fecc093b14aca7d0494003..db8e494f4d4f179aa8e02eddad9b909f53e9a951 100644 (file)
@@ -11,6 +11,8 @@ Physics::Physics(Game::Stage &s):
        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)
diff --git a/examples/bassteroids/source/playfield.cpp b/examples/bassteroids/source/playfield.cpp
new file mode 100644 (file)
index 0000000..8c6cc6c
--- /dev/null
@@ -0,0 +1,19 @@
+#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))
+{ }
diff --git a/examples/bassteroids/source/playfield.h b/examples/bassteroids/source/playfield.h
new file mode 100644 (file)
index 0000000..10596e9
--- /dev/null
@@ -0,0 +1,36 @@
+#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