Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
blueprint_test.cpp
Go to the documentation of this file.
1/*
2 * Blueprint Test - Demonstrates entity blueprint creation and serialization
3 *
4 * This test program shows how to:
5 * 1. Create entity blueprints programmatically
6 * 2. Save blueprints to JSON files
7 * 3. Load blueprints from JSON files
8 * 4. Access and modify component properties
9 */
10
11#include "EntityBlueprint.h"
12#include <iostream>
13
14using namespace Olympe::Blueprint;
15
17{
18 std::cout << "=== Test: Creating Entity Blueprint ===" << std::endl;
19
20 // Create a new entity blueprint
21 EntityBlueprint entity("TestEntity");
22 entity.description = "A test entity created programmatically";
23
24 // Add components using helper functions
25 entity.AddComponent("Position", CreatePositionComponent(100.0f, 200.0f).properties);
26 entity.AddComponent("BoundingBox", CreateBoundingBoxComponent(0.0f, 0.0f, 32.0f, 32.0f).properties);
27 entity.AddComponent("VisualSprite",
28 CreateVisualSpriteComponent("Resources/test.png", 0, 0, 32, 32, 16, 16).properties);
29 entity.AddComponent("Health", CreateHealthComponent(100, 100).properties);
30 entity.AddComponent("PhysicsBody", CreatePhysicsBodyComponent(1.0f, 50.0f).properties);
31
32 // Save to file
33 std::string filepath = "Blueprints/test_entity_generated.json";
34 if (entity.SaveToFile(filepath))
35 {
36 std::cout << "✓ Blueprint saved to: " << filepath << std::endl;
37 }
38 else
39 {
40 std::cout << "✗ Failed to save blueprint" << std::endl;
41 }
42
43 // Display JSON
44 std::cout << "\nGenerated JSON:\n" << entity.ToJson().dump(2) << std::endl;
45}
46
48{
49 std::cout << "\n=== Test: Loading and Modifying Blueprint ===" << std::endl;
50
51 // Load an existing blueprint
52 std::string filepath = "Blueprints/example_entity_simple.json";
54
55 if (entity.name.empty())
56 {
57 std::cout << "✗ Failed to load blueprint from: " << filepath << std::endl;
58 return;
59 }
60
61 std::cout << "✓ Loaded blueprint: " << entity.name << std::endl;
62 std::cout << " Description: " << entity.description << std::endl;
63 std::cout << " Components: " << entity.components.size() << std::endl;
64
65 // Display component list
66 std::cout << "\nComponents:" << std::endl;
67 for (const auto& comp : entity.components)
68 {
69 std::cout << " - " << comp.type << std::endl;
70 }
71
72 // Modify a component
73 if (auto* posComp = entity.GetComponent("Position"))
74 {
75 std::cout << "\n✓ Found Position component" << std::endl;
76 std::cout << " Original position: " << posComp->properties.dump() << std::endl;
77
78 // Modify position
79 posComp->properties["position"]["x"] = 500.0f;
80 posComp->properties["position"]["y"] = 600.0f;
81
82 std::cout << " Modified position: " << posComp->properties.dump() << std::endl;
83 }
84
85 // Add a new component
86 entity.AddComponent("Movement", CreateMovementComponent(1.0f, 0.0f, 0.0f, 0.0f).properties);
87 std::cout << "\n✓ Added Movement component" << std::endl;
88
89 // Check if component exists
90 std::cout << "\nComponent checks:" << std::endl;
91 std::cout << " Has Position: " << (entity.HasComponent("Position") ? "Yes" : "No") << std::endl;
92 std::cout << " Has Movement: " << (entity.HasComponent("Movement") ? "Yes" : "No") << std::endl;
93 std::cout << " Has Health: " << (entity.HasComponent("Health") ? "Yes" : "No") << std::endl;
94
95 // Save modified blueprint
96 std::string outpath = "Blueprints/test_entity_modified.json";
97 if (entity.SaveToFile(outpath))
98 {
99 std::cout << "\n✓ Modified blueprint saved to: " << outpath << std::endl;
100 }
101}
102
104{
105 std::cout << "\n=== Test: Complete Blueprint with All Components ===" << std::endl;
106
107 std::string filepath = "Blueprints/example_entity_complete.json";
109
110 if (entity.name.empty())
111 {
112 std::cout << "✗ Failed to load blueprint from: " << filepath << std::endl;
113 return;
114 }
115
116 std::cout << "✓ Loaded complete blueprint: " << entity.name << std::endl;
117 std::cout << " Component count: " << entity.components.size() << std::endl;
118
119 // List all components and their properties
120 std::cout << "\nDetailed component properties:" << std::endl;
121 for (const auto& comp : entity.components)
122 {
123 std::cout << "\n Component: " << comp.type << std::endl;
124 std::cout << " Properties: " << comp.properties.dump(4) << std::endl;
125 }
126}
127
128int main(int argc, char** argv)
129{
130 std::cout << "Olympe Blueprint Editor - Entity Blueprint Test\n" << std::endl;
131
132 try
133 {
137
138 std::cout << "\n=== All tests completed successfully ===" << std::endl;
139 }
140 catch (const std::exception& e)
141 {
142 std::cerr << "\n✗ Test failed with exception: " << e.what() << std::endl;
143 return 1;
144 }
145
146 return 0;
147}
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
int main()
void TestLoadAndModifyBlueprint()
void TestCreateAndSaveBlueprint()
void TestCompleteBlueprint()
ComponentData CreateVisualSpriteComponent(const std::string &spritePath, float srcX, float srcY, float srcWidth, float srcHeight, float hotSpotX, float hotSpotY)
ComponentData CreateBoundingBoxComponent(float x, float y, float width, float height)
ComponentData CreateMovementComponent(float dirX, float dirY, float velX, float velY)
ComponentData CreatePositionComponent(float x, float y)
ComponentData CreatePhysicsBodyComponent(float mass, float speed)
ComponentData CreateHealthComponent(int current, int max)
void AddComponent(const std::string &type, const json &properties)
static EntityBlueprint LoadFromFile(const std::string &filepath)
std::vector< ComponentData > components
bool SaveToFile(const std::string &filepath) const
ComponentData * GetComponent(const std::string &type)
bool HasComponent(const std::string &type) const