Changeset 4281 for OpenSceneGraph/trunk/examples/osgcatch/osgcatch.cpp
- Timestamp:
- 05/21/05 23:19:11 (8 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/examples/osgcatch/osgcatch.cpp
r4280 r4281 53 53 void reset(); 54 54 55 void resetCatches(); 56 55 57 bool addCatch(); 58 56 59 bool looseLife(); 57 60 … … 222 225 } 223 226 227 void Character::resetCatches() 228 { 229 _numCatches = 0; 230 _catchSwitch->setAllChildrenOff(); 231 } 232 224 233 bool Character::addCatch() 225 234 { … … 234 243 bool Character::looseLife() 235 244 { 236 if (!_livesSwitch || _numLives==0) return true;245 if (!_livesSwitch || _numLives==0) return false; 237 246 238 247 --_numLives; 239 248 _livesSwitch->setValue(_numLives,false); 240 249 241 return (_numLives ==0);250 return (_numLives!=0); 242 251 } 243 252 … … 505 514 float getFOVY() const { return _fovy; } 506 515 507 void setBackground(const std::string& background) { _backgroundImageFile = background; }508 509 516 void createNewCatchable(); 517 518 void resetLevel() 519 { 520 _level = 0; 521 _levelSwitch->setSingleChildOn(_level); 522 } 523 524 void nextLevel() 525 { 526 ++_level; 527 if (_level < _levelSwitch->getNumChildren()) 528 { 529 _levelSwitch->setSingleChildOn(_level); 530 } 531 } 532 533 bool gameComplete() 534 { 535 return _level >= _levelSwitch->getNumChildren(); 536 } 510 537 511 538 enum Players … … 574 601 575 602 float _fovy; 576 577 std::string _backgroundImageFile; 578 603 604 unsigned _level; 605 606 float _chanceOfExplodingAtStart; 607 float _initialNumDropsPerSecond; 608 579 609 osg::ref_ptr<osg::Group> _group; 610 osg::ref_ptr<osg::Switch> _levelSwitch; 580 611 581 612 unsigned int _numberOfPlayers; … … 589 620 590 621 bool _leftKeyPressed; 591 bool _rightKeyPressed; 622 bool _rightKeyPressed; 623 592 624 593 625 }; … … 605 637 _characterSize = _width.length()*0.2f; 606 638 607 _backgroundImageFile = "Catch/farm.JPG";608 609 639 _numberOfPlayers = 0; 640 _level = 0; 641 642 _chanceOfExplodingAtStart = 0.1f; 643 _initialNumDropsPerSecond = 1.0f; 610 644 611 645 _leftKeyPressed=false; 612 646 _rightKeyPressed=false; 613 647 614 _backgroundFiles.push_back("Catch/farm.JPG");615 648 _backgroundFiles.push_back("Catch/sky1.JPG"); 616 649 _backgroundFiles.push_back("Catch/sky2.JPG"); 617 650 _backgroundFiles.push_back("Catch/sky3.JPG"); 651 _backgroundFiles.push_back("Catch/farm.JPG"); 618 652 619 653 _benignCatachables.push_back("Catch/a.png"); … … 669 703 if ((*itr)->anyInside(_players[i].getLowerLeft(),_players[i].getUpperRight())) 670 704 { 671 _players[i].looseLife(); 705 if (!_players[i].looseLife()) 706 { 707 std::cout<<"Ouch you lost all your lives, Game Over!!"<<std::endl; 708 exit(0); 709 } 672 710 removeEntry = true; 673 711 } … … 677 715 if ((*itr)->centerInside(_players[i].getCurrentCenterOfBasket(),_players[i].getCurrentRadiusOfBasket())) 678 716 { 679 _players[i].addCatch(); 717 if (!_players[i].addCatch()) 718 { 719 _players[i].resetCatches(); 720 nextLevel(); 721 if (gameComplete()) 722 { 723 std::cout<<"Congratulations you've completed the game!!"<<std::endl; 724 exit(0); 725 } 726 } 727 728 680 729 removeEntry = true; 681 730 } … … 701 750 itr = _catchableObjects.erase(itr); 702 751 703 createNewCatchable();704 705 752 } 706 753 else if ((*itr)->anyInside(_origin, _origin+_width) && !(*itr)->stopped()) … … 713 760 714 761 } 715 762 763 764 // create new catchable objects 765 static double previousTime = ea.time(); 766 double deltaTime = ea.time()-previousTime; 767 previousTime = ea.time(); 768 769 float numDropsPerSecond = _initialNumDropsPerSecond * (_level+1); 770 float r = (float)rand()/(float)RAND_MAX; 771 if (r < deltaTime*numDropsPerSecond) 772 { 773 createNewCatchable(); 774 } 775 776 777 716 778 } 717 779 case(osgGA::GUIEventAdapter::KEYDOWN): … … 795 857 } 796 858 797 createNewCatchable();798 createNewCatchable();799 createNewCatchable();800 createNewCatchable();801 802 859 // background 803 860 { 804 osg::Image* image = osgDB::readImageFile(_backgroundImageFile); 805 if (image) 806 { 807 osg::Geometry* geometry = osg::createTexturedQuadGeometry(_origin,_width,_height); 808 osg::StateSet* stateset = geometry->getOrCreateStateSet(); 809 stateset->setTextureAttributeAndModes(0,new osg::Texture2D(image),osg::StateAttribute::ON); 810 811 osg::Geode* geode = new osg::Geode; 812 geode->addDrawable(geometry); 813 814 _group->addChild(geode); 815 816 } 861 _levelSwitch = new osg::Switch; 862 863 for(FileList::const_iterator itr = _backgroundFiles.begin(); 864 itr != _backgroundFiles.end(); 865 ++itr) 866 { 867 868 osg::Image* image = osgDB::readImageFile(*itr); 869 if (image) 870 { 871 osg::Geometry* geometry = osg::createTexturedQuadGeometry(_origin,_width,_height); 872 osg::StateSet* stateset = geometry->getOrCreateStateSet(); 873 stateset->setTextureAttributeAndModes(0,new osg::Texture2D(image),osg::StateAttribute::ON); 874 875 osg::Geode* geode = new osg::Geode; 876 geode->addDrawable(geometry); 877 878 _levelSwitch->addChild(geode); 879 } 880 } 881 _levelSwitch->setSingleChildOn(0); 882 _group->addChild(_levelSwitch.get()); 817 883 } 818 884 … … 841 907 _catchableObjects.push_back(catchableObject); 842 908 843 // catchableObject->explode(); 909 float r = (float)rand() / (float)RAND_MAX; 910 if (r < _chanceOfExplodingAtStart) 911 { 912 catchableObject->explode(); 913 } 844 914 845 915 _group->addChild(catchableObject->_object.get()); … … 872 942 viewer.getEventHandlerList().push_front(seh); 873 943 874 std::string filename;875 if (arguments.read("-b",filename))876 {877 seh->setBackground(filename);878 }879 880 881 944 while (arguments.read("--boy")) seh->addPlayer(GameEventHandler::PLAYER_BOY); 882 945 while (arguments.read("--girl")) seh->addPlayer(GameEventHandler::PLAYER_GIRL);
