Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/mergeFS18/src/modules/weapons/projectiles/BasicProjectile.cc @ 12175

Last change on this file since 12175 was 11108, checked in by fvultier, 9 years ago

A few modifications in the weapon system: WeaponModes may play a reload sound now. Fireing Sounds of WeaponModes may overlap now. New weapon: FlameGun, a flame thrower for close combat (e.g. for the FPS player)

  • Property svn:eol-style set to native
File size: 7.4 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 *      simonmie
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file BasicProjectile.h
31    @brief Implementation of the BasicProjectile class.
32*/
33
34#include "BasicProjectile.h"
35
36#include "core/CoreIncludes.h"
37#include "core/GameMode.h"
38#include "core/command/Executor.h"
39
40#include "graphics/ParticleSpawner.h"
41
42namespace orxonox
43{
44    RegisterClassNoArgs(BasicProjectile);
45
46    /**
47    @brief
48        Constructor. Registers the object and initializes some default values.
49    */
50    BasicProjectile::BasicProjectile()
51    {
52        RegisterObject(BasicProjectile);// Register the BasicProjectile class to the core
53
54        this->bDestroy_ = false;
55
56        // Default is destroying the projectile after collision
57        this->destroyAfterCollision_ = true;
58
59        // Default damage must be zero, otherwise it would be above zero if no settings are made in the weaponsettings xml file.
60        // same thing for all weaponmodes files
61        this->damage_ = 0.0f;
62        this->healthdamage_ = 0.0f;
63        this->shielddamage_ = 0.0f;
64    }
65
66    BasicProjectile::~BasicProjectile()
67    {
68    }
69
70    /**
71    @brief
72        The function called when a projectile hits another thing.
73        Calls the hit-function, starts the shield recharge countdown, displays visual hit effects defined in Pawn.
74        Needs to be called in the collidesAgainst() function by every Class directly inheriting from BasicProjectile.
75    @param otherObject
76        A pointer to the object the Projectile has collided against.
77    @param contactPoint
78        A btManifoldPoint indicating the point of contact/impact.
79    @param cs
80        The btCollisionShape of the other object
81    @return
82        Returns true if the collision resulted in a successful hit.
83    @see Pawn.h
84    */
85    bool BasicProjectile::processCollision(WorldEntity* otherObject, btManifoldPoint& contactPoint, const btCollisionShape* cs)
86    {
87        if (!this->bDestroy_ && GameMode::isMaster())
88        {
89            if (this->isObjectRelatedToShooter(otherObject) || otherObject->isA(Class(BasicProjectile))) // Prevents you from shooting yourself or other projectiles
90                return false;
91
92            if (getDestroyAfterCollision())
93            {
94                this->bDestroy_ = true; // If something is hit, the object is destroyed and can't hit something else.
95                                        // The projectile is destroyed by its tick()-function (in the following tick).
96                                        // TODO: Use destroyLater() for this
97            }
98
99            Pawn* victim = orxonox_cast<Pawn*>(otherObject); // If otherObject isn't a Pawn, then victim is nullptr
100
101            WorldEntity* entity = orxonox_cast<WorldEntity*>(this);
102            assert(entity); // The projectile must not be a WorldEntity.
103
104            // If visual effects after destruction cause problems, put this block below the effects code block
105            if (victim)
106            {
107                victim->hit(this->getShooter(), contactPoint, cs, this->getDamage(), this->getHealthDamage(), this->getShieldDamage());
108                victim->startShieldRechargeCountdown();
109            }
110
111            // Visual effects for being hit, depending on whether the shield is hit or not
112            if (this->getShooter()) // If the owner does not exist (anymore?), no effects are displayed.
113            {
114                // Damping and explosion effect is only played if the victim is no Pawn (see cast above)
115                // or if the victim is a Pawn, has no shield left, is still alive and any damage goes to the health
116                if (!victim || (victim && !victim->hasShield() && victim->getHealth() > 0.0f && (this->getDamage() > 0.0f || this->getHealthDamage() > 0.0f)))
117                {
118                    {
119                        ParticleSpawner* effect = new ParticleSpawner(this->getShooter()->getContext());
120                        effect->setPosition(entity->getPosition());
121                        effect->setOrientation(entity->getOrientation());
122                        effect->setDestroyAfterLife(true);
123                        effect->setSource("Orxonox/explosion3");
124                        effect->setLifetime(2.0f);
125                    }
126                    // Second effect with same condition
127                    {
128                        ParticleSpawner* effect = new ParticleSpawner(this->getShooter()->getContext());
129                        effect->setPosition(entity->getPosition());
130                        effect->setOrientation(entity->getOrientation());
131                        effect->setDestroyAfterLife(true);
132                        effect->setSource("Orxonox/smoke4");
133                        effect->setLifetime(3.0f);
134                    }
135                }
136
137                // victim->isAlive() is not false until the next tick, so getHealth() > 0 is used instead
138                if (victim && victim->hasShield() && (this->getDamage() > 0.0f || this->getShieldDamage() > 0.0f) && victim->getHealth() > 0.0f)
139                {
140                    ParticleSpawner* effect = new ParticleSpawner(this->getShooter()->getContext());
141                    effect->setDestroyAfterLife(true);
142                    effect->setSource("Orxonox/Shield");
143                    effect->setLifetime(0.5f);
144                    victim->attach(effect);
145                }
146            }
147            return true;
148        }
149        return false;
150    }
151
152    /**
153     * Returns true if otherObject is equal to the shooter or if one of otherObject's parents is equal to the shooter or if one of the shooter's parent is equal to otherObject.
154     */
155    bool BasicProjectile::isObjectRelatedToShooter(WorldEntity* otherObject)
156    {
157        for (WorldEntity* shooter = this->getShooter(); shooter != nullptr; shooter = shooter->getParent())
158            if (otherObject == shooter)
159                return true;
160        for (WorldEntity* object = otherObject; object != nullptr; object = object->getParent())
161            if (otherObject == this->getShooter())
162                return true;
163
164        return false;
165    }
166
167    /**
168    @brief
169        Check whether the projectile needs to be destroyed and destroys it if so.
170        Needs to be called in the tick() by every Class directly inheriting from BasicProjectile, to make sure the projectile is destroyed after it has hit something.
171    */
172    void BasicProjectile::destroyCheck(void)
173    {
174        if(GameMode::isMaster() && this->bDestroy_)
175            this->destroy();
176    }
177
178    /**
179    @brief
180        Destroys the object.
181    */
182    void BasicProjectile::destroyObject(void)
183    {
184        if(GameMode::isMaster())
185            this->destroy();
186    }
187}
Note: See TracBrowser for help on using the repository browser.