Meteor defense project

Dependencies:   N5110 mbed

Spawn/Spawn.cpp

Committer:
jasper0712
Date:
2017-04-12
Revision:
19:7ccbb19703f9
Parent:
18:d82059ce929b
Child:
20:32f115462bbc

File content as of revision 19:7ccbb19703f9:

#include "Spawn.h"

Spawn::Spawn()
{

}

Spawn::~Spawn()
{

}
void Spawn::init() {
    healthA = 10;
    healthC = 100;
    spawnRateA = 32;
    spawnRateB = 1;
    spawnRateC = 2;
}
void Spawn::randomizeSpawn(char Arr2[][Rows], char Arr3[][Rows]) {
        //there is 3 kind of different spawn - a, b and c.
        if (stopSpawnA == 0) { //spawn for this row
            spawnA(Arr2, Arr3);
            stopSpawnA= 1;
        } else { //stop spawn for the next row
            stopSpawnA = 0;
        }
        for ( int x = 0; x < Cols; x++) {
            spawnB(Arr2, Arr3);
            spawnC(Arr2, Arr3);
        }
        //printf("at x=2, y=0. value = %d \n",Arr1[2][0]);
}
void Spawn::spawnA(int Arr2[][Rows],char Arr3[][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
            Arr2[x][0] = healthA;
            Arr3[x][0] = 'a';
            //printf("spawn set at x = is %d \n",x);
        }
    }    
}
void Spawn::spawnB(int Arr2[][Rows],char Arr3[][Rows]) {
    bool TrueFalseB = (rand() % 200) < spawnRateB;
    if (TrueFalseB) {
        Arr2[x][0] = 100000;
        Arr3[x][0] = 'b';
    }
}
void Spawn:spawnC(int Arr2[][Rows],char Arr3[][Rows]) {
    bool TrueFalseC = (rand() % 200) < spawnRateC;
    if (TrueFalseC) {
        Arr2[x][0] = healthC;
        Arr3[x][0] = 'c';
    }
}
void Spawn::moveSpawnABC(int Arr1[][Rows], int Arr2[][Rows], char Arr3[][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 (Arr2[x][y] > 0) {
                //move everything down a row
                Arr1[x][y+1] = Arr2[x][y];       
                Arr3[x][y+1] = Arr3[x][y];
                Arr3[x][y] = '\0';
            } else {
                Arr1[x][y+1] = 0;
            }
        }
    }
    printf("at x=2, y=47. number is =%d \n",Arr2[2][47]);  
}
void Spawn::moveSpawnB(int Arr1[][Rows], int Arr2[][Rows], char Arr3[][Rows]){
    //printf("moving spawn B \n");
    for (int x = 0; x < Cols; x++) {
        for (int y = 0; y < Rows; y++) {
            if (Arr2[x][y] > 20000) { //any number larger than this must be spawn b.
                Arr1[x][y+1] = Arr2[x][y];
                Arr1[x][y] = 0;
                Arr3[x][y+1] = 'b';
                Arr3[x][y] = '\0';
            }
        }
    }            
}
void Spawn::updateSpawn(int Arr1[][Rows], int Arr2[][Rows], char Arr3[][Rows],N5110 &lcd) {
    for ( int x = 0; x < Cols; x++) {
        for ( int y = 0; y < Rows; y++) {
            deleteChar(x, y, Arr1, Arr3);
            if (Arr3[x][y] == 'a') {
                lcd.setPixel(x,y);
                //to update array1 
                Arr2[x][y] = Arr1[x][y];
            } else if (Arr3[x][y] == 'b') {
                lcd.setPixel(x,y); lcd.setPixel(x,y+1);
                Arr2[x][y] = Arr1[x][y];                
            } else if (Arr3[x][y] == 'c') {
                Arr3[x-1][y-1] = 'l';
                Arr3[x+1][y-1] = 'r';
                Arr2[x][y] = Arr1[x][y];
                lcd.setPixel(x,y); lcd.setPixel(x,y+1);
                lcd.setPixel(x+1,y); lcd.setPixel(x-1,y);
            }else {
                Arr2[x][y] = 0;
            }
        }
    }
}
void Spawn::deleteChar(int x, int y, int Arr1[][Rows], char Arr3[][Rows]) {
    if (Arr1[x][y] == 0) {
        Arr3[x][y] = '\0';
    }   
}