Skip to content

Commit 0b30d12

Browse files
committed
Merge branch 'dev' of https://github.com/Gallasko/PgEngine into dev
2 parents dcb27ec + 3818755 commit 0b30d12

5 files changed

Lines changed: 73 additions & 5 deletions

File tree

exemples/Asteroid/application.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ StandardSystemImpl* createPlayerSystem()
2929
.onEvent("OnSDLScanCode", "res/asteroid/move_player.pg")
3030
.onEvent("OnSDLScanCodeReleased", "res/asteroid/release_player.pg")
3131
.onEvent("PlayerHit", "res/asteroid/handle_player_hit.pg")
32+
.onEvent("RespawnPlayer", "res/asteroid/respawn_player.pg")
3233
.onDelta("res/asteroid/update_player.pg") // Update physics every frame
3334
.build();
3435
}
@@ -161,7 +162,6 @@ StandardSystemImpl* createGameOverSystem()
161162
return createStandardSystem("GameOverSystem")
162163
.onInit("res/asteroid/init_gameover.pg")
163164
.onEvent("GameOver", "res/asteroid/handle_gameover.pg")
164-
.onEvent("RespawnPlayer", "res/asteroid/respawn_player.pg")
165165
.onEvent("OnSDLScanCode", "res/asteroid/handle_restart.pg")
166166
.build();
167167
}

res/asteroid/handle_restart.pg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ if (sysData["restartTextId"] != 0)
3232
sysData["restartTextId"] = 0
3333
}
3434

35+
if (sysData["reasonTextId"] != 0)
36+
{
37+
removeEntity(sysData["reasonTextId"])
38+
sysData["reasonTextId"] = 0
39+
}
40+
3541
// Clear all asteroids
3642
var asteroids = getEntities("Asteroid")
3743
for (var asteroid : asteroids)

res/asteroid/respawn_player.pg

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@ var screenWidth = 800
99
var screenHeight = 600
1010

1111
// Create new player entity
12-
var player = createTexture("Ship", 32, 32)
12+
var player = createTexture("I", 32, 32)
13+
14+
player.attachComp("Player")
15+
player.attachComp("Collision", "scale", 0.5)
1316

1417
// Position in center of screen
15-
player["PositionComponent"].setX(screenWidth / 2)
16-
player["PositionComponent"].setY(screenHeight / 2)
17-
player["PositionComponent"].setRotation(0.0)
18+
player["PositionComponent"].x = screenWidth / 2
19+
player["PositionComponent"].y = screenHeight / 2
20+
player["PositionComponent"].rotation = 0.0
21+
22+
sysData["playerId"] = player.__entityId
1823

1924
// // Attach Player component with physics variables
2025
// player.attachComp("Player",

src/Engine/ECS/ecsnativemodule.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace pg
1515
*
1616
* - removeEntity(entityId or entityInstance): Remove an entity from the ECS
1717
* - getEntity(entityId): Get entity data as a table
18+
* - getEntities(componentName): Get all entities with the specified component as a table of entity tables
1819
* - sendEvent(eventName, key1, value1, key2, value2, ...): Send an event
1920
* - attachComponent(entityId or entityInstance, componentName, prop1, value1, prop2, value2, ...):
2021
* Attach a StandardComponent to an entity with properties
@@ -48,6 +49,12 @@ namespace pg
4849
* // Remove an entity
4950
* ecs.removeEntity(playerId)
5051
*
52+
* // Get all entities with a specific component
53+
* var allPlayers = ecs.getEntities("Player")
54+
* for (var player : allPlayers) {
55+
* __dprint("Player entity: " + player.__entityId)
56+
* }
57+
*
5158
* // ========== Creating Systems from Scripts ==========
5259
*
5360
* // Create a player management system
@@ -650,6 +657,52 @@ namespace pg
650657
return inst;
651658
});
652659

660+
addNativeFunction("getEntities", [ecsRefCopy](VM* vm, int argCount, Value* args) -> Value {
661+
if (argCount != 1)
662+
{
663+
throw std::runtime_error("getEntities expects exactly 1 argument (componentName)");
664+
}
665+
666+
if (!IS_STRING(args[0]))
667+
{
668+
throw std::runtime_error("getEntities expects a string component name");
669+
}
670+
671+
auto componentName = vm->asString(args[0]);
672+
673+
// Create a vector to hold all entity tables
674+
Value vectorValue = vm->createVector();
675+
ObjVector* vector = vm->asVector(vectorValue);
676+
677+
// Get the component owner from the registry
678+
auto* owner = ecsRefCopy->getComponentRegistry()->retrieveStandardComponent(componentName);
679+
680+
if (!owner)
681+
{
682+
throw std::runtime_error("getEntities: Standard Component '" + componentName + "' not found in ECS");
683+
}
684+
685+
// Get all components of this type from the owner
686+
auto componentView = owner->view();
687+
688+
for (auto* component : componentView)
689+
{
690+
// Get the full entity with ALL components, not just the requested one
691+
auto* entity = ecsRefCopy->getEntity(component->entityId);
692+
if (entity)
693+
{
694+
// Serialize the complete entity with all its components
695+
Value entityTable = serializeEntityToTable(vm, ecsRefCopy, entity);
696+
vector->fields.push_back(vm->retainValue(entityTable));
697+
}
698+
}
699+
700+
LOG_MILE("Ecs Compiled Module", "getEntities(\"" << componentName << "\") returned "
701+
<< vector->fields.size() << " entities");
702+
703+
return vectorValue;
704+
});
705+
653706
}
654707
};
655708
}

src/Engine/Memory/memorypool.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ namespace pg
161161

162162
if (index >= size) reserve(index);
163163

164+
// Track high-water mark for destructor cleanup
165+
if (index > maxAllocatedIndex)
166+
maxAllocatedIndex = index;
167+
164168
// Todo Check if the chunk was created before creating a new element
165169
PGMemChunk<T>* chunk = getChunk(index);
166170

0 commit comments

Comments
 (0)