Skip to content

Commit 4b87cd4

Browse files
committed
added csv parser
1 parent 192d9df commit 4b87cd4

File tree

6 files changed

+152
-4
lines changed

6 files changed

+152
-4
lines changed

Data/Formations/galaga_level_1.csv

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
298,130,bee,0,1
2+
335,130,bee,0,1
3+
298,162,bee,0,1
4+
335,162,bee,0,1
5+
298,70,butterfly,0,1
6+
335,70,butterfly,0,1
7+
298,102,butterfly,0,1
8+
335,102,butterfly,0,1
9+
261,70,butterfly,1,2
10+
372,70,butterfly,1,2
11+
261,102,butterfly,1,2
12+
372,102,butterfly,1,2
13+
298,38,boss,1,2
14+
335,38,boss,1,2
15+
261,38,boss,1,2
16+
372,38,boss,1,2
17+
187,70,butterfly,2,3
18+
224,70,butterfly,2,3
19+
409,70,butterfly,2,3
20+
446,70,butterfly,2,3
21+
187,102,butterfly,2,3
22+
224,102,butterfly,2,3
23+
409,102,butterfly,2,3
24+
446,102,butterfly,2,3
25+
224,130,bee,3,4
26+
261,130,bee,3,4
27+
372,130,bee,3,4
28+
409,130,bee,3,4
29+
224,162,bee,3,4
30+
261,162,bee,3,4
31+
372,162,bee,3,4
32+
409,162,bee,3,4
33+
150,130,bee,4,5
34+
187,130,bee,4,5
35+
446,130,bee,4,5
36+
483,130,bee,4,5
37+
150,162,bee,4,5
38+
187,162,bee,4,5
39+
446,162,bee,4,5
40+
483,162,bee,4,5

Galaga/CharactersManager.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "EventParams.h"
1212
#include "BulletsManager.h"
1313
#include "Structs.h"
14+
#include "Parser.h"
1415
#include <memory>
1516
#include <algorithm>
1617

@@ -24,6 +25,10 @@ CharactersManager::CharactersManager(Fluffy::GameObject* pOwner)
2425

2526
void CharactersManager::StartLevel1()
2627
{
28+
std::vector<EnemyEnteringData> data;
29+
Parser::GetInstance().ParseEnemyLayoutData("D:/Repos/FluffyEngine/Data/Formations/galaga_level_1.csv", data);
30+
data;
31+
2732
std::string beeFormationsData{ "Formations/Formation1Bees.txt" };
2833
std::string butterflyFormationsData{ "Formations/Formation1Butterflies.txt" };
2934
std::string bossFormationsData{ "Formations/Formation1Boss.txt" };
@@ -34,13 +39,13 @@ void CharactersManager::StartLevel1()
3439
BezierCurve firstCurve{ { -32.0f, -32.0f }, { -32.0f, -32.0f }, { 290.0f, 200.0f }, { 290.0f, 200.0f } };
3540
firstPath.AddCurve(firstCurve, 1);
3641

37-
enemiesData.push({ 4.0f, firstPath, EnemyType::Boss });
42+
enemiesData.push({ 4.0f, {}, firstPath, EnemyType::Boss });
3843

3944
BezierPath secondPath{};
4045
BezierCurve secondCurve{ { 640.0f, -32.0f }, { 640.0f, -32.0f }, { 330.0f, 200.0f }, { 330.0f, 200.0f } };
4146
secondPath.AddCurve(secondCurve, 1);
4247

43-
enemiesData.push({ 4.3f, secondPath, EnemyType::Bee });
48+
enemiesData.push({ 4.3f, {}, secondPath, EnemyType::Bee });
4449

4550
m_Level.StartLevel(enemiesData);
4651

Galaga/Galaga.vcxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@
195195
<ClInclude Include="FiniteStateMachine.h" />
196196
<ClInclude Include="FPSCounter.h" />
197197
<ClInclude Include="HealthDisplayComponent.h" />
198-
<ClInclude Include="JSONParser.h" />
198+
<ClInclude Include="Parser.h" />
199199
<ClInclude Include="KillEnemyCommand.h" />
200200
<ClInclude Include="Level.h" />
201201
<ClInclude Include="MoveCommand.h" />

Galaga/Galaga.vcxproj.filters

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
<ClInclude Include="CollisionLayers.h">
138138
<Filter>Header Files</Filter>
139139
</ClInclude>
140-
<ClInclude Include="JSONParser.h">
140+
<ClInclude Include="Parser.h">
141141
<Filter>Header Files</Filter>
142142
</ClInclude>
143143
</ItemGroup>

Galaga/Parser.h

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#pragma once
2+
#include <string>
3+
#include <vector>
4+
#include <iostream>
5+
#include <fstream>
6+
#include <sstream>
7+
#include <glm\glm.hpp>
8+
#include "Structs.h"
9+
#include "Singleton.h"
10+
11+
// Data layout: Position.x, Position.y, Type, Order, Time (int sec
12+
// Data format: .csv
13+
14+
class Parser : public Fluffy::Singleton<Parser>
15+
{
16+
public:
17+
static void ParseEnemyLayoutData(const std::string& inputFile, std::vector<EnemyEnteringData>& data)
18+
{
19+
std::ifstream file{ inputFile };
20+
if (!file)
21+
std::cout << "Cannot open " << inputFile << "!\n";
22+
23+
while (!file.eof())
24+
{
25+
EnemyEnteringData enemy{};
26+
27+
std::string line;
28+
std::getline(file, line);
29+
30+
std::istringstream iLine{ line };
31+
std::string posX;
32+
std::getline(iLine, posX, ',');
33+
std::string posY;
34+
std::getline(iLine, posY, ',');
35+
std::string type;
36+
std::getline(iLine, type, ',');
37+
std::string order;
38+
std::getline(iLine, order, ',');
39+
std::string time;
40+
std::getline(iLine, time);
41+
42+
if (!posX.empty())
43+
{
44+
bool isInt{ true };
45+
46+
std::string::const_iterator it = posX.begin();
47+
48+
while (it != posX.end())
49+
{
50+
if (!std::isdigit(*it))
51+
{
52+
isInt = false;
53+
break;
54+
}
55+
56+
++it;
57+
}
58+
59+
if (isInt)
60+
{
61+
enemy.position.x = float(std::stoi(posX));
62+
}
63+
}
64+
65+
if (!posY.empty())
66+
{
67+
bool isInt{ true };
68+
69+
std::string::const_iterator it = posY.begin();
70+
71+
while (it != posY.end())
72+
{
73+
if (!std::isdigit(*it))
74+
{
75+
isInt = false;
76+
break;
77+
}
78+
79+
++it;
80+
}
81+
82+
if (isInt)
83+
{
84+
enemy.position.y = float(std::stoi(posY));
85+
}
86+
}
87+
88+
if (type == "bee")
89+
enemy.type = EnemyType::Bee;
90+
else if (type == "butterfly")
91+
enemy.type = EnemyType::Butterfly;
92+
else if (type == "boss")
93+
enemy.type = EnemyType::Boss;
94+
95+
enemy.order = std::stoi(order);
96+
enemy.time = float(std::stoi(time));
97+
98+
data.push_back(enemy);
99+
}
100+
}
101+
102+
};

Galaga/Structs.h

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ struct EnemyEnteringData
9292
{
9393
public:
9494
float time{ 0.0f };
95+
glm::vec2 position{};
9596
BezierPath path{};
9697
EnemyType type{};
9798
int order{ 0 }; // order of entering the scene

0 commit comments

Comments
 (0)