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 | } |
---|
74 | |
---|
75 | |
---|
76 | |
---|
77 | void BspWeapon::tick( float dt ) |
---|
78 | { |
---|
79 | if (bFire) { |
---|
80 | if (bRate < 0) { |
---|
81 | bRate += fireRate; |
---|
82 | this->shoot(); |
---|
83 | } |
---|
84 | else { |
---|
85 | bRate = bRate - dt; |
---|
86 | } |
---|
87 | } |
---|
88 | else { |
---|
89 | bRate -= dt; |
---|
90 | if (bRate < 0) |
---|
91 | bRate = 0; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | void BspWeapon::init() |
---|
96 | { |
---|
97 | bRate = 0; |
---|
98 | bFire = true; |
---|
99 | range = 1000; |
---|
100 | damage = 10; |
---|
101 | fireRate = 0.5; |
---|
102 | alwaysHits = true; |
---|
103 | |
---|
104 | this->registerObject(this, BspWeapon::_objectList); |
---|
105 | this->aimingSystem = new AimingSystem( this ); |
---|
106 | this->aimingSystem->setParent( this ); |
---|
107 | this->aimingSystem->toList(OM_GROUP_00); |
---|
108 | } |
---|
109 | |
---|
110 | void BspWeapon::shoot() |
---|
111 | { |
---|
112 | std::list<WorldEntity*>::iterator entityIterator; |
---|
113 | // for all bsp managers check all entities |
---|
114 | Vector pos = this->getAbsCoor(); |
---|
115 | Vector dir = pos + this->getAbsDir().apply( Vector( 1, 0, 0 ) )*range; |
---|
116 | |
---|
117 | |
---|
118 | float shortestDist = range; |
---|
119 | for( ObjectList<BspEntity>::const_iterator bspIterator = BspEntity::objectList().begin(); |
---|
120 | bspIterator != BspEntity::objectList().end(); |
---|
121 | bspIterator++) { |
---|
122 | float res = (dynamic_cast<BspEntity*>(*bspIterator)->getBspManager())->checkCollisionRay( pos, dir, dir.len() + 1 ); |
---|
123 | |
---|
124 | if ( res < shortestDist ) |
---|
125 | shortestDist = res; |
---|
126 | } |
---|
127 | |
---|
128 | WorldEntity* target = aimingSystem->getNearestTarget(); |
---|
129 | aimingSystem->flushList(); |
---|
130 | |
---|
131 | bool hit = false; |
---|
132 | |
---|
133 | if ( target == NULL ) |
---|
134 | printf("NOTING HIT\n"); |
---|
135 | else |
---|
136 | { |
---|
137 | printf( "HIT %s\n", target->getClassName().c_str() ); |
---|
138 | |
---|
139 | if (!alwaysHits){ |
---|
140 | float r = rand(); |
---|
141 | r = r/RAND_MAX; |
---|
142 | float res = (target->getAbsCoor() - this->getAbsCoor()).len(); |
---|
143 | float p = 1 - res*res/range/range; |
---|
144 | if (r < p ) |
---|
145 | hit = true; |
---|
146 | } |
---|
147 | else hit = true; |
---|
148 | } |
---|
149 | |
---|
150 | if ( !hit ) |
---|
151 | { |
---|
152 | Vector explosionPos = this->getAbsCoor() + this->getAbsDir().apply( Vector( 1, 0, 0 ) )*shortestDist; |
---|
153 | |
---|
154 | //TODO create explosion at explosionPos |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | } |
---|
159 | |
---|
160 | void BspWeapon::draw() const |
---|
161 | { |
---|
162 | WorldEntity::draw(); |
---|
163 | |
---|
164 | |
---|
165 | glMatrixMode(GL_MODELVIEW); |
---|
166 | glPushMatrix(); |
---|
167 | |
---|
168 | glPushAttrib(GL_ENABLE_BIT); |
---|
169 | |
---|
170 | glDisable(GL_LIGHTING); |
---|
171 | glDisable(GL_TEXTURE_2D); |
---|
172 | glDisable(GL_BLEND); |
---|
173 | glLineWidth(2.0); |
---|
174 | |
---|
175 | |
---|
176 | Vector mp = this->getAbsCoor(); |
---|
177 | Vector op = this->getAbsDir().apply( Vector(1, 0, 0) ); |
---|
178 | op *= 100; |
---|
179 | op += mp; |
---|
180 | |
---|
181 | glColor3f(1.0, 1.0, 1.0 ); |
---|
182 | glBegin(GL_LINE_STRIP); |
---|
183 | glVertex3f(mp.x, mp.y, mp.z); |
---|
184 | glVertex3f(op.x, op.y, op.z); |
---|
185 | glEnd(); |
---|
186 | |
---|
187 | |
---|
188 | glPopAttrib(); |
---|
189 | glPopMatrix(); |
---|
190 | |
---|
191 | } |
---|