www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Group dots not working

reply Joel <joelcnz gmail.com> writes:
The dots are supposed to keep moving till they hit something then 
change direction.

Using DotsLogicType.solid the move at first, but then stop moving 
once they hit something.

What supposed to happen: First they check left or right 
(depending on the dir.x is negative or positive). If no dots hit 
anything then move the whole dot group, otherwise change 
direction x variable. Do the same again, but with up and down.

```d
module freedots;

import jart;

struct Dot {
     Vector2 pos, dir;
     Color col;
     Vector2 wpos;
     bool hit;
     bool hitx, hity; // old

     Vector2 logic(in DotsLogicType dotsLogicType, int hrd, ref 
char[][] hitMap, in RenderTexture canvas) {
         Vector2 result;
         final switch(dotsLogicType) with(DotsLogicType) {
             case none:
             break;
             case solid:
                 // mixin(tce("hrd dir.x dir.y".split));
                 // look horrisontal
                 if (hrd==0)
                     if (pos.x+dir.x>=canvas.texture.width || 
pos.x+dir.x<0 || 

                         hit=true;
                     }

                 // look virtical
                 if (hrd==1)
                     if (pos.y+dir.y>=canvas.texture.height || 
pos.y+dir.y<0 || 

                         hit=true;
                     }
             break;
             case fold:
                     if (pos.x+dir.x>=canvas.texture.width || 
pos.x+dir.x<0 || 

                         dir.x*=-1;
                     } else pos.x+=dir.x;
                     if (pos.y+dir.y>=canvas.texture.height || 
pos.y+dir.y<0 || 

                         dir.y*=-1;
                     } else pos.y+=dir.y;
             break;
         }
         return result;
     }

     void draw() {
         DrawRectangleV(Vector2(cast(int)pos.x, cast(int)pos.y)*3, 
Vector2(3,3), col);
     }
}

struct DotCnt {
     Dot[] dots;
     static DotsLogicType dotsLogicType=DotsLogicType.none;

     void clearDotsHitMap(ref char[][] hitMap) {
         foreach(ref d; dots) {
             hitMap[cast(size_t)d.pos.x][cast(size_t)d.pos.y]=' ';
         }
     }
     void setDotsHitMap(ref char[][] hitMap) {
         foreach(ref d; dots) {

         }
     }

     void logic(in RenderTexture canvas, ref char[][] hitMap) {
         final switch(dotsLogicType) with(DotsLogicType) {
             case none:
                 setDotsHitMap(hitMap);
             break;
             case solid:
                 clearDotsHitMap(hitMap);
                 foreach(hrd; 0..2) {
                     Vector2 mdir;
                     bool hit;
                     // go through dots looking
                     foreach(ref d; dots) {
                         mdir=(hrd==0 ? Vector2(d.dir.x,0) : 
Vector2(0,d.dir.y));
                         d.logic(DotsLogicType.solid, hrd, hitMap, 
canvas);
                         if (d.hit) {
                             hit=true;
                             break;
                         }
                     }
                     if (! hit)
                         foreach(ref d; dots) {
                             d.pos+=mdir;
                         }
                     if (hit)
                         foreach(ref d; dots) {
                             if (hrd==0)
                                 d.dir.x=-mdir.x;
                             if (hrd==1)
                                 d.dir.y=-mdir.y;
                             mixin(tce("hrd mdir d.pos 
d.dir".split));
                         }
                 } // for loop h and v
                 setDotsHitMap(hitMap);
             break;
             case fold:
                 foreach(ref d; dots)
                     d.logic(DotsLogicType.fold, 0, hitMap, 
canvas);
             break;
         }
     }

     void draw() {
         foreach(ref d; dots)
             d.draw;
     }

     void dotifyFrmTex(in PointerNSelecter pntsel, Texture2D tex, 
bool flip=false) {
         auto ds=buildXDollarsFromTex(tex, flip);
         foreach(y; 0..tex.height)
             foreach(x; 0..tex.width)
                 if (ds[x][y].a!=0)
                     dots~=Dot(pntsel.pos+Vector2(x,y), 
Vector2(1,1), ds[x][y]);
     }
}
```

Here's YouTube video that gives an idea of what my grouped dots 
can do. https://youtu.be/t5EYeV4BYzU?si=bcpGYwYIX2ecfa9Q
Dec 19 2023
parent Joel <joelcnz gmail.com> writes:
On Wednesday, 20 December 2023 at 01:45:57 UTC, Joel wrote:
 The dots are supposed to keep moving till they hit something 
 then change direction.

 [...]
Oh, I found the problem, I wasn't resetting the hit bool variable.
Dec 19 2023