has and remove component

main
cosmonaut 2020-12-05 14:51:45 -08:00
parent a7727bf750
commit 6a57453dc2
1 changed files with 60 additions and 8 deletions

View File

@ -36,7 +36,7 @@ pub const ComponentManager = struct {
self.component_stores.add(ptr.*);
}
pub fn add_component(self: *Self, entityID: usize, component: anytype) void {
pub fn set_component(self: *Self, entityID: usize, component: anytype) void {
self.component_stores.get(ComponentStore(@TypeOf(component))).*.set(entityID, component);
}
@ -44,6 +44,14 @@ pub const ComponentManager = struct {
return self.component_stores.get(ComponentStore(TComponent)).*.get(entityID) catch |err| return err;
}
pub fn has_component(self: *Self, comptime TComponent: type, entityID: usize) bool {
return self.component_stores.get(ComponentStore(TComponent)).*.has(entityID);
}
pub fn remove_component(self: *Self, comptime TComponent: type, entityID: usize) void {
return self.component_stores.get(ComponentStore(TComponent)).*.remove(entityID);
}
pub fn read_components(self: *Self, comptime TComponent: type) []TComponent {
return self.component_stores.get(ComponentStore(TComponent)).*.all_components();
}
@ -67,7 +75,7 @@ test "add component" {
component_manager.register(TestComponent);
component_manager.add_component(2, component);
component_manager.set_component(2, component);
var expectedComponent = try component_manager.get_component(TestComponent, 2);
expectedComponent.number = 5; // should be immutable!
@ -75,6 +83,50 @@ test "add component" {
testing.expect(expectedComponent.number == 3);
}
test "has component" {
const component = TestComponent {
.number = 69
};
var component_manager = ComponentManager.init(std.testing.allocator);
defer component_manager.deinit();
component_manager.register(TestComponent);
component_manager.set_component(420, component);
testing.expect(component_manager.has_component(TestComponent, 420));
testing.expect(!component_manager.has_component(TestComponent, 39));
}
test "remove component" {
const component = TestComponent {
.number = 3
};
const componentTwo = TestComponent {
.number = 8
};
const componentThree = TestComponent {
.number = 14
};
var component_manager = ComponentManager.init(std.testing.allocator);
defer component_manager.deinit();
component_manager.register(TestComponent);
component_manager.set_component(2, component);
component_manager.set_component(5, componentTwo);
component_manager.set_component(52, componentThree);
var components = component_manager.remove_component(TestComponent, 5);
testing.expect(!component_manager.has_component(TestComponent, 5));
testing.expect(component_manager.has_component(TestComponent, 52));
}
test "read components" {
const component = TestComponent {
.number = 3
@ -93,9 +145,9 @@ test "read components" {
component_manager.register(TestComponent);
component_manager.add_component(2, component);
component_manager.add_component(5, componentTwo);
component_manager.add_component(52, componentThree);
component_manager.set_component(2, component);
component_manager.set_component(5, componentTwo);
component_manager.set_component(52, componentThree);
var components = component_manager.read_components(TestComponent);
@ -122,9 +174,9 @@ test "read entities" {
component_manager.register(TestComponent);
component_manager.add_component(2, component);
component_manager.add_component(5, componentTwo);
component_manager.add_component(52, componentThree);
component_manager.set_component(2, component);
component_manager.set_component(5, componentTwo);
component_manager.set_component(52, componentThree);
var entities = component_manager.read_entities(TestComponent);