change train kill to fade out after pos change on direction change is low

This commit is contained in:
interfisch 2018-05-16 22:52:45 +02:00
parent 224a6bb3f7
commit b9cce276c7
3 changed files with 55 additions and 22 deletions

View File

@ -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 //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 int _randomlength=random(3,40); //3-> minimum vel 10, 40 -> minium vel 30
// pos, wagonlength, startvel , startacc, trainmass, wagoncolor // 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); wagon_arr.push_back(tmpr);
Serial.println("Spawned Wagon"); Serial.println("Spawned Wagon");
@ -442,9 +442,9 @@ void loop_achterbahn(){
if (random(0,SPAWNCHANCEDOUBLE)==0){ if (random(0,SPAWNCHANCEDOUBLE)==0){
spawnWagon(); spawnWagon();
} }
}else{ }//else{
Serial.println("no spawn"); //Serial.println("no spawn");
} //}
} }

View File

@ -5,6 +5,7 @@
#define WAGONLENGTH 5 #define WAGONLENGTH 5
#define EDGE_WALL #define EDGE_WALL
//#define EDGE_BOUNCE
//#define EDGE_WRAP //#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) 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; _acc=startacc;
_wagonmass=wagonmass; //mass of whole train _wagonmass=wagonmass; //mass of whole train
_spawntime=millis(); _spawntime=millis();
_lasttimefast=millis(); _lastvel=startvel;
_lastpositivedirchangepos=_numpixels+1000;
_lastpositivedirchangePosDifference=1000;
_wagoncolor=wagoncolor; _wagoncolor=wagoncolor;
_health=1.0;
} }
bool Wagon::operator==(const Wagon &r) const { bool Wagon::operator==(const Wagon &r) const {
@ -118,30 +121,47 @@ void Wagon::updatePhysics(float updatedelayms)
Serial.print(" Acc="); Serial.print(" Acc=");
Serial.println(_acc);*/ Serial.println(_acc);*/
float _testvel=_vel;
if (_testvel<0){ if (_lastvel*_vel<0 && _lastvel>=0){ //direction changed, positive
_testvel*=-1; _lastpositivedirchangePosDifference=_lastpositivedirchangepos-_pos;
} Serial.print("DC pos=");
if (_testvel>SLOWVELOCITY){ //for despawn if slow Serial.print(_pos);
_lasttimefast=millis(); Serial.print(" last=");
Serial.print(_lastpositivedirchangepos);
Serial.print(" diff=");
Serial.println(_lastpositivedirchangePosDifference);
_lastpositivedirchangepos=_pos;
} }
_lastvel=_vel;
if (_pos>=_numpixels){ if (_pos>=_numpixels){
#ifdef EDGE_WRAP #ifdef EDGE_WRAP
_pos-=_numpixels; //Wrap around edges _pos-=_numpixels; //Wrap around edges
#endif #endif
#ifdef EDGE_BOUNCE
_vel*=-1; //bounce at edges
#endif
#ifdef EDGE_WALL #ifdef EDGE_WALL
_vel*=-1; //wall at edges //nothing
#endif #endif
} }
if (_pos<0){ if (_pos<0){
#ifdef EDGE_WRAP #ifdef EDGE_WRAP
_pos=_numpixels+_pos; //warp around edges _pos=_numpixels+_pos; //warp around edges
#endif #endif
#ifdef EDGE_WALL #ifdef EDGE_BOUNCE
_vel*=-1;; //wall at edges _vel*=-1;; //bounce at edges
#endif #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 p=numpixels+p; //wrap edge
#endif #endif
#ifdef EDGE_WALL #ifdef EDGE_WALL
return _height[0]+p*-10; //edges as wall return _height[0]+p*-100.0; //edges as wall
#endif #endif
}else if(p>=_numpixels){ }else if(p>=_numpixels){
#ifdef EDGE_WRAP #ifdef EDGE_WRAP
p=p-numpixels; //wrap edge p=p-numpixels; //wrap edge
#endif #endif
#ifdef EDGE_WALL #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 #endif
} }
return _height[p]; return _height[p];
@ -191,10 +211,15 @@ void Wagon::updateGraphics()
//uint8_t b=abs(_vel)*255.0; //uint8_t b=abs(_vel)*255.0;
//uint32_t c=_strip->Color(b*featherbrightness,(255-b)*featherbrightness,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; uint32_t c=_wagoncolor;
uint8_t _r = (uint8_t)(c >> 16); uint8_t _r = (uint8_t)(c >> 16)*healtpositive;
uint8_t _g = (uint8_t)(c >> 8); uint8_t _g = (uint8_t)(c >> 8)*healtpositive;
uint8_t _b = (uint8_t)c; uint8_t _b = (uint8_t)c*healtpositive;
_r*=featherbrightness; _r*=featherbrightness;
_g*=featherbrightness; _g*=featherbrightness;
@ -245,7 +270,12 @@ bool Wagon::alive()
return false; 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; return false;
} }

View File

@ -28,8 +28,11 @@ class Wagon
float _trainlength; float _trainlength;
uint8_t *_height; uint8_t *_height;
long _spawntime; long _spawntime;
long _lasttimefast; float _lastvel;
float _lastpositivedirchangepos;
float _lastpositivedirchangePosDifference;
uint32_t _wagoncolor; uint32_t _wagoncolor;
float _health;
}; };
#endif #endif