Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 3, 2012, 6:05:24 PM (12 years ago)
Author:
landauf
Message:

replaced tabs with spaces (no code changed)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentation2012merge/src/modules/towerdefense/TowerDefense.cc

    r9271 r9272  
    8282#include "worldentities/pawns/SpaceShip.h"
    8383#include "controllers/WaypointController.h"
    84          
     84
    8585#include "graphics/Model.h"
    8686#include "infos/PlayerInfo.h"
    87          
     87
    8888#include "chat/ChatManager.h"
    8989
     
    9494{
    9595    CreateUnloadableFactory(TowerDefense);
    96        
    97         TowerDefense::TowerDefense(BaseObject* creator) : Deathmatch(creator)
     96
     97    TowerDefense::TowerDefense(BaseObject* creator) : Deathmatch(creator)
    9898    {
    9999        RegisterObject(TowerDefense);
    100                
     100
    101101        this->setHUDTemplate("TowerDefenseHUD");
    102102
    103                 this->stats_ = new TowerDefensePlayerStats();
    104 
    105                 /* Temporary hack to allow the player to add towers */
    106                 this->dedicatedAddTower_ = createConsoleCommand( "addTower", createExecutor( createFunctor(&TowerDefense::addTower, this) ) );
    107     }
    108        
     103        this->stats_ = new TowerDefensePlayerStats();
     104
     105        /* Temporary hack to allow the player to add towers */
     106        this->dedicatedAddTower_ = createConsoleCommand( "addTower", createExecutor( createFunctor(&TowerDefense::addTower, this) ) );
     107    }
     108
    109109    TowerDefense::~TowerDefense()
    110110    {
    111                 /* Part of a temporary hack to allow the player to add towers */
     111        /* Part of a temporary hack to allow the player to add towers */
    112112        if (this->isInitialized())
    113113        {
     
    116116        }
    117117    }
    118        
    119         void TowerDefense::setCenterpoint(TowerDefenseCenterpoint *centerpoint)
    120         {
    121                 orxout() << "Centerpoint now setting..." << endl;
    122                 this->center_ = centerpoint;
    123                 orxout() << "Centerpoint now set..." << endl;
    124         }
    125        
     118
     119    void TowerDefense::setCenterpoint(TowerDefenseCenterpoint *centerpoint)
     120    {
     121        orxout() << "Centerpoint now setting..." << endl;
     122        this->center_ = centerpoint;
     123        orxout() << "Centerpoint now set..." << endl;
     124    }
     125
    126126    void TowerDefense::start()
    127127    {
    128128        Deathmatch::start();
    129                
    130                 const int kInitialTowerCount = 3;
    131                 Coordinate initialTowerCoordinates[kInitialTowerCount] = {{3,2}, {8,5}, {12,10}};
    132                
    133                 for (int i = 0; i < kInitialTowerCount; i++)
    134                 {
    135                         Coordinate coordinate = initialTowerCoordinates[i];
    136                         addTower(coordinate.x, coordinate.y);
    137                 }
    138                
    139                 ChatManager::message("Use the console command addTower x y to add towers");
    140 
    141                 //TODO: let the player control his controllable entity && TODO: create a new ControllableEntity for the player
    142         }
    143        
    144         void TowerDefense::end()
    145         {
    146                 Deathmatch::end();
    147          
    148                 ChatManager::message("Match is over");
    149         }
    150        
    151         void TowerDefense::addTower(int x, int y)
    152         {
    153                 const TowerCost towerCost = TDDefaultTowerCost;
    154                
    155                 if (!this->hasEnoughCreditForTower(towerCost))
    156                 {
    157                         orxout() << "not enough credit: " << (this->stats_->getCredit()) << " available, " << TDDefaultTowerCost << " needed.";
    158                         return;
    159                 }
    160                
    161                 if (this->towerExists(x,y))
    162                 {
    163                         orxout() << "tower exists!!" << endl;
    164                         return;
    165                 }               
    166                
    167                 /*
    168                 unsigned int width = this->center_->getWidth();
    169                 unsigned int height = this->center_->getHeight();
    170                 */
    171                
    172                 int tileScale = (int) this->center_->getTileScale();
    173                        
    174                 if (x > 15 || y > 15 || x < 0 || y < 0)
    175                 {
    176                         //Hard coded: TODO: let this depend on the centerpoint's height, width and fieldsize (fieldsize doesn't exist yet)
    177                         orxout() << "Can not add Tower: x and y should be between 0 and 15" << endl;
    178                         return;
    179                 }
    180                
    181                 orxout() << "Will add tower at (" << (x-8) * tileScale << "," << (y-8) * tileScale << ")" << endl;
    182                
    183                 // Add tower to coordinatesStack
    184                 Coordinate newTowerCoordinates = {x, y};
    185                 addedTowersCoordinates_.push_back(newTowerCoordinates);
    186                
    187                 // Reduce credit
    188                 this->stats_->buyTower(towerCost);
    189                
    190                 // Create tower
    191                 Tower* newTower = new Tower(this->center_);
    192                 newTower->addTemplate(this->center_->getTowerTemplate());
    193 
    194                 newTower->setPosition(static_cast<float>((x-8) * tileScale), static_cast<float>((y-8) * tileScale), 75);
    195                 newTower->setGame(this);
    196         }
    197        
    198         bool TowerDefense::hasEnoughCreditForTower(TowerCost towerCost)
    199         {
    200                 return ((this->stats_->getCredit()) >= towerCost);
    201         }
    202        
    203         bool TowerDefense::towerExists(int x, int y)
    204         {
    205                 for(std::vector<Coordinate>::iterator it = addedTowersCoordinates_.begin(); it != addedTowersCoordinates_.end(); ++it)
    206                 {
    207                         Coordinate currentCoordinates = (Coordinate) (*it);
    208                         if (currentCoordinates.x == x && currentCoordinates.y == y)
    209                                 return true;
    210                 }
    211                
    212                 return false;
    213         }
    214        
    215        
    216         void TowerDefense::tick(float dt)
     129
     130        const int kInitialTowerCount = 3;
     131        Coordinate initialTowerCoordinates[kInitialTowerCount] = {{3,2}, {8,5}, {12,10}};
     132
     133        for (int i = 0; i < kInitialTowerCount; i++)
     134        {
     135            Coordinate coordinate = initialTowerCoordinates[i];
     136            addTower(coordinate.x, coordinate.y);
     137        }
     138
     139        ChatManager::message("Use the console command addTower x y to add towers");
     140
     141        //TODO: let the player control his controllable entity && TODO: create a new ControllableEntity for the player
     142    }
     143
     144    void TowerDefense::end()
     145    {
     146        Deathmatch::end();
     147
     148        ChatManager::message("Match is over");
     149    }
     150
     151    void TowerDefense::addTower(int x, int y)
     152    {
     153        const TowerCost towerCost = TDDefaultTowerCost;
     154
     155        if (!this->hasEnoughCreditForTower(towerCost))
     156        {
     157            orxout() << "not enough credit: " << (this->stats_->getCredit()) << " available, " << TDDefaultTowerCost << " needed.";
     158            return;
     159        }
     160
     161        if (this->towerExists(x,y))
     162        {
     163            orxout() << "tower exists!!" << endl;
     164            return;
     165        }
     166
     167        /*
     168        unsigned int width = this->center_->getWidth();
     169        unsigned int height = this->center_->getHeight();
     170        */
     171
     172        int tileScale = (int) this->center_->getTileScale();
     173
     174        if (x > 15 || y > 15 || x < 0 || y < 0)
     175        {
     176            //Hard coded: TODO: let this depend on the centerpoint's height, width and fieldsize (fieldsize doesn't exist yet)
     177            orxout() << "Can not add Tower: x and y should be between 0 and 15" << endl;
     178            return;
     179        }
     180
     181        orxout() << "Will add tower at (" << (x-8) * tileScale << "," << (y-8) * tileScale << ")" << endl;
     182
     183        // Add tower to coordinatesStack
     184        Coordinate newTowerCoordinates = {x, y};
     185        addedTowersCoordinates_.push_back(newTowerCoordinates);
     186
     187        // Reduce credit
     188        this->stats_->buyTower(towerCost);
     189
     190        // Create tower
     191        Tower* newTower = new Tower(this->center_);
     192        newTower->addTemplate(this->center_->getTowerTemplate());
     193
     194        newTower->setPosition(static_cast<float>((x-8) * tileScale), static_cast<float>((y-8) * tileScale), 75);
     195        newTower->setGame(this);
     196    }
     197
     198    bool TowerDefense::hasEnoughCreditForTower(TowerCost towerCost)
     199    {
     200        return ((this->stats_->getCredit()) >= towerCost);
     201    }
     202
     203    bool TowerDefense::towerExists(int x, int y)
     204    {
     205        for(std::vector<Coordinate>::iterator it = addedTowersCoordinates_.begin(); it != addedTowersCoordinates_.end(); ++it)
     206        {
     207            Coordinate currentCoordinates = (Coordinate) (*it);
     208            if (currentCoordinates.x == x && currentCoordinates.y == y)
     209                return true;
     210        }
     211
     212        return false;
     213    }
     214
     215
     216    void TowerDefense::tick(float dt)
    217217    {
    218218        SUPER(TowerDefense, tick, dt);
    219219    }
    220        
    221         // Function to test if we can add waypoints using code only. Doesn't work yet
    222        
    223         // THE PROBLEM: WaypointController's getControllableEntity() returns null, so it won't track. How do we get the controlableEntity to NOT BE NULL???
    224         /*
    225         void TowerDefense::addWaypointsAndFirstEnemy()
    226         {
    227                 SpaceShip *newShip = new SpaceShip(this->center_);
    228                 newShip->addTemplate("spaceshipassff");
    229                
    230                 WaypointController *newController = new WaypointController(newShip);
    231                 newController->setAccuracy(3);
    232                
    233                 Model *wayPoint1 = new Model(newController);
    234                 wayPoint1->setMeshSource("crate.mesh");
    235                 wayPoint1->setPosition(7,-7,5);
    236                 wayPoint1->setScale(0.2);
    237                        
    238                 Model *wayPoint2 = new Model(newController);
    239                 wayPoint2->setMeshSource("crate.mesh");
    240                 wayPoint2->setPosition(7,7,5);
    241                 wayPoint2->setScale(0.2);
    242                        
    243                 newController->addWaypoint(wayPoint1);
    244                 newController->addWaypoint(wayPoint2);
    245                        
    246                 // The following line causes the game to crash
    247 
    248                 newShip->setController(newController);
    249 //              newController -> getPlayer() -> startControl(newShip);
    250                 newShip->setPosition(-7,-7,5);
    251                 newShip->setScale(0.1);
    252                 //newShip->addSpeed(1);
    253                
    254                
    255                
    256 //              this->center_->attach(newShip);
    257         }
    258         */
    259         /*
    260          void TowerDefense::playerEntered(PlayerInfo* player)
    261          {
    262          Deathmatch::playerEntered(player);
    263          
    264          const std::string& message = player->getName() + " entered the game";
    265          ChatManager::message(message);
    266          }
    267          
    268          bool TowerDefense::playerLeft(PlayerInfo* player)
    269          {
    270          bool valid_player = Deathmatch::playerLeft(player);
    271          
    272          if (valid_player)
    273          {
    274          const std::string& message = player->getName() + " left the game";
    275          ChatManager::message(message);
    276          }
    277          
    278          return valid_player;
    279          }
    280          
    281          
    282          void TowerDefense::pawnKilled(Pawn* victim, Pawn* killer)
    283          {
    284          if (victim && victim->getPlayer())
    285          {
    286          std::string message;
    287          if (killer)
    288          {
    289          if (killer->getPlayer())
    290          message = victim->getPlayer()->getName() + " was killed by " + killer->getPlayer()->getName();
    291          else
    292          message = victim->getPlayer()->getName() + " was killed";
    293          }
    294          else
    295          message = victim->getPlayer()->getName() + " died";
    296          
    297          ChatManager::message(message);
    298          }
    299          
    300          Deathmatch::pawnKilled(victim, killer);
    301          }
    302          
    303          void TowerDefense::playerScored(PlayerInfo* player)
    304          {
    305          Gametype::playerScored(player);
    306          
    307          }*/
     220
     221    // Function to test if we can add waypoints using code only. Doesn't work yet
     222
     223    // THE PROBLEM: WaypointController's getControllableEntity() returns null, so it won't track. How do we get the controlableEntity to NOT BE NULL???
     224    /*
     225    void TowerDefense::addWaypointsAndFirstEnemy()
     226    {
     227        SpaceShip *newShip = new SpaceShip(this->center_);
     228        newShip->addTemplate("spaceshipassff");
     229
     230        WaypointController *newController = new WaypointController(newShip);
     231        newController->setAccuracy(3);
     232
     233        Model *wayPoint1 = new Model(newController);
     234        wayPoint1->setMeshSource("crate.mesh");
     235        wayPoint1->setPosition(7,-7,5);
     236        wayPoint1->setScale(0.2);
     237
     238        Model *wayPoint2 = new Model(newController);
     239        wayPoint2->setMeshSource("crate.mesh");
     240        wayPoint2->setPosition(7,7,5);
     241        wayPoint2->setScale(0.2);
     242
     243        newController->addWaypoint(wayPoint1);
     244        newController->addWaypoint(wayPoint2);
     245
     246        // The following line causes the game to crash
     247
     248        newShip->setController(newController);
     249//        newController -> getPlayer() -> startControl(newShip);
     250        newShip->setPosition(-7,-7,5);
     251        newShip->setScale(0.1);
     252        //newShip->addSpeed(1);
     253
     254
     255
     256//      this->center_->attach(newShip);
     257    }
     258    */
     259    /*
     260    void TowerDefense::playerEntered(PlayerInfo* player)
     261    {
     262        Deathmatch::playerEntered(player);
     263
     264        const std::string& message = player->getName() + " entered the game";
     265        ChatManager::message(message);
     266    }
     267
     268    bool TowerDefense::playerLeft(PlayerInfo* player)
     269    {
     270        bool valid_player = Deathmatch::playerLeft(player);
     271
     272        if (valid_player)
     273        {
     274            const std::string& message = player->getName() + " left the game";
     275            ChatManager::message(message);
     276        }
     277
     278        return valid_player;
     279    }
     280
     281
     282    void TowerDefense::pawnKilled(Pawn* victim, Pawn* killer)
     283    {
     284        if (victim && victim->getPlayer())
     285        {
     286            std::string message;
     287            if (killer)
     288            {
     289                if (killer->getPlayer())
     290                    message = victim->getPlayer()->getName() + " was killed by " + killer->getPlayer()->getName();
     291                else
     292                    message = victim->getPlayer()->getName() + " was killed";
     293            }
     294            else
     295                message = victim->getPlayer()->getName() + " died";
     296
     297            ChatManager::message(message);
     298        }
     299
     300        Deathmatch::pawnKilled(victim, killer);
     301    }
     302
     303    void TowerDefense::playerScored(PlayerInfo* player)
     304    {
     305        Gametype::playerScored(player);
     306    }*/
    308307}
Note: See TracChangeset for help on using the changeset viewer.