This commit is contained in:
+38
-42
@@ -2,15 +2,15 @@
|
||||
// This demonstrates creating entities with various components and managing rendering
|
||||
|
||||
// Resource IDs
|
||||
uint g_toonShader = 0;
|
||||
uint g_outlineShader = 0;
|
||||
uint g_cubeModel = 0;
|
||||
uint g_sphereModel = 0;
|
||||
uint g_defaultMaterial = 0;
|
||||
uint toonShader = 0;
|
||||
uint outlineShader = 0;
|
||||
uint cubeModel = 0;
|
||||
uint sphereModel = 0;
|
||||
uint defaultMaterial = 0;
|
||||
|
||||
// Scene entities
|
||||
uint g_camera = 0;
|
||||
uint g_light = 0;
|
||||
uint camera = 0;
|
||||
uint light = 0;
|
||||
|
||||
// Store entity IDs for manipulation
|
||||
array<uint> entities;
|
||||
@@ -23,45 +23,45 @@ void Init() {
|
||||
Toast::Success("ECS Script initialized successfully!");
|
||||
|
||||
// === Load shaders ===
|
||||
g_toonShader = Shader::Load("shaders/toon.vs", "shaders/toon.fs");
|
||||
g_outlineShader = Shader::Load("shaders/outline.vs", "shaders/outline.fs");
|
||||
toonShader = Shader::Load("shaders/toon.vs", "shaders/toon.fs");
|
||||
outlineShader = Shader::Load("shaders/outline.vs", "shaders/outline.fs");
|
||||
|
||||
if (g_toonShader == 0 || g_outlineShader == 0) {
|
||||
if (toonShader == 0 || outlineShader == 0) {
|
||||
Toast::Error("Failed to load shaders!");
|
||||
return;
|
||||
}
|
||||
|
||||
// === Load models ===
|
||||
g_cubeModel = Model::LoadCube(1.0, 1.0, 1.0);
|
||||
g_sphereModel = Model::LoadSphere(0.5, 16, 16);
|
||||
cubeModel = Model::LoadCube(1.0, 1.0, 1.0);
|
||||
sphereModel = Model::LoadSphere(0.5, 16, 16);
|
||||
|
||||
// === Create default material ===
|
||||
g_defaultMaterial = Material::Create(g_toonShader, 255, 255, 255, 255);
|
||||
defaultMaterial = Material::Create(toonShader, 255, 255, 255, 255);
|
||||
|
||||
// === Set up camera ===
|
||||
g_camera = ECS::CreateEntity();
|
||||
ECS::AddCamera3D(g_camera,
|
||||
5.0, 5.0, 5.0, // position
|
||||
0.0, 0.0, 0.0, // target
|
||||
45.0); // fovy
|
||||
ECS::AddTag(g_camera, "MainCamera");
|
||||
camera = ECS::CreateEntity();
|
||||
ECS::AddCamera3D(camera,
|
||||
5.0, 5.0, 5.0, // position
|
||||
0.0, 0.0, 0.0, // target
|
||||
45.0); // fovy
|
||||
ECS::AddTag(camera, "MainCamera");
|
||||
|
||||
// === Set up light ===
|
||||
g_light = ECS::CreateEntity();
|
||||
ECS::AddLight(g_light, 0.5, 0.7, 0.3, 1.0); // direction + intensity
|
||||
ECS::AddTag(g_light, "MainLight");
|
||||
light = ECS::CreateEntity();
|
||||
ECS::AddLight(light, 0.5, 0.7, 0.3, 1.0); // direction + intensity
|
||||
ECS::AddTag(light, "MainLight");
|
||||
|
||||
// === Create player entity at origin with outline ===
|
||||
player = ECS::CreateEntity();
|
||||
ECS::AddTransform(player, 0.0f, 0.0f, 0.0f);
|
||||
ECS::AddSprite(player, g_sphereModel, 0xFF0000FF, 0.0f); // Red sphere
|
||||
ECS::AddSprite(player, sphereModel, 0xFF0000FF, 0.0f); // Red sphere
|
||||
|
||||
// Create material for player
|
||||
uint playerMat = Material::Create(g_toonShader, 255, 0, 0, 255);
|
||||
uint playerMat = Material::Create(toonShader, 255, 0, 0, 255);
|
||||
ECS::AddMaterial(player, playerMat);
|
||||
|
||||
// Add outline pass (order=0) and main pass (order=1)
|
||||
ECS::AddRenderPass(player, 0, false, true, g_outlineShader);
|
||||
ECS::AddRenderPass(player, 0, false, true, outlineShader);
|
||||
|
||||
ECS::AddTag(player, "Player");
|
||||
Log(LOG_INFO, "Created player entity: " + player);
|
||||
@@ -69,10 +69,10 @@ void Init() {
|
||||
// === Create a spinning cube ===
|
||||
spinningCube = ECS::CreateEntity();
|
||||
ECS::AddTransform(spinningCube, 2.0f, 0.0f, 0.0f);
|
||||
ECS::AddSprite(spinningCube, g_cubeModel, 0x00FF00FF, 0.0f); // Green cube
|
||||
ECS::AddVelocity(spinningCube, 0.0f, 0.0f, 0.0f, 2.0f); // Angular velocity
|
||||
ECS::AddSprite(spinningCube, cubeModel, 0x00FF00FF, 0.0f); // Green cube
|
||||
ECS::AddVelocity(spinningCube, 0.0f, 0.0f, 0.0f, 2.0f); // Angular velocity
|
||||
|
||||
uint greenMat = Material::Create(g_toonShader, 0, 255, 0, 255);
|
||||
uint greenMat = Material::Create(toonShader, 0, 255, 0, 255);
|
||||
ECS::AddMaterial(spinningCube, greenMat);
|
||||
ECS::AddRenderPass(spinningCube, 1, true, true, 0); // Main pass
|
||||
|
||||
@@ -83,7 +83,7 @@ void Init() {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
uint entity = ECS::CreateEntity();
|
||||
|
||||
float angle = (float(i) / 5.0f) * 6.28318f; // 2*PI
|
||||
float angle = (float(i) / 5.0f) * 6.28318f; // 2*PI
|
||||
float radius = 3.0f;
|
||||
float x = Math::Cos(angle) * radius;
|
||||
float z = Math::Sin(angle) * radius;
|
||||
@@ -93,15 +93,11 @@ void Init() {
|
||||
// Different colors for each
|
||||
uint color = 0;
|
||||
uint8 r = 255, g = 255, b = 255;
|
||||
if (i == 0) { color = 0xFFFF00FF; r = 255; g = 255; b = 0; } // Yellow
|
||||
else if (i == 1) { color = 0xFF00FFFF; r = 255; g = 0; b = 255; } // Magenta
|
||||
else if (i == 2) { color = 0x00FFFFFF; r = 0; g = 255; b = 255; } // Cyan
|
||||
else if (i == 3) { color = 0xFFA500FF; r = 255; g = 165; b = 0; } // Orange
|
||||
else { color = 0xFF69B4FF; r = 255; g = 105; b = 180; } // Pink
|
||||
if (i == 0) { color = 0xFFFF00FF; r = 255; g = 255; b = 0; } // Yellow else if (i == 1) { color = 0xFF00FFFF; r = 255; g = 0; b = 255; } // Magenta else if (i == 2) { color = 0x00FFFFFF; r = 0; g = 255; b = 255; } // Cyan else if (i == 3) { color = 0xFFA500FF; r = 255; g = 165; b = 0; } // Orange else { color = 0xFF69B4FF; r = 255; g = 105; b = 180; } // Pink
|
||||
|
||||
ECS::AddSprite(entity, g_sphereModel, color, 0.0f);
|
||||
ECS::AddSprite(entity, sphereModel, color, 0.0f);
|
||||
|
||||
uint mat = Material::Create(g_toonShader, r, g, b, 255);
|
||||
uint mat = Material::Create(toonShader, r, g, b, 255);
|
||||
ECS::AddMaterial(entity, mat);
|
||||
ECS::AddRenderPass(entity, 1, true, true, 0);
|
||||
|
||||
@@ -112,10 +108,10 @@ void Init() {
|
||||
// === Create a moving entity ===
|
||||
uint mover = ECS::CreateEntity();
|
||||
ECS::AddTransform(mover, -3.0f, 0.5f, 0.0f);
|
||||
ECS::AddSprite(mover, g_cubeModel, 0xFFFFFFFF, 0.0f); // White cube
|
||||
ECS::AddVelocity(mover, 1.0f, 0.0f, 0.5f, 0.0f); // Moving velocity
|
||||
ECS::AddSprite(mover, cubeModel, 0xFFFFFFFF, 0.0f); // White cube
|
||||
ECS::AddVelocity(mover, 1.0f, 0.0f, 0.5f, 0.0f); // Moving velocity
|
||||
|
||||
ECS::AddMaterial(mover, g_defaultMaterial);
|
||||
ECS::AddMaterial(mover, defaultMaterial);
|
||||
ECS::AddRenderPass(mover, 1, true, true, 0);
|
||||
|
||||
ECS::AddTag(mover, "Mover");
|
||||
@@ -132,12 +128,12 @@ void Update(float dt) {
|
||||
time += dt;
|
||||
|
||||
// Update camera to orbit around the scene
|
||||
if (ECS::HasCamera3D(g_camera)) {
|
||||
if (ECS::HasCamera3D(camera)) {
|
||||
float angle = time * 0.3;
|
||||
float radius = 7.0;
|
||||
float x = Math::Cos(angle) * radius;
|
||||
float z = Math::Sin(angle) * radius;
|
||||
ECS::SetCameraPosition(g_camera, x, 5.0, z);
|
||||
ECS::SetCameraPosition(camera, x, 5.0, z);
|
||||
}
|
||||
|
||||
// Update player position - simple back and forth motion
|
||||
@@ -153,7 +149,7 @@ void Update(float dt) {
|
||||
float radius = 3.0f;
|
||||
float x = Math::Cos(angle) * radius;
|
||||
float z = Math::Sin(angle) * radius;
|
||||
float y = Math::Sin(time * 2.0f + float(i)) * 0.5f; // Bobbing motion
|
||||
float y = Math::Sin(time * 2.0f + float(i)) * 0.5f; // Bobbing motion
|
||||
|
||||
ECS::SetPosition(entity, x, y, z);
|
||||
ECS::SetRotation(entity, time + float(i));
|
||||
|
||||
Reference in New Issue
Block a user