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: Patrick Boenzli |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON |
---|
17 | |
---|
18 | #include "aiming_system.h" |
---|
19 | |
---|
20 | #include "util/loading/load_param.h" |
---|
21 | |
---|
22 | #include "state.h" |
---|
23 | |
---|
24 | #include "aabb.h" |
---|
25 | #include "obb_tree.h" |
---|
26 | |
---|
27 | #include <vector> |
---|
28 | |
---|
29 | |
---|
30 | using namespace std; |
---|
31 | |
---|
32 | |
---|
33 | /** |
---|
34 | * standart constructor |
---|
35 | */ |
---|
36 | AimingSystem::AimingSystem (WorldEntity* entity) |
---|
37 | { |
---|
38 | this->init(); |
---|
39 | } |
---|
40 | |
---|
41 | |
---|
42 | /** |
---|
43 | * destroys a AimingSystem |
---|
44 | */ |
---|
45 | AimingSystem::~AimingSystem () |
---|
46 | {} |
---|
47 | |
---|
48 | |
---|
49 | /** |
---|
50 | * initializes the AimingSystem |
---|
51 | */ |
---|
52 | void AimingSystem::init() |
---|
53 | { |
---|
54 | this->setClassID(CL_CROSSHAIR, "AimingSystem"); |
---|
55 | this->setName("AimingSystem"); |
---|
56 | |
---|
57 | this->loadModel("models/guns/targeting_system_body.obj"); |
---|
58 | |
---|
59 | // registering default reactions: |
---|
60 | this->subscribeReaction(CREngine::CR_OBJECT_DAMAGE, CL_WORLD_ENTITY); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | /** |
---|
66 | * get back the nearest target |
---|
67 | * @returns the nerest target |
---|
68 | */ |
---|
69 | WorldEntity* AimingSystem::getNearestTarget() |
---|
70 | { |
---|
71 | if( this->selectionList.size() == 0) |
---|
72 | return NULL; |
---|
73 | else if( this->selectionList.size() == 1) |
---|
74 | return this->selectionList.back(); |
---|
75 | |
---|
76 | WorldEntity* nearestEntity = NULL; |
---|
77 | float distance = 0.0f; |
---|
78 | float smalestDistance = this->range * 5.0f; |
---|
79 | |
---|
80 | |
---|
81 | for( int i = 0; i < this->selectionList.size(); i++) |
---|
82 | { |
---|
83 | distance = fabs((this->getAbsCoor() - this->selectionList[i]->getAbsCoor()).len()); |
---|
84 | if( distance < smalestDistance) |
---|
85 | { |
---|
86 | nearestEntity = this->selectionList[i]; |
---|
87 | smalestDistance = distance; |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | if( nearestEntity != this->owner) |
---|
92 | return nearestEntity; |
---|
93 | else return NULL; |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | /** |
---|
98 | * called when an object is "selected" |
---|
99 | * @param damage damage to be dealt |
---|
100 | * @param killer the entity |
---|
101 | */ |
---|
102 | void AimingSystem::hit(float damage, WorldEntity* killer) |
---|
103 | { |
---|
104 | this->selectionList.push_back(killer); |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | /** |
---|
110 | * ticks the AimingSystem |
---|
111 | * @param dt the time to ticks |
---|
112 | */ |
---|
113 | void AimingSystem::tick(float dt) |
---|
114 | { |
---|
115 | |
---|
116 | // this->selectionList.clear(); |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | /** |
---|
121 | * draws the crosshair |
---|
122 | */ |
---|
123 | void AimingSystem::draw() const |
---|
124 | { |
---|
125 | WorldEntity::draw(); |
---|
126 | |
---|
127 | if( this->getOBBTree () != NULL) |
---|
128 | this->getOBBTree()->drawBV(0, 1); |
---|
129 | } |
---|