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: Manuel Leuenberger |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
17 | |
---|
18 | |
---|
19 | #include "power_up.h" |
---|
20 | #include "extendable.h" |
---|
21 | #include "primitive_model.h" |
---|
22 | |
---|
23 | #include "sound/resource_sound_buffer.h" |
---|
24 | #include "util/loading/load_param.h" |
---|
25 | |
---|
26 | #include "debug.h" |
---|
27 | |
---|
28 | ObjectListDefinition(PowerUp); |
---|
29 | |
---|
30 | PowerUp::PowerUp(float r, float g, float b) |
---|
31 | { |
---|
32 | this->registerObject(this, PowerUp::_objectList); |
---|
33 | |
---|
34 | this->respawnType = RESPAWN_TIME; |
---|
35 | this->respawnStart = 10; |
---|
36 | this->model = NULL; |
---|
37 | /* if(!PowerUp::sphereModel) {*/ |
---|
38 | |
---|
39 | Model* sphereModel = new PrimitiveModel(PRIM_SPHERE, 7, 5); |
---|
40 | |
---|
41 | this->setModel(sphereModel); |
---|
42 | this->buildObbTree( 4); |
---|
43 | this->sphereMaterial = new Material; |
---|
44 | this->sphereMaterial->setTransparency(.8); |
---|
45 | this->sphereMaterial->setDiffuse(r, g, b); |
---|
46 | this->toList(OM_COMMON); |
---|
47 | |
---|
48 | this->soundSource.setSourceNode(this); |
---|
49 | |
---|
50 | this->collider = NULL; |
---|
51 | } |
---|
52 | |
---|
53 | PowerUp::~PowerUp () |
---|
54 | { |
---|
55 | delete this->sphereMaterial; |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | void PowerUp::loadParams(const TiXmlElement* root) |
---|
60 | { |
---|
61 | WorldEntity::loadParams(root); |
---|
62 | |
---|
63 | LoadParam(root, "respawnType", this, PowerUp, setRespawnType); |
---|
64 | |
---|
65 | LoadParam(root, "respawnTime", this, PowerUp, setRespawnTime); |
---|
66 | |
---|
67 | LoadParam(root, "pickup-sound", this, PowerUp, loadPickupSound); |
---|
68 | |
---|
69 | LoadParam(root, "respawn-sound", this, PowerUp, loadRespawnSound); |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | void PowerUp::loadPickupSound(const std::string& pickupSound) |
---|
74 | { |
---|
75 | if (!pickupSound.empty()) |
---|
76 | this->pickupBuffer = OrxSound::ResourceSoundBuffer(pickupSound); |
---|
77 | else |
---|
78 | this->pickupBuffer = OrxSound::SoundBuffer(); |
---|
79 | } |
---|
80 | |
---|
81 | void PowerUp::loadRespawnSound(const std::string& respawnSound) |
---|
82 | { |
---|
83 | if (!respawnSound.empty()) |
---|
84 | this->respawnBuffer = OrxSound::ResourceSoundBuffer(respawnSound); |
---|
85 | else |
---|
86 | this->respawnBuffer = OrxSound::SoundBuffer(); |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | void PowerUp::collidesWith (WorldEntity* entity, const Vector& location) |
---|
91 | { |
---|
92 | if(this->collider != entity && entity->isA(Extendable::staticClassID())) |
---|
93 | { |
---|
94 | this->collider = entity; |
---|
95 | if(dynamic_cast<Extendable*>(entity)->pickup(this)) |
---|
96 | { |
---|
97 | if(pickupBuffer.loaded()) |
---|
98 | this->soundSource.play(this->pickupBuffer); |
---|
99 | |
---|
100 | switch(respawnType) |
---|
101 | { |
---|
102 | case RESPAWN_NONE: |
---|
103 | this->toList(OM_DEAD); |
---|
104 | break; |
---|
105 | case RESPAWN_TIME: |
---|
106 | this->toList(OM_DEAD_TICK); |
---|
107 | this->respawnTime = this->respawnStart; |
---|
108 | break; |
---|
109 | default: |
---|
110 | /* NOT HANDLED */ |
---|
111 | break; |
---|
112 | } |
---|
113 | } |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | void PowerUp::tick(float dt) |
---|
118 | { |
---|
119 | if(this->getOMListNumber() != OM_COMMON) |
---|
120 | { |
---|
121 | this->respawnTime -= dt; |
---|
122 | if(this->respawnTime <= 0) |
---|
123 | { |
---|
124 | this->toList(OM_COMMON); |
---|
125 | this->collider = NULL; |
---|
126 | if (likely(this->respawnBuffer.loaded())) |
---|
127 | this->soundSource.play(this->respawnBuffer); |
---|
128 | |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | void PowerUp::draw() const |
---|
134 | { |
---|
135 | if(this->model != NULL) |
---|
136 | { |
---|
137 | glMatrixMode(GL_MODELVIEW); |
---|
138 | glPushMatrix(); |
---|
139 | glTranslatef (this->getAbsCoor ().x, |
---|
140 | this->getAbsCoor ().y, |
---|
141 | this->getAbsCoor ().z); |
---|
142 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
143 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
144 | this->model->draw(); |
---|
145 | glPopMatrix(); |
---|
146 | } |
---|
147 | this->sphereMaterial->select(); |
---|
148 | WorldEntity::draw(); |
---|
149 | } |
---|
150 | |
---|
151 | const char* PowerUp::respawnTypes[] = |
---|
152 | { |
---|
153 | "none", |
---|
154 | "time" |
---|
155 | }; |
---|
156 | |
---|
157 | |
---|
158 | void PowerUp::setRespawnType(const std::string& type) |
---|
159 | { |
---|
160 | for(int i = 0; i < RESPAWN_size; ++i) |
---|
161 | { |
---|
162 | if(type == respawnTypes[i]) |
---|
163 | { |
---|
164 | this->respawnType = (PowerUpRespawn)i; |
---|
165 | break; |
---|
166 | } |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | void PowerUp::setRespawnTime(const float respawnTime) |
---|
171 | { |
---|
172 | this->respawnStart = respawnTime; |
---|
173 | } |
---|
174 | |
---|
175 | |
---|