void BattleLayer::_initTower(){ TMXTiledMap *tiledMap = TMXTiledMap::create("res/map/battle_map.tmx"); TMXObjectGroup *towerObjectData = tiledMap->getObjectGroup("tower"); auto &towersData = towerObjectData->getObjects(); for (auto &towerDataItem : towersData){ ValueMap &towerData = towerDataItem.asValueMap(); // CCLOG("%s, %s", towerData["side"].asString().c_str(), towerData["type"].asString().c_str()); if (towerData["side"].asString() == "player"){ if (towerData["type"].asString() == "king"){ // _playerKingTower = Tower::createTower(TowerType::King, Side::Player, this); playerKingTower = createTower(TowerType::King, Side::Player, this); playerKingTower->setPosition(_battleMapSize.width / 2, 0); _battleMap->addChild(playerKingTower, getBattleElementZOrderOnBattleMap(playerKingTower)); playerTowers.push_back(playerKingTower); }else { if (towerData["road"].asString() == "left"){ // _playerDefendLeftTower = Tower::createTower(TowerType::Defend, Side::Player, this); playerDefendLeftTower = createTower(TowerType::Defend, Side::Player, this); playerDefendLeftTower->setPosition(_battleTileSize.width * 3.5, _battleTileSize.height * 5); _battleMap->addChild(playerDefendLeftTower, getBattleElementZOrderOnBattleMap(playerDefendLeftTower)); playerTowers.push_back(playerDefendLeftTower); } if (towerData["road"].asString() == "right"){ // _playerDefendRightTower = Tower::createTower(TowerType::Defend, Side::Player, this); playerDefendRightTower = createTower(TowerType::Defend, Side::Player, this); playerDefendRightTower->setPosition(_battleTileSize.width * 16.5, _battleTileSize.height * 5); _battleMap->addChild(playerDefendRightTower, getBattleElementZOrderOnBattleMap(playerDefendRightTower)); playerTowers.push_back(playerDefendRightTower); } } }else { if (towerData["type"].asString() == "king"){ // _npcKingTower = Tower::createTower(TowerType::King, Side::NPC, this); npcKingTower = createTower(TowerType::King, Side::NPC, this); npcKingTower->setPosition(_battleMapSize.width / 2, _battleTileSize.height * 26); _battleMap->addChild(npcKingTower, getBattleElementZOrderOnBattleMap(npcKingTower)); npcTowers.push_back(npcKingTower); }else { if (towerData["road"].asString() == "left"){ // _npcDefendLeftTower = Tower::createTower(TowerType::Defend, Side::NPC, this); npcDefendLeftTower = createTower(TowerType::Defend, Side::NPC, this); npcDefendLeftTower->setPosition(_battleTileSize.width * 3.5, _battleTileSize.height * 23); _battleMap->addChild(npcDefendLeftTower, getBattleElementZOrderOnBattleMap(npcDefendLeftTower)); npcTowers.push_back(npcDefendLeftTower); } if (towerData["road"].asString() == "right"){ // _npcDefendRightTower = Tower::createTower(TowerType::Defend, Side::NPC, this); npcDefendRightTower = createTower(TowerType::Defend, Side::NPC, this); npcDefendRightTower->setPosition(_battleTileSize.width * 16.5, _battleTileSize.height * 23); _battleMap->addChild(npcDefendRightTower, getBattleElementZOrderOnBattleMap(npcDefendRightTower)); npcTowers.push_back(npcDefendRightTower); } } } } }
void PlaypenApp::createObject(const std::string& material, ObjectType type) { switch(type) { case OBJECT_TYPE_BOX: { Ogre::Vector3 boxDim(5, 5, 5); opal::Solid* s = mSimulator->createSolid(); s->setPosition(mCreationPoint); opal::BoxShapeData data; data.dimensions.set(boxDim[0], boxDim[1], boxDim[2]); s->addShape(data); createPhysicalEntityBox("", material, boxDim, s); break; } case OBJECT_TYPE_SPHERE: { Ogre::Real radius = 3; // testing opal::Solid* s = mSimulator->createSolid(); s->setPosition(mCreationPoint); opal::SphereShapeData data; data.radius = radius; data.material.density = 1; // testing s->addShape(data); createPhysicalEntitySphere("", material, radius, s); break; } case OBJECT_TYPE_WALL: { opal::Matrix44r m; m.rotate(45, 0, 1, 0); m.translate(0, 0, -23); createWall(6, 8, opal::Vec3r(3, 1.5, 1.5), m, material); break; } case OBJECT_TYPE_TOWER: { createTower(2, 2, 15, opal::Vec3r(3, 1.5, 1.5), opal::Matrix44r(), material); break; } case OBJECT_TYPE_RAGDOLL: { opal::Blueprint ragdollBP; opal::loadFile(ragdollBP, "../../data/blueprints/ragdoll.xml"); opal::Matrix44r transform; transform.translate(mCreationPoint[0], mCreationPoint[1] - 5, mCreationPoint[2]); // Instantiate the Blueprint. opal::BlueprintInstance instance; mSimulator->instantiateBlueprint(instance, ragdollBP, transform, 16); unsigned int i=0; for (i=0; i<instance.getNumSolids(); ++i) { opal::Solid* s = instance.getSolid(i); const opal::SolidData& data = s->getData(); unsigned int j=0; for (j=0; j<data.getNumShapes(); ++j) { opal::ShapeData* shapeData = data.getShapeData(j); switch(shapeData->getType()) { case opal::BOX_SHAPE: { opal::Vec3r dim = ((opal::BoxShapeData*)shapeData)->dimensions; Ogre::Vector3 boxDim(dim[0], dim[1], dim[2]); createPhysicalEntityBox("", material, boxDim, s); break; } case opal::SPHERE_SHAPE: { opal::real r = ((opal::SphereShapeData*)shapeData)->radius; createPhysicalEntitySphere("", material, r, s); break; } case opal::CAPSULE_SHAPE: { opal::real r = ((opal::CapsuleShapeData*)shapeData)->radius; opal::real l = ((opal::CapsuleShapeData*)shapeData)->length; createPhysicalEntityCapsule("", material, r, l, s); break; } default: assert(false); } } } break; } default: assert(false); break; } }
/** * \fn bool manageEvent(SDL_Event event, Viewport* viewport) * \brief Manage an event. * * \param event The event to manage. * \param viewport The viewport that can be moved. * \return False if the the game ends, true if not. */ bool manageEvent(SDL_Event event, Viewport* viewport, Events *flags) { bool isInPlay = true; switch(event.type) { // Quit game case SDL_QUIT: isInPlay = false; break; // Key pressed case SDL_KEYDOWN: switch(event.key.keysym.sym) { // Quit game case SDLK_ESCAPE: isInPlay = false; break; // Move view case SDLK_UP: moveViewport(viewport, UP); break; case SDLK_DOWN: moveViewport(viewport, DOWN); break; case SDLK_LEFT: moveViewport(viewport, LEFT); break; case SDLK_RIGHT: moveViewport(viewport, RIGHT); break; default: break; } break; // Mouse left click case SDL_MOUSEBUTTONDOWN: if(event.button.button == SDL_BUTTON_LEFT) { Case* caseClicked = whichCase(event.button.x, event.button.y); TypeTo* simpleTowerType = flags->selectedTower; Case *viewportOffset = whichCase(viewport->mapsurface.x,viewport->mapsurface.y); int mapPositionX = caseClicked->xx + viewportOffset->xx; int mapPositionY = caseClicked->yy + viewportOffset->yy; Tower *tower = createTower(mapPositionX, mapPositionY, simpleTowerType); if(tower){ flags->enemy_Path_Calculation = true; pushList((void*)flags->towerList,tower); drawTower(tower); } } break; default: break; } return isInPlay; }
bool PlaypenApp::processUnbufferedKeyInput(Ogre::Real dt) { // Check if we should quit looping. if(mKeyboard->isKeyDown(OIS::KC_ESCAPE) || mKeyboard->isKeyDown(OIS::KC_Q)) { return false; } // Check if we should pause physics. if(mKeyboard->isKeyDown(OIS::KC_P) && mTimeUntilNextToggle <= 0) { mPaused = !mPaused; // Reset the timer for toggle keys. mTimeUntilNextToggle = 0.5; } // Reset the scene. if(mKeyboard->isKeyDown(OIS::KC_R)) { // Make sure the PhysicalCamera isn't grabbing anything. mPhysicalCamera->release(); destroyAllPhysicalEntities(); setupInitialPhysicalEntities(); } // Create various types of objects when the number keys are // pressed. // Create a box. if(mKeyboard->isKeyDown(OIS::KC_1) && mTimeUntilNextToggle <= 0) { Ogre::Vector3 boxDim(3, 3, 3); opal::Solid* s = mSimulator->createSolid(); s->setPosition(mCreationPoint); opal::BoxShapeData data; data.dimensions.set(boxDim[0], boxDim[1], boxDim[2]); s->addShape(data); createPhysicalEntityBox("", "Plastic/Yellow", boxDim, s); // Reset the timer for toggle keys. mTimeUntilNextToggle = 0.3; } // Create a sphere. if(mKeyboard->isKeyDown(OIS::KC_2) && mTimeUntilNextToggle <= 0) { Ogre::Real radius = 2; opal::Solid* s = mSimulator->createSolid(); s->setPosition(mCreationPoint); opal::SphereShapeData data; data.radius = radius; s->addShape(data); createPhysicalEntitySphere("", "Plastic/Purple", radius, s); // Reset the timer for toggle keys. mTimeUntilNextToggle = 0.3; } // Create a capsule. if(mKeyboard->isKeyDown(OIS::KC_3) && mTimeUntilNextToggle <= 0) { Ogre::Real radius = 2; Ogre::Real length = 5; opal::Solid* s = mSimulator->createSolid(); s->setPosition(mCreationPoint); opal::CapsuleShapeData data; data.radius = radius; data.length = length; s->addShape(data); createPhysicalEntityCapsule("", "Plastic/Red", radius, length, s); // Reset the timer for toggle keys. mTimeUntilNextToggle = 0.3; } // Create a cylinder. if(mKeyboard->isKeyDown(OIS::KC_4) && mTimeUntilNextToggle <= 0) { Ogre::Real radius = 3; Ogre::Real length = 5; opal::Solid* s = mSimulator->createSolid(); s->setPosition(mCreationPoint); opal::CylinderShapeData data; data.radius = radius; data.length = length; s->addShape(data); createPhysicalEntityCylinder("", "Plastic/Blue", radius, length, s); // Reset the timer for toggle keys. mTimeUntilNextToggle = 0.3; } // Create a long box. if(mKeyboard->isKeyDown(OIS::KC_5) && mTimeUntilNextToggle <= 0) { Ogre::Vector3 boxDim(2, 10, 3); opal::Solid* s = mSimulator->createSolid(); s->setPosition(mCreationPoint); opal::BoxShapeData data; data.dimensions.set(boxDim[0], boxDim[1], boxDim[2]); data.material.density = 10; s->addShape(data); createPhysicalEntityBox("", "Plastic/Green", boxDim, s); // Reset the timer for toggle keys. mTimeUntilNextToggle = 0.3; } //// Create a log. //if(mKeyboard->isKeyDown(OIS::KC_6) && mTimeUntilNextToggle <= 0) //{ // Ogre::Real logScale = 9; // Ogre::Vector3 boxDim(0.4243, 0.4243, 2); // boxDim *= logScale; // opal::Solid* s = mSimulator->createSolid(); // opal::Matrix44r m; // m.rotate(90, 1, 0, 0); // s->setTransform(m); // s->setPosition(mCreationPoint); // opal::BoxShapeData data; // data.dimensions.set(boxDim[0], boxDim[1], boxDim[2]); // s->addShape(data); // std::string name = generateUniqueName(); // Ogre::SceneNode* sn = mSceneMgr->getRootSceneNode()-> // createChildSceneNode(name); // sn->scale(logScale, logScale, logScale); // Entity* e = mSceneMgr->createEntity(name, "log.mesh"); // e->setNormaliseNormals(true); // e->setMaterialName("Textured/Wood"); // sn->attachObject(e); // createPhysicalEntity(name, sn, s); // // Reset the timer for toggle keys. // mTimeUntilNextToggle = 0.3; //} // Create a knot. if(mKeyboard->isKeyDown(OIS::KC_6) && mTimeUntilNextToggle <= 0) { opalSamples::PhysicalEntity* pe = createPhysicalEntityMesh("", "knot.mesh", "Textured/RustedMetal", false, 0.1); pe->getSolid()->setPosition(opal::Point3r(0, 40, 0)); // Reset the timer for toggle keys. mTimeUntilNextToggle = 0.3; } // Create a wall. if(mKeyboard->isKeyDown(OIS::KC_7) && mTimeUntilNextToggle <= 0) { opal::Matrix44r m; m.rotate(45, 0, 1, 0); m.translate(0, 0, -23); createWall(6, 8, opal::Vec3r(3, 1.5, 1.5), m); // Reset the timer for toggle keys. mTimeUntilNextToggle = 0.3; } // Create a tower. if(mKeyboard->isKeyDown(OIS::KC_8) && mTimeUntilNextToggle <= 0) { createTower(2, 2, 15, opal::Vec3r(3, 1.5, 1.5)); // Reset the timer for toggle keys. mTimeUntilNextToggle = 0.3; } // Create a ragdoll. if(mKeyboard->isKeyDown(OIS::KC_9) && mTimeUntilNextToggle <= 0) { opal::Blueprint ragdollBP; opal::loadFile(ragdollBP, "../data/blueprints/ragdoll.xml"); opal::Matrix44r transform; transform.translate(mCreationPoint[0], mCreationPoint[1] - 5, mCreationPoint[2]); // Instantiate the Blueprint. opal::BlueprintInstance instance; mSimulator->instantiateBlueprint(instance, ragdollBP, transform, 16); unsigned int i=0; for (i=0; i<instance.getNumSolids(); ++i) { opal::Solid* s = instance.getSolid(i); const opal::SolidData& data = s->getData(); unsigned int j=0; for (j=0; j<data.getNumShapes(); ++j) { opal::ShapeData* shapeData = data.getShapeData(j); switch(shapeData->getType()) { case opal::BOX_SHAPE: { opal::Vec3r dim = ((opal::BoxShapeData*)shapeData)->dimensions; Ogre::Vector3 boxDim(dim[0], dim[1], dim[2]); createPhysicalEntityBox("", "Plastic/LightBlue", boxDim, s); break; } case opal::SPHERE_SHAPE: { opal::real r = ((opal::SphereShapeData*)shapeData)->radius; createPhysicalEntitySphere("", "Plastic/LightBlue", r, s); break; } case opal::CAPSULE_SHAPE: { opal::real r = ((opal::CapsuleShapeData*)shapeData)->radius; opal::real l = ((opal::CapsuleShapeData*)shapeData)->length; createPhysicalEntityCapsule("", "Plastic/LightBlue", r, l, s); break; } default: assert(false); } } } // Reset the timer for toggle keys. mTimeUntilNextToggle = 0.5; } // The following code updates the camera's position. opal::Vec3r cameraDir(0, 0, 0); bool cameraMoved = false; if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A)) { // Move camera left. cameraDir[0] -= (dt * mMoveSpeed); cameraMoved = true; } if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D)) { // Move camera right. cameraDir[0] += (dt * mMoveSpeed); cameraMoved = true; } if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W)) { // Move camera forward. cameraDir[2] -= (dt * mMoveSpeed); cameraMoved = true; } if (mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S)) { // Move camera backward. cameraDir[2] += (dt * mMoveSpeed); cameraMoved = true; } if (!cameraMoved) { // Slow physical camera motion if necessary. } // Use the camera dir vector to translate the camera. mPhysicalCamera->moveRelative(cameraDir); // Toggle shadows. if(mKeyboard->isKeyDown(OIS::KC_H) && mTimeUntilNextToggle <= 0) { mUseShadows = !mUseShadows; if (mUseShadows) { mSceneMgr->setShadowTechnique(SHADOWTYPE_STENCIL_ADDITIVE); } else { mSceneMgr->setShadowTechnique(SHADOWTYPE_NONE); } // Reset the timer for toggle keys. mTimeUntilNextToggle = 0.5; } // Toggle second light source. if(mKeyboard->isKeyDown(OIS::KC_L) && mTimeUntilNextToggle <= 0) { Ogre::Light* light1 = mSceneMgr->getLight("light1"); if (light1->isVisible()) { light1->setVisible(false); } else { light1->setVisible(true); } // Reset the timer for toggle keys. mTimeUntilNextToggle = 0.5; } // Toggle GUI. if (mKeyboard->isKeyDown(OIS::KC_G) && mTimeUntilNextToggle <= 0) { mStatsOn = !mStatsOn; showDebugOverlay(mStatsOn); mTimeUntilNextToggle = 1; } // Handy screenshot saving procedure. if (mKeyboard->isKeyDown(OIS::KC_SYSRQ) && mTimeUntilNextToggle <= 0) { char tmp[20]; sprintf(tmp, "screenshot_%d.png", ++mNumScreenShots); ExampleApplication::mWindow->writeContentsToFile(tmp); mDebugText = String("Wrote ") + tmp; // Reset the timer for toggle keys. mTimeUntilNextToggle = 0.5; } // Return true to continue looping. return true; }
int main(int argc, char* argv[]) { // Init initPath(argv[0]); SDL_Surface* screen = NULL; SDL_Event event; int *seed; srand((int)seed); int previousTime = 0, currentTime = 0; Events *flags = createEventFlags(); SDL_Init(SDL_INIT_VIDEO); SDL_SetEventFilter(eventFilter); screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_DOUBLEBUF | SDL_NOFRAME); SDL_WM_SetCaption("Tower Defense", NULL); Action *actionList = initAction(); Map* map = createMap(getPath("resources/Forest.png")); _map = map; SDL_Rect surface = {0, 0, 720, 600}; Viewport* viewport = createViewport(screen, surface, map); _viewport = viewport; // FIXME uh? what's this thing? surface.x = 800 - 80; surface.y = 0; surface.h = 80; surface.w = 600; // Creation of the enemies TypeEn *whiteCat = createTypeEn(100, 5, false, true, true, false, 1,getPath("resources/white_transparent_cat.png")); TypeEn *blackCat = createTypeEn(100, 5, false, true, true, false, 1,getPath("resources/black_transparent_cat.png")); Enemy *cat1 = createEnemy(1,1,whiteCat); Enemy *cat2 = createEnemy(1,10,whiteCat); Enemy *cat3 = createEnemy(5,5,blackCat); Enemy *cat4 = createEnemy(21,4,blackCat); TypeEn *zombie = createTypeEn(100,5,false,true,true,false,1,getPath("resources/zombie.png")); Enemy *zombie1 = createEnemy(4,4,zombie); Enemy *zombie2 = createEnemy(9,4,zombie); Enemy *zombie3 = createEnemy(9,9,zombie); Enemy *zombie4 = createEnemy(7,14,zombie); //Add enemy in the List List *catList = newList(cat4); pushList((void*)catList,cat2); pushList((void*)catList,cat3); // pushList((void*)catList,cat1); List *zombieList = newList(zombie1); /* pushList((void*)zombieList,zombie2);*/ /* pushList((void*)zombieList,zombie3);*/ /* pushList((void*)zombieList,zombie4);*/ // removeEnemyFromList(cat4,catList); //TOWER TypeBul *bullet = createTypeBul(getPath("resources/bullet.png"), 1); TypeTo *tower = createTypeTo(0,5,0,0,false,false,false,false,bullet,NULL,getPath("resources/tower.png")); upgradeTypeTo(tower,0.5,getPath("resources/towerUP.png")); flags->selectedTower = tower->nextType; Tower *tower1 = createTower(7,7,tower); List *towerList = newList(tower1); flags->towerList = towerList; // Create and Renders the right panel game menu SDL_Rect surfaceMenu = {720, 0, 800, 600}; Menu* menu = menu_create(screen, surfaceMenu); menu_loadBackground(menu, "resources/enemyFont.gif"); // For testing only, we add a few random buttons menu_addButton(menu, button_createBuildButton(tower)); menu_addButton(menu, button_createBuildButton(tower)); menu_addButton(menu, button_createBuildButton(tower)); menu_render(menu); _cell = *getCase(20,11); // Main loop while(actionList[QUIT].boolean == NULL) { // Managing the events manageEvents(viewport, flags,actionList); for(int i=1; i<ACTION_LENGTH; i++) { if(actionList[i].boolean) { int repeat = (*actionList[i].action)(viewport,flags,actionList[i].boolean); if(!repeat) { actionList[i].boolean = NULL; } } } // Redraws the map (viewport contents) before blitting stuff on it updateViewport(viewport); ///////////////////////////// DEBUG WALL ///////////////////////////// SDL_Rect position; for(int i=0; i < _map->nbCaseW; i++) { for(int j=0; j < _map->nbCaseH; j++) { Case cell = *getCase(i,j); position.x = cell.x; position.y = cell.y; if(map->matrice[i][j].hasTower == 2) { SDL_Surface *wall = IMG_Load(getPath("resources/brick.png")); blitToViewport(viewport, wall, NULL, &position); } } } position.x = _cell.x; position.y = _cell.y; blitToViewport(viewport, IMG_Load(getPath("resources/candy_cane.png")), NULL, &position); ///////////////////////////////////////////////////////////////////// // Move enemies if(flags->enemy_Path_Calculation) { pathReCalculation(catList); pathReCalculation(zombieList); flags->enemy_Path_Calculation = false; } moveEnemyList(zombieList); moveEnemyList(catList); // Blit enemies drawEnemyList(zombieList); drawEnemyList(catList); //Blit TOWER /* if(event.key.keysym.sym == SDLK_u){*/ /* upgrade(tower1);*/ /* }*/ Bullet *bullet1 = createBullet(tower1); animateBullet(bullet1); drawTowerList(towerList); /* This should be handled by event.c switch(event.key.keysym.sym){ case SDLK_a: flags->selectedTower = tower; break; case SDLK_b: flags->selectedTower = tower->nextType; break; default: break; }*/ /* */ // Ask SDL to swap framebuffers to update the displayed screen SDL_Flip(screen); // Managing frames currentTime = SDL_GetTicks(); if (currentTime - previousTime <= 20) { SDL_Delay(20 - (currentTime - previousTime)); } // DEBUG printf("Frame %i : %ims\n", framecounter++, currentTime - previousTime); previousTime = SDL_GetTicks(); } free(actionList); SDL_Quit(); return EXIT_SUCCESS; }