1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
/* Copyright 2020 Stephen Branley
*
* This file is part of Help Hannah's Horse.
*
* Help Hannah's Horse is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Help Hannah's Horse is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Help Hannah's Horse. If not, see <https://www.gnu.org/licenses/>.
*/
#include "Level.h"
#include <iostream>
class Level;
Level::Level(){
px = rx = bx = py = ry = by = pillsleft = 0;
};
void Level::load(string filename){
std::string path = Global::getPath(filename.c_str(), "maps", "");
pillsleft = 0;
ifstream br(path.c_str());
char buffer[SIZEX+1];
// open file, read a line
if(!br.is_open()){
printf("Could not open map file %s!\n",path.c_str());
} else {
int x = 0;
int y = 0;
// map needs to be read BEFORE passing to ghosts, or they
// just get NULL and therefore have no way to hit a wall.
for(y=0; y<SIZEY; y++){
br.getline(buffer,SIZEX+1);
for(x=0; x<SIZEX; x++){
map[x][y].type = buffer[x];
if(map[x][y].type == '.') pillsleft++;
map[x][y].t = (y*SIZET) + 50;
map[x][y].b = (y*SIZET) + 50 + 49;
map[x][y].l = (x*SIZET) + 50;
map[x][y].r = (x*SIZET) + 50 + 49;
if(map[x][y].type == '.'){
gridpos pos = {x, y};
freecells.push_back(pos);
}
}
}
// After the level layout are lines for the ghosts and carrots
char tempx[5]; char tempy[5]; char spd[5];
int tempxi; int tempyi; // bit of geordi there :D
int tempspdi;
// Kill old ghosts and free mem
// before creating new ones
br.getline(buffer, SIZEX+1); // SIZEX+1 just to use all allocated space
string pinkline(buffer);
sscanf(pinkline.c_str(),"%*s %s %s",tempx,tempy);
tempxi = atoi(tempx);
tempyi = atoi(tempy);
px = tempxi;
py = tempyi;
br.getline(buffer, SIZEX+1);
string blueline(buffer);
sscanf(blueline.c_str(),"%*s %s %s",tempx,tempy);
tempxi = atoi(tempx);
tempyi = atoi(tempy);
bx = tempxi;
by = tempyi;
br.getline(buffer, SIZEX+1);
string redline(buffer);
sscanf(redline.c_str(),"%*s %s %s",tempx,tempy);
tempxi = atoi(tempx);
tempyi = atoi(tempy);
rx = tempxi;
ry = tempyi;
// Read file til end. Each line is a food item
while (br.getline(buffer, SIZEX+1)){
string foodline(buffer);
sscanf(foodline.c_str(),"%*s %s %s %s",tempx,tempy,spd);
tempxi = atoi(tempx);
tempyi = atoi(tempy);
tempspdi = atoi(spd);
foodinfo fd;
fd.sx = tempxi;
fd.sy = tempyi;
fd.speed = tempspdi;
foods.push_back(fd);
}
// kill any old ghosts before creating new ones
br.close();
}
};
gridpos Level::getFreeCell(){
//select a random cell from the available free cells;
int freecell = rand() % freecells.size();
return freecells[freecell];
};
Level::~Level(){
};
|