summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Worthe <justin.worthe@gmail.com>2015-06-03 21:22:14 +0200
committerJustin Worthe <justin.worthe@gmail.com>2015-06-03 21:22:14 +0200
commite42727977b3dab7aecff0ce8afa5b16abcd8b26b (patch)
treef1352d17910d3c4454b999def79f02ad68ab2866
parenteac5108541892a00f71e92d3e192598774696689 (diff)
Started reading of gamestate
-rw-r--r--.gitignore1
-rw-r--r--include/alien.h1
-rw-r--r--include/game_state.h2
-rw-r--r--src/game_state.cpp31
-rw-r--r--src/spacebot.cpp2
5 files changed, 35 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore
index 87be59f..bf013a7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,3 +7,4 @@ cppbot
*.sdf
*.suo
Debug/
+Makefile
diff --git a/include/alien.h b/include/alien.h
index 476990b..2f7d596 100644
--- a/include/alien.h
+++ b/include/alien.h
@@ -3,6 +3,7 @@
class Alien {
public:
Alien(int x, int y);
+ const static char MAP_CHAR = 'x';
private:
int x;
int y;
diff --git a/include/game_state.h b/include/game_state.h
index abb2efa..aa49fc8 100644
--- a/include/game_state.h
+++ b/include/game_state.h
@@ -11,7 +11,7 @@
class GameState
{
public:
- GameState(const std::istream& mapFile){}
+ GameState(std::istream& mapFile);
private:
std::vector<Alien> aliens;
std::vector<EnemyBullet> bullets;
diff --git a/src/game_state.cpp b/src/game_state.cpp
new file mode 100644
index 0000000..48a9e42
--- /dev/null
+++ b/src/game_state.cpp
@@ -0,0 +1,31 @@
+#include "game_state.h"
+#include <iostream>
+
+const int OPENING_LINES = 6;
+const int GAME_AREA_LINES = 25;
+
+GameState::GameState(std::istream& file)
+{
+ for (int i=0; i<OPENING_LINES; ++i)
+ {
+ file.ignore(numeric_limits<streamsize>::max(), '\n');
+ }
+
+ int x = -1;
+ int y = 0;
+ char nextChar = ' ';
+ while (char nextChar = file.get())
+ {
+ ++x;
+ switch (nextChar)
+ {
+ case Alien.MAP_CHAR:
+ aliens.push_back(Alien(x,y));
+ break;
+ case '\n':
+ ++y;
+ x = -1;
+ break;
+ }
+ }
+}
diff --git a/src/spacebot.cpp b/src/spacebot.cpp
index b297eb3..7460fe3 100644
--- a/src/spacebot.cpp
+++ b/src/spacebot.cpp
@@ -5,7 +5,7 @@
Spacebot::Spacebot(std::string outputPath)
: outputPath(std::move(outputPath)),
- gameState(std::ifstream(outputPath+"/map.txt", std::ifstream::in))
+ gameState(std::ifstream(outputPath+"/map.txt"))
{
}