Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK. Here's what I ended with.</p> <pre><code>class VictimEffect { public: template&lt;typename C&gt; void HitBy(C&amp;) {;} }; class MenaceEffect { public: template&lt;typename C&gt; void Hit(C&amp;) {;} }; template &lt;typename T, typename M = MenaceEffect, typename V = VictimEffect&gt; class Collidable { T m_Collider; M m_WeHit; V m_WeWereHit; public: Collidable(T collide, M menaceEffect, V victimEffect) : m_Collider(collide), m_WeHit(menaceEffect), m_WeWereHit(victimEffect) {;} Collidable(T collide) : m_Collider(collide) {;} Collidable(T collide, M menaceEffect) : m_Collider(collide), m_WeHit(menaceEffect) {;} T&amp; GetCollider() { return m_Collider; } template &lt;typename V&gt; void HitBy(V&amp; menace) { m_WeWereHit.HitBy(menace.GetCollider()); } template &lt;typename V&gt; void Hit(V&amp; victim) { m_WeHit.Hit(victim.GetCollider()); } template &lt;typename V&gt; bool CheckCollision(V&amp; menace) { return m_Collider.CheckCollision(menace); } }; template &lt;typename T, typename U&gt; inline void Collision(T&amp; t, U&amp; u) { if(u.CheckCollision(t.GetCollider())) { u.HitBy(t); t.Hit(u); } } </code></pre> <p>It is not necessary to inherit from Victim or Menace effects. They are there if you want catchall solutions. The M and V types can be any object so if you want to inherit or not or however you want to use it. I have to create all the variations for ptrs etc. If all using objects inherit from base classes then it can be used as if it was written with dynamic polymorphism. If you don't want to inherit and instead keep special separate lists you can do that too.</p> <p>Using it is easy</p> <pre><code>class Boundary { // Put whatever code here. bool CheckCollision(CollisionBox bounce) }; class BallCommand { public: void Hit(Boundary&amp; bounds) { if(bounds.XHit()) { xDir *= -1; } if(bounds.YHit()) { yDir *= -1; } } }; </code></pre> <p>Somewhere in your code you use it.</p> <pre><code> Collidable&lt;Boundary&gt; boundC(bounds); Collidable&lt;std::shared_ptr&lt;CollisionBox&gt;, BallCommand&amp;&gt; ballC(ballPhysics, ballBehavior); Collision(ballC, boundC); </code></pre> <p>There's more cleanup work to do, but I think it's a pretty good start. I'll convert to std::function to make it even easier.</p> <p>Thanks to Omnifarious and Giovanni for helping me think it out.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload