Meteor defense project

Dependencies:   N5110 mbed

Spawn/Spawn.cpp

Committer:
jasper0712
Date:
2017-04-13
Revision:
22:2e75b50b26f0
Parent:
21:e5585569a938
Child:
23:6d197a452d7c

File content as of revision 22:2e75b50b26f0:

#include "Spawn.h"

Spawn::Spawn()
{

}

Spawn::~Spawn()
{

}
void Spawn::init() {
    healthA = 10;
    healthC = 100;
    spawnRateA = 32;
    spawnRateB = 1;
    spawnRateC = 2;
    spawnRateD = 1;
}
void Spawn::randomizeSpawn(int Arr[][Rows], char cArr[][Rows]) {
        //there is 3 kind of different spawn - a, b and c.
        if (stopSpawnA == 0) { //spawn for this row
            spawnA(Arr, cArr);
            stopSpawnA= 1;
        } else { //stop spawn for the next row
            stopSpawnA = 0;
        }
        for ( int x = 0; x < Cols; x++) {
            spawnB(x, Arr, cArr);
            spawnC(x, Arr, cArr);
        }
        //printf("at x=2, y=0. value = %d \n",Arr1[2][0]);
}
void Spawn::spawnA(int Arr[][Rows],char cArr[][Rows]) {
    for ( int x = 0; x < Cols; x++) {
        //32/200 chance of spawning for A.
        //can depends on level
        bool TrueFalseA = (rand() % 200) < spawnRateA;
        if (TrueFalseA) {
            //spawning from the top of the screen
            Arr[x][0] = healthA;
            cArr[x][0] = 'a';
            //printf("spawn set at x = is %d \n",x);
        }
    }    
}
void Spawn::spawnB(int x, int Arr[][Rows],char cArr[][Rows]) {
    bool TrueFalseB = (rand() % 200) < spawnRateB;
    if (TrueFalseB) {
        Arr[x][0] = 100000;
        cArr[x][0] = 'b';
    }
}
void Spawn::spawnC(int x, int Arr[][Rows],char cArr[][Rows]) {
    bool TrueFalseC = (rand() % 200) < spawnRateC;
    if (TrueFalseC) {
        Arr[x][0] = healthC;
        cArr[x][0] = 'c';
    }
}
void Spawn::spawnD(int Arr[][Rows], char cArr[][Rows]) {
    
}
void Spawn::moveSpawnABC(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]) {
    //printf("x=2,y =0. value = %d \n",array[2][0]);
    //scan through the whole array
    for (int x = 0; x < Cols; x++) {
        for (int y = 0; y < Rows; y++) {
            if (cArr[x][y] == 'a' || cArr[x][y] == 'b' || cArr[x][y] == 'c') {
                //move everything down a row
                Arr2[x][y+1] = Arr[x][y];       
                cArr2[x][y+1] = cArr[x][y];
            } else {
                Arr2[x][y+1] = 0;
                cArr2[x][y+1] = '\0';
            }
        }
    }
    printf("at x=2, y=47. number is =%d \n",Arr2[2][47]);  
}
void Spawn::moveSpawnB(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]){
    //printf("moving spawn B \n");
    for (int x = 0; x < Cols; x++) {
        for (int y = 0; y < Rows; y++) {
            deleteChar(x, y, Arr2, cArr2);
            if (cArr[x][y] == 'b') { 
                Arr2[x][y+1] = Arr[x][y];
                cArr2[x][y+1] = 'b';
                cArr2[x][y] = '\0';
                Arr2[x][y] = 0;
            }
        }
    }            
}
void Spawn::updateSpawn(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows], N5110 &lcd) {
    for ( int x = 0; x < Cols; x++) {
        for ( int y = 0; y < Rows; y++) {
            deleteChar(x, y, Arr2, cArr2); //cleaning up the array
            if (cArr2[x][y] == 'a') {
                lcd.setPixel(x,y);
                //to update the main array
                Arr[x][y] = Arr2[x][y];
                cArr[x][y] = 'a';
            } else if (cArr2[x][y] == 'b') {
                lcd.setPixel(x,y); lcd.setPixel(x,y+1);
                Arr[x][y] = Arr2[x][y]; 
                cArr[x][y] = 'b';             
            } else if (cArr2[x][y] == 'c') {
                cArr[x-1][y-1] = 'l'; // L and R (left and right) side of the spawn C for bigger hit box.
                cArr[x+1][y-1] = 'r';
                cArr[x][y] = 'c';
                Arr[x][y] = Arr2[x][y];
                lcd.setPixel(x,y); lcd.setPixel(x,y+1);
                lcd.setPixel(x+1,y); lcd.setPixel(x-1,y);
            }else {
                Arr[x][y] = 0;
                cArr[x][y] = '\0';
            }
        }
    }
}
void Spawn::deleteChar(int x, int y, int Arr2[][Rows], char cArr2[][Rows]) { // to clean up the array after killing the spawn.
    if (Arr2[x][y] <= 0) { 
        cArr2[x][y] = '\0';
    }   
}