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
|
//system_state.cpp
SystemState::SystemState()
{
curLevel_ = new Level(std::string("resources/files/level1.txt"));
}
void SystemState::onUpdate()
{
if (curLevel_ != 0)
curLevel_->Update();
}
void SystemState::onRender( Canvas& canvas )
{
if (curLevel_ != 0)
curLevel_->DrawLevel(canvas);
}
bool SystemState::onKey(const IKeyEvent::KeyEvent& key_event)
{
//for testing purposes
if(key_event.key_state == IKeyEvent::KeyEvent::KB_DOWN && key_event.key == IKeyEvent::KeyEvent::KB_SPC_KEY)
{
LoadLevel(std::string("resources/files/level2.txt"));
}
if(key_event.key_state == IKeyEvent::KeyEvent::KB_DOWN && key_event.key == IKeyEvent::KeyEvent::KB_ESC_KEY)
{
application.exit();
return false;
}
return true;
}
void SystemState::LoadLevel(std::string filePath)
{
curLevel_ = new Level(filePath); //segmentation fault
}
//Level.cpp
Level::Level(std::string filePath)
{
SnakeFileReader sfr(filePath);
ImageFile(sfr.ReadLine()).load(map_layout);
mapWidth_ = sfr.ReadInt();
mapHeight_ = sfr.ReadInt();
level_cell_size_ = sfr.ReadInt();
map_ = new TileData*[mapWidth_];
for (int i = 0; i < mapWidth_; i++)
{
map_[i] = new TileData[mapHeight_];
}
int numUnwalk = 0;
bool foundPlayerDZ = false;
bool foundEnemyDZ = false;
Vector3 playerDZStart;
Vector3 enemyDZStart;
int iterations = sfr.ReadInt();
TileInfo mapTiles[iterations];
for (int i = 0; i < iterations; i++)
{
mapTiles[i].filePath = sfr.ReadLine();
mapTiles[i].readColour = Colour(sfr.ReadColour());
mapTiles[i].canWalk = sfr.ReadInt();
}
bool test = false;
for (int y = 0; y < mapHeight_; y++)
{
for (int x = 0; x < mapWidth_; x++)
{
Colour cellColour = map_layout.getPixel(x * level_cell_size_, y * level_cell_size_);
for (int i = 0; i < iterations; i++)
{
if (IsSameColour(cellColour, mapTiles[i].readColour))
{
test = true;
ImageFile(mapTiles[0].filePath).load(map_[x][y].tile);
if (!mapTiles[i].canWalk)
{
Image wall;
ImageFile(mapTiles[i].filePath).load(wall);
wall.setTransparentColour(Colour(255,0,255));
map_[x][y].tile.blit(wall);
unwalkableLocs_.push_back(Vector3(x, y, 0));
numUnwalk++;
}
}
}
if (IsSameColour(cellColour, humanDropZone_))
{
test = true;
if (!foundPlayerDZ)
{
playerDZStart = Vector3(x,y,0);
foundPlayerDZ = true;
}
ImageFile(mapTiles[0].filePath).load(map_[x][y].tile);
}
else if (IsSameColour(cellColour, computerDropZone_))
{
test = true;
if (!foundEnemyDZ)
{
enemyDZStart = Vector3(x,y,0);
foundEnemyDZ = true;
}
ImageFile(mapTiles[0].filePath).load(map_[x][y].tile);
}
}
}
human_ = new Player(sfr.ReadVector(), Size(level_cell_size_,level_cell_size_), HUMAN, this, sfr.ReadLine());
computer_ = new Player(sfr.ReadVector(), Size(level_cell_size_,level_cell_size_), COMPUTER, this, sfr.ReadLine(), human_);
human_->SetOpponent(computer_);
computer_->SetupDropZone(enemyDZStart);
human_->SetupDropZone(playerDZStart);
SetupCollectables();
}
|