198 lines
6.6 KiB
ActionScript 3
198 lines
6.6 KiB
ActionScript 3
// ECS-based game script demo with script-driven rendering
|
|
// This demonstrates creating entities with various components and managing rendering
|
|
|
|
// Resource IDs
|
|
uint toonShader = 0;
|
|
uint outlineShader = 0;
|
|
uint cubeModel = 0;
|
|
uint sphereModel = 0;
|
|
uint defaultMaterial = 0;
|
|
|
|
// Scene entities
|
|
uint camera = 0;
|
|
uint light = 0;
|
|
|
|
// Store entity IDs for manipulation
|
|
array<uint> entities;
|
|
uint player;
|
|
uint spinningCube;
|
|
float time = 0.0f;
|
|
|
|
void Init() {
|
|
Log(LOG_TRACE, "Initialization complete - ECS Demo with Script-Driven Rendering!");
|
|
Toast::Success("ECS Script initialized successfully!");
|
|
|
|
// === Load shaders ===
|
|
toonShader = Shader::Load("shaders/toon.vs", "shaders/toon.fs");
|
|
outlineShader = Shader::Load("shaders/outline.vs", "shaders/outline.fs");
|
|
|
|
if (toonShader == 0 || outlineShader == 0) {
|
|
Toast::Error("Failed to load shaders!");
|
|
return;
|
|
}
|
|
|
|
// === Load models ===
|
|
cubeModel = Model::LoadCube(1.0, 1.0, 1.0);
|
|
sphereModel = Model::LoadSphere(0.5, 16, 16);
|
|
|
|
// === Create default material ===
|
|
defaultMaterial = Material::Create(toonShader, 255, 255, 255, 255);
|
|
|
|
// === Set up camera ===
|
|
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 ===
|
|
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, sphereModel, 0xFF0000FF, 0.0f); // Red sphere
|
|
ECS::SetSpriteShader(player, toonShader); // Apply toon shader
|
|
|
|
ECS::AddTag(player, "Player");
|
|
Log(LOG_INFO, "Created player entity: " + player);
|
|
|
|
// === Create a spinning cube ===
|
|
spinningCube = ECS::CreateEntity();
|
|
ECS::AddTransform(spinningCube, 2.0f, 0.0f, 0.0f);
|
|
ECS::AddSprite(spinningCube, cubeModel, 0x00FF00FF, 0.0f); // Green cube
|
|
ECS::SetSpriteShader(spinningCube, toonShader); // Apply toon shader
|
|
ECS::AddVelocity(spinningCube, 0.0f, 0.0f, 0.0f, 2.0f); // Angular velocity
|
|
|
|
ECS::AddTag(spinningCube, "Spinner");
|
|
Log(LOG_INFO, "Created spinning cube: " + spinningCube);
|
|
|
|
// === Create orbiting entities ===
|
|
for (int i = 0; i < 5; i++) {
|
|
uint entity = ECS::CreateEntity();
|
|
|
|
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;
|
|
|
|
ECS::AddTransform(entity, x, 0.0f, z);
|
|
|
|
// 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
|
|
|
|
ECS::AddSprite(entity, sphereModel, color, 0.0f);
|
|
ECS::SetSpriteShader(entity, toonShader); // Apply toon shader
|
|
|
|
ECS::AddTag(entity, "Orbiter" + i);
|
|
entities.insertLast(entity);
|
|
}
|
|
|
|
// === Create a moving entity ===
|
|
uint mover = ECS::CreateEntity();
|
|
ECS::AddTransform(mover, -3.0f, 0.5f, 0.0f);
|
|
ECS::AddSprite(mover, cubeModel, 0xFFFFFFFF, 0.0f); // White cube
|
|
ECS::SetSpriteShader(mover, toonShader); // Apply toon shader
|
|
ECS::AddVelocity(mover, 1.0f, 0.0f, 0.5f, 0.0f); // Moving velocity
|
|
|
|
ECS::AddTag(mover, "Mover");
|
|
entities.insertLast(mover);
|
|
|
|
Toast::Success("Created " + (entities.length() + 2) + " entities with script-driven rendering!");
|
|
}
|
|
|
|
void Shutdown() {
|
|
Log(LOG_TRACE, "Shutdown complete!");
|
|
}
|
|
bool lightToggle = false;
|
|
void Update(float dt) {
|
|
time += dt;
|
|
|
|
// Update camera to orbit around the scene
|
|
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(camera, x, 5.0, z);
|
|
}
|
|
|
|
// Update player position - simple back and forth motion
|
|
float playerX = Math::Sin(time) * 2.0f;
|
|
ECS::SetPosition(player, playerX, 0.0f, 0.0f);
|
|
|
|
// Update orbiting entities in a circle
|
|
for (uint i = 0; i < entities.length() - 1; i++) {
|
|
uint entity = entities[i];
|
|
if (!ECS::IsValid(entity)) continue;
|
|
|
|
float angle = (float(i) / 5.0f) * 6.28318f + time * 0.5f;
|
|
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
|
|
|
|
ECS::SetPosition(entity, x, y, z);
|
|
ECS::SetRotation(entity, time + float(i));
|
|
}
|
|
|
|
// The mover entity wraps around using the Velocity component
|
|
// which is handled by the C++ movement system
|
|
if (entities.length() > 0) {
|
|
uint mover = entities[entities.length() - 1];
|
|
if (ECS::IsValid(mover) && ECS::HasTransform(mover)) {
|
|
float x, y, z;
|
|
ECS::GetPosition(mover, x, y, z);
|
|
|
|
// Wrap around
|
|
if (x > 5.0f) {
|
|
ECS::SetPosition(mover, -5.0f, y, z);
|
|
}
|
|
if (z > 5.0f) {
|
|
ECS::SetPosition(mover, x, y, -5.0f);
|
|
}
|
|
}
|
|
}
|
|
|
|
// === Input Action System Demo ===
|
|
// Toggle light with jump action
|
|
if (IsActionPressed("jump")) {
|
|
lightToggle = !lightToggle;
|
|
|
|
if (lightToggle) {
|
|
ECS::SetLightDirection(light, -0.5, 0.7, -0.3);
|
|
Toast::Info("Light direction changed!");
|
|
} else {
|
|
ECS::SetLightDirection(light, 0.5, 0.7, 0.3);
|
|
Toast::Info("Light direction reset!");
|
|
}
|
|
}
|
|
|
|
// Move player entity with input actions
|
|
if (ECS::IsValid(player) && ECS::HasTransform(player)) {
|
|
float x, y, z;
|
|
ECS::GetPosition(player, x, y, z);
|
|
float moveSpeed = 3.0f * dt;
|
|
|
|
if (IsActionDown("move_left")) {
|
|
x -= moveSpeed;
|
|
}
|
|
if (IsActionDown("move_right")) {
|
|
x += moveSpeed;
|
|
}
|
|
if (IsActionDown("move_up")) {
|
|
z -= moveSpeed;
|
|
}
|
|
if (IsActionDown("move_down")) {
|
|
z += moveSpeed;
|
|
}
|
|
|
|
ECS::SetPosition(player, x, y, z);
|
|
}
|
|
}
|