ELEC2645 (2018/19) / Mbed 2 deprecated el17aj

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EnemySpawner.cpp Source File

EnemySpawner.cpp

00001 #include "EnemySpawner.h"
00002 
00003 
00004 EnemySpawner::EnemySpawner()
00005 {
00006 
00007 }
00008 
00009 EnemySpawner::~EnemySpawner()
00010 {
00011 
00012 }
00013 
00014 Enemy* EnemySpawner::init(int level)
00015 { 
00016     //seed random number with time to give truly random values
00017     srand(time(NULL));
00018     
00019     //set the time tracking variable to 0
00020     _timeIntoLevel = 0.0;
00021     _level = level;
00022     _number_of_enemies = level; //sets number of enemies equal to level number
00023     
00024     
00025     //sets the minimum time between enemy spawns to decrease as level increases, up to a minimum
00026     _minTimeBetweenSpawns = 1.0f - (level*0.2f);
00027     if(_minTimeBetweenSpawns<0.1) {
00028         _minTimeBetweenSpawns=0.1;
00029     }
00030         
00031     //allocate memory for array of enemies 
00032     _enemies = (Enemy*)malloc(sizeof(Enemy)*_number_of_enemies);
00033     
00034     //initialise each enemy and add it to array
00035     //time to attack increases with each enemy added to ensure they are spaced out
00036     float timeToAttack = 0.0;
00037     for (int i=0; i < _number_of_enemies; i++) {
00038         timeToAttack = timeToAttack + _minTimeBetweenSpawns;    //incriment time to attack by minimum time between
00039         int variation = rand() % 100;   
00040         float actualVar = variation/100.0f; //add a second of variation between spawns to add unpredictability
00041         timeToAttack = timeToAttack + actualVar;
00042         float speed = 0.5 + ((rand() % 100)/100.0); //set speed to a minumim 0.5 up to a max of 1.5 to add unpredictability
00043         
00044         //initialise this enemy with the desired speed and time to attack
00045         _enemies[i].init(timeToAttack, speed);
00046     }
00047     
00048     return _enemies;
00049 }
00050 
00051     
00052 
00053 void EnemySpawner::update(int fps)
00054 {
00055     //incriment time
00056     _timeIntoLevel += 1.0f/fps;
00057     
00058     
00059     //checks if the time in to the level is higher than enemies time to attack
00060     //if so it changes the emenies state so it begins to move forwards
00061     for (int i=0; i < _number_of_enemies; i++) {
00062         if(abs(_enemies[i].get_timeToAttack() - _timeIntoLevel) < 0.2) {
00063             Action myAc;
00064             myAc = moving;
00065             _enemies[i].set_current_action(myAc);
00066         }
00067     }
00068     
00069 }
00070 
00071 int EnemySpawner::get_number_of_enemies() {
00072     return _number_of_enemies;
00073 }
00074 
00075