void HandleCollisions(){ if (ball->top() >= TOPBORDER) ball->hitTop(); if (paddle1->top() > TOPBORDER) paddle1->Stop(); if (paddle2->top() > TOPBORDER) paddle2->Stop(); if (ball->bottom() <= BOTTOMBORDER) ball->hitBottom(); if (paddle1->bottom() < BOTTOMBORDER) paddle1->Stop(); if (paddle2->bottom() < BOTTOMBORDER) paddle2->Stop(); if (ball->right() >= RIGHTBORDER){ ball->reset(); newGame = true; } if (ball->left() <= LEFTBORDER){ ball->reset(); newGame = true; } if (collided(*ball,*paddle1)) ball->hitLeftPaddle(); if (collided(*ball,*paddle2)) ball->hitRightPaddle(); }
bool Node<TBv>::collided(const Node<TBv>* query, TCollidedNodes& output) const { bool result = false; if (overlapped(query)) { /// Stores any pairs that overlapped. output.push_back(std::make_pair(this, query)); result = true; auto leftQuery = query->getLeft(); auto rightQuery = query->getRight(); auto left = getLeft(); auto right = getRight(); if (left != 0 && left->overlapped(query)) { output.push_back(std::make_pair(left, query)); left->collided(leftQuery, output); left->collided(rightQuery, output); } if (right != 0 && right->overlapped(query)) { output.push_back(std::make_pair(right, query)); right->collided(leftQuery, output); right->collided(rightQuery, output); } } return result; }
void keyinput() {extern int jump; if (key[KEY_RIGHT]) { facing = 1; player->x+=2; if (++player->framecount > player->framedelay) { player->framecount=0; if (++player->curframe > player->maxframe) player->curframe=1; } } else if (key[KEY_LEFT]) { facing = 0; player->x-=2; if (++player->framecount > player->framedelay) { player->framecount=0; if (++player->curframe > player->maxframe) player->curframe=1; } } else player->curframe=0; //handle jumping if (jump==JUMPIT) { //gravity code if (!collided(player->x + player->width/2, player->y + player->height + 5)) jump = 0; if (key[KEY_SPACE]) jump = 30; } else { player->y -= jump/3; jump--; if (collidedtop(player->x, player->y)) player->y = oldpy; } if (jump<0) { if (collided(player->x + player->width/2, player->y + player->height)) { jump = JUMPIT; while (collided(player->x + player->width/2, player->y + player->height)) player->y -= 2; } } }
void Entity::move(sfld::Vector2f direction, int frameTime, float magnitude){ EntityList* list = entityManager_->getEntities(); for (auto& it : *list){ if (it.get() != this){ float dist = sfld::Vector2f(it->getPosition() - getPosition()).length(); if (!it->isWalkthrough()){ if (dist <= TILE_SIZE*1.5f){ //need accurate collisions here MTV mtv(Collision::getCollision(getSprite(), getShape(), it->getSprite(), it->getShape())); if (!(mtv.axis == MTV::NONE.axis && mtv.overlap == MTV::NONE.overlap)){; //collided sfld::Vector2f n = mtv.axis; sfld::Vector2f comp_u(0, 0); if (direction.dot(n) < 0){ if (n != sfld::Vector2f(0, 0)){ comp_u = n * (direction.dot(n) / n.dot(n)); //component of hit axis in dir } } direction = direction - comp_u; collided(it.get()); if (it->getDynamic() == DYNAMIC_STATIC){ //because then it won't resolve its own collisions it->collided(this); } } } } else{//otherwise, it's a circle, and we are only concerned with checking if they touch, no more if (dist <= TILE_SIZE*1.5f){ MTV mtv(Collision::getCollision(getSprite(), getShape(), it->getSprite(), it->getShape())); if (!(mtv.axis == MTV::NONE.axis && mtv.overlap == MTV::NONE.overlap)){ collided(it.get()); if (it->getDynamic() == DYNAMIC_STATIC){ //because then it won't resolve its own collisions it->collided(this); } } } } } } if (direction != sf::Vector2f(0, 0) && !rotating_){ //lastdir = dir; sprite_.setRotation(maths::toDegrees(atan2(direction.y, direction.x))); } doOffset(direction*(float)frameTime*magnitude); }
void collide(ex::EventManager &events) { for (const std::vector<Candidate> &candidates : grid) { for (const Candidate &left : candidates) { for (const Candidate &right : candidates) { if (left.entity == right.entity) continue; if (collided(left, right)) events.emit<tev::Collision>(left.entity, right.entity); } } } }
QVariant ContactSensor::generateEmpty() { QVariantMap data = Sensor::generateEmpty("Contact sensor", contactSensor).toMap(); BrainIn collided(0,1); collided.connectRandomly(); data.insert("collisionInput", collided.serialize()); return data; }
const SUCCESS StaticObject::checkCollision(PhysicalObject &object) { if(getCollider() == NULL || object.getCollider() == NULL) { return SUCCEEDED; } else { Radians angle; if(getCollider()->canCollide(object.getCollider()->getType())) if(getCollider()->collides(*object.getCollider(), &angle)) return collided(object, angle); // this ensures that object collider is called by this collided function, likewise a derived class needs to make sure its own derived collided function is called else return SUCCEEDED; else if(object.getCollider()->canCollide(getCollider()->getType())) if(object.getCollider()->collides(*getCollider(), &angle)) return collided(object, angle); else return SUCCEEDED; else return FAILED; } }
void PhysicsComponent::moveWithCollisions(int frameTime){ sfld::Vector2f movement = getVelocity() * (float)frameTime; sfld::Vector2f newPos = parent_->getPosition() + movement; sf::FloatRect boundings = parent_->getBoundings(); boundings.left = newPos.x; boundings.top = newPos.y; FloatOrientedRect orientedBoundings = getOrientedBoundings(); PhysicsContainer potentialCollisions = collisionGrid_->getPotentialCollisions(orientedBoundings); PhysicsContainer::iterator it; sfld::Vector2f futureMtv; for (it = potentialCollisions.begin(); it != potentialCollisions.end(); it++){ if (*it != this){ sfld::Vector2f mtv = orientedBoundings.checkForSATCollision((*it)->getOrientedBoundings()); if (mtv != sfld::Vector2f(0, 0)){ //collision //hack if (movement != sfld::Vector2f(0, 0)){ if (abs(futureMtv.x) < abs(mtv.x)){ futureMtv.x += mtv.x; } if (abs(futureMtv.y) < abs(mtv.y)){ futureMtv.y += mtv.y; } } (*it)->collided(parent_); collided((*it)->parent_); } } } newPos -= futureMtv; parent_->setPosition(newPos); }
void updatebombs(){ int i,j; for(i=0;i<20;i++){ if(bomb[i].alive){ int dieatlast=0; bomb[i].x=bomb[i].x+bomb[i].velx; bomb[i].y-=bomb[i].jumpfactor/10; bomb[i].jumpfactor--; if(bomb[i].x<(mapxoff-WIDTH) || bomb[i].x>mapxoff+WIDTH || bomb[i].y<mapyoff || bomb[i].y>mapyoff+HEIGHT){ bomb[i].alive=0; } else if(bomb[i].y>mapblockheight*mapheight-30) bomb[i].alive=0; if( bomb[i].x-mapxoff>0 && (bomb[i].x-mapxoff)<(WIDTH-bomb[i].width)) bomb[i].draw(); if(bomb[i].jumpfactor<0){ if(collided(bomb[i].x+bomb[i].width/2,bomb[i].y+bomb[i].height)) { bomb[i].jump=false; while(collided(bomb[i].x+bomb[i].width/2,bomb[i].y+bomb[i].height)) bomb[i].y-=2; if(i<10){ for(int k=0;k<20;k++){ if(bulldogs[k].alive && bulldogs[k].hitpoints>0){ if(met(bulldogs[k].x,bulldogs[k].y,bomb[i].x-BOMBRADIUS,bomb[i].y-BOMBRADIUS,bulldogs[k].width,BOMBRADIUS*2,bulldogs[k].height,BOMBRADIUS*2)){ scores+=500; bulldogs[k].hitpoints-=bomb[i].damagept; if((bulldogs[k].hitpoints)<=0){ bulldogs[k].type2=REBELDYING; bulldogs[k].totalframes=REBELMAX[REBELDYING]; bulldogs[k].curframe=0; bulldogs[k].velx=0; bulldogs[k].jump=false; } } } if(k>=MAXBOMBERS) continue; if(bombman[k].alive && bombman[k].hitpoints>0){ if(met(bombman[k].x,bombman[k].y,bomb[i].x-BOMBRADIUS,bomb[i].y-BOMBRADIUS,bombman[k].width,BOMBRADIUS*2,bombman[k].height,BOMBRADIUS*2)){ scores+=1000; bombman[k].hitpoints-=bomb[i].damagept; if((bombman[k].hitpoints)<=0){ bombman[k].type2=REBELDYING; bombman[k].totalframes=REBELMAX[REBELDYING]; bombman[k].curframe=0; bombman[k].velx=0; bombman[k].jump=false; } } } } //bomb[i].alive=0; } if(i>=10 && inputflag){ if(met(tarma.x,tarma.y,bomb[i].x-BOMBRADIUS,bomb[i].y-BOMBRADIUS,tarma.width,BOMBRADIUS*2,tarma.height,BOMBRADIUS*2)){ tarma.hitpoints-=bomb[i].damagept; if(tarma.hitpoints<=0) resetplayer(); bomb[i].alive=0; } } bomb[i].alive=0; } } if(!bomb[i].alive) continue; for(j=0;j<20;j++){ if(bulldogs[j].alive &&i<10 && bulldogs[j].hitpoints>0){ if(met(bulldogs[j].x,bulldogs[j].y,bomb[i].x-BOMBRADIUS,bomb[i].y-BOMBRADIUS,bulldogs[j].width,BOMBRADIUS*2,bulldogs[j].height,BOMBRADIUS*2)){ dieatlast=1; scores+=500; bulldogs[j].hitpoints-=bomb[i].damagept; if((bulldogs[j].hitpoints)<=0){ bulldogs[j].type2=REBELDYING; bulldogs[j].totalframes=REBELMAX[REBELDYING]; bulldogs[j].curframe=0; bulldogs[j].velx=0; bulldogs[j].jump=false; } } } if(j>=MAXBOMBERS) continue; if(bombman[j].alive &&i<10 && bombman[j].hitpoints>0){ if(met(bombman[j].x,bombman[j].y,bomb[i].x-BOMBRADIUS,bomb[i].y-BOMBRADIUS,bombman[j].width,BOMBRADIUS*2,bombman[j].height,BOMBRADIUS*2)){ dieatlast=1; scores+=1000; bombman[j].hitpoints-=bomb[i].damagept; if((bombman[j].hitpoints)<=0){ bombman[j].type2=REBELDYING; bombman[j].totalframes=REBELMAX[REBELDYING]; bombman[j].curframe=0; bombman[j].velx=0; bombman[j].jump=false; } } } } if(dieatlast) bomb[i].alive=0; if(!bomb[i].alive) continue; if(i>=10 && inputflag){ if(met(tarma.x,tarma.y,bomb[i].x-BOMBRADIUS,bomb[i].y-BOMBRADIUS,tarma.width,BOMBRADIUS*2,tarma.height,BOMBRADIUS*2)){ tarma.hitpoints-=bomb[i].damagept; if(tarma.hitpoints<=0) resetplayer(); bomb[i].alive=0; } } } } }
void updatebombers(int& spawn){ int difficulty=2+tarma.x*10/(mapblockwidth*mapwidth); if(spawn==1){ int n,i; n=rand()%difficulty; for(i=0;i<MAXBOMBERS && n>0;i++){ if(!(bombman[i].alive)){ bombman[i].alive=1; bombman[i].totalframes=14; bombman[i].x=mapxoff+WIDTH+rand()%1200; bombman[i].y=SCREEN_H/2; bombman[i].curframe=0; bombman[i].hitpoints=2; bombman[i].jump=false; if((bombman[i].x+bombman[i].width/2)<(tarma.x+tarma.width/2)){ bombman[i].velx=rand()%3; bombman[i].facing=RIGHT; bombman[i].type2=ENEMY; bombman[i].curframe=0; } else { bombman[i].velx=-(rand()%3); bombman[i].facing=LEFT; bombman[i].type2=ENEMY; bombman[i].curframe=0; } n--; } } spawn=0; } for(int i=0;i<MAXBOMBERS;i++){ if(bombman[i].alive && bombman[i].hitpoints>0){ if(bombman[i].jump==false){ if(!collided(bombman[i].x+bombman[i].width/2,bombman[i].y+bombman[i].height+5)){ bombman[i].jump=true; bombman[i].jumpfactor = -1; } } if(bombman[i].jump==false){ for(int j=0;j<25;j++){ if(pistols[j].alive){ int dist=pistols[j].x-bombman[i].x; if(((dist>50 && dist<80) || (dist<-50 && dist>-80)) && pistols[j].y>bombman[i].y && pistols[j].y<bombman[i].y+ bombman[i].width){ bombman[i].jump=(rand()%(40/difficulty))?false:true; bombman[i].jumpfactor=24; } } } } if(bombman[i].jump) { bombman[i].y -= bombman[i].jumpfactor/3; bombman[i].jumpfactor--; } if(bombman[i].jumpfactor<0){ if(collided(bombman[i].x+bombman[i].width/2,bombman[i].y+bombman[i].height)) { bombman[i].jump=false; while(collided(bombman[i].x+bombman[i].width/2,bombman[i].y+bombman[i].height)) bombman[i].y -= 2; } } bombman[i].lastx=bombman[i].x; bombman[i].animate(); if(bombman[i].jump) bombman[i].x=bombman[i].lastx; if((counter-bombman[i].lastcount)>bombman[i].firedelay+1){ if((bombman[i].x-mapxoff) >0 && (bombman[i].x-mapxoff)<(640-bombman[i].width)){ firebombathero(i); bombman[i].lastcount=counter; } } if(bombman[i].facing==LEFT){ if(collided(bombman[i].x,bombman[i].y+bombman[i].height/2)){ bombman[i].x=bombman[i].lastx; } } if(bombman[i].facing==RIGHT){ if(collided(bombman[i].x+bombman[i].width,bombman[i].y+bombman[i].height/2)){ bombman[i].x=bombman[i].lastx; } } if(!collided(bombman[i].x+bombman[i].width/2,bombman[i].y+bombman[i].height) ){ bombman[i].x=bombman[i].lastx; } if(met(tarma.x,tarma.y,bombman[i].x,bombman[i].y,tarma.width,bombman[i].width,tarma.height,bombman[i].height)){ bombman[i].x=bombman[i].lastx; } //bombman[i].velx=0; if((bombman[i].x+bombman[i].width/2)<(tarma.x+tarma.width/2)){ bombman[i].velx=rand()%3; bombman[i].facing=RIGHT; bombman[i].type2=REBELRIGHT; } else { bombman[i].velx=-(rand()%3); bombman[i].facing=LEFT; bombman[i].type2=REBELLEFT; } //bombman[i].type2=REBELLEFT; //bombman[i].facing=LEFT; if(bombman[i].x+bombman[i].width<mapxoff) bombman[i].alive=0; if(bombman[i].y>mapheight*mapblockheight-bombman[i].height-50 || bombman[i].y<10) bombman[i].alive=0; if(bombman[i].alive && (bombman[i].x+bombman[i].width-mapxoff)>0 && (bombman[i].x-mapxoff<WIDTH)) bombman[i].draw(); } } }
void updatehero(){ tarma.animate(); if(tarma.crouch) tarma.height=35; else tarma.height=70; if(tarma.jump==false){ if(!collided(tarma.x+tarma.width/2,tarma.y+tarma.height+5)){ tarma.jump=true; tarma.jumpfactor=-1; } } if(tarma.jump) { tarma.lasty=tarma.y; tarma.y -= tarma.jumpfactor/5; if(tarma.y<20){ tarma.y=20; } tarma.jumpfactor--; } if(tarma.jumpfactor<0){ if(collided(tarma.x+tarma.width/2,tarma.y+tarma.height)) { tarma.jump=false; lastjump=mils; while(collided(tarma.x+tarma.width/2,tarma.y+tarma.height)) tarma.y-=2; } } if(tarma.facing==LEFT){ if(collided(tarma.x,tarma.y+tarma.height)){ tarma.x=tarma.lastx; } } if(tarma.facing==RIGHT){ if(collided(tarma.x+tarma.width,tarma.y+tarma.height)){ tarma.x=tarma.lastx; } } for(int i=0;i<20;i++){ if(bulldogs[i].alive){ if(met(tarma.x,tarma.y,bulldogs[i].x,bulldogs[i].y,tarma.width,bulldogs[i].width,tarma.height,bulldogs[i].height)){ tarma.x=tarma.lastx; } } if(i>=MAXBOMBERS) continue; if(bombman[i].alive){ if(met(tarma.x,tarma.y,bombman[i].x,bombman[i].y,tarma.width,bombman[i].width,tarma.height,bombman[i].height)){ tarma.x=tarma.lastx; } } } if(tarma.y>(mapheight*mapblockheight-tarma.height-50) ){ resetplayer(); if(gameover) return; } if(tarma.x<5) tarma.x=5; mapxoff=tarma.x+tarma.width/2-WIDTH/2+10; mapyoff=tarma.y+tarma.height/2-HEIGHT/2+10; if(mapxoff<0) mapxoff=0; if(mapxoff>mapwidth*mapblockwidth-WIDTH) mapxoff=mapwidth*mapblockwidth-WIDTH; if(mapyoff<0) mapyoff=0; if(mapyoff>mapheight*mapblockheight-HEIGHT) mapyoff=mapheight*mapblockheight-HEIGHT; MapDrawBG(buffer,mapxoff,mapyoff,0,0,WIDTH-1,HEIGHT-1); MapDrawFG(buffer,mapxoff,mapyoff,0,0,WIDTH-1,HEIGHT-1,0); tarma.draw(); }
void PhysicsBodyComponent::onCollide(PhysicsBodyComponent* other_body) { emit collided(other_body, this); }
//Main codee // int main(void){ int mapxoff, mapyoff; int oldpy, oldpx; int titleX, titleY; int facing = 0; int jump = JUMPIT; int n, VOLUME = 128, PAN = 128, PITCH = 1000; //general initialize allegro_init(); install_timer(); install_keyboard(); set_color_depth(16); set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0); set_window_title(" . . .J.U.M.P.E.R. . . :: Ryan Bridglal ::"); // //sound init install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, ""); //load title title = load_bitmap("title.bmp", NULL); //gameOver = load_bitmap("gameover.bmp", NULL); // load the player sprite temp = load_bitmap("player.bmp", NULL); for (n=0; n < 8; n++) player_image[n] = grabframe(temp, 50, 64, 0, 0, 8, n); destroy_bitmap(temp); // load the sound file music = load_sample("music.wav"); //jumpSound = load_sample("jump.wav"); // initialize the sprite player = new SPRITE; player->x = 80; player->y = 100; player->curframe = 0; player->framecount = 0; player->framedelay = 6; player->maxframe = 7; player->width = player_image[0]->w; player->height = player_image[0]->h; // load the map MapLoad("map.FMP"); // create the double buffer buffer = create_bitmap(WIDTH, HEIGHT); clear(buffer); //always play background music play_sample(music, VOLUME, PAN, PITCH, FALSE); // main loop while(!key[KEY_ESC]) { oldpy = player->y; oldpx = player->x; // // PLAYER controls // //right keypress if (key[KEY_RIGHT]) { clear_to_color(title, makecol(80, 80, 248)); facing = 1; player->x += 2; if (++player->framecount > player->framedelay) { player->framecount = 0; if (++player->curframe > player->maxframe) player->curframe = 1; } } //left keypress else if (key[KEY_LEFT]) { clear_to_color(title, makecol(80, 80, 248)); facing = 0; player->x -= 2; if (++player->framecount > player->framedelay) { player->framecount = 0; if (++player->curframe > player->maxframe) player->curframe = 1; } } else player->curframe = 0; // handle jumping if (jump == JUMPIT){ if (!collided(player->x + player->width/2, player->y + player->height + 5)) jump = 0; if (key[KEY_SPACE]) jump = 30; //play_sample(jumpSound, VOLUME, PAN, PITCH, FALSE); } else { player->y -= jump/3; jump--; } if (jump < 0){ if (collided(player->x + player->width/2, player->y + player->height)) { jump = JUMPIT; while (collided(player->x + player->width/2, player->y + player->height)) player->y -= 2; } } /// //collision detection // player collision detect with map if (!facing) { if (collided(player->x, player->y + player->height)) player->x = oldpx; } else { if (collided(player->x + player->width, player->y + player->height)) player->x = oldpx; } // update the map scroll position mapxoff = player->x + player->width/2 - WIDTH/2 + 10; mapyoff = player->y + player->height/2 - HEIGHT/2 + 10; // avoid moving beyond the map edge if (mapxoff < 0) mapxoff = 0; if (mapxoff > (mapwidth * mapblockwidth - WIDTH)) mapxoff = mapwidth * mapblockwidth - WIDTH; if (mapyoff < 0) mapyoff = 0; if (mapyoff > (mapheight * mapblockheight - HEIGHT)) mapyoff = mapheight * mapblockheight - HEIGHT; // //Draw things // draw the background tiles MapDrawBG(buffer, mapxoff, mapyoff, 0, 0, WIDTH - 1, HEIGHT - 1); // draw the foreground tiles MapDrawFG(buffer, mapxoff, mapyoff, 0, 0, WIDTH - 1, HEIGHT - 1, 0); // draw the player's sprite if (facing) draw_sprite(buffer, player_image[player->curframe], (player->x - mapxoff), (player->y - mapyoff)); else draw_sprite_h_flip(buffer, player_image[player->curframe], (player->x - mapxoff), (player->y - mapyoff)); //load title //draw_sprite(screen, title, 160, 20); //draw_sprite(buffer, title[titleSprite->curframe], (title->titleX - mapxoff), (title->y - mapyoff)); blit(title, screen, 0, 0, 0, 0, 640, 480); // blit the double buffer vsync(); acquire_screen(); blit(buffer, screen, 0, 0, 0, 0, WIDTH - 1, HEIGHT - 1); release_screen(); }// endwhile // else { // blit(gameOver, screen, 0, 0, 0, 0, 640, 480); //} gameOver(); // clean up for (n = 0; n < 8; n++) destroy_bitmap(player_image[n]); delete player; delete title; // for (n=0; n<NUM_SOUNDS; n++) // destroy_sample(sounds[n]); destroy_sample(music); // destroy_sample(jumpSound); destroy_bitmap(title); destroy_bitmap(buffer); MapFreeMem(); remove_sound(); allegro_exit(); return 0; }
void PhysicsBodyInfo::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &change) { Qt3DCore::QScenePropertyChangePtr e = qSharedPointerCast< Qt3DCore::QScenePropertyChange>(change); if (e->type() == Qt3DCore::NodeUpdated) { /*if (e->propertyName() == QByteArrayLiteral("attachPhysicsTransfrom")) { bool val = e->value().toBool(); if(val){ QVector<Qt3D::QEntity*> entities = this->entities(); if (entities.size() > 1) qWarning()<< "It is strongly discouraged to share PhysicsBodyInfo component between entities"; Q_FOREACH(Qt3D::QEntity* e, entities){ bool hasPhysicsTransform=false; QMatrix4x4 m; Q_FOREACH(Qt3D::QComponent* c, e->components()){ if(c->inherits("Qt3D::QTransform") && c->objectName().compare("@MaDeByPhYsIcS@")!=0){ c->setEnabled(false); m=qobject_cast<Qt3D::QTransform*>(c)->matrix(); } else if(c->inherits("Qt3D::QTransform") && c->objectName().compare("@MaDeByPhYsIcS@")==0){ hasPhysicsTransform=true; } } if(!hasPhysicsTransform){ Qt3D::QTransform* physics_transform=new Qt3D::QTransform(this->parentNode()); physics_transform->setObjectName("@MaDeByPhYsIcS@"); Qt3D::QMatrixTransform* matrix= new Qt3D::QMatrixTransform(m,physics_transform); physics_transform->addTransform(matrix); e->addComponent(physics_transform); m_attached_matrix=matrix; } } } }*/ if(e->propertyName() == QByteArrayLiteral("updateTransform")){ QMatrix4x4 mat= e->value().value<QMatrix4x4>(); m_outputTransform->setMatrix(mat); emit outputTransformChanged(); } if(e->propertyName() == QByteArrayLiteral("notifyCollision")){ PhysicsCollisionEventPtrList collisions_list=e->value().value<PhysicsCollisionEventPtrList>(); if(collisions_list.size()==0){ if(m_hasCollided){ m_hasCollided=false; emit hasCollidedChanged(m_hasCollided); } m_collisionsList.clear(); m_collisionsCache.clear(); emit collisionsListChanged(); } else{ m_collisionsList.clear(); m_collisionsCache.clear(); Q_FOREACH(PhysicsCollisionEventPtr event_ptr,collisions_list){ m_collisionsList.append(event_ptr); m_collisionsCache.insert(event_ptr->target()); if(m_collisionsList.last()->isNew()){ emit collided(m_collisionsList.last()); } } if(!m_hasCollided){ m_hasCollided=true; emit hasCollidedChanged(m_hasCollided); } emit collisionsListChanged(); } }
int main (void) { // Golobal Integers extern int level; extern int lifes; extern int score; extern int health; // Local Integers register int mapxoff, mapyoff; extern int oldpy, oldpx; extern int facing; extern int jump; int n, m; // DATAFILE *data; //initilization allegro_init(); install_timer(); install_keyboard(); set_color_depth(32); set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0); //load data data = load_datafile("test.dat"); temp = (BITMAP *)data[NEWGUY_BMP].dat; for (n=0; n<4; n++) player_image[n] = grabframe(temp,43,77,0,0,4,n); destroy_bitmap(temp); temp2 = (BITMAP *)data[BAT1_BMP].dat; for (m=0; m<2; m++) bat_img[m] = grabframe(temp2,87,72,0,0,2,m); destroy_bitmap(temp2); test_img = load_bitmap("test.bmp", NULL); gameover: Start(); first: //initilize Player player = malloc(sizeof(SPRITE)); player->x = 80; player->y = 200; player->curframe=0; player->framecount=0; player->framedelay=11; player->maxframe=3; player->width=player_image[0]->w; player->height=player_image[0]->h; //initilize Bat bat = malloc(sizeof(SPRITE)); bat->x = 170; bat->y = 100; bat->curframe=0; bat->framecount=0; bat->framedelay=11; bat->maxframe=1; bat->width=bat_img[0]->w; bat->height=bat_img[0]->h; bat->dir = 0; bat->xspeed = 0; //ini test test = malloc(sizeof(SPRITE)); test->x = 270; test->y = 400; test->curframe=0; test->framecount=0; test->framedelay=11; test->maxframe=1; test->width=bat_img[0]->w; test->height=bat_img[0]->h; test->dir = 0; test->xspeed = 0; loadlevel(); //main loop while (!key[KEY_ESC]) { oldpy = player->y; oldpx = player->x; if (player->x > 3000) {level = 2; goto first;} if (lifes == 0) {lifes = 3; goto gameover;} if (health < 1) {lifes -=1; health =100; goto first;} updateEnemy(150, 300, test); updateEnemy(150, 300, bat); enemycoll(player->x, player->y, bat); keyinput(); //collition control if (!facing) { if (collided(player->x, player->y + player->height)) player->x = oldpx; if (collidedobj(player->x, player->y + player->height)) {ClearCell(player->x, player->y + player->height); AddScore();} if (collidedkill(player->x+9, player->y + player->height)) {health -= 25;} } else { if (collided(player->x + player->width, player->y + player->height)) player->x = oldpx; if (collidedobj(player->x + player->width, player->y + player->height)) {ClearCell(player->x + player->width, player->y + player->height); AddScore();} if (collidedkill(player->x + player->width-15, player->y + player->height)) {health -= 25;} } //update the map scroll position mapxoff = player->x + player->width/2 - WIDTH/2 + 10; mapyoff = player->y + player->height/2 - HEIGHT/2 + 10; //avoid moving beyond the map edge if (mapxoff < 0) mapxoff = 0; if (mapxoff > (mapwidth * mapblockwidth - WIDTH)) mapxoff = mapwidth * mapblockwidth - WIDTH; if (mapyoff < 0) mapyoff = 0; if (mapyoff > (mapheight * mapblockheight - HEIGHT)) mapyoff = mapheight * mapblockheight - HEIGHT; //draw the background tiles MapDrawBG(buffer, mapxoff, mapyoff, 0, 0, WIDTH-1, HEIGHT-1); //draw foreground tiles MapDrawFG(buffer, mapxoff, mapyoff, 0, 0, WIDTH-1, HEIGHT-1, 0); //draw the player's sprite if (facing) draw_sprite(buffer, player_image[player->curframe], (player->x-mapxoff), (player->y-mapyoff+1)); else draw_sprite_h_flip(buffer, player_image[player->curframe], (player->x-mapxoff), (player->y-mapyoff)); //blit the double buffer vsync(); acquire_screen(); if (bat->dir == 1) draw_sprite(buffer, bat_img[bat->curframe],(bat->x-mapxoff),(bat->y-mapyoff)); else draw_sprite_h_flip(buffer, bat_img[bat->curframe],(bat->x-mapxoff),(bat->y-mapyoff)); draw_sprite(buffer, test_img,(test->x-mapxoff),(test->y-mapyoff)); textprintf(buffer,font,0,0,9999999,"lifes = %d Score = %d Health = %d",lifes,score,health); blit(buffer, screen, 0, 0, 0, 0, WIDTH-1, HEIGHT-1); release_screen(); } //End of game loop for (n=0; n<4; n++) destroy_bitmap(player_image[n]); for (m=0; m<2; m++) destroy_bitmap(bat_img[m]); destroy_bitmap(test_img); free(player); free(bat); free(test); destroy_bitmap(buffer); MapFreeMem (); MapFreeMem (); unload_datafile(data); allegro_exit(); return 0; }