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: Reto Luechinger |
---|
13 | */ |
---|
14 | |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
17 | #include "bsp_weapon.h" |
---|
18 | #include "loading/load_param.h" |
---|
19 | #include "debug.h" |
---|
20 | #include "loading/load_param_xml.h" |
---|
21 | |
---|
22 | #include "environments/bsp_entity.h" |
---|
23 | |
---|
24 | ObjectListDefinition(BspWeapon); |
---|
25 | CREATE_FACTORY(BspWeapon); |
---|
26 | |
---|
27 | /** |
---|
28 | * Standard constructor |
---|
29 | */ |
---|
30 | BspWeapon::BspWeapon () |
---|
31 | { |
---|
32 | this->init(); |
---|
33 | } |
---|
34 | |
---|
35 | /** |
---|
36 | * destructs the BspWeapon, deletes allocated memory |
---|
37 | */ |
---|
38 | BspWeapon::~BspWeapon () |
---|
39 | { |
---|
40 | // will be deleted |
---|
41 | } |
---|
42 | |
---|
43 | /** |
---|
44 | * Constructor with XML Element |
---|
45 | */ |
---|
46 | BspWeapon::BspWeapon (const TiXmlElement* root) |
---|
47 | { |
---|
48 | this->init(); |
---|
49 | if (root != NULL) |
---|
50 | { |
---|
51 | this->loadParams(root); |
---|
52 | } |
---|
53 | } |
---|
54 | /** |
---|
55 | * XML Loader |
---|
56 | */ |
---|
57 | void BspWeapon::loadParams(const TiXmlElement* root) |
---|
58 | { |
---|
59 | if (root != NULL) |
---|
60 | { |
---|
61 | WorldEntity::loadParams(root); |
---|
62 | |
---|
63 | LoadParam(root, "Range", this, BspWeapon, setRange) |
---|
64 | .describe("the range after which the Weapon hits no more") |
---|
65 | .defaultValues(""); |
---|
66 | LoadParam(root, "Damage", this, BspWeapon, setDamage) |
---|
67 | .describe("Damage that the weapon inflicts"); |
---|
68 | LoadParam(root, "FireRate", this, BspWeapon, setFireRate) |
---|
69 | .describe("how fast it shoots"); |
---|
70 | LoadParam(root, "AlwaysHits", this, BspWeapon, setAlwaysHits) |
---|
71 | .describe("No, if the weapon should hit with a probability"); |
---|
72 | |
---|
73 | LOAD_PARAM_START_CYCLE(root, element); |
---|
74 | { |
---|
75 | LoadParam_CYCLE(element, "addPoint", this, BspWeapon, addPoint) |
---|
76 | .describe("Adds a new Point for Gunfire"); |
---|
77 | } |
---|
78 | LOAD_PARAM_END_CYCLE(element); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | void BspWeapon::addPoint(float x, float y, float z) |
---|
83 | { |
---|
84 | if (element == 1 ){ |
---|
85 | gunFire1->setRelCoor(Vector(x,y,z)); |
---|
86 | element++; |
---|
87 | } |
---|
88 | if (element == 2 ){ |
---|
89 | gunFire2->setRelCoor(Vector(x,y,z)); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | void BspWeapon::tick( float dt ) |
---|
94 | { |
---|
95 | if (bFire) { |
---|
96 | if (bRate < 0) { |
---|
97 | bRate += fireRate; |
---|
98 | this->shoot(); |
---|
99 | } |
---|
100 | else { |
---|
101 | bRate = bRate - dt; |
---|
102 | } |
---|
103 | } |
---|
104 | else { |
---|
105 | bRate -= dt; |
---|
106 | if (bRate < 0) |
---|
107 | bRate = 0; |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | void BspWeapon::init() |
---|
112 | { |
---|
113 | bRate = 0; |
---|
114 | bFire = true; |
---|
115 | range = 1000; |
---|
116 | damage = 10; |
---|
117 | fireRate = 0.5; |
---|
118 | alwaysHits = true; |
---|
119 | element=1; |
---|
120 | |
---|
121 | this->registerObject(this, BspWeapon::_objectList); |
---|
122 | this->aimingSystem = new AimingSystem( this ); |
---|
123 | this->aimingSystem->setParent( this ); |
---|
124 | this->aimingSystem->toList(OM_GROUP_00); |
---|
125 | } |
---|
126 | |
---|
127 | void BspWeapon::shoot() |
---|
128 | { |
---|
129 | gunFire.explode(gunFire1,Vector(2,2,2)); |
---|
130 | gunFire.explode(gunFire2,Vector(2,2,2)); |
---|
131 | std::list<WorldEntity*>::iterator entityIterator; |
---|
132 | // for all bsp managers check all entities |
---|
133 | Vector pos = this->getAbsCoor(); |
---|
134 | Vector dir = pos + this->getAbsDir().apply( Vector( 1, 0, 0 ) )*range; |
---|
135 | |
---|
136 | |
---|
137 | float shortestDist = range; |
---|
138 | for( ObjectList<BspEntity>::const_iterator bspIterator = BspEntity::objectList().begin(); |
---|
139 | bspIterator != BspEntity::objectList().end(); |
---|
140 | bspIterator++) { |
---|
141 | float res = (dynamic_cast<BspEntity*>(*bspIterator)->getBspManager())->checkCollisionRay( pos, dir, dir.len() + 1 ); |
---|
142 | |
---|
143 | if ( res < shortestDist ) |
---|
144 | shortestDist = res; |
---|
145 | } |
---|
146 | |
---|
147 | WorldEntity* target = aimingSystem->getNearestTarget(); |
---|
148 | aimingSystem->flushList(); |
---|
149 | |
---|
150 | bool hit = false; |
---|
151 | |
---|
152 | if ( target == NULL ) |
---|
153 | printf("NOTING HIT\n"); |
---|
154 | else |
---|
155 | { |
---|
156 | printf( "HIT %s\n", target->getClassName().c_str() ); |
---|
157 | |
---|
158 | if (!alwaysHits){ |
---|
159 | float r = rand(); |
---|
160 | r = r/RAND_MAX; |
---|
161 | float res = (target->getAbsCoor() - this->getAbsCoor()).len(); |
---|
162 | float p = 1 - res*res/range/range; |
---|
163 | if (r < p ) |
---|
164 | hit = true; |
---|
165 | } |
---|
166 | else hit = true; |
---|
167 | } |
---|
168 | |
---|
169 | if ( !hit ) |
---|
170 | { |
---|
171 | Vector explosionPos = this->getAbsCoor() + this->getAbsDir().apply( Vector( 1, 0, 0 ) )*shortestDist; |
---|
172 | |
---|
173 | //TODO create explosion at explosionPos |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | } |
---|
178 | |
---|
179 | void BspWeapon::draw() const |
---|
180 | { |
---|
181 | WorldEntity::draw(); |
---|
182 | |
---|
183 | |
---|
184 | glMatrixMode(GL_MODELVIEW); |
---|
185 | glPushMatrix(); |
---|
186 | |
---|
187 | glPushAttrib(GL_ENABLE_BIT); |
---|
188 | |
---|
189 | glDisable(GL_LIGHTING); |
---|
190 | glDisable(GL_TEXTURE_2D); |
---|
191 | glDisable(GL_BLEND); |
---|
192 | glLineWidth(2.0); |
---|
193 | |
---|
194 | |
---|
195 | Vector mp = this->getAbsCoor(); |
---|
196 | Vector op = this->getAbsDir().apply( Vector(1, 0, 0) ); |
---|
197 | op *= 100; |
---|
198 | op += mp; |
---|
199 | |
---|
200 | glColor3f(1.0, 1.0, 1.0 ); |
---|
201 | glBegin(GL_LINE_STRIP); |
---|
202 | glVertex3f(mp.x, mp.y, mp.z); |
---|
203 | glVertex3f(op.x, op.y, op.z); |
---|
204 | glEnd(); |
---|
205 | |
---|
206 | |
---|
207 | glPopAttrib(); |
---|
208 | glPopMatrix(); |
---|
209 | |
---|
210 | } |
---|