Преглед изворни кода

:tada: added game loop

master
j пре 2 година
родитељ
комит
713c5197b7
3 измењених фајлова са 76 додато и 20 уклоњено
  1. 25
    20
      src/index.js
  2. 45
    0
      src/loop.js
  3. 6
    0
      test/system.spec.js

+ 25
- 20
src/index.js Прегледај датотеку

@@ -1,6 +1,8 @@
1
-const { makeContainerT } = require('./system.js')
2
-const { Container } = require('./container.js')
3
-const { fill, drain } = require('./utils.js')
1
+import { EventEmitter } from 'node:events'
2
+
3
+import loop from './loop.js'
4
+import { System, makeContainerT } from './system.js'
5
+import { Container } from './container.js'
4 6
 
5 7
 const fromConfig = {
6 8
     tank_1: {
@@ -36,23 +38,26 @@ const digestConfig = config => {
36 38
 /** Holds systems or containers */
37 39
 class Controller {}
38 40
 
39
-mySystem = digestConfig(fromConfig)
40
-
41
-console.log('mySystem :>> ', mySystem.inventory)
42
-console.log('\ntank capacity:>> ', mySystem.containers[0].liquidVolumeCapacity)
43
-console.log('tank level:>> ', mySystem.containers[0].liquidVolumeFilled)
44
-console.log('pass:>> ', mySystem.containers[0].liquidVolumeFilled === 0)
41
+const mySystem = digestConfig(fromConfig)
45 42
 
46
-const postFill = fill({ container: mySystem.containers[0], amount: 10 })
47
-mySystem.replaceContainer(postFill)
48
-console.log('\nfilled tank :>> ', mySystem.containers[0].liquidVolumeFilled)
49
-console.log('pass:>> ', mySystem.containers[0].liquidVolumeFilled === 10)
43
+// Start the loop!
44
+const emitter = new EventEmitter()
45
+emitter.on('onUpdate', ({ delta }) => {
46
+    const start = Date.now()
47
+    const milli = 111
48
+    while (Date.now() < start + milli) {
49
+        console.log(new Date(start).toISOString())
50
+        console.log(delta)
51
+    }
50 52
 
51
-const postDrain = drain({ container: mySystem.containers[0], amount: 3 })
52
-mySystem.replaceContainer(postDrain)
53
-console.log('\ndrained tank :>> ', mySystem.containers[0].liquidVolumeFilled)
54
-console.log('pass:>> ', mySystem.containers[0].liquidVolumeFilled === 7)
53
+    // Check all inputs
54
+    // Queue tasks based on inputs
55
+    // Queue one-off tasks based on date/time
56
+    // Execute the task queue
57
+})
58
+loop.start(emitter)
55 59
 
56
-console.log('\ninputs + outputs')
57
-console.log('inputs :>> ', mySystem.inputsFor(mySystem.containers[0]))
58
-console.log('outputs :>> ', mySystem.outputsFor(mySystem.containers[0]))
60
+setTimeout(function () {
61
+    console.log('6000ms passed, stopping the game loop')
62
+    loop.active.splice(loop.active.indexOf(0), 1)
63
+}, 6000)

+ 45
- 0
src/loop.js Прегледај датотеку

@@ -0,0 +1,45 @@
1
+const active = []
2
+const getLoopId = (function () {
3
+    let staticLoopId = 0
4
+    return function () {
5
+        return staticLoopId++
6
+    }
7
+})()
8
+
9
+/** https://github.com/tangmi/node-gameloop/tree/master */
10
+const start = emitter => {
11
+    // FPS
12
+    const _fps = 20
13
+    const _tickLength = 1000 / _fps
14
+    let _lastTick = Date.now()
15
+    let _loopCount = 0
16
+
17
+    let id = getLoopId()
18
+    active.push(id)
19
+
20
+    const run = () => {
21
+        const now = Date.now()
22
+        _loopCount++
23
+        if (_lastTick + _tickLength <= now) {
24
+            const delta = (now - _lastTick) / 1000
25
+            _lastTick = now
26
+
27
+            emitter.emit('onUpdate', { delta })
28
+
29
+            _loopCount = 0
30
+        }
31
+
32
+        if (active.indexOf(id) === -1) {
33
+            return
34
+        }
35
+
36
+        if (Date.now() - _lastTick < _tickLength - 16) {
37
+            setTimeout(run)
38
+        } else {
39
+            setImmediate(run)
40
+        }
41
+    }
42
+    run()
43
+}
44
+
45
+export default { active, start }

+ 6
- 0
test/system.spec.js Прегледај датотеку

@@ -14,6 +14,12 @@ test('system - instantiates and stores inventory correctly', () => {
14 14
     expect(testSystem.outputs).toStrictEqual(['output_test'])
15 15
     expect(testSystem.containers[0].id).toStrictEqual('aaa')
16 16
 
17
+    const inputs = testSystem.inputsFor({ id: 'aaa' })
18
+    expect(inputs).toStrictEqual('input_test')
19
+
20
+    const outputs = testSystem.outputsFor({ id: 'aaa' })
21
+    expect(outputs).toStrictEqual('output_test')
22
+
17 23
     testSystem.remove('aaa')
18 24
     expect(testSystem.inventory).toStrictEqual({})
19 25
 })

Loading…
Откажи
Сачувај