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
|
#include "trapdoor.h"
trapdoor::trapdoor(ofVec2f _boundTR,ofVec2f _boundBR,ofVec2f _doorSize)
{
surround=morphmesh("trapdoor-surround.xml");
lid=morphmesh("trapdoor-lid.xml");
if (!surround.isLoaded()||!lid.isLoaded()) printf("problem loading trap door mesh.\n");
texture.loadImage("TextureTrapdoor.jpg");
boundTR=_boundTR;
boundBR=_boundBR;
printf("trapdoor bounds: %f,%f - %f,%f\n",boundTR.x,boundTR.y,boundBR.x,boundBR.y);
size=_doorSize;
start();
}
trapdoor::~trapdoor() {}
void trapdoor::start(){
//place trapdoor within bounds
float u=ofRandom(-0.9,0.9); //(-1 to 1)
float v=ofRandom(0.05,0.95);
float mx=ofGetWidth()/2;
float x=mx+((boundTR.x-mx)*u*(1-v))+((boundBR.x-mx)*u*v);
float y=boundTR.y+((boundBR.y-boundTR.y)*v);
printf("new trapdoor: %f,%f\n",x,y);
position=ofVec2f(x,y);
startTime=ofGetElapsedTimef();
doorAngle=0;
doorSpeed=0;
}
vector<ofVec2f> trapdoor::getCorners(){
vector<ofVec2f> corners;
corners.push_back(ofVec2f(-size.x/2,-size.y/2));
corners.push_back(ofVec2f(size.x/2,-size.y/2));
corners.push_back(ofVec2f(size.x/2,size.y/2));
corners.push_back(ofVec2f(-size.x/2,size.y/2));
return corners;
}
ofVec2f trapdoor::bounds2UV(ofVec2f point){
//returns the 0-1 UV coords of a point on the ground plane relative to its bounds.
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);
}
void trapdoor::checkUpdate(const vector<ofVec3f>& players) {
float segTime=(ofGetElapsedTimef()-startTime);
if (segTime>5) start();
if (segTime>3) {
doorSpeed=(doorSpeed+((cos(doorAngle*0.0174532925)/ofGetFrameRate())*50))*0.95;
doorAngle-=doorSpeed;
}
}
void trapdoor::draw() {
glEnable(GL_DEPTH_TEST);
ofSetHexColor(0xffffff);
bindTexture(texture);
ofPushMatrix();
ofRotate(90,-1,0,0);
ofTranslate(ofVec3f(position.x,0,position.y));
//ofRotate(180,-1,0,0);
//for now size =40x40
ofScale(.1,.1,.1);
surround.draw();
ofPushMatrix();
ofTranslate(90,0,0);
ofRotate(doorAngle,0,0,-1);
lid.draw();
ofPopMatrix();
ofPushMatrix();
ofTranslate(-90,0,0);
ofRotate(180,0,1,0);
ofRotate(doorAngle,0,0,-1);
lid.draw();
ofPopMatrix();
ofPopMatrix();
unbindTexture(texture);
glDisable(GL_DEPTH_TEST);
}
|