int main(int argc,char* argv[]) { sf::RenderWindow window(sf::VideoMode(600,800),"Example core"); sfutils::ResourceManager<sf::Texture,int> textures; textures.load(Eye,"media/img/eye.png"); sfutils::Animation walkLeft(&textures.get(Eye)); walkLeft.addFramesLine(4,2,0); sfutils::Animation walkRight(&textures.get(Eye)); walkRight.addFramesLine(4,2,1); sfutils::AnimatedSprite sprite(&walkLeft,sfutils::AnimatedSprite::Playing,sf::seconds(0.1)); sf::Clock clock; while (window.isOpen()) { sf::Time delta = clock.restart(); sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } float speed = 50; if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { sprite.setAnimation(&walkLeft); sprite.play(); sprite.move(-speed*delta.asSeconds(),0); } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { sprite.setAnimation(&walkRight); sprite.play(); sprite.move(speed*delta.asSeconds(),0); } else { //sprite.pause(); } window.clear(); sprite.update(delta); window.draw(sprite); //equivalent to : window.draw(frame); window.display(); } return 0; };
int PlayerObject::update(long elapsedTime) { if (isKeyDown(IM_W)) walkForward(); if (isKeyDown(IM_S)) walkBackward(); if (isKeyDown(IM_A)) walkLeft(); if (isKeyDown(IM_D)) walkRight(); //if (isKeyPressed(IM_SPACE)) jump(); if (isKeyDown(IM_SPACE)) jump(); if (isKeyPressed(IM_M1)) primaryClick(); if (isKeyReleased(IM_M1)) primaryRelease(); if (isKeyDown(IM_M1)) primaryDown(); if (isKeyUp(IM_M1)) primaryUp(); if (isKeyPressed(IM_M2)) secondaryClick(); if (isKeyReleased(IM_M2)) secondaryRelease(); if (isKeyDown(IM_M2)) secondaryDown(); if (isKeyUp(IM_M2)) secondaryUp(); if (isKeyPressed(IM_1)) changeSpell(0); if (isKeyPressed(IM_2)) changeSpell(1); if (isKeyPressed(IM_3)) changeSpell(2); if (isKeyPressed(IM_4)) changeSpell(3); if (isKeyPressed(IM_5)) changeSpell(4); if (getNetworkState() == NETWORK_STATE_OFFLINE) { if (isMouseScrollUp()) { PropObject * temp = new PropObject("sword.obj", "error.tga", "default.glsl", camera->getPos() + camera->getLookAtVector(), camera->getLookAtVector(), camera->getUpVector(), "sword.prop"); //RagdollObject * temp = new RagdollObject(box, t, s, camera->getPos(), camera->getLookAtVector(), camera->getUpVector()); temp->setVelocity(camera->getLookAtVector()*10.0f); temp->setRotation(camera->getLookAtVector(), camera->getUpVector()); } if (isMouseScrollDown()) { PropObject * temp = new PropObject("box.obj", "error.tga", "default.glsl", camera->getPos() + camera->getLookAtVector(), camera->getLookAtVector(), camera->getUpVector(), "box1x1.prop"); //RagdollObject * temp = new RagdollObject(box, t, s, camera->getPos(), camera->getLookAtVector(), camera->getUpVector()); temp->setVelocity(camera->getLookAtVector()*10.0f); temp->setRotation(camera->getLookAtVector(), camera->getUpVector()); } } POINT p = inputManager->getMouseMovement(); turn(p.x*0.05f, p.y*0.05f); int ret = CharacterObject::update(elapsedTime); //setDirection(camera->getLookAtVector()); camera->setPosition(btToGLM3(&getBTPosition()) + glm::vec3(0,1,0)); camera->setLookAtVector(getDirection()); //camera->update(elapsedTime); return ret; }
void KGrEnemy::walkTimeDone () { if (KGrObject::frozen) {walkFrozen = TRUE; return; } // Check we are alive BEFORE checking for friends being in the way. // Maybe a friend overhead is blocking our escape from a brick. if ((*playfield)[x][y]->whatIam()==BRICK) { // sollte er aber in einem Brick dieAndReappear(); // sein, dann stirbt er wohl return; // Must leave "walkTimeDone" when an enemy dies. } if (! bumpingFriend()) { switch (direction) { case UP: walkUp (WALKDELAY); if ((rely == 0) && ((*playfield)[x][y+1]->whatIam() == USEDHOLE)) // Enemy kletterte grad aus einem Loch hinaus // -> gib es frei! ((KGrBrick *)(*playfield)[x][y+1])->unUseHole(); break; case DOWN: walkDown (WALKDELAY, FALLDELAY); break; case RIGHT: walkRight (WALKDELAY, FALLDELAY); break; case LEFT: walkLeft (WALKDELAY, FALLDELAY); break; default: // Switch search direction in KGoldrunner search (only). searchStatus = (searchStatus==VERTIKAL) ? HORIZONTAL : VERTIKAL; // In KGoldrunner rules, if a hole opens under an enemy // who is standing and waiting to move, he should fall. if (!(canStand()||hangAtPole())) { initFall (actualPixmap, FALLDELAY); } else { status = STANDING; } break; } // wenn die Figur genau ein Feld gelaufen ist if (status == STANDING) { // dann suche den Helden direction = searchbestway(x,y,herox,heroy); // und if (walkCounter >= 4) { if (! nuggets) collectNugget(); else dropNugget(); } status = WALKING; // initialisiere die Zählervariablen und walkCounter = 1; // den Timer um den Held weiter walkTimer->start ((WALKDELAY * NSPEED) / speed, TRUE); // zu jagen startWalk (); } } else { // A friend is in the way. Try a new direction, but not if leaving a hole. Direction dirn; // In KGoldrunner rules, change the search strategy, // to avoid enemy-enemy deadlock. searchStatus = (searchStatus==VERTIKAL) ? HORIZONTAL : VERTIKAL; dirn = searchbestway (x, y, herox, heroy); if ((dirn != direction) && ((*playfield)[x][y]->whatIam() != USEDHOLE)) { direction = dirn; status = WALKING; walkCounter = 1; relx = 0; absx = 16 * x; rely = 0; absy = 16 * y; startWalk (); } walkTimer->start ((WALKDELAY * NSPEED) / speed, TRUE); } showFigure(); }
void KGrHero::walkTimeDone () { if (! started) return; // Ignore signals from earlier play. if (KGrObject::frozen) {walkFrozen = TRUE; return; } if ((*playfield)[x][y]->whatIam() == BRICK) { emit caughtHero(); // Brick closed over hero. return; } if ((y==1)&&(nuggets<=0)) { // If on top row and all nuggets collected, emit leaveLevel(); // the hero has won and can go to next level. return; } if (status == STANDING) setNextDir(); if ((status == STANDING) && (nextDir != STAND)) { if ((standOnEnemy()) && (nextDir == DOWN)) { emit caughtHero(); // Hero is going to step down into an enemy. return; } startWalk(); } if (status != STANDING) { switch (direction) { case UP: walkUp (WALKDELAY); break; case DOWN: walkDown (WALKDELAY, FALLDELAY); break; case RIGHT: walkRight (WALKDELAY, FALLDELAY); break; case LEFT: walkLeft (WALKDELAY, FALLDELAY); break; default : // The following code is strange. It makes the hero fall off a pole. // It works because of other strange code in "startWalk(), case DOWN:". if (!canStand()||hangAtPole()) // falling initFall(FALL1, FALLDELAY); else status = STANDING; break; } herox=x;heroy=y; // Koordinatenvariablen neu // wenn Held genau ein Feld weitergelaufen ist, if ((relx==0)&&(rely==0)) // dann setzte statische { collectNugget(); // und nehme evtl. Nugget } showFigure(); // Is this REDUNDANT now? See showFigure() below. ////////////////////////////////////////////////// } if (status == STANDING) if (!canStand()&&!hangAtPole()) initFall(FALL1, FALLDELAY); else walkTimer->start ((WALKDELAY * NSPEED) / speed, TRUE); // This additional showFigure() is to update the hero position after it is // altered by the hero-enemy deadlock fix in standOnEnemy(). Messy, but ... //////////////////////////////////////////////////////////////////////////// showFigure(); if(isInEnemy()) { walkTimer->stop(); emit caughtHero(); } }