diff --git a/src/core/asset-manager.js b/src/core/asset-manager.js index e69de29..559692d 100644 --- a/src/core/asset-manager.js +++ b/src/core/asset-manager.js @@ -0,0 +1,24 @@ +const fs = require('fs'); +const path = require('path'); + +class AssetManager { + constructor(assetDir) { + this.assetDir = path.resolve(assetDir); + this.assets = {}; + } + + loadAssets() { + if (!fs.existsSync(this.assetDir)) return; + const files = fs.readdirSync(this.assetDir); + files.forEach(file => { + const filePath = path.join(this.assetDir, file); + this.assets[file] = filePath; + }); + } + + getAsset(name) { + return this.assets[name]; + } +} + +module.exports = AssetManager; diff --git a/src/core/config-loader.js b/src/core/config-loader.js index e69de29..26253b4 100644 --- a/src/core/config-loader.js +++ b/src/core/config-loader.js @@ -0,0 +1,15 @@ +const fs = require('fs'); +const path = require('path'); + +class ConfigLoader { + static load(configPath) { + const fullPath = path.resolve(configPath); + if (!fs.existsSync(fullPath)) { + throw new Error(`Config file not found: ${fullPath}`); + } + const raw = fs.readFileSync(fullPath); + return JSON.parse(raw); + } +} + +module.exports = ConfigLoader; diff --git a/src/core/event-bus.js b/src/core/event-bus.js index e69de29..30184de 100644 --- a/src/core/event-bus.js +++ b/src/core/event-bus.js @@ -0,0 +1,20 @@ +class EventBus { + constructor() { + this.listeners = {}; + } + + on(event, callback) { + if (!this.listeners[event]) { + this.listeners[event] = []; + } + this.listeners[event].push(callback); + } + + emit(event, data) { + if (this.listeners[event]) { + this.listeners[event].forEach(cb => cb(data)); + } + } +} + +module.exports = EventBus; diff --git a/src/core/game-loop.js b/src/core/game-loop.js index e69de29..0d38b8d 100644 --- a/src/core/game-loop.js +++ b/src/core/game-loop.js @@ -0,0 +1,23 @@ +class GameLoop { + constructor({ onUpdate, onEnd }) { + this.running = false; + this.onUpdate = onUpdate; + this.onEnd = onEnd; + } + + start() { + this.running = true; + while (this.running) { + if (this.onUpdate) this.onUpdate(); + // Simpel voorbeeld: direct stoppen + this.stop(); + } + if (this.onEnd) this.onEnd(); + } + + stop() { + this.running = false; + } +} + +module.exports = GameLoop; diff --git a/src/index.js b/src/index.js index e69de29..d4097de 100644 --- a/src/index.js +++ b/src/index.js @@ -0,0 +1,26 @@ +const ConfigLoader = require('./core/config-loader'); +const AssetManager = require('./core/asset-manager'); +const EventBus = require('./core/event-bus'); +const GameLoop = require('./core/game-loop'); + +// Config pad kan via argumenten komen, standaard default.json +const configPath = process.argv[2] || '../configs/default.json'; +const config = ConfigLoader.load(configPath); + +const assetManager = new AssetManager(config.assetsDir || '../assets/default'); +assetManager.loadAssets(); + +const eventBus = new EventBus(); + +const gameLoop = new GameLoop({ + onUpdate: () => { + // Hier komt de game logica per tick + console.log('Game update'); + }, + onEnd: () => { + console.log('Game ended'); + } +}); + +console.log('Engine gestart met config:', configPath); +gameLoop.start();