1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON |
---|
17 | |
---|
18 | #include "aim.h" |
---|
19 | |
---|
20 | #include "load_param.h" |
---|
21 | #include "graphics_engine.h" |
---|
22 | #include "state.h" |
---|
23 | #include "material.h" |
---|
24 | #include "t_animation.h" |
---|
25 | #include "text.h" |
---|
26 | |
---|
27 | #include "world_entity.h" |
---|
28 | |
---|
29 | using namespace std; |
---|
30 | |
---|
31 | |
---|
32 | /** |
---|
33 | * standart constructor |
---|
34 | */ |
---|
35 | Aim::Aim (PNode* source, const TiXmlElement* root) |
---|
36 | { |
---|
37 | this->init(); |
---|
38 | |
---|
39 | this->source = source; |
---|
40 | |
---|
41 | if (root) |
---|
42 | this->loadParams(root); |
---|
43 | else |
---|
44 | this->setTexture("maps/aim.png"); |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * destroys a Aim |
---|
49 | */ |
---|
50 | Aim::~Aim () |
---|
51 | { |
---|
52 | if (this->material) |
---|
53 | delete this->material; |
---|
54 | |
---|
55 | /* if (this->text != NULL) |
---|
56 | delete this->text;*/ |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * initializes the Aim |
---|
61 | */ |
---|
62 | void Aim::init() |
---|
63 | { |
---|
64 | this->setClassID(CL_CROSSHAIR, "Aim"); |
---|
65 | this->setName("Aim"); |
---|
66 | |
---|
67 | this->setLayer(E2D_LAYER_TOP); |
---|
68 | this->setRotationSpeed(30.0* (float)rand()/RAND_MAX + 10.0); |
---|
69 | this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0); |
---|
70 | |
---|
71 | this->setBindNode(this); |
---|
72 | this->material = new Material; |
---|
73 | this->source = NULL; |
---|
74 | |
---|
75 | this->range = 10000; |
---|
76 | this->angle = M_PI_4; |
---|
77 | this->group = OM_GROUP_01; |
---|
78 | this->anim = new tAnimation<Aim>(this, &Aim::setSize); |
---|
79 | this->anim->setInfinity(ANIM_INF_CONSTANT); |
---|
80 | this->anim->addKeyFrame(500, .3, ANIM_LINEAR); |
---|
81 | this->anim->addKeyFrame(100, .2, ANIM_LINEAR); |
---|
82 | this->anim->addKeyFrame(50, .01, ANIM_LINEAR); |
---|
83 | |
---|
84 | /* this->text = new Text(); |
---|
85 | this->text->setLayer(this->getLayer()); |
---|
86 | this->text->setParent2D(this); |
---|
87 | this->text->setRelCoor2D(10, -50); |
---|
88 | this->text->setParentMode2D(E2D_PARENT_MOVEMENT); |
---|
89 | this->text->setText("Testing");*/ |
---|
90 | } |
---|
91 | |
---|
92 | void Aim::loadParams(const TiXmlElement* root) |
---|
93 | { |
---|
94 | PNode::loadParams(root); |
---|
95 | |
---|
96 | LoadParam(root, "texture", this, Aim, setTexture) |
---|
97 | .describe("the texture-file to load onto the Aim"); |
---|
98 | |
---|
99 | LoadParam(root, "size", this, Aim, setSize) |
---|
100 | .describe("the size of the Aim in Pixels"); |
---|
101 | |
---|
102 | LoadParam(root, "rotation-speed", this, Aim, setRotationSpeed) |
---|
103 | .describe("the Speed with which the Aim should rotate"); |
---|
104 | } |
---|
105 | |
---|
106 | void Aim::searchTarget() |
---|
107 | { |
---|
108 | std::list<WorldEntity*>::iterator entity; |
---|
109 | |
---|
110 | for (entity = State::getObjectManager()->getObjectList(group).begin(); |
---|
111 | entity != State::getObjectManager()->getObjectList(group).end(); |
---|
112 | entity ++) |
---|
113 | { |
---|
114 | diffVec = ( (*entity)->getAbsCoor() - this->source->getAbsCoor() ); |
---|
115 | |
---|
116 | if ( diffVec.len() < range && acos( (this->source->getAbsDirX()).dot(diffVec)/(diffVec.len() * (this->source->getAbsDirX()).len() ) ) < angle) |
---|
117 | { |
---|
118 | //if (this->getParent() != (*entity)) |
---|
119 | { |
---|
120 | this->anim->replay(); |
---|
121 | this->setParentSoft(*entity, 5); |
---|
122 | return; |
---|
123 | } |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | //if no target found: |
---|
128 | this->setParent(PNode::getNullParent()); |
---|
129 | |
---|
130 | |
---|
131 | |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | /** |
---|
136 | * @brief sets the size of the Aim. |
---|
137 | * @param size the size in pixels |
---|
138 | */ |
---|
139 | void Aim::setSize(float size) |
---|
140 | { |
---|
141 | this->setSize2D(size/2, size/2); |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * sets the material to load |
---|
146 | * @param textureFile The texture-file to load onto the crosshair |
---|
147 | */ |
---|
148 | void Aim::setTexture(const char* textureFile) |
---|
149 | { |
---|
150 | this->material->setDiffuseMap(textureFile); |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * ticks the Aim |
---|
155 | * @param dt the time to ticks |
---|
156 | */ |
---|
157 | void Aim::tick(float dt) |
---|
158 | { |
---|
159 | // let the crosshair rotate |
---|
160 | this->shiftDir2D(dt * rotationSpeed); |
---|
161 | |
---|
162 | // char outputText[100]; |
---|
163 | // sprintf(outputText, "%s - distance: %f\n", this->getParent()->getName(), (this->source->getAbsCoor() - this->getAbsCoor()).len()); |
---|
164 | // this->text->setText(outputText); |
---|
165 | |
---|
166 | |
---|
167 | // if (this->source->getAbsCoor().x > this->getAbsCoor().x ) |
---|
168 | diffVec = ( this->getAbsCoor() - this->source->getAbsCoor() ); |
---|
169 | //only look for target if the aim hasn`t locked a target yet or if the actual target is out of range |
---|
170 | if(this->getParent() == PNode::getNullParent() || |
---|
171 | diffVec.len() > range || |
---|
172 | ( acos( (this->source->getAbsDirX()).dot(diffVec)/(diffVec.len() * (this->source->getAbsDirX()).len() ) ) > angle)) |
---|
173 | { |
---|
174 | this->searchTarget(); |
---|
175 | } |
---|
176 | |
---|
177 | // float z = 0.0f; |
---|
178 | // glReadPixels ((int)this->getAbsCoor2D().x, |
---|
179 | // GraphicsEngine::getInstance()->getResolutionY()-(int)this->getAbsCoor2D().y-1, |
---|
180 | // 1, |
---|
181 | // 1, |
---|
182 | // GL_DEPTH_COMPONENT, |
---|
183 | // GL_FLOAT, |
---|
184 | // &z); |
---|
185 | // |
---|
186 | // |
---|
187 | // GLdouble objX=.0, objY=.0, objZ=.0; |
---|
188 | // gluUnProject(this->getAbsCoor2D().x, |
---|
189 | // GraphicsEngine::getInstance()->getResolutionY()-this->getAbsCoor2D().y-1, |
---|
190 | // .99, // z |
---|
191 | // GraphicsEngine::modMat, |
---|
192 | // GraphicsEngine::projMat, |
---|
193 | // GraphicsEngine::viewPort, |
---|
194 | // &objX, |
---|
195 | // &objY, |
---|
196 | // &objZ ); |
---|
197 | // |
---|
198 | // this->setAbsCoor(objX, objY, objZ); |
---|
199 | } |
---|
200 | |
---|
201 | /** |
---|
202 | * draws the crosshair |
---|
203 | */ |
---|
204 | void Aim::draw() const |
---|
205 | { |
---|
206 | |
---|
207 | if( this->getParent() != PNode::getNullParent() ) |
---|
208 | { |
---|
209 | glPushMatrix(); |
---|
210 | glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); |
---|
211 | |
---|
212 | glRotatef(this->getAbsDir2D(), 0,0,1); |
---|
213 | this->material->select(); |
---|
214 | glBegin(GL_TRIANGLE_STRIP); |
---|
215 | glTexCoord2f(0, 0); |
---|
216 | glVertex2f(-this->getSizeX2D(), -this->getSizeY2D()); |
---|
217 | glTexCoord2f(1, 0); |
---|
218 | glVertex2f(this->getSizeX2D(), -this->getSizeY2D()); |
---|
219 | glTexCoord2f(0, 1); |
---|
220 | glVertex2f(-this->getSizeX2D(), this->getSizeY2D()); |
---|
221 | glTexCoord2f(1, 1); |
---|
222 | glVertex2f(this->getSizeX2D(), this->getSizeY2D()); |
---|
223 | glEnd(); |
---|
224 | glPopMatrix(); |
---|
225 | } |
---|
226 | |
---|
227 | } |
---|