Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/towerdefenseFabien/src/modules/weapons/projectiles/SplitGunProjectile.cc @ 10594

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

improved documentation

File size: 5.1 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 *      Fabien Vultier
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file SplitGunProjectile.h
31    @brief Implementation of the SplitGunProjectile class.
32*/
33
34#include "SplitGunProjectile.h"
35
36#include "core/CoreIncludes.h"
37#include "core/command/Executor.h"
38#include "util/Convert.h"
39#include "util/Math.h"
40
41namespace orxonox
42{
43    RegisterClass(SplitGunProjectile);
44
45    SplitGunProjectile::SplitGunProjectile(Context* context) : BillboardProjectile(context)
46    {
47        RegisterObject(SplitGunProjectile);
48
49        this->numberOfSplits_ = 0;
50        this->numberOfChilds_ = 0;
51        this->splitTime_ = 1.0;     
52        this->spread_ = 0.2;
53    }
54
55    void SplitGunProjectile::setNumberOfSplits(int numberOfSplits)
56    {
57        if (numberOfSplits >= 0)
58        {
59            this->numberOfSplits_ = numberOfSplits;
60        }
61        else
62        {
63            this->numberOfSplits_ = 0;
64        }
65    }
66
67    void SplitGunProjectile::setNumberOfChilds(int numberOfChilds)
68    {
69        if (numberOfChilds >= 0)
70        {
71            this->numberOfChilds_ = numberOfChilds;
72        }
73        else
74        {
75            this->numberOfChilds_ = 0;
76        }
77    }
78
79    /**
80    @brief
81        This function starts a timer that will cause the projectile to split after a time defined by the argument @param splitTime.       
82    */
83    void SplitGunProjectile::setSplitTime(float splitTime)
84    {
85        if (splitTime >= 0)
86        {
87            this->splitTime_ = splitTime;
88            this->textureTimer_.setTimer(this->splitTime_, false, createExecutor(createFunctor(&SplitGunProjectile::split, this)));
89        }
90        else
91        {
92            this->splitTime_ = 0;
93        }
94    }
95
96    void SplitGunProjectile::setSpread(float spread)
97    {
98        spread_ = spread;
99    }
100
101    /**
102    @brief
103        If this function is called the projectile splits up into many child projectiles. The original projectiles does not get destroyed but it will never split up again.
104    */
105    void SplitGunProjectile::split()
106    {
107        if (numberOfSplits_ > 0)
108        {
109            -- numberOfSplits_;
110
111            // Reduce damage of this projectile by the number of childs plus one. This way the total amount of possible damage contained in the original projectile and its childs is unchanged after s aplit.
112            this->setDamage(this->getDamage()/(numberOfChilds_+1));
113
114            // Calculate a normalized vector (velocityOffset) that is perpendicluar to the velocity of this projectile. Since there are infinitly many perpendicular vectors a random one is chosen.
115            Vector3 velocityInitial = this->getVelocity();
116            Vector3 velocityOffset = velocityInitial.perpendicular();
117            velocityOffset.normalise();
118            Degree angle = Degree(rnd(0,360));
119            velocityOffset = Quaternion(angle, velocityInitial.normalisedCopy()) * velocityOffset;
120            velocityOffset.normalise();
121
122            // Create as many childs as defined by numberOfChilds_
123            for (int i = 0; i < numberOfChilds_; ++i)
124            {
125                // Every child projectile should fly uniform in different directions -> Rotate the velocityOffset in every iteration
126                velocityOffset = Quaternion(Degree(360/numberOfChilds_), velocityInitial.normalisedCopy()) * velocityOffset;
127                velocityOffset.normalise();
128
129                SplitGunProjectile* projectile = new SplitGunProjectile(this->getContext());               
130
131                projectile->setMaterial(this->getMaterial());
132                projectile->setOrientation(this->getOrientation());
133                projectile->setPosition(this->getPosition());
134                projectile->setVelocity(velocityInitial + spread_*velocityOffset*velocityInitial.length());
135
136                projectile->setNumberOfSplits(this->numberOfSplits_);
137                projectile->setNumberOfChilds(this->numberOfChilds_);
138                projectile->setSplitTime(this->splitTime_);
139                projectile->setSpread(this->spread_);
140
141                projectile->setShooter(this->getShooter());
142                projectile->setDamage(this->getDamage());
143                projectile->setShieldDamage(this->getShieldDamage());
144                projectile->setHealthDamage(this->getHealthDamage());
145            }
146
147            numberOfSplits_ = 0;
148        }
149    }
150}
Note: See TracBrowser for help on using the repository browser.