Esempio n. 1
0
double distancePointPoint( const Point2D &a, const Point2D &b )
{
    return distancePointPoint( Point(a), Point(b) );
}
Esempio n. 2
0
void datacollector::updateData(vector<Point2f> newData,double minDist){
    if(newData.size()>0 && newData.size()<300){
        ///POPULATE DISTANCE MATIX
        for(int i=0;i<newData.size();i++){
            used[i]=false;
        }
        for(mapIt = data.begin(); mapIt != data.end( ); ++mapIt){
            int numB=0;
            for (it = newData.begin(); it!=newData.end(); ++it){
                distances[mapIt->first][numB]=distancePointPoint(Point2f(mapIt->second.x,mapIt->second.y),*it);
                numB++;
            }
        }
        ///* TO AID IN THE SEARCH *///
        map<int,Point2f> aux=data;
        bool buscar=true;
        while(buscar){
            int mI=0;
            Point2f pI(0,0);
            double dist=1000000000;
            bool hayMin=false;
            int numB=0,numU=10000000;
        for (it = newData.begin(); it!=newData.end(); ++it){
            if(!used[numB]){
           for(mapIt = aux.begin(); mapIt != aux.end( ); ++mapIt){
               if(dist>distances[mapIt->first][numB]){
                   dist=distances[mapIt->first][numB];
                   hayMin=true;
                   mI=mapIt->first;
                   pI.x=it->x;pI.y=it->y;
                   numU=numB;
               }
           }
           }
           numB++;
        }
        if(hayMin){
            used[numU]=true;
            ///UPDATE THE VALUE
            /// HERE CHANGE UPDATE ONLY IF DISTANCE IS MORE THAN...
            if(distances[mI][numU]>minDist){
                data[mI]=pI;
            }
            aux.erase(aux.find(mI));
            if(onUpdate==NULL){
            cout << "set cur " << mI << " : " << data[mI].x << "," <<  data[mI].y << endl;
            if(useMouse && mI==0){
                tmw->move(data[mI].x,data[mI].y);
            }
            }
            /////////////////////////////////////////////////////////////////////////////////////////////////////
            else{onUpdate(mI,data[mI].x,data[mI].y);}
            /////////////////////////////////////////////////////////////////////////////////////////////////////
        }
        else{
            buscar=false;
        }
        }
        for(mapIt = aux.begin(); mapIt != aux.end( ); ++mapIt ){
            //REMOVE THOSE NOT USED
            data.erase(data.find(mapIt->first));
            if(onRemove==NULL){
            cout << "del cur " << mapIt->first << endl;
            if(useMouse && mapIt->first==0){
                tmw->release();
            }
            }
            /////////////////////////////////////////////////////////////////////////////////////////////////////
            else{onRemove(mapIt->first);}
            /////////////////////////////////////////////////////////////////////////////////////////////////////
        }
        int numB=0;
        for (it = newData.begin(); it!=newData.end(); ++it){
            if(!used[numB]){
                ///CHECK FOR FREE ID AND ASSIGN THE NEW VALUE:
                for(int u=0;u<300;u++){
                    if(data.count(u)==0){
                        data[u]=Point2f(it->x,it->y);
                        if(onAdd==NULL){
                            cout << "add cur "<< u << " : " << it->x <<"," <<it->y << endl;
                            if(useMouse && u==0){
                                tmw->press(it->x,it->y);
                            }
                        }
                        /////////////////////////////////////////////////////////////////////////////////////////////////////
                        else{
                            onAdd(u,it->x,it->y);
                        }
                        /////////////////////////////////////////////////////////////////////////////////////////////////////
                        break;
                    }
                }
            }
            numB++;
        }
        //cout << "DATA SIZE : " << data.size() << endl;
    }
    else{
        if(data.size()>0){
        for(mapIt = data.begin(); mapIt != data.end( ); ++mapIt ){
            //REMOVE ALL
            if(onRemove==NULL){
            cout << "del cur " << mapIt->first << endl;
            if(useMouse && mapIt->first==0){
                tmw->release();
            }
            }
            /////////////////////////////////////////////////////////////////////////////////////////////////////
            else{onRemove(mapIt->first);}
            /////////////////////////////////////////////////////////////////////////////////////////////////////
        }
        data.clear();
        }
    }
}
Esempio n. 3
0
void Game::update() {
	sf::Time t = clock.getElapsedTime();
	updateDirection();

	sf::Vector2i pixelPos = sf::Mouse::getPosition(window);
	sf::Vector2f v = window.mapPixelToCoords(pixelPos) - player.position;

	float len = sqrt(powf(v.x, 2) + powf(v.y, 2));
	// vector unitari que va del centre del jugador
	// en direcció a on apuntem
	player.facing = v / len;

	// get hurt!
	for (auto &enemy : enemies) {
        enemy.update(player, map);
		enemy.collisions(map);
		if (distancePointPoint(enemy.position, player.position) < 30.f) {

			enemy.attacking_timeout = clock.getElapsedTime() + sf::milliseconds(250);

			player.hit(player.position - enemy.position);
			flash_timeout = clock.getElapsedTime() + sf::milliseconds(40);

			player.hp -= 20;
			hurt_sound.play();
			if (player.hp <= 0) {
                death_scream_sound.play();
                music.stop();
				next_game_state = GameOver;
				input_timeout = clock.getElapsedTime() + sf::milliseconds(1000);
				game_over_sound.play();
				gameOver_music.play();
			}
		}
	}

	player.update();
	player.collisions(map);

	for (auto &bullet : bullets) {
        bullet.update(map);

		for (auto &enemy : enemies) {
			// if we already hit that enemy, don't hit it again
			if (std::find(
						bullet.enemies_hit.begin(),
						bullet.enemies_hit.end(),
						&enemy) != bullet.enemies_hit.end()) {
				continue;
			}
			if (lineCrossesCircle(bullet.prev_pos, bullet.position, enemy.position, enemy.radius)) {
				bullet.enemies_hit.push_back(&enemy);
				if (rand() % 3 == 0) {
					splashBlood(enemy, bullet.vvel, 10);
					enemy.stagger_timeout = clock.getElapsedTime() + sf::milliseconds(250);
				} else {
				    zombie_death_sound.play();
					enemy.alive = false;
					//bullet.alive = false;

					splashBlood(enemy, bullet.vvel);
					if (rand() % 10 == 0) {
						Magazine mag(&textures[3], &clock, &window, enemy.position);
						magazines.push_back(mag);
					}
				}
			}
		}
	}
	spent_in_a += clock.getElapsedTime() - t;
	t = clock.getElapsedTime();

	for (auto &part : particles) {
        part.update(map);
	}

	for (auto &knife : knives) {
        knife.update(map);
		if (knife.alive) {
			if (knife.vvel.x != 0 && knife.vvel.y != 0) {
				for (auto &enemy : enemies) {
					if (distancePointPoint(knife.position, enemy.position) < enemy.radius) {
						enemy.alive = false;
						splashBlood(enemy, knife.vvel);

						if (rand() % 6 == 0) {
							Magazine mag(&textures[3], &clock, &window, enemy.position);
							magazines.push_back(mag);
						}
					}
				}
			} else {
				if (distancePointPoint(knife.position, player.position) < player.radius + knife.radius) {
					knife.alive = false;
				}
			}
		}
	}

	for (auto &mag : magazines) {
		if (distancePointPoint(mag.position, player.position) < player.radius + 3) {
			mag.alive = false;
			player.extra_ammo += mag_size;
		}
	}

	if (enemies.size() == 0 &&
			distancePointPoint(player.position, warp_pos) < 30) {
		map_n += 1;
		if(loadMap(map_n) != 0) {
			// fin
			music.stop();
			next_game_state = GameOver;
			input_timeout = clock.getElapsedTime() + sf::milliseconds(1000);
			victory_music.play();
		}
	}

	// el jugador al centre de la pantalla
	sf::View view = window.getView();
	view.setCenter(player.position);
	window.setView(view);
	spent_in_b += clock.getElapsedTime() - t;
}
Esempio n. 4
0
static void zhullInitialFacets(zhull_t *zh)
{
  list_t assoc = emptyList();
  list_t new_facets = emptyList();
  index_t i;
  index_t idx[3]= {0,1,2};
  list_t list=initListIndex(idx,3);
  if (getNumPoints(zh->pts)>= 3) {
    //      printf("initListFromTo: %d..%d\n", 0,getNumPoints(zh->pts)-1);
    assoc = initListFromTo(0,getNumPoints(zh->pts)-1);
    //     printList(assoc);
    new_facets = appendNewFacets(zh,2);
    if (getLength(new_facets)==2) {
      do {  // circumvent coincident points
        if (distancePointPoint(
              getPoint(zh->pts,idx[0]),
              getPoint(zh->pts,idx[1])
            )>TOL_DEGENERATE) {
          break;
        } else {
          idx[1]++;
        }
      } while (idx[1]<getNumPoints(zh->pts));
      if (idx[1]<getNumPoints(zh->pts)) {
        do { // circumvent degenerate triangles
          list=initListIndex(idx,3);
          if (lengthVector(normalOfListedPoints(zh->pts,list))>TOL_DEGENERATE) {
            break;
          } else {
            idx[2]++;
            freeList(&list);
          }
        } while (idx[2]<getNumPoints(zh->pts));
        if (idx[2]<getNumPoints(zh->pts)) {
          getFacetByIndex(new_facets,0)->corners = list;
          appendListToList(&(zh->used_pts),list);
          list=initListIndex(idx,3);
          reverseList(&list);
          getFacetByIndex(new_facets,1)->corners = list;
          getFacetByIndex(new_facets,0)->neighbors =
            initConstantList(entry_makePointer(getFacetByIndex(new_facets,1)),3);
          getFacetByIndex(new_facets,1)->neighbors =
            initConstantList(entry_makePointer(getFacetByIndex(new_facets,0)),3);
          for (i=0; i<2; i++) {
            getFacetByIndex(new_facets,i)->plane =
              planeFromListedPoints(zh->pts, getFacetByIndex(new_facets,i)->corners);
            getFacetByIndex(new_facets,i)->outsideset = emptyList();
            getFacetByIndex(new_facets,i)->insideset = emptyList();
            getFacetByIndex(new_facets,i)->maxdistance = 0.0f;
            //printf("removing facests\n");
            //printList(getFacetByIndex(new_facets,i)->corners);
            removeValueListFromList(&assoc,getFacetByIndex(new_facets,i)->corners);
          }
          //printf("dividePoints...");
          //printList(assoc);
          dividePointsBetweenNewFacets(zh, assoc, new_facets);
        }
      }
    }
    freeList(&new_facets);
    freeList(&assoc);
  }
}