void World::update() { for(int i=0; i < fishes.size(); ++i) { //各fishがどこの領域に属するかを更新 Fish* fish = fishes.at(i); vec2d pos = fish->get_pos(); int pre_idx = fish->get_pidx(); int idx = pos.x / (int)(width / PARTITION_X) + (int)(pos.y / (int)(height / PARTITION_Y))*PARTITION_X; if(pre_idx != idx) { partitons.at(pre_idx).erase(partitons.at(pre_idx).find(fish->get_id())); partitons.at(idx).insert(std::make_pair(fish->get_id(), fish)); fish->set_pidx(idx); } fishes.at(i)->update(this); } for(int i=0; i < sharks.size(); ++i) { int idx = sharks.at(i)->get_pos().x / (int)(width / PARTITION_X) + (int)(sharks.at(i)->get_pos().y / (int)(height / PARTITION_Y))*PARTITION_X; sharks.at(i)->set_pidx(idx); sharks.at(i)->update(this); } }
World::World(): width(640), height(640) { partitons.resize(PARTITION_X*PARTITION_Y); for(int i = 0; i < 400; ++i) { //位置はランダム、初期速度はゼロ Fish* fish = new Fish(vec2d(100+(double)rand()/RAND_MAX*400, 100+(double)rand()/RAND_MAX*400), vec2d()); fishes.push_back(fish); //どこの領域にいるかを計算・更新 int idx = fish->get_pos().x / (int)(width / PARTITION_X) + (int)(fish->get_pos().y / (int)(height / PARTITION_Y))*PARTITION_X; partitons.at(idx).insert(std::make_pair(fish->get_id(), fish)); fish->set_pidx(idx); } //三匹sharkを追加 sharks.push_back(new Shark(vec2d(100,100), vec2d(0,0))); sharks.push_back(new Shark(vec2d(250,250), vec2d(0,0))); sharks.push_back(new Shark(vec2d(450,450), vec2d(0,0))); }