Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 4, 2015, 12:18:01 PM (9 years ago)
Author:
landauf
Message:

merged branch towerdefenseFS15

Location:
code/branches/presentationFS15merge
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentationFS15merge

  • code/branches/presentationFS15merge/src/modules/towerdefense/TowerDefense.cc

    r10258 r10615  
    3737 *
    3838 *
    39  *
    4039 *TIPP: Eclipse hilft euch schnell auf bereits vorhanden Funktionen zuzugreifen:
    4140 * einfach "this->" eingeben und kurz warten. Dann tauch eine Liste mit Vorschlägen auf. Wenn ihr jetzt weiter
     
    7776#include "TowerDefenseCenterpoint.h"
    7877//#include "TDCoordinate.h"
    79 #include "TowerTurret.h"
    8078#include "worldentities/SpawnPoint.h"
    8179#include "worldentities/pawns/Pawn.h"
     
    8886/* Part of a temporary hack to allow the player to add towers */
    8987#include "core/command/ConsoleCommand.h"
     88#include <cmath>
     89
    9090
    9191namespace orxonox
    9292{
     93    static const std::string __CC_addTower_name  = "addTower";
     94    static const std::string __CC_upgradeTower_name = "upgradeTower";
     95    static const int upgradeCost = 20;
     96    static const int towerCost = 20;
     97    unsigned int maxspaceships = 30;
     98    int maxspaceshipsstandard = 30;
     99
     100
     101
     102    SetConsoleCommand("TowerDefense", __CC_addTower_name,  &TowerDefense::addTower ).addShortcut().defaultValues(1);
     103    SetConsoleCommand("TowerDefense", __CC_upgradeTower_name, &TowerDefense::upgradeTower).addShortcut().defaultValues(0);
     104
    93105    RegisterUnloadableClass(TowerDefense);
    94106
    95     TowerDefense::TowerDefense(Context* context) : Deathmatch(context)
     107    TowerDefense::TowerDefense(Context* context) : TeamDeathmatch(context)
    96108    {
    97109        RegisterObject(TowerDefense);
     
    103115        }*/
    104116
     117        //Timer for the waves (10 seconds between the waves)
     118        selecter = NULL;
     119        this->player_ = NULL;       
    105120        this->setHUDTemplate("TowerDefenseHUD");
    106 
    107         //this->stats_ = new TowerDefensePlayerStats();
    108 
    109         /* Temporary hack to allow the player to add towers and upgrade them */
    110         this->dedicatedAddTower_ = createConsoleCommand( "addTower", createExecutor( createFunctor(&TowerDefense::addTower, this) ) );
    111         this->dedicatedUpgradeTower_ = createConsoleCommand( "upgradeTower", createExecutor( createFunctor(&TowerDefense::upgradeTower, this) ) );
     121        this->nextwaveTimer_.setTimer(10, false, createExecutor(createFunctor(&TowerDefense::nextwave, this)));
     122        this->nextwaveTimer_.stopTimer();
     123        this->waves_ = 0;
     124        this->time = 0;
     125        this->credit_ = 0;
     126        this->lifes_ = 0;
     127        this->timeSetTower_ = 0;
     128        spaceships =15;
     129        eggs=3;
     130        ufos=7;
     131        randomships=5;
     132
     133
     134        ModifyConsoleCommand(__CC_addTower_name).setObject(this);
     135        ModifyConsoleCommand(__CC_upgradeTower_name).setObject(this);
    112136    }
    113137
     
    117141        if (this->isInitialized())
    118142        {
    119             if( this->dedicatedAddTower_ )
    120                 delete this->dedicatedAddTower_;
     143            ModifyConsoleCommand(__CC_addTower_name).setObject(NULL);
     144            ModifyConsoleCommand(__CC_upgradeTower_name).setObject(NULL);
    121145        }
    122146    }
     
    131155    void TowerDefense::start()
    132156    {
    133 
    134         Deathmatch::start();
    135 
    136 // Waypoints: [1,3] [10,3] [10,11] [13,11] -> add the points to a matrix so the player cant place towers on the path
    137         for (int i=0; i < 16 ; i++){
    138             for (int j = 0; j< 16 ; j++){
    139                 towermatrix[i][j] = false;
     157        if (center_ != NULL) // There needs to be a TowerDefenseCenterpoint, i.e. the area the game takes place.
     158        {
     159            if (selecter == NULL)
     160            {
     161                selecter = new TowerDefenseSelecter(this->center_->getContext());               
    140162            }
    141         }
    142 
     163            selecter->addTemplate(center_->getSelecterTemplate());
     164            center_->attach(selecter);
     165        }
     166        else // If no centerpoint was specified, an error is thrown and the level is exited.
     167        {
     168            orxout(internal_error) << "Jump: No Centerpoint specified." << endl;
     169            return;
     170        }
     171
     172        TeamDeathmatch::start();
     173
     174        // Waypoints: [1,3] [10,3] [10,11] [13,11] -> add the points to a matrix so the player cant place towers on the path
     175        for (int i=0; i < 16 ; i++)
     176        {
     177            for (int j = 0; j< 16 ; j++)
     178            {
     179                towerModelMatrix[i][j] = NULL;
     180                towerTurretMatrix[i][j] = NULL;
     181            }
     182        }
     183
     184       
     185
     186        if (player_ != NULL)
     187        {
     188            //this->player_->startControl(selecter);
     189        }
     190        else
     191        {
     192            orxout() << "player=NULL" << endl;
     193        }
     194
     195
     196        Model* dummyModel = new Model(this->center_->getContext());
     197
     198        //the path of the spacehips has to be blocked, so that no towers can be build there
    143199        for (int k=0; k<3; k++)
    144             towermatrix[1][k]=true;
     200            towerModelMatrix[1][k]=dummyModel;
    145201        for (int l=1; l<11; l++)
    146             towermatrix[l][3]=true;
     202                towerModelMatrix[l][3]=dummyModel;
    147203        for (int m=3; m<12; m++)
    148             towermatrix[10][m]=true;
     204                towerModelMatrix[10][m]=dummyModel;
    149205        for (int n=10; n<14; n++)
    150             towermatrix[n][11]=true;
     206                towerModelMatrix[n][11]=dummyModel;
    151207        for (int o=13; o<16; o++)
    152             towermatrix[13][o]=true;
     208                towerModelMatrix[13][o]=dummyModel;
     209
    153210
    154211        //set initial credits, lifes and WaveNumber
    155         this->setCredit(200);
    156         this->setLifes(50);
     212        this->setCredit(100);
     213        this->setLifes(100);
    157214        this->setWaveNumber(0);
    158215        time=0.0;
    159216
     217        /*
    160218        //adds initial towers
    161219        for (int i=0; i <7; i++){
    162220            addTower(i+3,4);
    163         }/*
    164         for (int j=0; j < 7; j++){
    165             addTower(9,j+5);
    166         }*/
     221        }
     222                */
    167223    }
    168224
     
    178234            en1->addTemplate("enemytowerdefense1");
    179235            en1->setScale(3);
    180             en1->setHealth(en1->getHealth() + this->getWaveNumber()*4);
     236            en1->lookAt(Vector3(0,0,100000));
     237            en1->setHealth(en1->getHealth() +50 + this->getWaveNumber()*4);
    181238            break;
    182239
     
    184241            en1->addTemplate("enemytowerdefense2");
    185242            en1->setScale(2);
    186             en1->setHealth(en1->getHealth() + this->getWaveNumber()*4);
     243            en1->lookAt(Vector3(0,0,100000));
     244            en1->roll(Degree(120));
     245            en1->setHealth(en1->getHealth() -30 + this->getWaveNumber()*4);
    187246            //  en1->setShieldHealth(en1->getShield() = this->getWaveNumber()*2))
    188247            break;
     
    191250            en1->addTemplate("enemytowerdefense3");
    192251            en1->setScale(1);
    193             en1->setHealth(en1->getHealth() + this->getWaveNumber()*4);
     252            en1->lookAt(Vector3(0,0,100000));
     253            en1->roll(Degree(120));
     254            en1->setHealth(en1->getHealth() -10 + this->getWaveNumber()*4);
    194255            break;
    195256        }
    196257
     258        en1->setTeam(2);
    197259        en1->getController();
    198         en1->setPosition(path.at(0)->get3dcoordinate());
     260        en1->setPosition(path.at(0)->get3dcoordinate());       
    199261        TowerDefenseEnemyvector.push_back(en1);
    200262
     
    209271    {
    210272
    211         Deathmatch::end();
     273        TeamDeathmatch::end();
    212274        ChatManager::message("Match is over! Gameover!");
    213275
     276    }
     277
     278    void TowerDefense::spawnPlayer(PlayerInfo* player)
     279    {
     280        assert(player);
     281        this->player_ = player;
     282
     283        if (selecter->getPlayer() == NULL)
     284        {
     285            this->player_ = player;
     286            player->startControl(selecter);
     287            players_[player].state_ = PlayerState::Alive;
     288        }
     289    }
     290
     291    /**
     292    @brief
     293        Get the player.
     294    @return
     295        Returns a pointer to the player. If there is no player, NULL is returned.
     296    */
     297    PlayerInfo* TowerDefense::getPlayer(void) const
     298    {
     299        return this->player_;
    214300    }
    215301
    216302    //not working yet
    217303    void TowerDefense::upgradeTower(int x,int y)
    218     {/*
    219         const int upgradeCost = 20;
     304    {
     305        TDCoordinate* coord = new TDCoordinate(x,y);
     306        x = coord->GetX();
     307        y = coord->GetY();
     308       
    220309
    221310        if (!this->hasEnoughCreditForTower(upgradeCost))
     
    225314        }
    226315
    227         if (towermatrix [x][y] == NULL)
     316
     317        Model* dummyModel2 = new Model(this->center_->getContext());
     318
     319        if (towerModelMatrix [x][y] == NULL || (towerModelMatrix [x][y])->getMeshSource() == dummyModel2->getMeshSource())
    228320        {
    229321            orxout() << "no tower on this position" << endl;
     
    233325        else
    234326        {
    235             (towermatrix [x][y])->upgradeTower();
    236         }*/
     327            (towerTurretMatrix [x][y])->upgradeTower();
     328            switch(towerTurretMatrix[x][y]->upgrade)
     329                   {
     330                   case 1 :
     331                           towerModelMatrix[x][y]->setMeshSource("TD_T2.mesh");
     332                           break;
     333
     334                   case 2 :
     335                           towerModelMatrix[x][y]->setMeshSource("TD_T3.mesh");
     336                           break;
     337                   case 3 :
     338                           towerModelMatrix[x][y]->setMeshSource("TD_T4.mesh");
     339                           break;
     340                   case 4 :
     341                           towerModelMatrix[x][y]->setMeshSource("TD_T5.mesh");
     342                           break;
     343
     344                   }
     345
     346            this->buyTower(upgradeCost);
     347        }
    237348    }
    238349
    239350    /*adds Tower at Position (x,y) and reduces credit and adds the point to the towermatrix. template ("towerturret")
    240351    so towers have ability if the turrets
    241 
    242352    */
     353
    243354    void TowerDefense::addTower(int x, int y)
    244     {
    245         const int towerCost = 20;
     355    {       
     356        TDCoordinate* coord = new TDCoordinate(x,y);
     357        x = coord->GetX();
     358        y = coord->GetY();
     359
    246360
    247361        if (!this->hasEnoughCreditForTower(towerCost))
     
    251365        }
    252366
    253         if (towermatrix [x][y]==true)
     367        if (towerModelMatrix [x][y]!=NULL)
    254368        {
    255369            orxout() << "not possible to put tower here!!" << endl;
     
    264378        int tileScale = (int) this->center_->getTileScale();
    265379
    266         if (x > 15 || y > 15 || x < 0 || y < 0)
     380        /*if (x > 15 || y > 15 || x < 0 || y < 0)
    267381        {
    268382            //Hard coded: TODO: let this depend on the centerpoint's height, width and fieldsize (fieldsize doesn't exist yet)
    269383            orxout() << "Can not add Tower: x and y should be between 0 and 15" << endl;
    270384            return;
    271         }
    272 
    273         orxout() << "Will add tower at (" << (x-8) * tileScale << "," << (y-8) * tileScale << ")" << endl;
    274 
    275        //Reduce credit
    276         this->buyTower(towerCost);
    277         towermatrix [x][y]=true;
     385        }*/
     386
     387        //orxout() << "Will add tower at (" << (x-8) * tileScale << "," << (y-8) * tileScale << ")" << endl;
     388        orxout() << "Will add tower at (" << x << "," << y << ")" << endl;
     389
     390
     391        //Create Model
     392        Model* newTowerModel = new Model(this->center_->getContext());
     393        newTowerModel->setMeshSource("TD_T1.mesh");
     394        newTowerModel->setScale(30);
     395        newTowerModel->pitch(Degree(90));
     396        newTowerModel->setPosition(static_cast<float>((x-8) * tileScale), static_cast<float>((y-8) * tileScale), 80);
    278397
    279398        //Creates tower
    280399        TowerDefenseTower* towernew = new TowerDefenseTower(this->center_->getContext());
    281         towernew->addTemplate("towerturret");
    282         towernew->setPosition(static_cast<float>((x-8) * tileScale), static_cast<float>((y-8) * tileScale), 75);
     400        towernew->setPosition(static_cast<float>((x-8) * tileScale), static_cast<float>((y-8) * tileScale), 275);
    283401        towernew->setGame(this);
    284     }
     402        towernew->setTeam(1);
     403
     404        //Reduce credit
     405         this->buyTower(towerCost);
     406         towerModelMatrix [x][y]= newTowerModel;
     407         towerTurretMatrix [x][y]= towernew;
     408    }   
    285409
    286410    bool TowerDefense::hasEnoughCreditForTower(int towerCost)
     
    296420
    297421 
     422    void TowerDefense::nextwave()
     423    {
     424
     425        orxout() << "newwave" << endl;
     426        TowerDefenseEnemyvector.clear();
     427        waves_++;
     428        //maxspaceships = round(maxspaceshipsstandard + 0.25*(waves_));
     429        time=0;
     430
     431        int helpnumber = 40 -(waves_);
     432        if(helpnumber <= 0) {helpnumber =1;}
     433        float numSpaceships = std::abs((rand() % 100)*5*(helpnumber));
     434        float numEggs = std::abs((rand() % 100)*1*(waves_));
     435        float numUfos = std::abs((rand() % 100)*1.5*(0.5*(waves_))) ;
     436
     437        float totalnumber = (numSpaceships + numEggs + numUfos)*1.3;
     438
     439        int newspaceships = (int)(maxspaceships* numSpaceships / totalnumber);
     440        int neweggs = (int)(maxspaceships*numEggs / totalnumber);
     441        int newufos = (int)(maxspaceships*numUfos / totalnumber);
     442        int newrandomships = maxspaceships -newspaceships - neweggs - newufos;
     443        spaceships =newspaceships;
     444        eggs=neweggs;
     445        ufos=newufos;
     446        randomships=newrandomships;
     447
     448        orxout() << spaceships << endl;
     449        orxout() << eggs << endl;
     450        orxout() << ufos << endl;
     451        orxout() << randomships << endl;
     452
     453
     454
     455
     456
     457    }
     458
    298459    void TowerDefense::tick(float dt)
    299460    {
    300461        SUPER(TowerDefense, tick, dt);
    301462        time +=dt;
     463        timeSetTower_ +=dt;
     464
     465        //Check if tower has to be set (because TowerDefenseSelecter asks for it)
     466        if(timeSetTower_ >= 0.25)
     467        {
     468                timeSetTower_ =0;
     469                        if(selecter != NULL && selecter->firePressed_)
     470                        {
     471
     472                                int x = selecter->selectedPos_->GetX();
     473                                int y = selecter->selectedPos_->GetY();
     474                                Model* dummyModel2 = new Model(this->center_->getContext());
     475
     476
     477
     478                                if(towerModelMatrix[x][y] == NULL)
     479                                {
     480                                        addTower(x,y);
     481                                }
     482                                else
     483                                {
     484                                        if(!((towerModelMatrix [x][y])->getMeshSource() == dummyModel2->getMeshSource()))
     485                                        {
     486                                                towerTurretMatrix[x][y]->upgradeTower();
     487                                        if(towerTurretMatrix[x][y]->upgrade < towerTurretMatrix[x][y]->upgradeMax)
     488                                        {
     489                                                int specificupgradecost = (int)(upgradeCost*(std::pow(1.5,towerTurretMatrix[x][y]->upgrade)));
     490                                                if(this->credit_ >= specificupgradecost)
     491                                                {
     492                                                        this->buyTower(specificupgradecost);
     493                                                                switch(towerTurretMatrix[x][y]->upgrade)
     494                                                           {
     495                                                                   case 1 :
     496                                                                           towerModelMatrix[x][y]->setMeshSource("TD_T2.mesh");
     497                                                                           break;
     498
     499                                                                   case 2 :
     500                                                                           towerModelMatrix[x][y]->setMeshSource("TD_T3.mesh");
     501                                                                           break;
     502                                                   case 3 :
     503                                                           towerModelMatrix[x][y]->setMeshSource("TD_T4.mesh");
     504                                                           break;
     505                                                   case 4 :
     506                                                           towerModelMatrix[x][y]->setMeshSource("TD_T5.mesh");
     507                                                           break;
     508
     509                                                           }
     510                                                }
     511
     512
     513                                        }
     514                                        }
     515                                }
     516                                selecter->firePressed_ = false;
     517                        }
     518        }
    302519
    303520        TDCoordinate* coord1 = new TDCoordinate(1,1);
    304521        std::vector<TDCoordinate*> path;
    305522        path.push_back(coord1);
    306         if(time>1 && TowerDefenseEnemyvector.size() < 30)
    307         {
    308             //adds different types of enemys depending on the WaveNumber
    309             addTowerDefenseEnemy(path, this->getWaveNumber() % 3 +1 );
    310             time = time-1;
    311         }
     523
     524
     525
     526
     527
     528        if(time>=TowerDefenseEnemyvector.size() && TowerDefenseEnemyvector.size() < maxspaceships)
     529                {
     530
     531                //adds different types of enemys depending on the WaveNumber progressively making the combination of enemys more difficult
     532                if(spaceships>0)
     533                {
     534                        addTowerDefenseEnemy(path, 1);
     535                        spaceships--;
     536
     537                }else if(ufos>0)
     538                {
     539                        addTowerDefenseEnemy(path, 3);
     540                        ufos--;
     541                }else if(eggs>0)
     542                {
     543                        addTowerDefenseEnemy(path, 2);
     544                        eggs--;
     545                }else if(randomships>0)
     546                {
     547                        addTowerDefenseEnemy(path, rand() % 3 +1);
     548                        randomships--;
     549
     550                }
     551
     552                }
     553
    312554
    313555        Vector3* endpoint = new Vector3(500, 700, 150);
     
    317559            if(TowerDefenseEnemyvector.at(i) != NULL && TowerDefenseEnemyvector.at(i)->isAlive())
    318560            {
    319                 //destroys enemys at the end of teh path and reduces the life by 1. no credits gifted
     561                //destroys enemys at the end of the path and reduces the life by 1. no credits gifted
    320562
    321563                Vector3 ship = TowerDefenseEnemyvector.at(i)->getRVWorldPosition();
     
    333575            }
    334576        }
     577
    335578        //goes thorugh vector to see if an enemy is still alive. if not next wave is launched
    336579        int count= 0;
     
    343586        }
    344587
     588        if (count == 0 && !this->nextwaveTimer_.isActive())
     589            this->nextwaveTimer_.startTimer();
     590
     591/*            time2 +=dt;
    345592        if(count== 0)
    346593        {
    347             time2 +=dt;
    348594            if(time2 > 10)
    349595            {
     
    354600            }
    355601        }
    356 
    357 
    358     }
     602*/
     603
     604    }
     605
    359606
    360607    // Function to test if we can add waypoints using code only. Doesn't work yet
     
    399646    void TowerDefense::playerEntered(PlayerInfo* player)
    400647    {
    401         Deathmatch::playerEntered(player);
     648        TeamDeathmatch::playerEntered(player);
    402649
    403650        const std::string& message = player->getName() + " entered the game";
     
    407654    bool TowerDefense::playerLeft(PlayerInfo* player)
    408655    {
    409         bool valid_player = Deathmatch::playerLeft(player);
     656        bool valid_player = TeamDeathmatch::playerLeft(player);
    410657
    411658        if (valid_player)
     
    437684        }
    438685
    439         Deathmatch::pawnKilled(victim, killer);
     686        TeamDeathmatch::pawnKilled(victim, killer);
    440687    }
    441688
Note: See TracChangeset for help on using the changeset viewer.