Ejemplo n.º 1
0
Game::Game(MainWindow *ui)
{
    this->ui = ui;
    gameCounter = 0;
    qsrand(QDateTime::currentDateTime().toTime_t());
    int locations[15][15] =    {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                                0,1,1,1,1,1,1,0,1,1,1,1,1,1,0,
                                0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,
                                0,1,0,1,2,1,1,1,1,1,2,1,0,1,0,
                                0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,
                                0,1,0,1,0,1,1,0,1,1,0,1,0,1,0,
                                0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,
                                0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,
                                0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,
                                0,1,0,1,0,1,1,0,1,1,0,1,0,1,0,
                                0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,
                                0,1,0,1,2,1,1,1,1,1,2,1,0,1,0,
                                0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,
                                0,1,1,1,1,1,1,0,1,1,1,1,1,1,0,
                                2,0,0,0,0,0,0,0,0,0,0,0,0,0,2};


    for(int i = 0; i < 15; ++i){
        for(int j = 0;j < 15; ++j){
            if(locations[i][j] == 1){
                board[j][i] = new GameItem(j,i,this);
                board[j][i]->setGoal(false);
                board[j][i]->setBrush(Qt::red);
                board[j][i]->setBlocks(true);
                board[j][i]->setZValue(0);
            }else if(locations[i][j] == 0){
                board[j][i] = new GameItem(j,i,this);
                board[j][i]->setBlocks(false);
                board[j][i]->setGoal(false);
                QPolygonF myPolygon;
                myPolygon << QPointF(-5, 0) << QPointF(0, 5)
                        << QPointF(5, 0) << QPointF(0, -5)
                        << QPointF(-5, 0);
                board[j][i]->setPolygon(myPolygon);
                board[j][i]->setZValue(0);
                board[j][i]->setBrush(Qt::white);
                board[j][i]->setPen(QPen(QColor(Qt::transparent)));
            }else if(locations[i][j] == 2){
                board[j][i] = new GameItem(j,i,this);
                board[j][i]->setBlocks(false);
                board[j][i]->setGoal(true);
                QPolygonF myPolygon;
                myPolygon << QPointF(-5, 0) << QPointF(0, 5)
                        << QPointF(5, 0) << QPointF(0, -5)
                        << QPointF(-5, 0);
                board[j][i]->setPolygon(myPolygon);
                board[j][i]->setZValue(0);
                board[j][i]->setBrush(Qt::blue);
                goals.append(board[j][i]);
            }
            board[j][i]->occupant = NULL;
            this->addItem(board[j][i]);
        }
    }

    /*for(int i = 0; i < 16; ++i){
        this->addLine((float)(i * 20),0.0,(float)(i * 20),300.0,QPen(QColor(Qt::gray)));
        this->addLine(0.0,(float)(i * 20),300.0,(float)(i * 20),QPen(QColor(Qt::gray)));
    }*/
    


    // --------------------------------------
    QString fileName = "test2.glbt.xml";

    QFile file(fileName);
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
        qDebug("Could not open behavior tree file");
        return;
    }
    QByteArray byteArray = file.readAll();
    QString fileContents(byteArray.data());
    file.close();

/*    QString logFilename = "log.txt";

    logFile = new QFile(logFilename);
    if(!logFile->open(QIODevice::Append | QIODevice::Text)){
        qDebug("Could not open file");
        return;
    }

    QTextStream out(logFile);
    out << ";Board;Killtime;Avg Killtime;Interest;Challenge;Behavior Div;Spatial Div;Max Deviation;Standard Deviation;\n";//*/


    //QByteArray byteArray = logFile.readAll();
   // QString fileContents(byteArray.data());

    //file.close();

    btBrain *brain = new btBrain(fileContents);
    
    //this->setBackgroundBrush(QBrush(QColor(Qt::black)));

   /* ss = new ScenarioSet();
    Scenario *s1 = new Scenario();

    ss->addScenario(s1);*/
    
    marker = new Player(this,QPoint(8,12));//new GameItem(this);
    connect(marker, SIGNAL(pacmanLost()), this, SLOT(resetAfterLost()));
    connect(marker, SIGNAL(pacmanWon()), this, SLOT(resetAfterWon()));


    marker->setBrush(Qt::magenta);
    
    Enemy *player = new Enemy(marker,brain->getBehaviorTree(2));
    player->thename = "mr. anderson";
    Runner* playerRunner = new Runner(player);
    connect(playerRunner, SIGNAL(finished()), this, SLOT(resetGame()));
    runners.append(playerRunner);//*/
    
    //int trees[3] = {6,7,9}; // pinky
    //int trees[3] = {7,7,9}; // clyde
    //int trees[3] = {9,7,9}; // Blinky
    //int trees[3] = {0,7,9}; // Pure Random*/
	
	//int trees[1] = {0};
	int trees[1] = {1};

    ColorGen colors(0,255,30);

    for (int i = 0; i < this->numberOfEnemies(); i++)
    {
        Guard* agent = new Guard(this,QPoint(0,0));
        Enemy *enemy = new Enemy(agent,brain->getBehaviorTree(trees[i]));
        agent->setBrush(QBrush(colors.nextColor()));
        
        Runner* runner = new Runner(enemy);
        connect(runner, SIGNAL(finished()), this, SLOT(resetGame()));
        runners.append(runner);
        enemy->thename = "mr. smith " + QVariant(i).toString();
        agent->addObjective(marker);
        //connect(agent, SIGNAL(enteredNewCell(int,int)), s1, SLOT(visit(int,int)));
        marker->addObjective(agent);
    }//*/

    foreach(GameItem* goal,goals){
        marker->addGoal(goal);
    }