Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 21, 2019, 2:19:16 PM (6 years ago)
Author:
ahuwyler
Message:

A new Game is born

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/OrxoBlox_FS19/src/modules/OrxoBlox/OrxoBloxBall.cc

    r12210 r12212  
    1 
    2 //TODO: Sounds (all the sounds are still from the pong module...)
    3 //TODO: Blocks (the Ball-Block comunication is based on how the blocks are implemented)
    4 //TODO: The bottom boundary/ the Ball collecter
    5 //TODO: Ability to shoot the ball (the ball is still constructed like the pong ball)
    6 
    71/*
    82 *   ORXONOX - the hottest 3D action shooter ever to exist
     
    4539#include "gametypes/Gametype.h"
    4640
    47 #include "OrxoBloxBlocks.h"
     41#include "OrxoBloxBat.h"
    4842
    4943#include "sound/WorldSound.h"
     
    6761        this->speed_ = 0;
    6862        this->accelerationFactor_ = 1.0f;
    69         this->block_ = nullptr;
    70         this->bDeleteBlock_ = false;
    71         this->blockID_ = new unsigned int[100];
    72         for (int i = 0; i < 100; i++) {
    73                 this->blockID_[i] = OBJECTID_UNKNOWN;
    74         }
     63        this->bat_ = nullptr;
     64        this->bDeleteBats_ = false;
     65        this->batID_ = new unsigned int[2];
     66        this->batID_[0] = OBJECTID_UNKNOWN;
     67        this->batID_[1] = OBJECTID_UNKNOWN;
     68        this->relMercyOffset_ = 0.05f;
    7569
    7670        this->registerVariables();
     
    10296        if (this->isInitialized())
    10397        {
    104             if (this->bDeleteBlock_)
    105                 delete[] this->block_;
    106 
    107             delete[] this->blockID_;
     98            if (this->bDeleteBats_)
     99                delete[] this->bat_;
     100
     101            delete[] this->batID_;
    108102        }
    109103    }
     
    126120        registerVariable( this->fieldWidth_ );
    127121        registerVariable( this->fieldHeight_ );
    128         registerVariable( this->blocklength_ );
     122        registerVariable( this->batlength_ );
    129123        registerVariable( this->speed_ );
    130         registerVariable( this->blockID_[0] );
    131         registerVariable( this->blockID_[1], VariableDirection::ToClient, new NetworkCallback<OrxoBloxBall>( this, &OrxoBloxBall::applyBlock) );
     124        registerVariable( this->relMercyOffset_ );
     125        registerVariable( this->batID_[0] );
     126        registerVariable( this->batID_[1], VariableDirection::ToClient, new NetworkCallback<OrxoBloxBall>( this, &OrxoBloxBall::applyBats) );
    132127    }
    133128
     
    135130    @brief
    136131        Is called every tick.
    137         Handles the movement of the ball and its interaction with the boundaries and blocks.
     132        Handles the movement of the ball and its interaction with the boundaries and bats.
    138133    @param dt
    139134        The time since the last tick.
     
    148143        Vector3 acceleration = this->getAcceleration();
    149144
    150         // If the ball has hit the boundaries on either the right side or the left
    151         if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2)
    152         {
    153             defBoundarySound_->play(); //play boundary sound
    154             // Its velocity in x-direction is inverted (i.e. it bounces off).
    155             velocity.x = -velocity.x;
    156             // And its position is set as to not overstep the boundary it has just crossed.
    157             if (position.x > this->fieldWidth_ / 2)
    158                 position.x = this->fieldWidth_ / 2;
    159             if (position.x < -this->fieldWidth_ / 2)
    160                 position.x = -this->fieldWidth_ / 2;
    161 
    162             this->fireEvent();
    163         }
    164 
    165         // If the ball has hit the boundary on the top
    166         if (position.z > this->fieldHeight_ / 2)
     145        // If the ball has gone over the top or bottom boundary of the playing field (i.e. the ball has hit the top or bottom delimiters).
     146        if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2)
    167147        {
    168148            defBoundarySound_->play(); //play boundary sound
     
    170150            velocity.z = -velocity.z;
    171151            // And its position is set as to not overstep the boundary it has just crossed.
    172             position.z = this->fieldHeight_ / 2;
     152            if (position.z > this->fieldHeight_ / 2)
     153                position.z = this->fieldHeight_ / 2;
     154            if (position.z < -this->fieldHeight_ / 2)
     155                position.z = -this->fieldHeight_ / 2;
    173156
    174157            this->fireEvent();
    175158        }
    176159
    177         // If the ball has crossed the bottom boundary
    178         if (position.z < -this->fieldHeight_ / 2)
    179         {
    180         //TODO: Ball Collector
     160        // If the ball has crossed the left or right boundary of the playing field (i.e. a player has just scored, if the bat isn't there to parry).
     161        if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2)
     162        {
     163            float distance = 0;
     164
     165            if (this->bat_ != nullptr) // If there are bats.
     166            {
     167                // If the right boundary has been crossed.
     168                if (position.x > this->fieldWidth_ / 2 && this->bat_[1] != nullptr)
     169                {
     170                    // Calculate the distance (in z-direction) between the ball and the center of the bat, weighted by half of the effective length of the bat (with additional 10%)
     171                    distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2);
     172                    if (fabs(distance) <= 1) // If the bat is there to parry.
     173                    {
     174                        defBatSound_->play(); //play bat sound
     175                        // Set the ball to be exactly at the boundary.
     176                        position.x = this->fieldWidth_ / 2;
     177                        // Invert its velocity in x-direction (i.e. it bounces off).
     178                        velocity.x = -velocity.x;
     179                        // Adjust the velocity in the z-direction, depending on where the ball hit the bat.
     180                        velocity.z = distance * distance * sgn(distance) * OrxoBloxBall::MAX_REL_Z_VELOCITY * this->speed_;
     181                        acceleration = this->bat_[1]->getVelocity() * this->accelerationFactor_ * -1;
     182
     183                        this->fireEvent();
     184                    }
     185                    // If the left player scores.
     186                    else if (GameMode::isMaster() && position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
     187                    {
     188                        defScoreSound_->play();//play score sound
     189                        if (this->getGametype() && this->bat_[0])
     190                        {
     191                            this->getGametype()->playerScored(this->bat_[0]->getPlayer());
     192                            return;
     193                        }
     194                    }
     195                }
     196                // If the left boundary has been crossed.
     197                else if (position.x < -this->fieldWidth_ / 2 && this->bat_[0] != nullptr)
     198                {
     199                    // Calculate the distance (in z-direction) between the ball and the center of the bat, weighted by half of the effective length of the bat (with additional 10%)
     200                    distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2);
     201                    if (fabs(distance) <= 1) // If the bat is there to parry.
     202                    {
     203                        defBatSound_->play(); //play bat sound
     204                        // Set the ball to be exactly at the boundary.
     205                        position.x = -this->fieldWidth_ / 2;
     206                        // Invert its velocity in x-direction (i.e. it bounces off).
     207                        velocity.x = -velocity.x;
     208                        // Adjust the velocity in the z-direction, depending on where the ball hit the bat.
     209                        velocity.z = distance * distance * sgn(distance) * OrxoBloxBall::MAX_REL_Z_VELOCITY * this->speed_;
     210                        acceleration = this->bat_[0]->getVelocity() * this->accelerationFactor_ * -1;
     211
     212                        this->fireEvent();
     213                    }
     214                    // If the right player scores.
     215                    else if (GameMode::isMaster() && position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
     216                    {
     217                        defScoreSound_->play();//play score sound
     218                        if (this->getGametype() && this->bat_[1])
     219                        {
     220                            this->getGametype()->playerScored(this->bat_[1]->getPlayer());
     221                            return;
     222                        }
     223                    }
     224                }
     225            }
    181226        }
    182227
     
    215260    /**
    216261    @brief
    217         Set the blocks for the ball.
     262        Set the bats for the ball.
    218263    @param bats
    219         An array (of size n (n=#Blocks) of weak pointers, to be set as the new blocks.
    220     */
    221     void OrxoBloxBall::setBlock(WeakPtr<OrxoBloxBlock>* block, int n)
    222     {
    223         if (this->bDeleteBlock_) // If there are already some blocks, delete them.
    224         {
    225             delete[] this->block_;
    226             this->bDeleteBlock_ = false;
    227         }
    228 
    229         this->block_ = block;
     264        An array (of size 2) of weak pointers, to be set as the new bats.
     265    */
     266    void OrxoBloxBall::setBats(WeakPtr<OrxoBloxBat>* bats)
     267    {
     268        if (this->bDeleteBats_) // If there are already some bats, delete them.
     269        {
     270            delete[] this->bat_;
     271            this->bDeleteBats_ = false;
     272        }
     273
     274        this->bat_ = bats;
    230275        // Also store their object IDs, for synchronization.
    231         for (int i = 0; i < n; i++) {
    232                 this->blockID_[i] = this->block_[i]->getObjectID();
    233         }
    234     }
    235 
    236     /**
    237     @brief
    238         Get the blocks over the network.
    239     */
    240     void OrxoBloxBall::applyBlock(int n)
    241     {
    242         // Make space for the blocks, if they don't exist, yet.
    243         if (this->block_ == nullptr)
    244         {
    245             this->block_ = new WeakPtr<OrxoBloxBlock>[n];
    246             this->bDeleteBlock_ = true;
    247         }
    248        
    249         for (int i = 0; i < n; i++) {
    250                 if (this->blockID_[i] != OBJECTID_UNKNOWN)
    251                 this->bat_[i] = orxonox_cast<OrxoBloxBlock*>(Synchronisable::getSynchronisable(this->blockID_[i]));
    252         }
    253     }
    254 
    255     void OrxoBloxBall::setDefScoreSound(const std::string &pongSound)
     276        this->batID_[0] = this->bat_[0]->getObjectID();
     277        this->batID_[1] = this->bat_[1]->getObjectID();
     278    }
     279
     280    /**
     281    @brief
     282        Get the bats over the network.
     283    */
     284    void OrxoBloxBall::applyBats()
     285    {
     286        // Make space for the bats, if they don't exist, yet.
     287        if (this->bat_ == nullptr)
     288        {
     289            this->bat_ = new WeakPtr<OrxoBloxBat>[2];
     290            this->bDeleteBats_ = true;
     291        }
     292
     293        if (this->batID_[0] != OBJECTID_UNKNOWN)
     294            this->bat_[0] = orxonox_cast<OrxoBloxBat*>(Synchronisable::getSynchronisable(this->batID_[0]));
     295        if (this->batID_[1] != OBJECTID_UNKNOWN)
     296            this->bat_[1] = orxonox_cast<OrxoBloxBat*>(Synchronisable::getSynchronisable(this->batID_[1]));
     297    }
     298
     299    void OrxoBloxBall::setDefScoreSound(const std::string &OrxoBloxSound)
    256300    {
    257301        if( defScoreSound_ )
    258             defScoreSound_->setSource(pongSound);
     302            defScoreSound_->setSource(OrxoBloxSound);
    259303        else
    260304            assert(0); // This should never happen, because soundpointer is only available on master
     
    270314    }
    271315
    272     void OrxoBloxBall::setDefBatSound(const std::string &pongSound)
     316    void OrxoBloxBall::setDefBatSound(const std::string &OrxoBloxSound)
    273317    {
    274318        if( defBatSound_ )
    275             defBatSound_->setSource(pongSound);
     319            defBatSound_->setSource(OrxoBloxSound);
    276320        else
    277321            assert(0); // This should never happen, because soundpointer is only available on master
     
    287331    }
    288332
    289     void OrxoBloxBall::setDefBoundarySound(const std::string &pongSound)
     333    void OrxoBloxBall::setDefBoundarySound(const std::string &OrxoBloxSound)
    290334    {
    291335        if( defBoundarySound_ )
    292             defBoundarySound_->setSource(pongSound);
     336            defBoundarySound_->setSource(OrxoBloxSound);
    293337        else
    294338            assert(0); // This should never happen, because soundpointer is only available on master
Note: See TracChangeset for help on using the changeset viewer.