/************************************************************************************** COLLISION DETECTION **************************************************************************************/ void collisionDetection() { // Collision with a new pine if(hero.testCollision(pine)) { // creating a new pine to add into the queue pines_collected[score_points].setPosition(vec3(pine.getPosition().v[0], 0.0f, pine.getPosition().v[2])); // changing the position of the pine to be collected pine.generatePosition(); // setting the following mode if(score_points == 0) { pines_collected[score_points].setTargetToFollow(&hero); } else { pines_collected[score_points].setTargetToFollow(&pines_collected[score_points-1]); } pines_collected[score_points].setSpeed(game_speed); pines_collected[score_points].hitted = GL_TRUE; pines_collected[score_points].setRadius(2.0f); // Up the score! score_points++; } // Collision with a pine already collected else { for(int i=0; i<score_points; i++) { if(hero.testCollision(pines_collected[i]) && i != score_points-1) { end_game = GL_TRUE; } } } // Collision with the walls if(hero.getPosition().v[0] > 195 || hero.getPosition().v[0] < -195 || hero.getPosition().v[2] > 195 || hero.getPosition().v[2] < -195) end_game = GL_TRUE; // Collision with the Monster for(int i=0; i<NUMBER_OF_MONSTERS; i++) if(hero.testCollision(monster[i])) end_game = GL_TRUE; }
/************************************************************************************** UPDATE **************************************************************************************/ void update() { // Updating score if(!end_game) { score = "Score: " + to_string(score_points); speed_string = "Speed: " + to_string(game_speed); } update_text (score_txtLabel, score.c_str()); update_text(speed_txtLabel, speed_string.c_str()); // Moving my objects if(!camera.initial_rotation) { for(int i=0; i<NUMBER_PARTICLES; i++) snow[i].update(dt); hero.setSpeed(game_speed); hero.update(dt); for(int i=0; i<score_points; i++) { pines_collected[i].moveFollowing(dt); pines_collected[i].setSpeed(game_speed); } for(int i=0; i<NUMBER_OF_MONSTERS; i++) { monster[i].moveFollowing(dt); monster[i].setSpeed(monster_speed_factor * game_speed); } } // Setting Camera camera.setDirection(hero.getDirection()); camera.setTarget(hero.getPosition()); camera.update(); // Setting cameras if(!end_game) { hero.setCamera(*camera.getViewMatrix()); game_floor.setCamera(*camera.getViewMatrix()); for(int i=0; i<NUMBER_OF_MONSTERS; i++) monster[i].setCamera(*camera.getViewMatrix()); pine.setCamera(*camera.getViewMatrix()); for(int i=0; i<score_points; i++) pines_collected[i].setCamera(*camera.getViewMatrix()); for(int i=0; i<NUMBER_PARTICLES; i++) snow[i].setCamera(*camera.getViewMatrix()); } }
// load level 1 of the map int MapManager::loadMap1(){ currentLevel = 1; stackNeeded = 7; new Ladder(df::Position(5, 5), 10); new Ladder(df::Position(25, 5), 10); new Ladder(df::Position(40, 5), 10); new Floor(df::Position(35, 5), 25); new Floor(df::Position(0, 5), 35); new Floor(df::Position(0, 15), 10); new Floor(df::Position(20, 15), 27); Enemy *enem = new Enemy(df::Position(3, 5)); Hero *hero = new Hero(df::Position(45, 14)); df::Position pos = hero->getPosition(); pos.setX(pos.getX() - 5); new Power(SHIELD, pos); Button *butt = new Button(); butt->addWall(new Wall(df::Position(30, 0), 25)); butt->setPosition(df::Position(45, 5)); new Block(df::Position(40, 5)); new Block(df::Position(40, 7)); new Block(df::Position(40, 9)); new Block(df::Position(40, 11)); new Shelf(df::Position(40, 15)); new Shelf(df::Position(40, 23), true); new Block(df::Position(25, 5)); new Block(df::Position(25, 7)); new Block(df::Position(25, 9)); new Block(df::Position(25, 11)); new Shelf(df::Position(25, 15)); new Shelf(df::Position(25, 23), true); return 0; }
// load level 2 of the map int MapManager::loadMap2(){ currentLevel = 2; stackNeeded = 15; new Floor(df::Position(3, 3), 72); new Floor(df::Position(3, 10), 72); new Floor(df::Position(3, 17), 72); new Ladder(df::Position(3, 4), 13); new Ladder(df::Position(27, 4), 13); new Ladder(df::Position(51, 4), 13); new Ladder(df::Position(75, 4), 13); for (int i = 3; i < 76; i += 24){ for (int j = 3; j < 14; j += 3){ new Block(df::Position(i, j)); new Shelf(df::Position(i, j)); if (j > 10){ new Shelf(df::Position(i, 23), true); } } } Enemy *enem = new Enemy(df::Position(3, 5)); Enemy *enem1 = new Enemy(df::Position(75, 5)); Hero *hero = new Hero(df::Position(51, 15)); df::Position pos = hero->getPosition(); pos.setX(pos.getX() - 5); new Power(SHIELD, df::Position(3, 2)); Button *butt = new Button(); butt->addWall(new Wall(df::Position(35, 0), 25)); butt->setPosition(df::Position(42, 3)); return 0; return 0; }
Vector2D GameState::getNextCreaturePos(){ //get current creature Creature* c = plyr.getCreature(); if (!c){ //TODO return any creature cerr << "not set"; return Vector2D(-1,-1); } bool currFound = false; Hero* heros = wrld.getHeros(); for (int i = 0; i < wrld.getHeroSize(); i++){ if (wrld.getStarts()[i] != Vector2D(-1,-1)) continue; Hero h = heros[i]; if (h.getName() == c->getName() && h.getPlayer() == c->getPlayer()){ //found current currFound = true; continue; } if (currFound && h.getPlayer() == plyr.getName()){ //found next return h.getPosition(); } } vector<Monster*>& monsters = wrld.getMonsters(); for (unsigned i = 0; i < monsters.size(); i++){ Monster* m = monsters[i]; if (m->getName() == c->getName() && m->getPlayer() == c->getPlayer()){ //found current currFound = true; continue; } if (currFound && m->getPlayer() == plyr.getName()){ //found next return m->getPosition(); } } for (int i = 0; i < wrld.getHeroSize(); i++){ if (wrld.getStarts()[i] != Vector2D(-1,-1)) continue; Hero h = heros[i]; if (h.getName() == c->getName() && h.getPlayer() == c->getPlayer()){ //found current currFound = true; continue; } if (currFound && h.getPlayer() == plyr.getName()){ //found next return h.getPosition(); } } for (unsigned i = 0; i < monsters.size(); i++){ Monster* m = monsters[i]; if (m->getName() == c->getName() && m->getPlayer() == c->getPlayer()){ //found current currFound = true; continue; } if (currFound && m->getPlayer() == plyr.getName()){ //found next return m->getPosition(); } } //nothing suitable found, so the next is the old return c->getPosition(); }
void InputOutputHandler::sterowaniePostacia(Hero& hero) { // Sprawdzanie stanu kolizji std::vector<bool> bStany = hero.getZnacznikiKolizji().getCollisionState(); // Blad - Kiedy ustawia sie na niewidoczny, detektor przestaje dzialac. if (IsKeyDown( irr::KEY_KEY_Q )) { hero.getZnacznikiKolizji().setVisible(false); } if (IsKeyDown( irr::KEY_KEY_E )) { hero.getZnacznikiKolizji().setVisible(true); } konsola.setMessage(0, bStany[0] ? "left: true" : "left: false"); konsola.setMessage(1, bStany[1] ? "right: true" : "right: false"); konsola.setMessage(2, bStany[2] ? "forward: true" : "forward: false"); konsola.setMessage(3, bStany[3] ? "back: true" : "back: false"); konsola.setMessage(4, bStany[4] ? "up: true" : "up: false"); konsola.setMessage(5, bStany[5] ? "down: true" : "down: false"); if(!bStany[SideCollisionDetector::down]) hero.fallDown(); hero.getZnacznikiKolizji().setPosition(hero.getPosition(), hero.getRotation(), hero.getDirection()); hero.getZnacznikiKolizji().setRotation(hero.getRotation(), hero.getDirection()); if ( IsKeyDown( klawiszSkoku ) && !hero.getJumpState() && bStany[SideCollisionDetector::down] ) { hero.setJumpState(true); hero.stopFallingDown(); } else if (bStany[SideCollisionDetector::down] || bStany[SideCollisionDetector::up]) hero.setJumpState(false); if(hero.getJumpState()) hero.jump(); int player_direction = 0; if ( IsKeyDown( irr::KEY_KEY_W ) ) player_direction |= FORWARD; if ( IsKeyDown( irr::KEY_KEY_S ) ) player_direction |= BACKWARD; if ( IsKeyDown( irr::KEY_KEY_A ) ) player_direction |= LEFT; if ( IsKeyDown( irr::KEY_KEY_D ) ) player_direction |= RIGHT; //nic się nie zmieniło if(player_direction == 0) { if (hero.getBezwladnosc() <= 0) hero.move(STAND, FORWARD); else hero.decelerate(); hero.getZnacznikiKolizji().setPosition(hero.getPosition(), hero.getRotation(), hero.getDirection()); hero.getZnacznikiKolizji().setRotation(hero.getRotation(), hero.getDirection()); moveCameraControl(); hero.rotate(direction - 90); return; } else { hero.setBezwladnosc(1); hero.resetAnimAndSpeed(); hero.setDirection(static_cast<DIRECTION>(player_direction)); hero.getZnacznikiKolizji().setPosition(hero.getPosition(), hero.getRotation(), hero.getDirection()); hero.getZnacznikiKolizji().setRotation(hero.getRotation(), hero.getDirection()); hero.move(WALK, hero.getDirection()); moveCameraControl(); } hero.rotate(direction - 90); }
//定时器调用的移动处理函数 void OptionLayer::updateMove(float dt) { OptionLayer *optionLayer = global->optionLayer; CCTMXTiledMap *tmxTileMap = global->tmxTileMap; DirectionControlButton *ControlWheelSprite = optionLayer->getDirectionCrtl(); CCPoint Direction = ControlWheelSprite->getDirection(); Hero *heroCtrl = global->hero; //控制的角色 CCPoint position = heroCtrl->getPosition() + Direction; //下一次需要移动的位置(加上偏移量) //----------------------------控制主角移动------------------------------------------------- //瓦片地图允许移动到该位置,且摇杆对象不为空 if(ControlWheelSprite && global->tileAllowMove(position)){ if(Direction.x != 0||Direction.y != 0) { heroCtrl->RunMovingAction();//只要偏移量不为0就执行行走动画 }else if(heroCtrl->getActionState() == ActionStateNone){ heroCtrl->RunIdleAction(); } //设置英雄的zOrder,以其y轴为zOrder heroCtrl->setZOrder(1/heroCtrl->getPositionY()*ScreenHeight*4+1);//同Y轴 人物比NPC zOrder值大1 if (heroCtrl->getAllowMove()&&(Direction.x != 0||Direction.y != 0)) { CCSize size=CCDirector::sharedDirector()->getWinSize(); //制造一个矩形框,人物只会在当前框里活动,可通过地图移动来造成更大面积的移动范围 CCRect rect=CCRectMake(heroCtrl->getContentSize().width/2,heroCtrl->getContentSize().height/2,size.width-(heroCtrl->getContentSize().width/2),size.height-(heroCtrl->getContentSize().height/2)); //创建一个逻辑矩形,范围是屏幕的大小,用于判断边界 if(rect.containsPoint(position)){ float mapMaxX = 0; //计算是否是人移动还是地图移动,注意:地图在初始化时锚点已设为cpp(0,0) if(heroCtrl->getPosition().x <= SCREEN.width/2 && Direction.x>0 && position.x > SCREEN.width/2){ //地图需要左移动,position表示人物下一帧的位置 //计算地图最右端x轴下一次位置 mapMaxX = tmxTileMap->getPositionX()+tmxTileMap->getContentSize().width-Direction.x; //保证地图不会把越界部分移出来 if(mapMaxX > SCREEN.width){ global->tmxTileMap->setPositionX(global->tmxTileMap->getPositionX()-Direction.x); OptionLayer::npcMoveByMap(ccp(-Direction.x,0)); if(Direction.y != 0){ heroCtrl->setPositionY(heroCtrl->getPositionY() + Direction.y);//防止地图移动时,人物Y轴不能移动 } }else{ heroCtrl->setPosition(position); //地图快要越界,人物移动即可 } }else if(heroCtrl->getPosition().x >= SCREEN.width/2 && Direction.x<0 && position.x < SCREEN.width/2){//地图需要像右移动 //计算地图最左端x轴下一次位置 mapMaxX = tmxTileMap->getPositionX()+Direction.x; //保证地图不会把越界部分移出来 if(mapMaxX < 0){ global->tmxTileMap->setPositionX(global->tmxTileMap->getPositionX()-Direction.x); OptionLayer::npcMoveByMap(ccp(-Direction.x,0)); if(Direction.y != 0){ heroCtrl->setPositionY(heroCtrl->getPositionY() + Direction.y);//防止地图移动时,人物Y轴不能移动 } }else{ heroCtrl->setPosition(position); //地图快要越界,人物移动 } }else{ heroCtrl->setPosition(position); //人物移动即可 } } } } //--------------------------控制怪物移动----------------------------------- CCArray *npcs = global->npcs; CCObject *pObject = NULL; CCARRAY_FOREACH(npcs, pObject){ NPC* npc =(NPC*)pObject; npc->setZOrder(1/npc->getPositionY()*ScreenHeight*4);//设置 怪物zOrder //如果是非动作状态则执行idle动画 if(npc->getActionState() == ActionStateNone){ npc->RunIdleAction(); } npc->setPosition(npc->getPosition()+npc->getVector()); //怪物移动 CCLOG("=========npc.x=%f/npc.y=%f======",npc->getPositionX(),npc->getPositionY()); npcMoveToHero(npc);//坐标处理函数 }