Advertisement
Guest User

Ex

a guest
Feb 14th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SIEngine.Scene = function () {
  2.   this.componentDictionary = {};
  3.   this.systemList = [];
  4. };
  5.  
  6. SIEngine.Scene.prototype = Object.create(Object.prototype, {
  7.   constructor: {
  8.     value: SIEngine.Scene
  9.   },
  10.   componentDictionary: {
  11.     enumerable: true, configurable: true, writable: true
  12.   },
  13.   systemList: {
  14.     enumerable: true, configurable: true, writable: true
  15.   }
  16. });
  17.  
  18. SIEngine.Scene.prototype.getComponentDictionary = function () {
  19.   return this.componentDictionary;
  20. }
  21.  
  22. SIEngine.Scene.prototype.getSystemList = function () {
  23.   return this.systemList;
  24. }
  25.  
  26. SIEngine.Scene.prototype.addComponent = function (entity, component) {
  27.   var entityIdentifier = entity.getIdentifier();
  28.   var componentType = component.getType();
  29.  
  30.   var componentDictionary = this.getComponentDictionary();
  31.  
  32.   component.setParentEntity(entity);
  33.  
  34.   if (!(componentType in componentDictionary)) {
  35.     componentDictionary[componentType] = {};
  36.   }
  37.  
  38.   componentDictionary[componentType][entityIdentifier] = component;
  39. };
  40.  
  41. SIEngine.Scene.prototype.removeComponent = function (entity, component) {
  42.   var entityIdentifier = entity.getIdentifier();
  43.   var componentType = component.getType();
  44.  
  45.   var componentDictionary = this.getComponentDictionary();
  46.  
  47.   component.deleteParentEntity();
  48.  
  49.   delete componentDictionary[componentType][entityIdentifier];
  50.  
  51.   if (Object.keys(componentDictionary[componentType]).length === 0) {
  52.     delete componentDictionary[componentType];
  53.   }
  54. };
  55.  
  56. SIEngine.Scene.prototype.removeEntity = function (entity) {
  57.   var entityIdentifier = entity.getIdentifier();
  58.  
  59.   var componentDictionary = this.getComponentDictionary();
  60.  
  61.   for (var componentType in componentDictionary) {
  62.     delete componentDictionary[componentType][entity];
  63.  
  64.     if (Object.keys(componentDictionary[componentType]).length === 0) {
  65.       delete componentDictionary[componentType];
  66.     }
  67.   }
  68. };
  69.  
  70. SIEngine.Scene.prototype.addSystem = function (system) {
  71.   var systemList = this.getSystemList();
  72.  
  73.   system.setParentScene(this);
  74.  
  75.   systemList.push(system);
  76.   systemList.sort(function(a, b) {return a.getPriority() - b.getPriority()});
  77. };
  78.  
  79. SIEngine.Scene.prototype.removeSystem = function (system) {
  80.   var systemList = this.getSystemList();
  81.   var systemIndex = systemList.indexOf(system);
  82.  
  83.   if (systemIndex !== -1) {
  84.     system.deleteParentScene();
  85.  
  86.     systemList.splice(systemIndex, 1);
  87.   }
  88. };
  89.  
  90. SIEngine.Scene.prototype.updateSystems = function () {
  91.   var systemList = this.getSystemList();
  92.  
  93.   for (var i = 0; i < systemList.length; i++) {
  94.     systemList[i].update();
  95.   }
  96. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement