bool NickScene::onInitialize() { Vector3 nodeCenter = Vector3(0.0f, 0.0f, 0.0f); //----------------------------------------------------------------- // create the initial platform which has a button that user can hit // to turn the globe on //----------------------------------------------------------------- m_graph->pushNode(); { nodeCenter = Vector3(0.0f, 100.0f, 300.0f); //start position Waypoint this is where the player starts/resets m_WPstart = new Start(this, "Objects/goal.obj", 0.125f); if ( !m_WPstart->initialize(new LightShader(0.2f, 1.0, 8, RAND_COLOR))) { GLOG("ERROR: could not create OBJmodel\n"); return false; } m_WPstart->setPosition(nodeCenter + Vector3(0.0f, 0.0f, 10.0f)); m_WPstart->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(m_WPstart); // create a platform Platform *platform = new Platform(this, Vector3(40.0f, 1.0f, 40.0f)); if ( !platform->initialize(new LightShader(0.2f, 0.8f, 8, RAND_COLOR))) { GLOG("ERROR: Could not initialize initial platform\n"); return false; } platform->setPosition(nodeCenter + Vector3(0.0f, -0.5f, 0.0f)); platform->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(platform); } m_graph->popNode(); //----------------------------------------------------------------- // create long platform that has cannons shooting at it //----------------------------------------------------------------- m_graph->pushNode(); { nodeCenter += Vector3(0.0f, -30.0f, -130.0f); // create a platform Platform *platform = new Platform(this, Vector3(10.0f, 1.0f, 220.0f)); if ( !platform->initialize(new LightShader(0.3f, 1.0f, 8, RAND_LIGHT_COLOR))) { GLOG("WARNING: Could not initialize cannon platform\n"); } platform->setPosition(nodeCenter + Vector3(0.0f, 0.0f, 0.0f)); platform->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(platform); bool rightSide = false; for (int i = 0; i < 10; i++) { float cannonZ = (platform->getSize().z * 0.5f) - 50.0f - (i * 15.0f); float fireTime = 0.4f + RANDF(1.0f); // add a cannon aiming at the platform Cannon *cannon = new Cannon(this, Vector3(3.0f, 3.0f, 6.0f), 120.0f, fireTime); if (!cannon->initialize(new LightShader(0.1f, 1.0f, 8, Color(0.1f, 0.8f, 0.2f)))) { GLOG("WARNING: could not initialize first cannon\n"); } float cannonX = rightSide ? 20.0f : -20.0f; cannon->setPosition(nodeCenter + Vector3(cannonX, 10.0f, cannonZ)); float yaw = rightSide ? 90.0f : -90.0f; cannon->setOrientation(yaw, -20.0f, 0.0f); cannon->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(cannon); rightSide = !rightSide; m_cannons.push_back(cannon); } // create a button that will stop the cannons NickButton *stop_cannon_button = new NickButton(this, &NickScene::stopCannonsHit, Vector3(8.0f, 0.5f, 4.0f)); if (!stop_cannon_button->initialize(new LightShader(0.3f, 1.0f, 8, RAND_COLOR))) { GLOG("ERROR: could not create stop_cannon_button\n"); } stop_cannon_button->setPosition(nodeCenter + Vector3(0.0f, stop_cannon_button->getSize().y, -105.0f)); stop_cannon_button->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(stop_cannon_button); } m_graph->popNode(); //----------------------------------------------------------------- // create platform after the cannon platform, which just has cube pyramid // that covers a gap to the next platform //----------------------------------------------------------------- m_graph->pushNode(); { nodeCenter += Vector3(0.0f, 0.0f, -120.0f); // create a checkpoint now that user has gotten through cannons NickCheckPoint *cp = new NickCheckPoint(this, "Objects/goal.obj", 0.125f); if ( !cp->initialize(new LightShader(0.2f, 1.0, 8, RAND_COLOR))) { GLOG("ERROR: could not create OBJmodel\n"); return false; } cp->setPosition(nodeCenter + Vector3(8.0f, 0.5f, 0.0f)); m_allCheckpoints.push_back(cp); cp->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(cp); cp->attachParticleSystem(new WaypointParticleSystem(200)); // create a platform Platform *platform = new Platform(this, Vector3(50.0f, 1.0f, 12.0f)); if ( !platform->initialize(new LightShader(0.2f, 0.8f, 8, RAND_COLOR))) { GLOG("ERROR: Could not initialize gap platform\n"); return false; } platform->setPosition(nodeCenter + Vector3(25.0f, -0.5f, 0.0f)); platform->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(platform); // cube pyramid right before gap m_graph->pushNode(); Vector3 cubeSize = Vector3(1.5f, 1.5f, 0.5f); Vector3 startPoint = nodeCenter + Vector3(49.0f, cubeSize.y / 2.0f, -5.0f); CubeGroups::createCubePyramid(this, 7, startPoint, cubeSize, 0.1f, 90.0f); m_graph->popNode(); // create a second platform after gap platform = new Platform(this, Vector3(20.0f, 1.0f, 10.0f)); if ( !platform->initialize(new LightShader(0.2f, 0.8f, 8, RAND_COLOR))) { GLOG("ERROR: Could not initialize 2nd gap platform\n"); return false; } platform->setPosition(nodeCenter + Vector3(65.0f, -0.5f, 0.0f)); platform->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(platform); } m_graph->popNode(); // create a conveyor to take player up to next section Conveyor *conveyor1 = new Conveyor(this, Vector3(10.0f, 1.0f, 80.0f), 2.5f ); if ( !conveyor1->initialize(new LightTexScrollShader(0.3f, 1.0f, 4, Color(1.0f, 1.0f, 1.0f), Vector3(0.0f, -1.0f, 0.0f), conveyor1->getSpeed() * 0.5f, 3.0f))) { GLOG("ERROR: Could not initialize conveyor1\n"); return false; } conveyor1->setPosition(nodeCenter + Vector3(85.0f, 2.0f, -35.0f)); conveyor1->setOrientation(180.0f, 5.0f, 0.0f); conveyor1->loadTexture("Textures/conveyor.tga"); conveyor1->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(conveyor1); //----------------------------------------------------------------- // create seemingly random small platform that user has to jump between //----------------------------------------------------------------- m_graph->pushNode(); { nodeCenter += Vector3(85.0f, 5.0f, -85.0f); float lastZ = 0.0f; for (int i = 0; i < 20; i++) { Vector3 position = Vector3(-10.0f + RANDF(20.0f), -2.0f + RANDF(4.0f), lastZ - (5.0f + RANDF(5.0f))); Platform *platform = new Platform(this, Vector3(4.0f + RANDF(5.0f), 1.0f + RANDF(2.0f), 4.0f + RANDF(5.0f))); if ( !platform->initialize(new LightShader(0.3f, 0.8f, 8, RAND_COLOR))) { GLOG("ERROR: Could not initialize sparse platform\n"); return false; } platform->setPosition(nodeCenter + position); platform->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(platform); lastZ = position.z - platform->getSize().z * 0.5f; } nodeCenter.z += lastZ - 10.0f; // larger platform at the end of the small platforms Platform *platform = new Platform(this, Vector3(15.0f, 1.0f, 15.0f)); if ( !platform->initialize(new LightShader(0.3f, 0.8f, 8, RAND_COLOR))) { GLOG("ERROR: Could not initialize post-sparse platform\n"); return false; } platform->setPosition(nodeCenter + Vector3(0.0f, 0.0f, 0.0f)); platform->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(platform); // create a checkpoint now that user has gotten through sparse platforms NickCheckPoint *cp = new NickCheckPoint(this, "Objects/goal.obj", 0.125f); if ( !cp->initialize(new LightShader(0.2f, 1.0, 8, RAND_COLOR))) { GLOG("ERROR: could not create OBJmodel\n"); return false; } cp->setPosition(nodeCenter + Vector3(0.0f, 0.5f, 0.0f)); m_allCheckpoints.push_back(cp); cp->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(cp); cp->attachParticleSystem(new WaypointParticleSystem(200)); } m_graph->popNode(); //----------------------------------------------------------------- // create a slanted platform that drops down onto a large trampoline //----------------------------------------------------------------- m_graph->pushNode(); { nodeCenter += Vector3(-25.0f, -5.0f, -10); // angled platform down to trampoline Platform *platform = new Platform(this, Vector3(30.0f, 1.0f, 15.0f)); if ( !platform->initialize(new LightShader(0.3f, 0.8f, 8, RAND_COLOR))) { GLOG("ERROR: Could not initialize slanted1 platform\n"); return false; } platform->setPosition(nodeCenter + Vector3(0.0f, 0.0f, 0.0f)); platform->setOrientation(0.0f, 0.0f, -20.0f); platform->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(platform); // create large trampoline to bounce off Trampoline *tramp = new Trampoline(this, 80.0f, 50.0f); if ( !tramp->initialize(new TrampolineShader()) ) { GLOG("ERROR: Could not initialize large trampoline\n"); return false; } tramp->setPosition(nodeCenter + Vector3(-30.0f, -75.0f, 0.0f)); tramp->loadTexture("Textures/trampoline.tga"); tramp->addToDynamicWorld(m_dynamicsWorld); registerForSimulationTicks(tramp); m_graph->attach(tramp); // angled trampoline up on other side of trampoline platform = new Platform(this, Vector3(30.0f, 1.0f, 15.0f)); if ( !platform->initialize(new LightShader(0.3f, 0.8f, 8, RAND_COLOR))) { GLOG("ERROR: Could not initialize slanted2 platform\n"); return false; } platform->setPosition(nodeCenter + Vector3(-70.0f, -15.0f, 0.0f)); platform->setOrientation(0.0f, 0.0f, 20.0f); platform->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(platform); } m_graph->popNode(); //----------------------------------------------------------------- // create a row of trampolines that takes you towards the globe //----------------------------------------------------------------- m_graph->pushNode(); { nodeCenter += Vector3(-105.0f, -10.0f, -20.0f); // platform before trampolines Platform *platform = new Platform(this, Vector3(20.0f, 1.0f, 50.0f)); if ( !platform->initialize(new LightShader(0.3f, 0.8f, 8, RAND_COLOR))) { GLOG("ERROR: Could not initialize pre-tramp platform\n"); return false; } platform->setPosition(nodeCenter + Vector3(0.0f, 0.0f, 0.0f)); platform->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(platform); nodeCenter += Vector3(0.0f, 0.0f, -15.0f); // create 4 trampolines in a line at alternating angles float sign = -1.0f; for (int i = 0; i < 4; i++) { // first trampoline Trampoline *tramp = new Trampoline(this, 10.0f, 15.0f); if ( !tramp->initialize(new TrampolineShader()) ) { GLOG("ERROR: Could not initialize an angled tramp\n"); return false; } tramp->setPosition(nodeCenter + Vector3(sign * 6.0, 0.0f, -30.0f + (i * -15.0f))); sign *= -1.0f; tramp->setRoll(sign * 30.0f); tramp->loadTexture("Textures/trampoline.tga"); tramp->addToDynamicWorld(m_dynamicsWorld); registerForSimulationTicks(tramp); m_graph->attach(tramp); } // platform after trampolines platform = new Platform(this, Vector3(50.0f, 1.0f, 20.0f)); if ( !platform->initialize(new LightShader(0.3f, 0.8f, 8, RAND_COLOR))) { GLOG("ERROR: Could not initialize after tramp platform\n"); return false; } platform->setPosition(nodeCenter + Vector3(-25.0f, 0.0f, -100.0f)); platform->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(platform); // create a checkpoint now that user has gotten through trampolines NickCheckPoint *cp = new NickCheckPoint(this, "Objects/goal.obj", 0.125f); if ( !cp->initialize(new LightShader(0.1f, 1.0, 8, RAND_COLOR))) { GLOG("ERROR: could not create OBJmodel\n"); return false; } cp->setPosition(nodeCenter + Vector3(-10.0f, 1.0f, -100.0f)); m_allCheckpoints.push_back(cp); cp->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(cp); cp->attachParticleSystem(new WaypointParticleSystem(200)); } m_graph->popNode(); //----------------------------------------------------------------- // create a row of trampolines that takes you towards the globe //----------------------------------------------------------------- m_graph->pushNode(); { nodeCenter += Vector3(-50.0f, 0.0f, -170.0f); // create a conveyor to take player past fire breathers Conveyor *conveyor1 = new Conveyor(this, Vector3(15.0f, 1.0f, 100.0f), 2.5f ); if ( !conveyor1->initialize(new LightTexScrollShader(0.3f, 1.0f, 4, Color(1.0f, 1.0f, 1.0f), Vector3(0.0f, -1.0f, 0.0f), conveyor1->getSpeed() * 0.5f, 3.0f))) { GLOG("ERROR: Could not initialize conveyor1\n"); return false; } conveyor1->setPosition(nodeCenter + Vector3(0.0f, 0.0f, 0.0f)); conveyor1->setOrientation(180.0f, 0.0f, 0.0f); conveyor1->loadTexture("Textures/conveyor.tga"); conveyor1->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(conveyor1); // create a fire breathing hydralisk FireBreather *fireBreather = new FireBreather(this, 15.0f, 5.0f, 2.0f); if ( !fireBreather->initialize(new LightTexShader(0.2f, 1.0, 8, Color(1.0f, 1.0f, 1.0f)))) { GLOG("ERROR: could not create fire breather\n"); return false; } fireBreather->setPosition(nodeCenter + Vector3(-20.0f, 5.0f, 15.0f)); fireBreather->setOrientation(-90.0f, 0.0f, 0.0f); fireBreather->loadTexture("Textures/hydralisk.tga"); fireBreather->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(fireBreather); fireBreather->attachParticleSystem(new FireParticleSystem(500)); // create a fire breathing hydralisk fireBreather = new FireBreather(this, 15.0f, 5.0f, 2.0f); if ( !fireBreather->initialize(new LightTexShader(0.2f, 1.0, 8, Color(1.0f, 1.0f, 1.0f)))) { GLOG("ERROR: could not create fire breather\n"); return false; } fireBreather->setPosition(nodeCenter + Vector3(20.0f, 5.0f, -10.0f)); fireBreather->setOrientation(90.0f, 0.0f, 0.0f); fireBreather->loadTexture("Textures/hydralisk.tga"); fireBreather->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(fireBreather); fireBreather->attachParticleSystem(new FireParticleSystem(500)); } m_graph->popNode(); //----------------------------------------------------------------- // create platform that has cannon which shoots globe //----------------------------------------------------------------- m_graph->pushNode(); { nodeCenter += Vector3(20.0f, 0.0f, -65.0f); // platform after fire breathers Platform *platform = new Platform(this, Vector3(15.0f, 1.0f, 30.0f)); if ( !platform->initialize(new LightShader(0.3f, 0.8f, 8, RAND_COLOR))) { GLOG("ERROR: Could not initialize fire platform\n"); return false; } platform->setPosition(nodeCenter + Vector3(0.0f, 0.0f, 0.0f)); platform->setOrientation(30.0f, 0.0f, 0.0f); platform->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(platform); // create a button that will shoot the cannon at globe NickButton *globe_shoot_button = new NickButton(this, &NickScene::shootGlobeHit, Vector3(10.0f, 0.5f, 6.0f)); if (!globe_shoot_button->initialize(new LightShader(0.3f, 1.0f, 8, RAND_COLOR))) { GLOG("ERROR: could not create globe_button\n"); return false; } globe_shoot_button->setPosition(nodeCenter + Vector3(5.0f, globe_shoot_button->getSize().y + 0.25f, -8.0f)); globe_shoot_button->setOrientation(30.0f, 0.0f, 0.0f); globe_shoot_button->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(globe_shoot_button); m_globeCannon = new Cannon(this, Vector3(5.0f, 5.0f, 10.0f), 80.0f); if (!m_globeCannon->initialize(new LightShader(0.1f, 1.0f, 8, Color(0.1f, 0.8f, 0.2f)))) { GLOG("WARNING: could not initialize first cannon\n"); } m_globeCannon->setPosition(nodeCenter + Vector3(-0.0f, 2.0f, -40.0f)); m_globeCannon->setOrientation(260.0f, 0.0f, 0.0f); m_globeCannon->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(m_globeCannon); } m_graph->popNode(); //----------------------------------------------------------------- // create the ending platform that has the globe and waypoint on it //----------------------------------------------------------------- m_graph->pushNode(); { nodeCenter = Vector3(0.0f, 30.0f, -600.0f); //// create a platform Platform *platform = new Platform(this, Vector3(70.0f, 2.0f, 70.0f)); if ( !platform->initialize(new LightShader(0.2f, 1.0f, 4, RAND_COLOR))) { GLOG("ERROR: Could not initialize ending platform\n"); return false; } platform->setPosition(nodeCenter + Vector3(0.0f, 0.0f, 0.0f)); platform->addToDynamicWorld(m_dynamicsWorld); m_graph->attach(platform); // create a fracture globe that explodes Sphere *globe = new Sphere(this, 20.0f); if ( !globe->initialize(new LightTexScrollShader(1.0f, 0.2f, 2, Color(1.0f, 1.0f, 1.0f, 1.0f), Vector3(1.0f, 0.0f, 0.0f), 0.001f))) { GLOG("ERROR: could not initialize globe.\n"); return false; } globe->setPosition(nodeCenter + Vector3(0.0f, 30.0f, 0.0f)); globe->setOrientation(0.0f, -90.0f, 0.0f); globe->loadTexture("Textures/earth.tga"); m_globeFracture = new FractureObject(globe, "Textures/voronoi_fracure.tga", 80.0f); if ( !m_globeFracture->addToScene(this) ) { GLOG("ERROR: could not add globe fracture to scene.\n"); return false; } } m_graph->popNode(); // create a particle system ParticleSystem *m_particleSystem = new StarryParticleSystem(2000); m_particleSystem->startSystem(); m_graph->attach(m_particleSystem); m_playerParticleSystem = new PlayerParticleSystem(300); m_playerParticleSystem->startSystem(); m_graph->attach(m_playerParticleSystem); // create the player and return result return resetPlayer(); }
bool MainClass::update() { if (m_isHelpOpened && ( Engine::instance().getJoystickMgr().isPressed(JoystickManager::PLAYER_ONE, JoystickManager::BUTTON_X) || Engine::instance().getJoystickMgr().isPressed(JoystickManager::PLAYER_ONE, JoystickManager::BUTTON_START) || Engine::instance().getJoystickMgr().isPressed(JoystickManager::PLAYER_TWO, JoystickManager::BUTTON_X) || Engine::instance().getJoystickMgr().isPressed(JoystickManager::PLAYER_TWO, JoystickManager::BUTTON_START))) { m_isHelpOpened = false; } iterateList(m_clouds, Cloud*) { (*it)->manage(); } if (!m_burning->isGameOver() && !m_isHelpOpened) { m_chara->manage(m_collisionImage, m_waterSystem); m_cannon->manage(m_swarm, *m_engineSystem, m_burning->hasCannonDamage()); m_burning->manage(m_cannon->getCollisionPos()); m_engineSystem->manage(m_burning); std::list<WaterDrop*>& waterDrops = m_waterSystem->manage(); iterateList(waterDrops, WaterDrop*) { m_burning->checkCollisions(*it); }
// Camera Motion (called on special key pressed) void specialkeyboard(int key, int x, int y) { switch(key) { case GLUT_KEY_UP : // Camera up cannon1.MoveForward(); break; case GLUT_KEY_DOWN : // Camera down cannon1.MoveBackward(); break; case GLUT_KEY_LEFT : // Camera left cannon1.TurnLeft(); break; case GLUT_KEY_RIGHT : // Camera right cannon1.TurnRight(); break; //Exit from the program, by pressing Esc key. case 27: exit(0); break; } glutPostRedisplay(); }
void MenuBuyCannons::update(sf::Event const &event) { m_lightCannon.update(event); m_heavyCannon.update(event); if (m_lightCannon.getValue() != 0) { // BUY LIGHT CANNONBALLS if (m_netLight == 0) { m_netLight = this->request(0, BUY_LIGHT_CANNONS, m_lightCannon.getValue()); } else if (m_netLight == 1) { m_netLight = this->request(1, BUY_LIGHT_CANNONS, m_lightCannon.getValue()); } else if (m_netLight == 2) { *m_packet >> m_ammo >> m_golds; Cannon c; c.setType(LIGHT_CANNON_TYPE); c.setCannon(m_ammo); m_ship->addCannon(c); m_ship->setGolds(m_golds); //std::cout << "Ammo: " << m_ammo << " Golds: " << m_ship->getGolds() << std::endl; m_lightCannon.resetValue(); m_packet->clear(); m_netLight = 0; } }
Cannon* Cannon::create(CannonType type) { Cannon* cannon = new Cannon(); cannon->init(type); cannon->autorelease(); return cannon; }
void Map::addCannon(int entityID, float x, float y, float angle, float xVel, float yVel, float size) { if (getCannon(entityID) == NULL) { Cannon* c = new Cannon(entityID, this, size); physicsEngine->makeBodyForCannon(c, x, y, angle, size); cannons.insert(std::pair<int,Cannon*>(entityID, c)); //std::cout << "Vel " << xVel << " , " << yVel << std::endl; c->getPhysicsBody()->ApplyForce(b2Vec2(xVel,yVel), c->getPhysicsBody()->GetWorldCenter(), true); } }
void Map::removeCannon(int entityID) { Cannon* c = NULL; if (cannons.find(entityID) != cannons.end()) { c = cannons.find(entityID)->second; } if (c != NULL) { cannons.erase(cannons.find(entityID)); physicsEngine->removeBody(c->getPhysicsBody()); //std::cout << cannons[entityID] << std::endl; } }
Cannon* Cannon::createWithCannonType(GameLayer *pGameLayer, cocos2d::CCSpriteBatchNode *pBatchNode,CCSprite* pCannonSprite){ Cannon* pCannon = new Cannon(pGameLayer,pBatchNode,pCannonSprite); if (pCannon == NULL) { CC_SAFE_DELETE(pCannon); return NULL; } if(pCannon->initWithCannonType(pCannon->m_nCannonType)){ pCannon->autorelease();//放入内存池管理,调用方需要使用retain操作,才能避免被释放,参考[C1] } return pCannon; }
// アクションの実行 void PlayerShootAction::execute( Node* contactNode, LiquidFunFixture* fixture ) { // やりたくないけど。 Cannon* cannon { dynamic_cast< Cannon* >( contactNode ) }; if ( !cannon ) { return; } // 発射の準備をする。 ready( cannon->getPosition() ); // 発射時に呼び出されるコールバック関数を登録する。 cannon->registerShootListener( CC_CALLBACK_1( PlayerShootAction::onShoot, this ) ); }
void Map::draw(sf::RenderTarget* window) { for (int i = 0; i < blocks.size(); i++) { Block* b = blocks[i]; b->draw(window); } for( std::map<int, Cannon*>::iterator ii=cannons.begin(); ii!=cannons.end(); ++ii) { Cannon* c = ii->second; //if (c != NULL) { c->draw(window); //} } for( std::map<int, Player*>::iterator ii=players.begin(); ii!=players.end(); ++ii) { Player* p = ii->second; //if (p != NULL) { p->draw(window); //} } }
void init(void) { /*Set the light, just help to modify the cannon model. */ glClearColor(0.4, 0.4, 0.5, 0.0); glShadeModel(GL_SMOOTH); glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); glEnable(GL_QUAD_STRIP); glEnable(GL_LIGHTING); // Enable material properties for lighting glEnable ( GL_COLOR_MATERIAL ); glColorMaterial ( GL_FRONT, GL_AMBIENT_AND_DIFFUSE ); glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); //glEnable(GL_BLEND); //Enable alpha blending //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //Set the blend function glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //spotlight //glLightfv(GL_LIGHT0, GL_SPECULAR, white_light); //glLightfv(GL_LIGHT0, GL_POSITION, spotlightpoz); //directional light glLightfv(GL_LIGHT1, GL_AMBIENT, ambient); glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse); glLightfv(GL_LIGHT1, GL_SPECULAR, white_light); glLightfv(GL_LIGHT1, GL_POSITION, diractionpoz); map.init(); target.init(); cannon1.textureInit(); cannon1.glCreateParticles(); }
void idle() { for (int i = 0; i < allBullets.size(); i++) { allBullets.at(i).shoot(); } cannon1.glUpdateParticles(); glutPostRedisplay(); }
void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //light control if(light0 == OFF) glEnable(GL_LIGHT0); else glDisable(GL_LIGHT0); if(light1 == ON) glEnable(GL_LIGHT1); else glDisable(GL_LIGHT1); if(spotlight == ON) { glEnable(GL_LIGHT2);glEnable(GL_LIGHT3);glEnable(GL_LIGHT4);glEnable(GL_LIGHT5);glEnable(GL_LIGHT6); glEnable(GL_LIGHT7);} else { glDisable(GL_LIGHT2);glDisable(GL_LIGHT3);glDisable(GL_LIGHT4);glDisable(GL_LIGHT5);glDisable(GL_LIGHT6); glDisable(GL_LIGHT7); } if (fog == ON) glEnable(GL_FOG); else glDisable(GL_FOG); GLfloat fogColor[] = {0.3f, 0.3f, 0.3f, 1}; //Sets the color for the fog glFogfv(GL_FOG_COLOR, fogColor); //Modes for the fog are linear, exp and exp2 glFogi(GL_FOG_MODE, GL_LINEAR); // Linear interpolation between start and end values glFogi(GL_FOG_MODE, GL_EXP); // y = 1 - e-0.05x ; x is distance from camera glFogi(GL_FOG_MODE, GL_EXP2); // y = 1 - e-(0.05x)^2 ; x is distance from camera glFogf(GL_FOG_DENSITY, 0.013f); //camera control if (camera == true) { gluLookAt (eyex, eyey, eyez, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glRotatef(cameraX, 1, 0, 0); glRotatef(cameraY, 0, 1, 0); } else { //gluLookAt (-cos(cannon1.turn*PI/180)+12, cannon1.CannonPoz.y+3, sin(cannon1.turn*PI/180), cannon1.CannonPoz.x, cannon1.CannonPoz.y, cannon1.CannonPoz.z, 0.0, 1.0, 0.0); gluLookAt(cannon1.CannonPoz.x+12, 3, cannon1.CannonPoz.z, cannon1.CannonPoz.x, cannon1.CannonPoz.y, cannon1.CannonPoz.z, 0, 1, 0); } //display cannon if (spotlightChange==false) { glLightf(GL_LIGHT0, GL_SPOT_CUTOFF,50.0); // set cutoff angle glLightf(GL_LIGHT0, GL_SPOT_EXPONENT,4.0); // set focusing strength } else { glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotlightdirN); glLightf(GL_LIGHT0, GL_SPOT_CUTOFF,40.0); // set cutoff angle glLightf(GL_LIGHT0, GL_SPOT_EXPONENT,15.0); // set focusing strength } for (int i = 0; i < allBullets.size(); ++i) { allBullets.at(i).Draw_Bullet(); target.collision(allBullets.at(i).BulletPoz); } cannon1.Draw_Cannon(); target.Draw_Target(); target.Response(); if( target.gamestatus==WIN ) { glLoadIdentity(); glMatrixMode(GL_MODELVIEW); angle=60; glViewport(0, 0, width, height); gluLookAt (0, 5, 6.5, 0.0, 5.0, 1.0, 0.0, 1.0, 0.0); glPushMatrix(); glRotatef(90, 1, 0, 0); map.win(); glPopMatrix(); } glDisable(GL_COLOR_MATERIAL); map.ground(); map.sky(); map.wall(); glutSwapBuffers(); }
void keyboard(unsigned char key, int x, int y) { switch (key) { //Wire frame or Shaded Model case 'w': case 'W': WireFrame = 1-WireFrame; if (WireFrame) glPolygonMode (GL_FRONT_AND_BACK, GL_FILL); // Solid else glPolygonMode (GL_FRONT_AND_BACK, GL_LINE); // Wireframes break; //Camera forward case 'f': //glMatrixMode(GL_MODELVIEW); glLoadIdentity(); eyez -= 1.5f; gluLookAt(eyex, eyey, eyez, 0, 0, 0, 0, 1, 0); break; //Camera backward case 'b': glLoadIdentity(); eyez += 1.5f; gluLookAt(eyex, eyey, eyez, 0, 0, 0, 0, 1, 0); break; //Zoom in case 'z': angle -=2.0; default_reshape(width, height); break; //Zoom out case 'Z': angle +=2.0; default_reshape(width, height); break; //Perspective View case 'p': case 'P': mode = PERSPECTIVE; screen_reshape(width, height); break; //Orthographic View case 'o': case 'O': mode = ORTHO; screen_reshape(width, height); break; //fog control case '0': fog = !fog; break; //Light control case '1': light0 = !light0; //Cannnon spot light light1 = !light1; //sun light spotlight = !spotlight; //Target spot light map.skyChange = !map.skyChange; //Change to night sky break; //camera control case '2': camera = !camera; break; case '3': spotlightChange = !spotlightChange; break; //animation control case ' ': allBullets.push_back(Bullet(cannon1.CannonPoz,cannon1.turn,cannon1.beta)); break; //Gun control case '[': cannon1.Gun(UP); break; //Gun control case ']': cannon1.Gun(DOWN); break; //smoke control case 's': //smoke = !smoke; break; case '/': glutFullScreen(); break; case 'i': cameraX += 5.0f; break; case 'k': cameraX -= 5.0f; break; case 'j': cameraY -= 10.0f; break; case 'l': cameraY += 10.0f; break; //Clear viewing parameter case 'c': case 'C': cameraX = 0.0f; cameraY = 0.0f; cameraZ = 0.0f; eyex = 0.0f; eyey = 10.0f; eyez = 35.0f; angle = 50.0f; default_reshape(width, height); break; //Exit from the program, by pressing "Esc" key. case 27: exit(0); break; } glutPostRedisplay(); }
GameState::GameState( Scene* scene, SimpleVertexShader* vertexShader, SimplePixelShader* pixelShader ) : State( scene ) { addGameObject( *_scene->camera() ); _vertexShader = vertexShader; _pixelShader = pixelShader; DXInfo& dx = DXInfo::getInstance(); _toonLightingTexture = Texture::createTexture( L"../Assets/toonlighting.png", dx.device, dx.deviceContext ); Texture* whiteTexture = Texture::createTexture( L"../Assets/white.png", dx.device, dx.deviceContext ); whiteTexture->addTex( _toonLightingTexture->resourceViews[0] ); Texture* toothTexture = Texture::createTexture( L"../Assets/toothCloud.png", dx.device, dx.deviceContext ); toothTexture->addTex( _toonLightingTexture->resourceViews[0] ); Texture* cannonTexture = Texture::createTexture( L"../Assets/cannon_texture.png", dx.device, dx.deviceContext ); cannonTexture->addTex( _toonLightingTexture->resourceViews[0] ); Texture* bulletTexture = Texture::createTexture( L"../Assets/bullet_texture.png", dx.device, dx.deviceContext ); bulletTexture->addTex( _toonLightingTexture->resourceViews[0] ); Texture* shipTexture = Texture::createTexture( L"../Assets/shipDiffuse.png", dx.device, dx.deviceContext ); shipTexture->addTex( _toonLightingTexture->resourceViews[0] ); Material* cannonMaterial = Material::createMaterial( L"cannon", cannonTexture, vertexShader, pixelShader, _scene->camera() ); Material* bulletMaterial = Material::createMaterial( L"bullet", bulletTexture, vertexShader, pixelShader, _scene->camera() ); Material* shipMaterial = Material::createMaterial( L"ship", shipTexture, vertexShader, pixelShader, _scene->camera() ); Material* material2 = Material::createMaterial( L"material2", toothTexture, vertexShader, Shader::getShader<SimplePixelShader>( L"ToothPixel" ), _scene->camera() ); Material* material3 = Material::createMaterial( L"material3", whiteTexture, vertexShader, pixelShader, _scene->camera() ); Mesh* shipMesh = Mesh::createMesh( "../Assets/ship.obj" ); Mesh* cubeMesh = Mesh::createMesh( "../Assets/cube.obj" ); Mesh* cannonMesh = Mesh::createMesh( "../Assets/cannon.obj" ); Mesh* bulletMesh = Mesh::createMesh( "../Assets/bullet.obj" ); audioManager = new AudioManager(); audioManager->playFile(_TEXT("../Assets/Music.wav"), 5.0, true); ship = new Ship(shipMesh, shipMaterial, audioManager); ship->transform.rotate( 3.1415f * -.5f, vec3( 0, 0, -1 ) ); ship->transform.position( vec3( -55, 0, 0 ) ); CollisionManager::getInstance().addObject( ship ); vec3 camPos = ship->transform.position(); camPos.z -= 5.f; cannons = std::vector<Cannon*>(); ( *_scene->camera() )->transform.position( camPos ); for( int i = 0; i < 4; i++ ) { for( int j = 0; j < 4; j++ ) { Cannon* cannon = new Cannon( cannonMesh, cannonMaterial, bulletMesh, bulletMaterial, this, ship ); vec3 pos = vec3( -45 + i * 90.f / 3, -45 + j * 90.f / 3, .5f ); cannon->transform.rotate( 3.1415f * -.5f, vec3( -1, 0, 0 ) ); cannon->transform.position( pos ); cannon->transform.scale( vec3( 2, 1, 2 ) ); cannon->spawnBullets( 4 ); cannons.push_back( cannon ); CollisionManager::getInstance().addObject( cannon ); } } ColliderObject* cube = new ColliderObject( cubeMesh, material3 ); cube->rigidBody().floating( true ); cube->transform.position( vec3( 0, 0, 2 ) ); cube->transform.scale( vec3( 100, 100, 1 ) ); cube->rigidBody().solid( 0 ); ToothFront* tooth = new ToothFront( cubeMesh, material2, _scene->camera(), dx.device ); tooth->transform.position( vec3( 0, 0, -1 ) ); tooth->transform.scale( vec3( 100, 100, 1 ) ); tooth->ship = ship; text = new UIText(); addGameObject( ship ); for( int i = 0; i < (int)cannons.size(); i++ ) { addGameObject( cannons[i] ); } addGameObject( cube ); addGameObject( tooth ); addGameObject( text ); score = 0; }
int main() { //setting alien position float alienposx = 100; float alienposy = 100; float aliensdiffx = 100; float aliensdiffy = 50; int downMovement = 0; int score = 0, died = 0; sf::Text scoreText; sf::Clock clock; //player bullet std::vector<Bullet> bullet; //alien bullet std::list<Bullet> bulletListEnemy; //Font for text sf::Font font; if (!font.loadFromFile("space.ttf")) { // error... } //initialize score text scoreText.setFont(font); scoreText.setCharacterSize(24); scoreText.setColor(sf::Color::Green); scoreText.setStyle(sf::Text::Bold); //initialize game window sf::RenderWindow window(sf::VideoMode(1024, 786), "Space Invader EA Native"); //creating cannon Cannon *cannon = new Cannon(); cannon->LoadTexture("Cannon.png"); cannon->setPosition((1024 / 2) - 100, 700); cannon->setScale(0.5, 0.25); Aliens *aliens[5][11]; //generating aliens on the display for (int i = 0; i < 5; i++) { for (int j = 0; j < 11; j++) { aliens[i][j] = new Aliens(); switch (i) { case 0: aliens[i][j]->LoadTexture("AlienType0.png"); break; case 1: aliens[i][j]->LoadTexture("AlienType1.png"); break; case 2: aliens[i][j]->LoadTexture("AlienType1.png"); break; case 3: aliens[i][j]->LoadTexture("AlienType2.png"); break; case 4: aliens[i][j]->LoadTexture("AlienType2.png"); break; } aliens[i][j]->setPosition((alienposx + aliensdiffx), alienposy); aliens[i][j]->setScale(0.40, 0.40); alienposx += 60; } alienposx = 100; alienposy += 60; } //====================================================================================================== //act as game loop while (window.isOpen()) { sf::Time elasped = clock.getElapsedTime(); if (elasped.asSeconds() >2) { int col = rand() % 11; int row = -1; for (int j = 0; j < 5; j++) { if (aliens[j][col] == NULL) continue; else row = j; } if (row != -1) { bulletListEnemy.push_back(Bullet(2)); bulletListEnemy.back().LoadTexture("Bullet.png"); bulletListEnemy.back().setPosition(aliens[row][col]->getPosition().x + aliens[row][col]->getBoundaryRectangle().width / 2, aliens[row][col]->getPosition().y + 40); bulletListEnemy.back().setScale(0.25, 0.25); } clock.restart(); } //get events from the display sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } //check if space key is pressed if yes then fire one bullet if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) { if (bullet.size() == 0) { bullet.push_back(Bullet(1)); bullet.back().LoadTexture("Bullet.png"); bullet.back().setPosition(cannon->getPosition().x+cannon->getBoundaryRectangle().width/2 , cannon->getPosition().y); bullet.back().setScale(0.25, 0.25); } } //check if keyboard is pressed, if yes move cannon if (sf::Keyboard::isKeyPressed) { cannon->Update(); } window.clear(); //drawing checking collision==================================================================== //cannon drawing cannon->Draw(window); //aliens drawing and movement for (int i = 0; i < 5; i++) { for (int j = 0; j < 11; j++) { if (aliens[i][j] == NULL) { continue; } else { aliens[i][j]->Draw(window); downMovement = aliens[i][j]->moveLeftRight(); if (downMovement == 1) { for (int i = 0; i < 5; i++) { for (int j = 0; j < 11; j++) { if (aliens[i][j] != NULL) { aliens[i][j]->moveDown(20); } } } downMovement = 0; } } } } //draw bullet on scren for (int i = 0; i < bullet.size(); i++) { bullet[i].Draw(window); int j = bullet[i].Move(); if (j == 2) { bullet.clear(); } //check for collision for (int k = 0; k < 5; k++) { for (int j = 0 ; j < 11; j++) { if (aliens[k][j] != NULL) { if (bullet[i].getBoundaryRectangle().intersects(aliens[k][j]->getBoundaryRectangle())) { bullet.clear(); delete(aliens[k][j]); aliens[k][j] = NULL; switch (k) { case 0: score += 40; break; case 1:score += 20; break; case 2:score += 20; break; case 3:score += 10; break; case 4:score += 10; break; default: break; } break; } } } } } for (std::list<Bullet>::iterator itr = bulletListEnemy.begin(); itr != bulletListEnemy.end(); itr++) { (*itr).Draw(window); int j = (*itr).Move(); if (j == 2) { itr = bulletListEnemy.erase(itr); } else if ((*itr).getBoundaryRectangle().intersects(cannon->getBoundaryRectangle())) { itr = bulletListEnemy.erase(itr); died++; } } //showing score std::string scorestr = std::to_string(score); std::string diedstr = std::to_string(died); scoreText.setString("Score: "+scorestr+" Died: "+diedstr); window.draw(scoreText); //drawing checking collision==================================================================== window.display(); } return 0; }