#include "food.h" //------------------------------------------------------------------ food::food(){ meat = 0.0; danger = 1; radius = 12.0f; alpha = 255.0f; bUnderAttack = false; int margin = 20; pos.x = ofRandom(margin, ofGetWidth()-margin); pos.y = ofRandom(margin, ofGetHeight()-margin); angle = 0; modAngle = 0; trail.maxNumPts = 500; trail.color.r = 255; trail.color.g = 255; trail.color.b = 255; trail.color.a = 200; trailTimer = 0; trailRate = 0; // rasters glow.loadImage("glow.png"); } //------------------------------------------------------------------ void food::update(){ // regulate the danger if (1 < danger) danger -= 0.0025f; if (1 > danger) danger = 1; modAngle += 0.003f; float modPct = 0.5f * sin(modAngle) + 0.5f; angle += 0.002f * danger; float modSin = sin(angle) * modPct; float pct = 0.5f * modSin + 0.5f; strut(); primary_rotation = (1-pct) * 0 + (pct) * (360+meat); cout << modPct << endl; meat += 0.0025; // cout << "meat: " << meat << endl; // lay down food trail leaveTrail(); } //------------------------------------------------------------------ void food::draw(){ radius = (0.5f + sin(meat*(200/danger)) + 0.5f) * 2.0f + 12.0; // glow ofEnableAlphaBlending(); if (!bUnderAttack) { ofSetColor(255, 255, 255, ofRandom(40, 60)); } else { ofSetColor(255, 255, 255, ofRandom(10, 220)); } glow.draw(pos.x, pos.y); ofDisableAlphaBlending(); glPushMatrix(); glTranslatef(pos.x, pos.y, 0); glRotatef(primary_rotation, 0, 0, 1); // show orientation ofSetColor(255, 255, 255); //ofLine(0, 0, 0, 50); ofEnableAlphaBlending(); ofEnableSmoothing(); ofNoFill(); glLineWidth(6.0); ofSetColor(255, 255, 255, alpha); ofCircle(0, 0, radius); glLineWidth(3.0); ofSetColor(255, 255, 255, alpha); ofCircle(0, 0, radius+6.0f); ofDisableSmoothing(); ofDisableAlphaBlending(); glPopMatrix(); trail.draw(); primary_rotation++; } //------------------------------------------------------------------ void food::drop(){ alpha = 0.0f; radius = 200.0f; int margin = 0; pos.x = ofRandom(margin, ofGetWidth()-margin); pos.y = ofRandom(margin, ofGetHeight()-margin); } //------------------------------------------------------------------ void food::strut(){ ofPoint prev_pos = pos; pos.x = cos(primary_rotation*0.01f) * 2.0f + pos.x; // 3.0f is about good pos.y = sin(primary_rotation*0.01f) * 2.0f + pos.y; if (0 > pos.y) pos.y = ofGetHeight(); if (ofGetWidth() < pos.x) pos.x = 0; if (ofGetHeight() < pos.y) pos.y = 0; if (0 > pos.x) pos.x = ofGetWidth(); /* catchUpSpeed = (1.0f-pct) * speedMin + (pct) * speedMax; //catchUpSpeed = 0.004f; prevPos = pos; pos.x = catchUpSpeed * catchX + (1-catchUpSpeed) * pos.x; pos.y = catchUpSpeed * catchY + (1-catchUpSpeed) * pos.y; angle = atan2(pos.y - prevPos.y, pos.x - prevPos.x); */ } //------------------------------------------------------------------ void food::leaveTrail(){ if (0 == trailTimer) { trail.addPoint( ofPoint(pos.x, pos.y, 0) ); } if (trailRate < trailTimer++) trailTimer = 0; }