Skip to content

Commit

Permalink
add in templates for core classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Squeakrats committed Feb 7, 2016
1 parent 186abc6 commit 9afbcf7
Show file tree
Hide file tree
Showing 17 changed files with 334 additions and 25 deletions.
34 changes: 33 additions & 1 deletion engine/core/Actor.js
Original file line number Diff line number Diff line change
@@ -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() {

}


}
9 changes: 9 additions & 0 deletions engine/core/Actor3D.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Actor from "./Actor";
import Transform3D from "./Transform3D";

export default class Actor3D extends Actor {
constructor() {
super();
this.addComponent(new Transform3D());
}
}
9 changes: 7 additions & 2 deletions engine/core/Cache.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Pipline from "./Pipeline";
import http from "http";
import * as http from "engine/core/http";

export default class Cache {
constructor() {
Expand All @@ -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);
})

})
}
Expand Down
10 changes: 10 additions & 0 deletions engine/core/Component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default class Component {
constructor(type) {
this.type = type;
this.actor = null;
}

setActor(actor) {
this.actor = actor;
}
}
Empty file added engine/core/EventManager.js
Empty file.
36 changes: 36 additions & 0 deletions engine/core/Process.js
Original file line number Diff line number Diff line change
@@ -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}
26 changes: 26 additions & 0 deletions engine/core/ProcessManager.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
13 changes: 13 additions & 0 deletions engine/core/Scene3D.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
42 changes: 42 additions & 0 deletions engine/core/Transform3D.js
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
21 changes: 21 additions & 0 deletions engine/core/http.js
Original file line number Diff line number Diff line change
@@ -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};
33 changes: 33 additions & 0 deletions engine/graphics/glutil.js
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions example/public/assets/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"test":12}
12 changes: 11 additions & 1 deletion example/public/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
<html>
<head>
<script src="/bower_components/traceur/traceur.js"></script>
<script src="dist/app.js"></script>
<script>

//idk how to global variables?
var processManager = null;
var eventManager = null;
var game = null;
var scene = null;
var cache = null;

</script>
<script src="dist/app.js"></script>
</head>
<body>
</body>
Expand Down
19 changes: 19 additions & 0 deletions example/public/js/TimerProcess.js
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
53 changes: 49 additions & 4 deletions example/public/main.js
Original file line number Diff line number Diff line change
@@ -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)

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)
Loading

0 comments on commit 9afbcf7

Please sign in to comment.