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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
#include "trapdoor.h"
trapdoor::trapdoor(ofVec2f pos,float _doorSize)
{
surround=morphmesh("trapdoor-top.xml");
lid=morphmesh("trapdoor-lid.xml");
if (!surround.isLoaded()||!lid.isLoaded()) printf("problem loading trap door mesh.\n");
texture.loadImage("TextureTrapdoor.jpg");
splashFrames=new ofImage[3];
splashFrames[0].loadImage("Drops_1.tga");
splashFrames[1].loadImage("Drops_2.tga");
splashFrames[2].loadImage("Drops_3.tga");
for (int i=0;i<3;i++) {
splashFrames[i].setAnchorPercent(0.5,0.5);
}
size=_doorSize;
caught.clear();
caughtTime.clear();
doorAngle=0;
doorSpeed=0;
position=pos;
setBoundingRect(pos.x, pos.y,size,size);
active=false;
triggeredTime=-1;
}
trapdoor::~trapdoor() {
for (int i=0;i<3;i++) splashFrames[i].clear();
//delete splashFrames;
}
vector<ofVec2f> trapdoor::getCorners(){
vector<ofVec2f> corners;
corners.push_back(ofVec2f(position.x-(size/2),position.y-(size/2)));
corners.push_back(ofVec2f(position.x+(size/2),position.y-(size/2)));
corners.push_back(ofVec2f(position.x+(size/2),position.y+(size/2)));
corners.push_back(ofVec2f(position.x-(size/2),position.y+(size/2)));
return corners;
}
void trapdoor::setBoundingRect(float x,float y, float width,float height){
boundingRect=ofRectangle(x,y,width,height);
}
ofRectangle trapdoor::getBoundingRect(){
return boundingRect;
}
ofRectangle trapdoor::getInnerRect() {
//return ofRectangle(boundingRect.x-(boundingRect.width/4),boundingRect.y-(boundingRect.height/4),boundingRect.width/2,boundingRect.height/2);
return ofRectangle(position.x-(size/3),position.y-(size/3),(2*size)/3,(2*size)/3);
}
void trapdoor::trigger() {
triggeredTime=ofGetElapsedTimef();
}
/*
ofVec2f trapdoor::bounds2UV(ofVec2f point){
//returns the 0-1 UV coords of a point on the ground plane relative to its bounds.
//this should not be in the trapdoor class????
float v=(point.y-boundTR.y)/(boundBR.y-boundTR.y);
float mx=ofGetWidth()/2;
float u=((point.x-mx)/(((boundTR.x-mx)*(1-v))+((boundBR.x-mx)*v)))+0.5;
return ofVec2f(u,v);
}
*/
float trapdoor::getFalldist(){
if (triggeredTime>0) return ((ofGetElapsedTimef()-triggeredTime)*.01);
else return 0;
}
bool trapdoor::checkUpdate(map<int,player>& players) {
map<int,player>::iterator it;
bool gotcha=false;
for (it=players.begin();it!=players.end();it++) {
if (it->second.active) {
ofVec3f p=it->second.getWorldPosition();
ofRectangle r=getInnerRect();
if (getInnerRect().inside(p.x,p.y)&&!it->second.isCaught) {
gotcha=true;
trigger();
it->second.caught();
printf("caught!\n");
caught.push_back(&(it->second));
caughtTime.push_back(ofGetElapsedTimef());
}
}
}
for (int i=0;i<caught.size();i++) {
float sc=pow(0.2,ofGetElapsedTimef()-caughtTime[i]);
caught[i]->setCatchTransform(ofVec3f(0,0,0),ofVec3f(sc,sc,sc));
}
float segTime=(ofGetElapsedTimef()-triggeredTime);
if (triggeredTime>0) {
doorSpeed=(doorSpeed+((cos(doorAngle*0.0174532925)/ofGetFrameRate())*50))*0.95;
doorAngle-=doorSpeed;
}
return gotcha;
}
void trapdoor::drawDebug() {
ofSetHexColor(0xff0000);
glBegin(GL_LINES);
glVertex3f(getInnerRect().x,getInnerRect().y,0);
glVertex3f(getInnerRect().x+(getInnerRect().width),getInnerRect().y,0);
glVertex3f(getInnerRect().x+(getInnerRect().width),getInnerRect().y,0);
glVertex3f(getInnerRect().x+(getInnerRect().width),getInnerRect().y+(getInnerRect().height),0);
glVertex3f(getInnerRect().x+(getInnerRect().width),getInnerRect().y+(getInnerRect().height),0);
glVertex3f(getInnerRect().x,getInnerRect().y+(getInnerRect().height),0);
glVertex3f(getInnerRect().x,getInnerRect().y+(getInnerRect().height),0);
glVertex3f(getInnerRect().x,getInnerRect().y,0);
glEnd();
}
void trapdoor::draw() {
glEnable(GL_DEPTH_TEST);
ofSetHexColor(0xffffff);
bindTexture(texture);
ofPushMatrix();
ofTranslate(ofVec3f(position.x,position.y,0));
ofPushMatrix();
ofRotate(90,-1,0,0);
ofScale(size*.005,size*.005,size*.005);
surround.draw();
ofPushMatrix();
ofTranslate(90,0,-5);
ofRotate(doorAngle,0,0,-1);
lid.draw();
ofPopMatrix();
ofPushMatrix();
ofTranslate(-90,0,-5);
ofRotate(180,0,1,0);
ofRotate(doorAngle,0,0,-1);
lid.draw();
ofPopMatrix();
ofPopMatrix();
ofPopMatrix();
unbindTexture(texture);
glDisable(GL_DEPTH_TEST);
}
void trapdoor::drawSplash(float angle) {
for (int i=0;i<caught.size();i++) {
float splashTime = (ofGetElapsedTimef()-caughtTime[i]);
if (splashTime>0.3) {
ofPushMatrix();
ofTranslate(ofVec3f(position.x,position.y,0));
ofRotate(angle,-1,0,0);
ofScale(size*.01,size*.01,size*.01);
if (splashTime<0.5) splashFrames[0].draw(0,0,0);
else if (splashTime<0.7) splashFrames[1].draw(0,0,0);
else if (splashTime<0.9) splashFrames[2].draw(0,0,0);
ofPopMatrix();
}
}
}
|