void GameWorld::CreateGame() { // create the environment background_manager_ = BackgroundManager::create(); background_manager_->game_world_ = this; addChild(background_manager_, E_LAYER_BACKGROUND); // create & add the batch node sprite_batch_node_ = CCSpriteBatchNode::create("cjtexset_01.png", 128); game_object_layer_->addChild(sprite_batch_node_); CreateWall(); CreateClown(); CreateBasePlatform(); CreatePlatform(); CreateCollectibles(); CreateHUD(); // enable touch & accelerometer setTouchEnabled(true); setAccelerometerEnabled(true); // everything created, start updating scheduleUpdate(); }
// initialize initializes the general display design coordinator, creates the // primitive sets, textures, objects, lights, sounds, cameras, and text items // void Design::initialize() { // general display design // Reflectivity redish = Reflectivity(red); Reflectivity greenish = Reflectivity(green); Reflectivity bluish = Reflectivity(blue); Reflectivity whitish = Reflectivity(white); setProjection(0.9f, 1.0f, 1000.0f); setAmbientLight(1, 1, 1); // camera at a distance - in lhs coordinates // camera at a distance - in lhs coordinates iCamera* camera = CreateCamera(); camera->translate(0, 190,-500); camera->setRadius(17.8f); lastUpdate = now; hud = CreateHUD(0.72f, 0.01f, 0.27f, 0.99f, CreateTexture(HUD_IMAGE)); // cameras ---------------------------------------------------------------- velocitytxt_=CreateText(Rectf(0.05f,0.27f,0.95f,0.37f),hud,L"",TEXT_HEIGHT,TEXT_TYPEFACE,TEXT_LEFT); deltatxt_=CreateText(Rectf(0.05f,0.17f,0.95f,0.27f),hud,L"",TEXT_HEIGHT,TEXT_TYPEFACE,TEXT_LEFT); positiontxt_=CreateText(Rectf(0.05f,0.38f,0.95f,0.48f),hud,L"",TEXT_HEIGHT,TEXT_TYPEFACE,TEXT_LEFT); lasttextupdate=now; // game ---------------------------------------------------------------------- setBackground(CreateTexture(L"farm.png")); catcher = CreatePhysicsBox(-40, -5, 0, 40, 5, 0, &bluish, 1, PHYS_Floating, true); iAPIWindow* win = getWindow(); catcher->translate(0, -70, 0); truck = CreatePhysicsBox(-100, -2, 0, 100, 2, 0, &redish, 1, PHYS_Floating, true); truck->translate(300, -50, 0); Reflectivity yellowish = Reflectivity(yellow); iPhysics* fallingBox = CreatePhysicsBox(-10, -10, -10, 10, 10, 10, &yellowish, 1, PHYS_Falling, true); fallingBox->translate(-350, 350, 0); fallingBox->setVelocity(Vector(5, 20, 0)); fallingBox->addBodyForce(Vector(0, -10, 0)); fallingBox->setCollision(CreateCSphere(fallingBox, 5)); objects.insert(objects.end(), fallingBox); wchar_t str[MAX_DESC + 1]; StringCbPrintfW(str, MAX_DESC, L"Score: 0"); velocitytxt_->set(str); StringCbPrintfW(str, MAX_DESC, L"Life left: 5"); deltatxt_->set(str); }
// - main ---------------------------------------------------------------------- int main(int argc, char* argv[]) { // Create viewer osgViewer::Viewer viewer; viewer.setUpViewInWindow(0, 0, 1024, 768); // Construct the scene graph, set it as the data to be viewed osg::ref_ptr<osg::Group> sgRoot = LoadModels(); sgRoot->addChild(CreateHUD(1024, 768)); viewer.setSceneData(sgRoot); // Create the OSGUIsh event handler osg::ref_ptr<OSGUIsh::EventHandler> guishEH( new OSGUIsh::EventHandler( 0.0, OSGUIsh::FocusPolicyFactoryMason<OSGUIsh::MouseOverFocusPolicy>(), OSGUIsh::FocusPolicyFactoryMason<OSGUIsh::MouseDownFocusPolicy>())); viewer.addEventHandler(guishEH); // Add an event handler for changing the policies osg::ref_ptr<ChangePolicyEventHandler> focusPolicyEH( new ChangePolicyEventHandler(guishEH)); viewer.addEventHandler(focusPolicyEH); // Adds the node to the event handler, so that it can get events guishEH->addNode(TreeNode); guishEH->addNode(StrawberryNode); guishEH->addNode(FishNode); // Register event handlers guishEH->getSignal(TreeNode, OSGUIsh::EVENT_MOUSE_ENTER) ->connect(&HandleMouseEnter); guishEH->getSignal(StrawberryNode, OSGUIsh::EVENT_MOUSE_ENTER) ->connect(&HandleMouseEnter); guishEH->getSignal(FishNode, OSGUIsh::EVENT_MOUSE_ENTER) ->connect(&HandleMouseEnter); guishEH->getSignal(TreeNode, OSGUIsh::EVENT_MOUSE_LEAVE) ->connect(&HandleMouseLeave); guishEH->getSignal(StrawberryNode, OSGUIsh::EVENT_MOUSE_LEAVE) ->connect(&HandleMouseLeave); guishEH->getSignal(FishNode, OSGUIsh::EVENT_MOUSE_LEAVE) ->connect(&HandleMouseLeave); guishEH->getSignal(TreeNode, OSGUIsh::EVENT_MOUSE_WHEEL_UP) ->connect(&HandleMouseWheelUp); guishEH->getSignal(StrawberryNode, OSGUIsh::EVENT_MOUSE_WHEEL_UP) ->connect(&HandleMouseWheelUp); guishEH->getSignal(FishNode, OSGUIsh::EVENT_MOUSE_WHEEL_UP) ->connect(&HandleMouseWheelUp); guishEH->getSignal(TreeNode, OSGUIsh::EVENT_MOUSE_WHEEL_DOWN) ->connect(&HandleMouseWheelDown); guishEH->getSignal(StrawberryNode, OSGUIsh::EVENT_MOUSE_WHEEL_DOWN) ->connect(&HandleMouseWheelDown); guishEH->getSignal(FishNode, OSGUIsh::EVENT_MOUSE_WHEEL_DOWN) ->connect(&HandleMouseWheelDown); guishEH->getSignal(TreeNode, OSGUIsh::EVENT_KEY_DOWN) ->connect(&HandleKeyDown); guishEH->getSignal(StrawberryNode, OSGUIsh::EVENT_KEY_DOWN) ->connect(&HandleKeyDown); guishEH->getSignal(FishNode, OSGUIsh::EVENT_KEY_DOWN) ->connect(&HandleKeyDown); // Enter rendering loop viewer.run(); }
// initialize initializes the general display design coordinator, creates the // primitive sets, textures, objects, lights, sounds, cameras, and text items // void Design::initialize() { // general parameters // Reflectivity redish = Reflectivity(red); Reflectivity greenish = Reflectivity(green); Reflectivity bluish = Reflectivity(blue); Reflectivity whitish = Reflectivity(white); iGraphic* box; setProjection(0.9f, 1.0f, 1000.0f); setAmbientLight(0.9f, 0.9f, 0.9f); iCamera* camera = CreateCamera(); camera->translate(0,150,0); camera->setRadius(17.8f); camera->rotatex(3.1459/2.0f); int i=0; for(i=0;i<map_.numvert();i++){ box = CreateBox(-3, -3, -3 * MODEL_Z_AXIS, 3, 3, 3 * MODEL_Z_AXIS); CreateObject(box,&redish)->translate(map_.vx(i),map_.vy(i),map_.vz(i)); } for(int j=0;j<map_.numvert();j++){ LList<EdgeInfo>& edgelist=map_.edges(j); Node<EdgeInfo>* curr; while(curr=edgelist.curr()){ int from=curr->data().from(); int to=curr->data().to(); if(from < to){ Vector frompos=map_.pos(from); Vector topos=map_.pos(to); float speed= curr->data().speed(); iGraphic* path=CreatePath(frompos.x,frompos.y,frompos.z,topos.x,topos.y,topos.z); Reflectivity* pathcolour=(speed<FAST)?((speed<MEDIUM)?&redish:&bluish):&greenish; CreateObject(path,pathcolour); #if DEBUG==1 fprintf(debugfile,"from/to: %d %d\n",from,to); fprintf(debugfile,"frompos %f %f %f\n",frompos.x,frompos.y,frompos.z); fprintf(debugfile,"topos %f %f %f\n",topos.x,topos.y,topos.z); fprintf(debugfile,"v %f %f %f\n",v.x,v.y,v.z); fprintf(debugfile,"edglen= %f\n",edgelen); #endif i++; } edgelist.gonext(); } } bucket_=CreatePhysicsBox(-2,-2,-2,2,2,2,&bluish,1,PHYS_FixedInSpace); whichbox_=0; Vector boxloc=map_.pos(whichbox_); bucket_->translate(boxloc.x,boxloc.y+4,boxloc.z); box = CreateBox(-3.1, -3.1, -3.1 * MODEL_Z_AXIS, 3.1, 3.1, 3.1 * MODEL_Z_AXIS); (highlighter_ = CreateObject(box,&greenish))->translate(map_.vx(0),map_.vy(0),map_.vz(0)); selectloc_=0; lastFireTime_=0; ismoving_=false; searchroutine_=GREEDY; hud = CreateHUD(0.72f, 0.01f, 0.27f, 0.99f, CreateTexture(HUD_IMAGE)); velocitytxt_=CreateText(Rectf(0.05f,0.27f,0.95f,0.37f),hud,L"",TEXT_HEIGHT,TEXT_TYPEFACE,TEXT_LEFT); deltatxt_=CreateText(Rectf(0.05f,0.17f,0.95f,0.27f),hud,L"",TEXT_HEIGHT,TEXT_TYPEFACE,TEXT_LEFT); positiontxt_=CreateText(Rectf(0.05f,0.38f,0.95f,0.48f),hud,L"",TEXT_HEIGHT,TEXT_TYPEFACE,TEXT_LEFT); lasttextupdate=now; }