diff --git a/engine/core/Actor.js b/engine/core/Actor.js index 0886f05..f66158e 100644 --- a/engine/core/Actor.js +++ b/engine/core/Actor.js @@ -1,5 +1,37 @@ +var nextActorGuid = 0; export default class Actor { constructor() { - + this.parent = null; + this.id = nextActorGuid++; + this.components = {}; + this.children = []; + this.eventManager = null; } + + addComponent(component) { + component.setActor(this); + this.components[component.type] = component; + } + + getComponent(type) { + return this.components[type] + } + + removeComponent() { + + } + + addChild() { + + } + + setEventManager(eventManager) { + this.eventManager = eventManager; + } + + removeChild() { + + } + + } \ No newline at end of file diff --git a/engine/core/Actor3D.js b/engine/core/Actor3D.js new file mode 100644 index 0000000..7eeeded --- /dev/null +++ b/engine/core/Actor3D.js @@ -0,0 +1,9 @@ +import Actor from "./Actor"; +import Transform3D from "./Transform3D"; + +export default class Actor3D extends Actor { + constructor() { + super(); + this.addComponent(new Transform3D()); + } +} \ No newline at end of file diff --git a/engine/core/Cache.js b/engine/core/Cache.js index 347b8e9..778df4d 100644 --- a/engine/core/Cache.js +++ b/engine/core/Cache.js @@ -1,5 +1,5 @@ import Pipline from "./Pipeline"; -import http from "http"; +import * as http from "engine/core/http"; export default class Cache { constructor() { @@ -15,7 +15,12 @@ export default class Cache { return; } - //#TODO + http.get(url).then((e)=>{ + assets[url] = e.target.responseText; + resolve(assets[url]); + }, (e)=> { + reject(e); + }) }) } diff --git a/engine/core/Component.js b/engine/core/Component.js new file mode 100644 index 0000000..fadffcc --- /dev/null +++ b/engine/core/Component.js @@ -0,0 +1,10 @@ +export default class Component { + constructor(type) { + this.type = type; + this.actor = null; + } + + setActor(actor) { + this.actor = actor; + } +} \ No newline at end of file diff --git a/engine/core/EventManager.js b/engine/core/EventManager.js new file mode 100644 index 0000000..e69de29 diff --git a/engine/core/Process.js b/engine/core/Process.js new file mode 100644 index 0000000..fe935be --- /dev/null +++ b/engine/core/Process.js @@ -0,0 +1,36 @@ +/* +var STATES = { + UNINITIALIZED : 0, + RUNNING : 1, + SUCCEDED : 2, + FAILED : 3 +}*/ + +export default class Process { + constructor(){ + this.state = "UNINITIALIZED"; + this.children = []; + } + + initialize(){ + this.state = "RUNNING"; + } + + succeed(){ + this.state = "SUCCEDED"; + } + + succeed(){ + this.state = "FAILED"; + } + + addChild(child) { + this.children.push(child); + } + + update(deltaMs){ + + } +} + +export {states} \ No newline at end of file diff --git a/engine/core/ProcessManager.js b/engine/core/ProcessManager.js new file mode 100644 index 0000000..dab206b --- /dev/null +++ b/engine/core/ProcessManager.js @@ -0,0 +1,26 @@ +export default class ProcessManager { + constructor(){ + this.processes = []; + } + + addChild(proc) { + this.processes.push(proc); + } + + update(deltaMs){ + for(let proc of this.processes){ + if(proc.state === "UNINITIALIZED"){ + proc.initialize(); + } + + if(proc.state === "RUNNING"){ + proc.update(deltaMs); + } + + if(proc.state === "SUCCEEDED"){ + proc.succeed(); + this.processes.push(...this.children); + } + } + } +} \ No newline at end of file diff --git a/engine/core/Scene3D.js b/engine/core/Scene3D.js new file mode 100644 index 0000000..97b26d6 --- /dev/null +++ b/engine/core/Scene3D.js @@ -0,0 +1,13 @@ +import Actor3D from "./Actor3D"; + +export default class Scene3D extends Actor3D { + constructor() { + super(); + this.actors = {}; + } + + addChild(child){ + super.addChild(child); + this.actors[child.id] = child; + } +} \ No newline at end of file diff --git a/engine/core/Transform3D.js b/engine/core/Transform3D.js new file mode 100644 index 0000000..5f44271 --- /dev/null +++ b/engine/core/Transform3D.js @@ -0,0 +1,42 @@ +import Component from "./Component.js"; + + +export default class Transform3D extends Component { + constructor() { + super("transform"); + + this._position = vec3.create(); + this._rotation = vec3.create(); + + this.toWorld = mat4.create(); + mat4.identity(this.toWorld); + } + + get position () { + return vec3.clone(this._position); + } + + set position (position) { + this._position = position; + this.updateMatrix(); + eventManager.emit("actor:move", this.actor); + } + + get rotation () { + return vec3.clone(this._rotation); + } + + set rotation (rotation) { + this._rotation = rotation; + this.updateMatrix(); + eventManager.emit("actor:move", this.actor); + } + + updateMatrix() { + //set matrix according to values in rotation/position; + //TODO assume this works fine. + for(let child of this.actor.children){ + child.updateMatrix(); + } + } +} \ No newline at end of file diff --git a/engine/core/http.js b/engine/core/http.js new file mode 100644 index 0000000..d9f17d6 --- /dev/null +++ b/engine/core/http.js @@ -0,0 +1,21 @@ + + +var request = (type, url, progress, data) => { + return new Promise((resolve, reject) => { + var xhr = new XMLHttpRequest(); + xhr.addEventListener("progress", progress); + xhr.addEventListener("load", resolve); + xhr.addEventListener("error", reject); + xhr.open(type, url); + xhr.send(data); + }) +} + +var get = (url, async, progress) => { + return request("GET", url, progress, null); +} +var post = (url, async, progress, data) => { + return request("POST", url, progress, data); +} + +export {get, post, request}; \ No newline at end of file diff --git a/engine/graphics/glutil.js b/engine/graphics/glutil.js new file mode 100644 index 0000000..04908e7 --- /dev/null +++ b/engine/graphics/glutil.js @@ -0,0 +1,33 @@ +var createShader = (gl, type, src) => { + var shader = gl.createShader(type); + gl.shaderSource(shader, src); + gl.compileShader(shader); + if(!gl.getShaderParameter(shader, gl.COMPILE_STATUS)){ + console.log(gl.getShaderInfoLog(shader)); + return null;//or somthing else? + } +} + +var createProgram = (gl, vSrc, fSrc) => { + var program = gl.createProgram(); + gl.attachShader(createShader(gl.VERTEX_SHASDER, vSrc)); + gl.attachShader(createShader(gl.FRAGMENT_SHADER, fSrc)); + gl.linkProgram(); + if(!gl.getProgramParameter(program, gl.LINK_STATUS)) { + console.log("FAILED TO COMPILE SHADER"); + return null; + } + return program; +} + +var createTexture = () => { + +} + +var createBufferWithData = (gl, type, data, usage) => { + var buffer = gl.createBuffer(); + gl.bindBuffer(type, usage); + gl.bufferData(type, data, usage); + gl.bindBuffer(type, null); + return buffer; +} \ No newline at end of file diff --git a/example/public/assets/test.json b/example/public/assets/test.json new file mode 100644 index 0000000..cdb4ed4 --- /dev/null +++ b/example/public/assets/test.json @@ -0,0 +1 @@ +{"test":12} \ No newline at end of file diff --git a/example/public/index.html b/example/public/index.html index 7703eea..b6e111f 100644 --- a/example/public/index.html +++ b/example/public/index.html @@ -1,7 +1,17 @@
- + + diff --git a/example/public/js/TimerProcess.js b/example/public/js/TimerProcess.js new file mode 100644 index 0000000..6f043bb --- /dev/null +++ b/example/public/js/TimerProcess.js @@ -0,0 +1,19 @@ +import Process from "engine/core/Process" + +export default class TimerProcess extends Process { + constructor(delay, cb) { + super(); + this.cb = cb; + this.delay = delay; + this.reference = Date.now();//@TODO change this to some uniform game clock; + } + + update() { + var now = Date.now(); + var dif = now - this.reference; + if(dif > this.delay) { + this.reference += this.delay; + this.cb(); + } + } +} \ No newline at end of file diff --git a/example/public/main.js b/example/public/main.js index 3f32753..4909286 100644 --- a/example/public/main.js +++ b/example/public/main.js @@ -1,5 +1,50 @@ -import asad from "./js/test" -import Actor from "engine/core/Actor" -import Cache from "engine/core/Cache" +import asad from "./js/test"; +import Actor from "engine/core/Actor"; +import Cache from "engine/core/Cache"; +import {getEventManager} from "engine/core/EventManager"; +import Actor3D from "engine/core/Actor3D"; +import Scene3D from "engine/core/Scene3D"; +import EventEmitter from "events"; +import ProcessManager from "engine/core/ProcessManager"; +import TimerProcess from "./js/TimerProcess" -console.log(Actor, Cache) \ No newline at end of file + +eventManager = new EventEmitter(); +processManager = new ProcessManager(); +scene = new Scene3D(); +cache = new Cache(); + +//test actor +var actor = new Actor3D(); +eventManager.on("actor:move", (actor) => { + console.log("Actor : " + actor.id + " Moved!") +}) + + +//test cache +cache.get("assets/test.json").then((data)=>{ + console.log(data) +}, (err) => { + console.log(err) +}) + +//test processes +processManager.addChild(new TimerProcess(1000, ()=>{ + var pos = vec3.create(); + actor.getComponent("transform").position = pos; +})) + + + +//game loop thingy (not using req anim frame because reasons) + + +var last = Date.now(); +setInterval( () => { + var now = Date.now(); + var deltaMs = now - last; + last = now; + + + processManager.update(deltaMs); +}, 17) \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js index a88f741..7398ca2 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -5,6 +5,7 @@ var source = require('vinyl-source-stream'); var concat = require("gulp-concat"); var del = require("del"); var gls = require('gulp-live-server'); +var runSequence = require('run-sequence'); var ENGINE_SRC = ["engine/**/*.js", "!engine/lib/**/*.js"]; var GAME_SRC = ["example/public/**/*.js", "!example/public/bower_components/**/*.js", "!example/public/dist/**/*.js"]; @@ -15,21 +16,27 @@ var onerror = function (e) { this.emit("end"); } -gulp.task("js:harmony", ["js:clean"], function () { - - gulp.src(ENGINE_SRC) + +gulp.task("js:clean", function () { + return del(["tmp/"]); +}) + +gulp.task("js:engine", function () { + return gulp.src(ENGINE_SRC) .pipe(tracuer()) .on("error", onerror) .pipe(gulp.dest("tmp/node_modules/engine")) +}) - gulp.src(GAME_SRC) +gulp.task("js:game", function () { + return gulp.src(GAME_SRC) .pipe(tracuer()) .on("error", onerror) .pipe(gulp.dest("tmp/")) - }) -gulp.task("js:browserify", ["js:harmony"], function () { + +gulp.task("js:browserify", function () { return browserify("tmp/main.js") .bundle() .on("error", onerror) @@ -38,21 +45,20 @@ gulp.task("js:browserify", ["js:harmony"], function () { }) -gulp.task("js:clean", function () { - return del(["tmp/**/*.js"]); -}) - -gulp.task("js:build", ["js:browserify"], function () { - return gulp.src(["tmp/build/bundle.js", "engine/lib/**/*.js"]) +gulp.task("js:build", function () { + return gulp.src(["engine/lib/gl-matrix.js", "tmp/build/bundle.js"]) .pipe(concat("app.js")) .pipe(gulp.dest("example/public/dist/")) }) +gulp.task("js:all", function () { + runSequence("js:clean", ["js:engine", "js:game"], "js:browserify", "js:build"); +}) + //echo fs.inotify.max_user_watches=582222 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p -gulp.task("default", ["js:build"], function () { +gulp.task("default", ["js:all"], function () { var server = gls.new("server.js"); server.start(); - - gulp.watch(ENGINE_SRC, ["js:build"]); - gulp.watch(GAME_SRC, ["js:build"]); + gulp.watch(ENGINE_SRC, ["js:all"]); + gulp.watch(GAME_SRC, ["js:all"]); }) \ No newline at end of file diff --git a/package.json b/package.json index dfeacf2..f304b02 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "gulp-traceur": "~0.17.2", "browserify": "~13.0.0", "gulp-live-server": "0.0.29", - "express": "~4.13.4" + "express": "~4.13.4", + "run-sequence": "~1.1.5" } }