diff --git a/achterbahn.ino b/achterbahn.ino index fd795e7..78a8bb2 100644 --- a/achterbahn.ino +++ b/achterbahn.ino @@ -215,7 +215,7 @@ void spawnWagon(){ //Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, random(0, 20), random(3,20), random(0.2, 50)/10.0, 0 , random(5,100) , Wheel(random(0,256))); //spawn new wagon int _randomlength=random(3,40); //3-> minimum vel 10, 40 -> minium vel 30 // pos, wagonlength, startvel , startacc, trainmass, wagoncolor - Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, random(0, 20), _randomlength, random(map(_randomlength,3,40,10,30), map(_randomlength,3,40, 20,60))/10.0 , 0 , 20 , Wheel(random(0,256))); //spawn new wagon + Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, random(0, 20), _randomlength, random(map(_randomlength,3,40,10,30), map(_randomlength,3,40, 20,60))/10.0 , 0 , 20 , Wheel((uint8_t)random(0,255))); //spawn new wagon wagon_arr.push_back(tmpr); Serial.println("Spawned Wagon"); @@ -442,9 +442,9 @@ void loop_achterbahn(){ if (random(0,SPAWNCHANCEDOUBLE)==0){ spawnWagon(); } - }else{ - Serial.println("no spawn"); - } + }//else{ + //Serial.println("no spawn"); + //} } diff --git a/wagon.cpp b/wagon.cpp index 1974d7c..e05e48b 100644 --- a/wagon.cpp +++ b/wagon.cpp @@ -5,6 +5,7 @@ #define WAGONLENGTH 5 #define EDGE_WALL +//#define EDGE_BOUNCE //#define EDGE_WRAP Wagon::Wagon(int id,int numpixels, Adafruit_NeoPixel *strip,uint8_t *height,float pos, float trainlength,float startvel,float startacc, float wagonmass, uint32_t wagoncolor) @@ -19,9 +20,11 @@ Wagon::Wagon(int id,int numpixels, Adafruit_NeoPixel *strip,uint8_t *height,floa _acc=startacc; _wagonmass=wagonmass; //mass of whole train _spawntime=millis(); - _lasttimefast=millis(); + _lastvel=startvel; + _lastpositivedirchangepos=_numpixels+1000; + _lastpositivedirchangePosDifference=1000; _wagoncolor=wagoncolor; - + _health=1.0; } bool Wagon::operator==(const Wagon &r) const { @@ -118,30 +121,47 @@ void Wagon::updatePhysics(float updatedelayms) Serial.print(" Acc="); Serial.println(_acc);*/ - float _testvel=_vel; - if (_testvel<0){ - _testvel*=-1; - } - if (_testvel>SLOWVELOCITY){ //for despawn if slow - _lasttimefast=millis(); + + if (_lastvel*_vel<0 && _lastvel>=0){ //direction changed, positive + _lastpositivedirchangePosDifference=_lastpositivedirchangepos-_pos; + Serial.print("DC pos="); + Serial.print(_pos); + Serial.print(" last="); + Serial.print(_lastpositivedirchangepos); + + Serial.print(" diff="); + Serial.println(_lastpositivedirchangePosDifference); + _lastpositivedirchangepos=_pos; } + _lastvel=_vel; if (_pos>=_numpixels){ #ifdef EDGE_WRAP _pos-=_numpixels; //Wrap around edges #endif + #ifdef EDGE_BOUNCE + _vel*=-1; //bounce at edges + #endif #ifdef EDGE_WALL - _vel*=-1; //wall at edges + //nothing #endif } if (_pos<0){ #ifdef EDGE_WRAP _pos=_numpixels+_pos; //warp around edges #endif - #ifdef EDGE_WALL - _vel*=-1;; //wall at edges + #ifdef EDGE_BOUNCE + _vel*=-1;; //bounce at edges #endif + #ifdef EDGE_WALL + //nothing + #endif + } + + //fade out if health got low + if (_health<1.0 && _health>0.0){ + _health-=0.0001; //reduce until 0 } } @@ -154,14 +174,14 @@ float Wagon::getHeight(int p){ p=numpixels+p; //wrap edge #endif #ifdef EDGE_WALL - return _height[0]+p*-10; //edges as wall + return _height[0]+p*-100.0; //edges as wall #endif }else if(p>=_numpixels){ #ifdef EDGE_WRAP p=p-numpixels; //wrap edge #endif #ifdef EDGE_WALL - return _height[_numpixels-1]+(p-_numpixels)*10; //edges as wall + return _height[_numpixels-1]+(p-_numpixels)*100.0; //edges as wall #endif } return _height[p]; @@ -191,10 +211,15 @@ void Wagon::updateGraphics() //uint8_t b=abs(_vel)*255.0; //uint32_t c=_strip->Color(b*featherbrightness,(255-b)*featherbrightness,0); + float healtpositive=_health; + if(healtpositive<0){ //only positive values for color + healtpositive=0; + } + uint32_t c=_wagoncolor; - uint8_t _r = (uint8_t)(c >> 16); - uint8_t _g = (uint8_t)(c >> 8); - uint8_t _b = (uint8_t)c; + uint8_t _r = (uint8_t)(c >> 16)*healtpositive; + uint8_t _g = (uint8_t)(c >> 8)*healtpositive; + uint8_t _b = (uint8_t)c*healtpositive; _r*=featherbrightness; _g*=featherbrightness; @@ -245,7 +270,12 @@ bool Wagon::alive() return false; }*/ - if (millis()>_lasttimefast+5000 ){ //too long too slow + + if (_lastpositivedirchangePosDifference<=5){ //reduce health + //return false; + _health-=0.1; + } + if (_health<=0) { return false; } diff --git a/wagon.h b/wagon.h index 75de601..e3d5f50 100644 --- a/wagon.h +++ b/wagon.h @@ -28,8 +28,11 @@ class Wagon float _trainlength; uint8_t *_height; long _spawntime; - long _lasttimefast; + float _lastvel; + float _lastpositivedirchangepos; + float _lastpositivedirchangePosDifference; uint32_t _wagoncolor; + float _health; }; #endif