bool APScatterAction::initWithDuration(float _height, float _minX, float _maxX, float _rotate) { float xOffset = getRandFloat(_minX, _maxX); m_height = _height; //左右位移与抛出的高有关 auto x = m_height-xOffset; if(getRandFloat(-m_height,m_height)<0){ x = -m_height+xOffset; } m_width = x; //默认世界重力为0.9798(地球重力) m_gravity = 0.9798; //随机质量 m_mass = getRandFloat(4.5f, 6.0f); //四舍五入取弹跳次数(弹力与高度,重力及质量有关) m_jumps = m_height/(m_mass*m_gravity*40)+0.5; //约动作时间 m_duration = m_height/(m_gravity*285); //旋转 m_rotation = _rotate==0.0 ? _rotate : m_width / 25 + _rotate; ActionInterval::initWithDuration(m_duration); return true; }
/** Smoothes a single point based on the average of its neighbors */ static float get_smooth_value(int x, int y){ float corners_value = (getRandFloat(x-1,y-1) + getRandFloat(x+1,y-1) + getRandFloat(x-1,y+1) + getRandFloat(x+1,y+1)) / 16; float sides = (getRandFloat(x-1,y) + getRandFloat(x+1,y) + getRandFloat(x,y-1) + getRandFloat(x,y+1)) / 8; float value = getRandFloat(x,y) / 4; return corners_value + sides + value; }
float getRandExponential( ) //Interpolate between points on { //graph: lower bound + slope float x, deviate; //of line * distance on x axis. //Note more approximation x = getRandFloat( ); //points in exponential tail. if (x == 0.0) deviate = 0.0; else if (x <= 0.1) deviate = ((x - 0.0) * 1.04 + 0.0); else if (x <= 0.2) deviate = ((x - 0.1) * 1.18 + 0.104); else if (x <= 0.3) deviate = ((x - 0.2) * 1.33 + 0.222); else if (x <= 0.4) deviate = ((x - 0.3) * 1.54 + 0.355); else if (x <= 0.5) deviate = ((x - 0.4) * 1.81 + 0.509); else if (x <= 0.6) deviate = ((x - 0.5) * 2.25 + 0.690); else if (x <= 0.7) deviate = ((x - 0.6) * 2.85 + 0.915); else if (x <= 0.75) deviate = ((x - 0.70) * 3.60 + 1.2); else if (x <= 0.8) deviate = ((x - 0.75) * 4.40 + 1.38); else if (x <= 0.84) deviate = ((x - 0.8) * 5.75 + 1.60); else if (x <= 0.88) deviate = ((x - 0.84) * 7.25 + 1.83); else if (x <= 0.9) deviate = ((x - 0.88) * 9.00 + 2.12); else if (x <= 0.92) deviate = ((x - 0.90) * 11.0 + 2.30); else if (x <= 0.94) deviate = ((x - 0.92) * 14.5 + 2.52); else if (x<= 0.95) deviate = ((x - 0.94) * 18.0 + 2.81); else if (x <= 0.96) deviate = ((x - 0.95) * 21.0 + 2.99); else if (x <= 0.97) deviate = ((x - 0.96) * 30.0 + 3.20); else if (x <= 0.98) deviate = ((x - 0.97) * 40.0 + 3.50); else if (x <= 0.99) deviate = ((x - 0.98) * 70.0 + 3.90); else if (x <= 0.995) deviate = ((x - 0.99) * 140.0 + 4.60); else if (x <= 0.998) deviate = ((x - 0.995) * 300.0 + 5.30); else if (x <= 0.999) deviate = ((x - 0.998) * 800.0 + 6.20); else deviate = ((x - 0.9997) * 1000.0 + 8.0); return (float)deviate; }
void Manager::setNumberOfEnemies(int number){ numOfEnemies = number; int pw = player.getMFS()->getFrame()->getHeight(); int ph = player.getMFS()->getFrame()->getWidth(); if(number > static_cast<int>(enemies.size())){ number -= enemies.size(); for(int i = 0; i < number; ++i) { enemies.push_back(new Enemy( new TwoWaySprite("heli2", getRandFloat(pw + Gamedata::getInstance()->getXmlInt("startRoom"), Gamedata::getInstance()->getXmlInt("worldWidth") ), getRandFloat(ph + Gamedata::getInstance()->getXmlInt("startRoom"), Gamedata::getInstance()->getXmlInt("worldHeight") - (Gamedata::getInstance()->getXmlInt("heli2Height") / Gamedata::getInstance()->getXmlInt("heli2YFrames")) ), (rand()%2?1:-1)*getRandFloat( -Gamedata::getInstance()->getXmlInt("heli2SpeedX"), Gamedata::getInstance()->getXmlInt("heli2SpeedX") ), (rand()%2?1:-1)*getRandFloat( -Gamedata::getInstance()->getXmlInt("heli2SpeedY"), Gamedata::getInstance()->getXmlInt("heli2SpeedY") ), 1 ), &player, Gamedata::getInstance()->getXmlFloat("enemyAttackRange"), Gamedata::getInstance()->getXmlFloat("enemyAttackSpeed")) ); } } else{ number = enemies.size() - number; for(int i = 0; i < number; ++i){ delete enemies.back()->getMFS(); delete enemies.back(); enemies.pop_back(); } } }
double RandomGen::gaussian(double mu, double sigma) { if(sigma <= 0.0) { //printf("Sigma<=0.0 in _sta!"); //assert(0); return 0; } double sum = 0.0; for(int i = 1; i <= 12;i++) { sum = sum + getRandFloat(1); } return (sum - 6.00) * sigma + mu; }
Manager::Manager() : env( SDL_putenv(const_cast<char*>("SDL_VIDEO_CENTERED=center")) ), gdata( Gamedata::getInstance() ), io( IOManager::getInstance() ), clock( Clock::getInstance() ), screen( io.getScreen() ), world1(FrameFactory::getInstance().getFrame("back3"), 8), world2(FrameFactory::getInstance().getFrame("back2"), 4), world3(FrameFactory::getInstance().getFrame("back"), 1), viewport( Viewport::getInstance() ), hud(), sound(), player(new TwoWaySprite("plane", 1)), delay(Gamedata::getInstance()->getXmlInt("delay")), numOfEnemies(Gamedata::getInstance()->getXmlInt("numOfEnemies")) { if (SDL_Init(SDL_INIT_VIDEO) != 0) { throw string("Unable to initialize SDL: "); } viewport.setObjectToTrack(player.getMFS()); srand(time(0)); clouds.reserve(Gamedata::getInstance()->getXmlInt("cloud2Num")); pman.reserve(Gamedata::getInstance()->getXmlInt("pmanNum")); int ww = Gamedata::getInstance()->getXmlInt("worldWidth"); int wh = Gamedata::getInstance()->getXmlInt("worldHeight"); int pw = player.getMFS()->getFrame()->getHeight(); int ph = player.getMFS()->getFrame()->getWidth(); for(int i = 0; i < Gamedata::getInstance()->getXmlInt("cloud2Num"); i++){ double scale = static_cast<double>(getRandFloat(5 , 30) / 10.0); clouds.push_back( new ScaledDrawable( new Sprite("cloud2", FrameFactory::getInstance().getFrame("cloud2", scale)), scale) ); } sort(clouds.begin(), clouds.end(), ScaledDrawableFO()); for(int i = 0; i < Gamedata::getInstance()->getXmlInt("pmanNum"); i++){ double scale = static_cast<double>(getRandFloat(5 , 200) / 100.0); pman.push_back( new ScaledDrawable( new OneWaySprite("pman", getRandFloat(0, ww), getRandFloat(0, wh), (rand()%2?1:-1)*getRandFloat( -Gamedata::getInstance()->getXmlInt("pmanSpeedX"), Gamedata::getInstance()->getXmlInt("pmanSpeedX") ), (rand()%2?1:-1)*getRandFloat( -Gamedata::getInstance()->getXmlInt("pmanSpeedY"), Gamedata::getInstance()->getXmlInt("pmanSpeedY") ), scale), scale) ); } sort(pman.begin(), pman.end(), ScaledDrawableFO()); for(int i = 0; i < numOfEnemies; i++){ enemies.push_back(new Enemy( new TwoWaySprite("heli2", getRandFloat(pw + Gamedata::getInstance()->getXmlInt("startRoom"), ww ), getRandFloat(ph + Gamedata::getInstance()->getXmlInt("startRoom"), wh - (Gamedata::getInstance()->getXmlInt("heli2Height") / Gamedata::getInstance()->getXmlInt("heli2YFrames")) ), (rand()%2?1:-1)*getRandFloat( -Gamedata::getInstance()->getXmlInt("heli2SpeedX"), Gamedata::getInstance()->getXmlInt("heli2SpeedX") ), (rand()%2?1:-1)*getRandFloat( -Gamedata::getInstance()->getXmlInt("heli2SpeedY"), Gamedata::getInstance()->getXmlInt("heli2SpeedY") ), 1 ), &player, Gamedata::getInstance()->getXmlFloat("enemyAttackRange"), Gamedata::getInstance()->getXmlFloat("enemyAttackSpeed")) ); } atexit(SDL_Quit); clock.pause(); }