Created
January 31, 2014 22:19
-
-
Save triplefox/8744424 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ludamix.entity; | |
class Entity | |
{ | |
public var id(default, null) : Int; | |
public var properties : Dynamic; | |
public function new(id, properties) | |
{ | |
this.id = id; | |
this.properties = properties; | |
} | |
public function has(property : String) | |
{ | |
return Reflect.hasField(properties, property); | |
} | |
} | |
class EntitySystem | |
{ | |
public var entities : Map<Int, Entity>; | |
public var array_cache : Map<String, Array<Entity>>; | |
private var id : Int; | |
public function new() | |
{ | |
id = 0; | |
entities = new Map(); | |
array_cache = new Map(); | |
} | |
public function addArrayCache(property_name : String) | |
{ | |
array_cache.set(property_name, new Array()); | |
} | |
public function spawn(properties : Dynamic) | |
{ | |
while (entities.exists(id)) id++; | |
var ent = new Entity(id, properties); | |
entities.set(id, ent); | |
checkForCaches(ent); | |
id++; | |
return ent; | |
} | |
public function checkForCaches(ent : Entity) | |
{ | |
onAdd(ent); | |
for (k in array_cache.keys()) | |
{ | |
if (Reflect.hasField(ent.properties, k)) | |
{ | |
var ar = array_cache.get(k); | |
ar.push(ent); | |
} | |
} | |
} | |
public function flushCaches(ent : Entity) | |
{ | |
onRemove(ent); | |
for (k in array_cache.keys()) | |
{ | |
if (Reflect.hasField(ent.properties, k)) | |
{ | |
var ar = array_cache.get(k); | |
ar.remove(ent); | |
} | |
} | |
} | |
public function despawn(ent : Entity) | |
{ | |
flushCaches(ent); | |
entities.remove(ent.id); | |
} | |
/* Subclass with these fields overridden to create customized cache structures */ | |
public function onAdd(ent : Entity) | |
{ | |
} | |
public function onRemove(ent : Entity) | |
{ | |
} | |
} | |
class Example | |
{ | |
public static function main() | |
{ | |
var es = new EntitySystem(); | |
// automatically index by these following when the entity is spawned with the same property name. | |
// if we want to add or remove indexable properties from the entity after it's spawned, | |
// we should use flushCaches() and checkForCaches() to update it. | |
es.addArrayCache("friendly"); | |
es.addArrayCache("enemy"); | |
es.addArrayCache("position"); | |
// spawn some things | |
var player = es.spawn( {friendly:true, position:{x:10,y:5} } ); | |
for (n in 0...10) es.spawn( {enemy:true, position:{x:3,y:n} } ); | |
// accessing the arrays to do various things | |
for (p in es.array_cache.get("friendly")) | |
{ | |
p.properties.position.x -= 1; // moves "player" | |
} | |
for (e in es.array_cache.get("enemy")) | |
{ | |
e.properties.position.x += 1; // moves all 10 spawned "enemy" | |
} | |
var sort_by_x = function (a, b):Int | |
{ | |
return a.properties.position.x - b.properties.position.x; | |
} ; | |
es.array_cache.get("position").sort(sort_by_x); | |
// when despawning we generally operate on a copy of the array | |
for (e in es.array_cache.get("enemy").copy()) | |
es.despawn(e); | |
trace([for (n in es.entities) Std.string(n.properties)]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment