summaryrefslogtreecommitdiff
path: root/gaunt01/src/bird.cpp
blob: a6dfcf8ed6f841195d6c83da685d4ee03300fc05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "bird.h"

/*
keep track of the high-level properties of the bird here

activate and control sequences of targets in the morphmesh

finally drawAnimated()

*/

bird::bird()
{
	if (model.loadMesh("Bird-poses.xml")) printf("mesh loaded with %i vertices, %i face indices, %i targets\n",model.getNumVertices(),model.getNumIndices(),model.getNumTargets());
	else printf("mesh XML file not parsed\n");

	if (model.loadSeqs("Bird-anim.xml")) printf("animation loaded with %i sequences\n",model.getNumSequences());
	else printf("animation XML file not parsed\n");

	model.sequences["flap"].start();
	currentseq="hover";

	texture.loadImage("TextureBird.jpg");

	//starting pos
	position=ofVec3f(ofGetWidth()/2,ofGetHeight(),-ofGetHeight()/10);
	heading=-90;
	direction=ofVec3f(0,-1,0); //director for a heading of 0, level
	velocity=ofGetWidth()/50;

	turnRate=20;
	diveRate=0;
	
	fieldofview=60;

	lastTime=ofGetElapsedTimef();
	
	centrePoint=ofVec2f(ofGetWidth()/2,600); //quick and dirty
}

bird::~bird()
{
    //dtor
}

void bird::update(map<int,player>& players, float angle,vector<ofPoint> border){
	float time=ofGetElapsedTimef();
	float timeSeg=time-lastTime;
	lastTime=time;
	heading=heading+(turnRate*timeSeg);
	while (heading>180) heading=heading-360;
	while (heading <-180) heading=heading+360;

	position-=direction.rotated(heading,ofVec3f(0,0,-1))*velocity*timeSeg; //.rotate(heading,ofVec3f(0,1,0))
	
	//absolute ray pointer
	pointer=ofRay(position.rotated(angle,ofVec3f(1,0,0)),-direction.rotated(heading,ofVec3f(0,0,-1)).rotated(angle,ofVec3f(1,0,0))*1000.0f,false);

	ofRay relpointer=ofRay(position,-direction.rotated(heading,ofVec3f(0,0,-1))*1000.0f,false);
	
	playang.clear();
	playdist.clear();
	playpos.clear();
	playhead.clear();
	playdip.clear();
	
	map<int,player>::iterator it;
	for (it=players.begin();it!=players.end();it++) {
		if (it->second.active) {
			ofVec3f p=it->second.getWorldPosition()-position;
			playang.push_back(p.angle(-direction.rotated(heading,ofVec3f(0,0,-1))));
			playdist.push_back(p.length());
			playpos.push_back(it->second.getWorldPosition());
			float headif=(atan2(it->second.getWorldPosition().x-position.x,it->second.getWorldPosition().y-position.y)*RAD_TO_DEG)-heading;
			while (headif>180) headif=headif-360;
			while (headif <-180) headif=headif+360;
			playhead.push_back(headif);
		}
	}
	//check if flying out of play
	ofVec3f futurepos=position-direction.rotated(heading,ofVec3f(0,0,-1))*velocity*2.0;
	ofPoint futurepoint=ofPoint(futurepos.x,futurepos.y);
	centrehead=atan2(position.x-centrePoint.x,position.y-centrePoint.y)*RAD_TO_DEG;
	while (centrehead>180) centrehead=centrehead-360;
	while (centrehead <-180) centrehead=centrehead+360;
	if (OutsidePolygon(border,futurepoint)) {
	//if (ofVec
		leaving=true;
		turnRate=centrehead/5;
	}
	else {
		leaving=false;
		//fly towards nearest visible player
		int nearest=-1;
		float distance=9999;
		for(int i=0;i<playhead.size();i++) {
			if ((abs(playhead[i])<(fieldofview/2))&&playdist[i]<distance) {
				nearest=i;
				distance=playdist[i];
			}
		}
		if (nearest>-1) turnRate=-playhead[nearest];
	}
	
}
void bird::draw(){
	glEnable(GL_DEPTH_TEST);
	ofPushMatrix();
		ofTranslate(position);
		ofRotate(90,0,0,1);
		ofRotate(90,-1,0,0);
		ofRotate(heading+90,0,1,0);
		ofSetHexColor(0xffffff);
		ofScale(.25,.25,.25);
		bindTexture(texture);
            model.drawAnimated();
		unbindTexture(texture);
	ofPopMatrix();
	glDisable(GL_DEPTH_TEST);
}
void bird::drawShadow(){
    ofPushMatrix();
		ofTranslate(position);
		ofRotate(90,0,0,1);
		ofRotate(90,-1,0,0);
		ofRotate(heading+90,0,1,0);
        ofSetHexColor(0x303030);
        ofTranslate(0,(-ofGetHeight()/4)+5,0);
        ofScale(.15,0,.15);
        model.drawAnimated();
    ofPopMatrix();
}
void bird::drawDebug(){
	if (leaving) ofSetHexColor(0xff0000);
        else ofSetHexColor(0xff00ff);
        ofLine(pointer.s,pointer.s+1000*pointer.t);
}