#include "ofApp.h"
//--------------------------------------------------------------
Actor::Actor(vector<glm::vec3>& location_list, vector<vector<int>>& next_index_list, vector<int>& destination_list) {
this->select_index = ofRandom(location_list.size());
while (true) {
this->select_index = (this->select_index + 1) % location_list.size();
if (next_index_list[this->select_index].size() < 1) continue;
auto itr = find(destination_list.begin(), destination_list.end(), this->select_index);
if (itr == destination_list.end()) {
destination_list.push_back(this->select_index);
break;
}
}
this->next_index = this->select_index;
this->color.setHsb(ofMap(location_list[this->select_index].x, -300, 300, 0, 255), 150, 255);
}
//--------------------------------------------------------------
void Actor::update(const int& frame_span, vector<glm::vec3>& location_list, vector<vector<int>>& next_index_list, vector<int>& destination_list) {
if (ofGetFrameNum() % frame_span == 0) {
auto tmp_index = this->select_index;
this->select_index = this->next_index;
int retry = next_index_list[this->select_index].size()+ 1;
this->next_index = next_index_list[this->select_index][(int)ofRandom(next_index_list[this->select_index].size())];
while (--retry > 0) {
auto destination_itr = find(destination_list.begin(), destination_list.end(), this->next_index);
if (destination_itr == destination_list.end()) {
if (tmp_index != this->next_index) {
destination_list.push_back(this->next_index);
break;
}
}
this->next_index = next_index_list[this->select_index][(this->next_index + 1) % next_index_list[this->select_index].size()];
}
if (retry <= 0) {
destination_list.push_back(this->select_index);
this->next_index = this->select_index;
}
}
auto param = ofGetFrameNum() % frame_span;
auto distance = location_list[this->next_index] - location_list[this->select_index];
this->location = location_list[this->select_index] + distance / frame_span * param;
this->log.push_front(this->location);
while (this->log.size() > 5) { this->log.pop_back(); }
}
//--------------------------------------------------------------
glm::vec3 Actor::getLocation() {
return this->location;
}
//--------------------------------------------------------------
deque<glm::vec3> Actor::getLog() {
return this->log;
}
//--------------------------------------------------------------
void ofApp::setup() {
ofSetFrameRate(25);
ofSetWindowTitle("openFrameworks");
ofBackground(39);
ofSetRectMode(ofRectMode::OF_RECTMODE_CENTER);
auto span = 20;
for (int x = -500; x <= 500; x += span) {
for (int y = -500; y <= 500; y += span) {
if (glm::length(glm::vec2(x, y)) > 150 && glm::length(glm::vec2(x, y)) < 300) {
this->location_list.push_back(glm::vec3(x, y, 0));
}
}
}
while (this->location_list.size() > 1000) {
int delete_index = ofRandom(this->location_list.size());
this->location_list.erase(this->location_list.begin() + delete_index);
}
for (auto& location : this->location_list) {
vector<int> next_index = vector<int>();
int index = -1;
for (auto& other : this->location_list) {
index++;
if (location == other) { continue; }
float distance = glm::distance(location, other);
if (distance <= span) {
next_index.push_back(index);
}
}
this->next_index_list.push_back(next_index);
}
for (int i = 0; i < 480; i++) {
this->actor_list.push_back(make_unique<Actor>(this->location_list, this->next_index_list, this->destination_list));
}
}
//--------------------------------------------------------------
void ofApp::update() {
int frame_span = 4;
int prev_index_size = 0;
if (ofGetFrameNum() % frame_span == 0) {
prev_index_size = this->destination_list.size();
}
for (auto& actor : this->actor_list) {
actor->update(frame_span, this->location_list, this->next_index_list, this->destination_list);
}
if (prev_index_size != 0) {
this->destination_list.erase(this->destination_list.begin(), this->destination_list.begin() + prev_index_size);
}
}
//--------------------------------------------------------------
void ofApp::draw() {
ofTranslate(ofGetWidth() * 0.5, ofGetHeight() * 0.5);
ofFill();
ofSetColor(139);
for (int i = 0; i < this->location_list.size(); i++) {
for (int k = 0; k < this->next_index_list[i].size(); k++) {
ofDrawLine(this->location_list[i], this->location_list[this->next_index_list[i][k]]);
}
ofDrawCircle(this->location_list[i], 2);
}
int index = 0;
ofColor color;
for (auto& actor : this->actor_list) {
color.setHsb(ofMap(index++, 0, this->actor_list.size(), 0, 255), 230, 255);
ofFill();
ofSetColor(color);
ofDrawRectangle(actor->getLocation(), 14, 14);
ofNoFill();
ofSetColor(255);
ofDrawRectangle(actor->getLocation(), 15, 15);
}
/*
int start = 150;
if (ofGetFrameNum() > start) {
ostringstream os;
os << setw(4) << setfill('0') << ofGetFrameNum() - start;
ofImage image;
image.grabScreen(0, 0, ofGetWidth(), ofGetHeight());
image.saveImage("image/cap/img_" + os.str() + ".jpg");
if (ofGetFrameNum() - start >= 25 * 20) {
std::exit(1);
}
}
*/
}
//--------------------------------------------------------------
int main() {
ofSetupOpenGL(720, 720, OF_WINDOW);
ofRunApp(new ofApp());
}