aboutsummaryrefslogtreecommitdiff
path: root/test/integration.js
blob: 7d88432bb73859c460fb2be51474cd23eee3af09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict';

let test = function test (Serpentity) {

  /* eslint no-console: 0 */

  /////////////////
  // Load the stuff
  /////////////////
  console.log('\n## Loading');
  console.log('Serpentity: ' + (typeof Serpentity !== 'undefined' ? 'LOAD OK' : 'FAIL'));
  console.log('Serpentity.Entity: ' + (typeof Serpentity !== 'undefined' && Serpentity.Entity ? 'LOAD OK' : 'FAIL'));
  console.log('Serpentity.Component: ' + (typeof Serpentity !== 'undefined' && Serpentity.Component ? 'LOAD OK' : 'FAIL'));
  console.log('Serpentity.System: ' + (typeof Serpentity !== 'undefined' && Serpentity.System ? 'LOAD OK' : 'FAIL'));
  console.log('Serpentity.Node: ' + (typeof Serpentity !== 'undefined' && Serpentity.Node ? 'LOAD OK' : 'FAIL'));
  console.log('Serpentity.NodeCollection: ' + (typeof Serpentity !== 'undefined' && Serpentity.NodeCollection ? 'LOAD OK' : 'FAIL'));

  //////////////////////
  // Create test classes
  //////////////////////
  console.log('\n## Creating Test Classes');
  let TestSystem = class TestSystem extends Serpentity.System {
    added (engine) {
      this.testNodes = engine.getNodes(TestNode);
      console.log('Engine is serpentity: ' + (engine instanceof Serpentity ? 'OK' : 'FAIL'));
      console.log('System added callback: EXEC OK');
    }

    removed (engine) {
      this.testNodes = null;
      console.log('Engine is serpentity: ' + (engine instanceof Serpentity ? 'OK' : 'FAIL'));
      console.log('System removed callback: EXEC OK');
    }

    update (dt) {
      this.testNodes.forEach(function (node) {
        console.log('Running Node: ' + (node.test.testMessage === 'test' ? 'SYSTEM OK' : 'FAIL'));
      });
      console.log('dt is number: ' + (typeof dt === 'number' ? 'OK' : 'FAIL'));
      console.log('System update callback: EXEC OK');
    }
  };
  let testSystem = new TestSystem();

  let LowProTestSystem = class LowProTestSystem extends Serpentity.System {
    added (engine) {
      this.testNodes = engine.getNodes(TestNode);
      console.log('Engine is serpentity: ' + (engine instanceof Serpentity ? 'OK' : 'FAIL'));
      console.log('System added callback: EXEC OK');
    }

    removed (engine) {
      this.testNodes = null;
      console.log('Engine is serpentity: ' + (engine instanceof Serpentity ? 'OK' : 'FAIL'));
      console.log('System removed callback: EXEC OK');
    }

    update (dt) {
      this.testNodes.forEach(function (node) {
        console.log('Running Low Priority Node: ' + (node.test.testMessage === 'test' ? 'SYSTEM OK' : 'FAIL'));
      });
      console.log('dt is number: ' + (typeof dt === 'number' ? 'OK' : 'FAIL'));
      console.log('System update callback: EXEC OK');
    }
  };
  let lowProTestSystem = new LowProTestSystem();
  console.log('LowProTestSystem: CREATE OK');

  let MidProTestSystem = class MidProTestSystem extends Serpentity.System {
    added (engine) {
      this.testNodes = engine.getNodes(TestNode);
      console.log('Engine is serpentity: ' + (engine instanceof Serpentity ? 'OK' : 'FAIL'));
      console.log('System added callback: EXEC OK');
    }

    removed (engine) {
      this.testNodes = null;
      console.log('Engine is serpentity: ' + (engine instanceof Serpentity ? 'OK' : 'FAIL'));
      console.log('System removed callback: EXEC OK');
    }

    update (dt) {
      this.testNodes.forEach(function (node) {
        console.log('Running Mid Priority Node: ' + (node.test.testMessage === 'test' ? 'SYSTEM OK' : 'FAIL'));
      });
      console.log('dt is number: ' + (typeof dt === 'number' ? 'OK' : 'FAIL'));
      console.log('System update callback: EXEC OK');
    }
  };
  var midProTestSystem = new MidProTestSystem();
  console.log('MidProTestSystem: CREATE OK');


  let TestComponent = class TestComponent extends Serpentity.Component {
    constructor (config) {
      super(config);

      this.testMessage = this.testMessage || 'test';
    }
  };
  console.log('TestComponent: CREATE OK');

  let TestNode = class TestNode extends Serpentity.Node {};
  TestNode.types = {
    test : TestComponent
  };
  console.log('TestNode: CREATE OK');

  console.log('\n## Adding system to the engine');

  let engine = new Serpentity();
  console.log('engine: CREATE OK');

  engine.addSystem(testSystem, 0);

  console.log('\n## Running update without any entities');
  engine.update(10);

  console.log('\n## Adding system to the engine and updating');
  let entity = new Serpentity.Entity();
  entity.addComponent(new TestComponent());
  engine.addEntity(entity);
  engine.update(10);

  console.log('\n## Adding Low Priority System');
  engine.addSystem(lowProTestSystem, 10);
  engine.update(10);

  console.log('\n## Adding Mid Priority System');
  engine.addSystem(midProTestSystem, 5);
  engine.update(10);

  console.log('\n## Removing the system and readding');
  engine.removeSystem(testSystem);
  engine.update(10);
  engine.addSystem(testSystem, 0);
  engine.update(10);

  console.log('\n## Adding a second entity');
  entity = new Serpentity.Entity();
  entity.addComponent(new TestComponent());
  engine.addEntity(entity);
  engine.update(10);

  console.log('\n## Removing  entity');
  engine.removeEntity(entity);
  engine.update(10);

  console.log('\n## Removing  system');
  engine.removeSystem(testSystem);
  engine.update(10);

};

if (typeof require === 'function') {
  let Serpentity = require('serpentity');
  test(Serpentity);
} else {
  window.addEventListener('load', function () {
    test(window.Serpentity);
  });
}