Skip to content

Commit

Permalink
Updated entity and engine to use an event system for updating informa…
Browse files Browse the repository at this point in the history
…tion between the two.

Added enum for designating what changes were made to an entity.
  • Loading branch information
Gamerfiend committed Apr 13, 2019
1 parent 40dd5e4 commit b2dcfa1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
40 changes: 31 additions & 9 deletions src/ecs.kit
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import kit.map;

enum EntityState{
COMPONENT_ADDED;
COMPONENT_REMOVED;

public function toString(): CString{
match this{
COMPONENT_ADDED => return "added";
COMPONENT_REMOVED => return "removed";
}
}
}

trait Component {
function typeIdentifier(): CString;
public function base(): Ptr[Void]{
Expand All @@ -13,10 +25,10 @@ trait System {

struct Entity {
private var components: Map[CString, Box[Component]];
private var allocator: Box[Allocator];
public var uniqueID: Uint32;
public var name: CString;
private var allocator: Box[Allocator];
public var engine: Ptr[Engine];
private var engine: Ptr[Engine];

private static var id: Uint32 = 0;

Expand All @@ -26,16 +38,16 @@ struct Entity {

return struct Self {
components,
allocator,
uniqueID,
name,
allocator,
engine: null
};
}

public function addComponent(component: Box[Component]) {
if !this.containsComponent(component) {
if !this.containsComponent(component.typeIdentifier()) {
this.components.put(component.typeIdentifier(), component);
this.notifiyChange(EntityState.COMPONENT_ADDED, component.typeIdentifier());
}
else {
this.components.remove(component.typeIdentifier());
Expand All @@ -54,12 +66,19 @@ struct Entity {
return this.components.keys();
}

public function containsComponent(component: Box[Component]): Bool {
return this.components.exists(component.typeIdentifier());
public function containsComponent(type: CString): Bool {
return this.components.exists(type);
}

public function removeComponent(typeIdentifier: CString): Void {
this.components.remove(typeIdentifier);
if(this.containsComponent(typeIdentifier)){
this.components.remove(typeIdentifier);
this.notifiyChange(EntityState.COMPONENT_REMOVED, typeIdentifier);
}
}

private function notifiyChange(change: EntityState, typeIdentifier: CString){
this.engine.entityHasChanged(change, typeIdentifier, this.uniqueID);
}
}

Expand Down Expand Up @@ -103,10 +122,13 @@ struct Engine {
}
}

entity.engine = null;
}
}

public function entityHasChanged(change: EntityState, typeIdentifier: CString, entityID: Uint32){
printf("Entity: %i, has %s, Component: %s\n", entityID, change.toString(), typeIdentifier);
}

public function addSystem(system: Box[System]) {

}
Expand Down
9 changes: 2 additions & 7 deletions tests/position.kit
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,12 @@ function main()
z: 12.0
};

var boxComponent: Box[Component] = positionComponent;
entity1.addComponent(boxComponent);

engine.addEntity(entity1);
printf("%i\n", );
if(entity1.getComponent("Position").isNone()){

}
entity1.addComponent(positionComponent);
var returnedBoxedComponent: Box[Component] = entity1.getComponent("Position").unwrap();

var positionPtr = returnedBoxedComponent.base() as Ptr[PositionComponent];
var positionPtr: Ptr[PositionComponent] = returnedBoxedComponent.base();

printf("%f\n", positionPtr.x);
}

0 comments on commit b2dcfa1

Please sign in to comment.