Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/AsteroidMining_HS17/src/modules/asteroidmining/AsteroidMinable.cc @ 11553

Last change on this file since 11553 was 11553, checked in by remartin, 6 years ago

That pickup stuff is a bloody mess. Protected methods and weird interaction.

File size: 12.6 KB
Line 
1
2 /*   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      Simon Miescher
26 *
27 */
28
29/*
30
31*
32*
33* An asteroid which can be destroyed. Some smaller asteroids are created and a pickup
34* spawns.
35*
36*
37*
38
39*/
40
41/*
42Veraenderungstagebuch
43- Dynamische Definition von Argumenten funktioniert nicht (Immer auf default Values gesetzt)
44---> Was tut registerVariable? Wann werden die Argumente im XML-File angewendet?
45    (Fall Konstruktor -> Methoden: Beim ersten Tick initialisieren?)
46---> Mesh-Teil klappt, Hitbox noch nicht.
47---> Maximale Hitpunkte = 200?
48---> Meshgroesse passt, aber Hitbox wird iwieso nicht mehr generiert.
49
50- Neue Asteroiden generieren -> Absturz
51- Pickup spawning -> Absturz
52
53o hat's mit den Context-Eingenschaften zu tun? Je einen Neuen/Aktuellen besorgen?
54
55Nicht Pointer uebergeben?
56- Manchmal: Absturz bei Zerstoerung eines Asteroiden
57- Was tut 'Register Variables'?
58
59o Error messages: change context::pickups to something else
60
61
62OFFEN:
63o Add custom pickup 'resources'
64
65
66
67Erlegt:
68o Rand() geht bis zu riesigen Nummern!
69*/
70
71
72#include "../../orxonox/worldentities/pawns/Pawn.h"
73#include "../../orxonox/worldentities/WorldEntity.h"
74
75#include "AsteroidMinable.h"
76
77#include <algorithm>
78
79#include "core/CoreIncludes.h"
80#include "core/GameMode.h"
81#include "core/XMLPort.h"
82#include "core/EventIncludes.h"
83#include "network/NetworkFunction.h"
84
85// #include "infos/PlayerInfo.h"
86// #include "controllers/Controller.h"
87// #include "gametypes/Gametype.h"
88// #include "graphics/ParticleSpawner.h"
89// #include "worldentities/ExplosionChunk.h"
90// #include "worldentities/ExplosionPart.h"
91
92// #include "core/object/ObjectListIterator.h"
93// #include "controllers/FormationController.h"
94
95#include "../pickup/items/HealthPickup.h"
96#include "../pickup/PickupSpawner.h"
97#include "../pickup/Pickup.h"
98//#include "../pickup/pickup ..... pickupable
99#include "../objects/collisionshapes/SphereCollisionShape.h"
100#include "../../orxonox/graphics/Model.h"
101
102
103
104
105
106
107namespace orxonox
108{
109    RegisterClass(AsteroidMinable);
110
111    AsteroidMinable::AsteroidMinable(Context* context) : Pawn(context)
112    {
113        RegisterObject(AsteroidMinable);
114
115        // Old stuff from pawn
116        this->setRadarObjectColour(ColourValue(1.0f, 1.0f, 0.0f, 1.0f));
117        this->setRadarObjectShape(RadarViewable::Shape::Dot);
118        this->setCollisionType(WorldEntity::CollisionType::Dynamic);
119        this->bAlive_ = true;
120        this->bVulnerable_ = true;
121
122
123
124        // Default Values
125        this->generateSmaller = true;
126        //this->size = this->getSize(); // Value doesn-t get accepted. Weird.
127        this->size = 10;
128        this->context = context;
129        this->initialised = false;
130        //this->roll = rand()*5; //etwas Drehung. richtige Variable
131
132
133
134
135
136
137
138
139
140        // DELETE if other stuff works! (wrong size etc.)
141        SphereCollisionShape* cs = new SphereCollisionShape(this->context);
142        cs->setRadius((this->size)*2); //OFFEN: Feinabstimmung der Radien
143        this->attachCollisionShape(cs); 
144
145
146
147
148
149
150        // Old from Pawn
151        this->registerVariables();
152
153    }
154
155    AsteroidMinable::~AsteroidMinable()
156    {
157
158    }
159
160    void AsteroidMinable::putStuff(){
161
162        // Add Model
163        //<Model position="0,-40,40" yaw="90" pitch="-90" roll="0" scale="4" mesh="ast6.mesh" />
164        Model* hull = new Model(this->context);
165        // random one of the 6 shapes
166        char meshThingy[] = "";
167        sprintf(meshThingy, "ast%.0f.mesh", (round((5*(double)rand()/(double)RAND_MAX))+1));
168        //    sprintf(str, "Value of Pi = %f", M_PI);
169        //char meshThingy[] = "ast5.mesh";
170        hull->setMeshSource(meshThingy);
171        hull->setScale(this->size);
172        this->attach(hull);
173        //hull->setPosition(this->getPosition());
174
175        // Collision shape
176        SphereCollisionShape* cs = new SphereCollisionShape(this->context);
177        cs->setRadius((this->size)*2); //OFFEN: Feinabstimmung der Radien. 2.5 in AsteroidField.lua
178        this->attachCollisionShape(cs); 
179
180
181        this->initialised=true;
182    }
183
184    void AsteroidMinable::XMLPort(Element& xmlelement, XMLPort::Mode mode)
185    {
186        SUPER(AsteroidMinable, XMLPort, xmlelement, mode);
187        //        XMLPortParam(PickupSpawner, "pickup", setPickupTemplateName, getPickupTemplateName, xmlelement, mode);
188        XMLPortParam(AsteroidMinable, "size", setSize, getSize, xmlelement, mode);
189
190    }
191
192    void AsteroidMinable::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
193    {
194        SUPER(AsteroidMinable, XMLEventPort, xmlelement, mode);
195
196        XMLPortEventState(AsteroidMinable, BaseObject, "vulnerability", setVulnerable, xmlelement, mode);
197    }
198
199    void AsteroidMinable::registerVariables()
200    {
201        registerVariable(this->bAlive_,            VariableDirection::ToClient);
202        registerVariable(this->bVulnerable_,       VariableDirection::ToClient);
203        registerVariable(this->health_,            VariableDirection::ToClient);
204        registerVariable(this->maxHealth_,         VariableDirection::ToClient);
205
206        registerVariable(this->size, VariableDirection::ToClient);
207        registerVariable(this->generateSmaller, VariableDirection::ToClient);
208
209        registerVariable(this->initialised, VariableDirection::ToClient);
210        //registerVariable(this->context, VariableDirection::ToClient); // can't link that since it's a context
211
212
213            //         float size;
214            // bool generateSmaller;
215            // bool initialised;
216
217            // Context* context;
218    }
219
220    void AsteroidMinable::tick(float dt)
221    {
222        // SUPER(Pawn, tick, dt);
223        if(!(this->initialised)){this->putStuff();}
224
225        if(this->health_ <=0){this->death();}
226
227    }
228
229    void AsteroidMinable::setSize(float s){
230        this->size = s;
231        this->health_ = 5*s;// capped at 200 in pawn or smth?
232        this->setHealth(health_);
233    }
234
235    float AsteroidMinable::getSize(){
236        return this->size;
237    }
238
239    void AsteroidMinable::death() //ueberschreiben
240    {
241
242
243        //Spawn Pickup // Create at start already? ( Hidden in CollisionShape)
244        HealthPickup* hP = new HealthPickup(this->context);
245        if(hP == nullptr){
246            orxout(internal_error, context::pickups) << "Weird, can't create new HealthPickup." << endl;
247        }
248        // hP->setPosition(this->getPosition()); // Does not have a member named 'setPosition' (Inherits from Pickup//BAse Object)
249
250        // invoke spawnPickup on the pikcup itself? (returns bool, creates the hideous thing)
251        // HealthPickup inherits from Pickup.
252        // createSpawner is protected ->can be used as well in inheriting classes, but not here!
253        // bool unnecessary = hP->createSpawner();
254        // hP->setPosition(this->getPosition());
255        //OFFEN: Add custom pickup 'resources'
256
257        // createPickup is private, hP is of type Pickup!
258        // CreateDroppedPickup returns a PickupSpawner!
259        // PickupSpawner don't have public set methods, just that ugly 'factory method' which returns another PickupSpawner
260        // PickupSpawner* thingy = new PickupSpawner(this->context);
261        // What is Pickupable? says: Interface, no way to set type. Why do I even need the spawner? Trigger Distance?
262       
263
264        // Plan: Solve the Pickupable mystery, try to adapt the spawner somehow. Ask to modify it if that fails. (Change method types)
265
266
267        // PickupSpawner* thingy = (new PickupSpawner(this->context))->createDroppedPickup(this->context, hP, nullptr, 10);
268//    /*static*/ PickupSpawner* PickupSpawner::createDroppedPickup(Context* context, Pickupable* pickup, PickupCarrier* carrier, float triggerDistance)
269
270
271
272        // if(thingy == nullptr){
273        //     orxout(internal_error, context::pickups) << "Weird, can't create new PickupSpawner." << endl;
274        // }
275        // thingy->setPosition(this->getPosition());
276        // thingy->setMaxSpawnedItems(1);//Default is 1 already, private anyways
277       
278   
279
280        // // Smaller Parts
281        // if(this->generateSmaller){
282        //     this->spawnChildren();
283        // }
284
285
286        // OFFEN: Sauber kapputten
287        // just copied other stuff:
288        this->setHealth(1);
289        this->bAlive_ = false;
290        this->destroyLater();
291        this->setDestroyWhenPlayerLeft(false);
292        // pawn -> addExplosionPart
293        // this->goWithStyle();
294
295
296        // if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_))
297        // {
298        //     // Set bAlive_ to false and wait for destroyLater() to do the destruction
299        //     this->bAlive_ = false;
300        //     this->destroyLater();
301
302        //     this->setDestroyWhenPlayerLeft(false);
303
304        // }
305    }
306
307
308void AsteroidMinable::spawnChildren(){
309   
310    // Spawn smaller Children
311    int massRem = this->size-1; //some mass is lost
312    int num = round((massRem-1)*(double)rand() / (double)RAND_MAX)+1; // random number of children, at least one
313    massRem = massRem-num;
314    int extra = 0;
315    for(int fisch=num; fisch>=1; fisch++){
316        // to distribute remaining mass
317        if(fisch==1){ 
318            extra = massRem;
319        }else{
320            extra = round(massRem*(double)rand() / (double)RAND_MAX);
321            massRem = massRem-extra;
322        }
323
324
325
326        //Spawn this child  Game crashes!
327        AsteroidMinable* child = new AsteroidMinable(this->context);
328        // if(!(child == nullptr)){//Errorgebastel
329
330
331
332            if(child == nullptr){
333                orxout(internal_error, context::pickups) << "Weird, can't create new AsteroidMinable." << endl;
334            }
335
336            child->setSize(extra + 1);
337               
338            //OFFEN:Kollision der Kinder verhindern
339            //Relativ zu Elternteil automatisch?
340            //Typ position:rand()*Vektoriwas?
341            //pawn->getWorldPosition() + Vector3(30,0,-30);
342            child->setPosition(this->getPosition() + Vector3(num*5, 0, 0));
343
344        // }
345
346
347                //         Pawn* pawn = this->carrierToPawnHelper();
348                // if(pawn == nullptr) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
349                //     this->Pickupable::destroy();
350
351                // //Attach to pawn
352                // Drone* drone = new Drone(pawn->getContext()); // this is neccessary because the projectiles fired need a valid creator for the particlespawner (when colliding against something)
353                // drone->addTemplate(this->getDroneTemplate());
354
355
356    }
357}
358
359
360
361
362}
363
364
365
366
367
368/*
369    void DronePickup::changedUsed(void)
370    {
371        SUPER(DronePickup, changedUsed);
372
373        // If the pickup has transited to used.
374        if(this->isUsed())
375        {
376
377                Pawn* pawn = this->carrierToPawnHelper();
378                if(pawn == nullptr) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
379                    this->Pickupable::destroy();
380
381                //Attach to pawn
382                Drone* drone = new Drone(pawn->getContext()); // this is neccessary because the projectiles fired need a valid creator for the particlespawner (when colliding against something)
383                drone->addTemplate(this->getDroneTemplate());
384
385                Controller* controller = drone->getController();
386                DroneController* droneController = orxonox_cast<DroneController*>(controller);
387                if(droneController != nullptr)
388                {
389                    droneController->setOwner(pawn);
390                }
391
392                Vector3 spawnPosition = pawn->getWorldPosition() + Vector3(30,0,-30);
393                drone->setPosition(spawnPosition);
394
395                // The pickup has been used up.
396                this->setUsed(false);
397        }
398        else
399        {
400            // If either the pickup can only be used once or it is continuous and used up, it is destroyed upon setting it to unused.
401            if(this->isOnce() || (this->isContinuous() ))
402            {
403                this->Pickupable::destroy();
404            }
405        }
406    }
407*/
Note: See TracBrowser for help on using the repository browser.