Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/weaponFS15/src/modules/weapons/projectiles/GravityBomb.cc @ 10436

Last change on this file since 10436 was 10436, checked in by meggiman, 9 years ago

Fixed dangling pointer bug when GravityBomb was destroyed by tick of base class because of collision with other pawns.

File size: 2.5 KB
Line 
1/*
2 * GravityBomb.cc
3 *
4 *  Created on: Mar 26, 2015
5 *      Author: meggiman
6 */
7#include "GravityBomb.h"
8#include "graphics/Model.h"
9
10
11namespace orxonox{
12        RegisterClass(GravityBomb);
13
14        const float GravityBomb::LIFETIME = 5;
15
16        GravityBomb::GravityBomb(Context* context):
17                                BasicProjectile(),
18                                MovableEntity(context),
19                                RadarViewable(this,static_cast<WorldEntity*>(this))
20                {
21                        RegisterObject(GravityBomb);
22
23                        this->setMass(10.0);
24                        this->isDetonated_ = false;
25                        if (GameMode::isMaster())
26                        {
27                                //Define CollisionType of the bomb
28                                this->timeToLife_= LIFETIME;
29                                this->setCollisionResponse(false);
30                                this->setCollisionType(WorldEntity::Dynamic);
31                                this->enableCollisionCallback();
32
33                                //Add Collision Shape
34                                SphereCollisionShape* collisionShape = new SphereCollisionShape(context);
35                                collisionShape->setRadius(1.0);
36                                this->attachCollisionShape(collisionShape);
37
38                                //Create Bomb Model
39                    Model* rocketModel = new Model(this->getContext());
40                    rocketModel->setMeshSource("GravityBombRocket.mesh"); //Demo Model from SimpleRocket
41                    rocketModel->scale(3.0f);
42                    this->attach(rocketModel);
43
44                    Model* bombModel =  new Model(this->getContext());
45                    bombModel->setMeshSource("GravityBomb.mesh"); //Demo Model from SimpleRocket
46                                bombModel->scale(3.0f);
47                                this->attach(bombModel);
48
49                        }
50                }
51
52        GravityBomb::~GravityBomb(){}
53
54        void GravityBomb::tick(float dt)
55        {
56                        timeToLife_ -= dt;
57                        if(timeToLife_ < 0)
58                        {
59                                orxout(debug_output) << "bomb has stoped moving" <<endl;
60                                setVelocity(Vector3::ZERO);
61                                setAcceleration(Vector3::ZERO);
62                                isDetonated_ = true;
63                        }
64                        else
65                        {
66                                orxout(debug_output)<< "Time to live:" << timeToLife_ <<endl;
67                                destroyCheck();
68                        }
69                        if(isDetonated_) detonate();
70                        else SUPER(GravityBomb, tick, dt);
71        }
72
73        bool GravityBomb::collidesAgainst(WorldEntity* otherObject, const btCollisionShape* cs, btManifoldPoint& contactPoint)
74        {
75                if(otherObject != getShooter())
76                {
77                        orxout(debug_output) << "collides" << endl;
78                        processCollision(otherObject, contactPoint,cs);
79                        isDetonated_ = true;
80                        return true;
81                }
82                else{
83                        orxout(debug_output) << "collided with shooter. Has no effect..." << endl;
84                        return false;
85                }
86        }
87
88        void GravityBomb::detonate()
89        {
90                GravityBombField* field = new GravityBombField(this->getContext());
91                field->setShooter(this->getShooter());
92                field->setPosition(getPosition());
93                orxout(debug_output) << "detonating. Creating GravityBombField." <<endl;
94                orxout(debug_output) << "Field is at Position: " << getPosition() << endl;
95                this->destroy();
96        }
97}
98
99
Note: See TracBrowser for help on using the repository browser.