Skip to content

Commit

Permalink
adding Input, removing initialize method
Browse files Browse the repository at this point in the history
  • Loading branch information
Squeakrats committed Mar 5, 2016
1 parent d4e14e8 commit ff5c86e
Show file tree
Hide file tree
Showing 23 changed files with 268 additions and 304 deletions.
6 changes: 5 additions & 1 deletion cruft/core/Actor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default class Actor extends Emitter {
this.components = {};
this.children = {};
this.initialized = false;
this.destroyed = false;
}

addComponent(component) {
Expand Down Expand Up @@ -66,6 +67,8 @@ export default class Actor extends Emitter {
this.components[name].initialize();
}

this.initialized = true;

for(let id in this.children) {
this.children[id].initialize();
}
Expand Down Expand Up @@ -100,7 +103,8 @@ export default class Actor extends Emitter {
}
this.children = null;
}


this.destroyed = true;
this.emit("destroy", this);
}
}
7 changes: 5 additions & 2 deletions cruft/core/ActorFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ export default class ActorFactory { //def move this to core at some point
this.creators = {};
}

register(type, creator) {
this.creators[type] = creator;
register(creators) {
for(var name in creators) {
this.creators[name] = creators[name];
}
}

create(type, config) {//right now can only have config code for things in a skeleton
Expand All @@ -16,6 +18,7 @@ export default class ActorFactory { //def move this to core at some point


var creator = this.creators[type];

if(!creator){
console.error(`Creator ${type} not found.`);
return null;
Expand Down
11 changes: 11 additions & 0 deletions cruft/core/Controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default class Controller {

constructor() {

}

update() {

}

}
14 changes: 14 additions & 0 deletions cruft/core/Input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Emitter from "./Emitter";

class Input extends Emitter {

constructor() {
this.controllers = [];
}

update() {
for(let controller of this.controllers){
controller.update();
}
}
}
14 changes: 0 additions & 14 deletions cruft/core/Scheduler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import setIntervalMs from "../util/setIntervalMs";
import Process from "./Process";
import {UNINITIALIZED, RUNNING, FAILED, SUCCEEDED} from "./Process";//can you import default and named at same time?

Expand All @@ -12,19 +11,6 @@ export default class Scheduler {
this.processes.push(proc);
}

start(delay) {
if(this.interval) return;

this.interval = setIntervalMs((now, deltaMs)=>{
this.update(now, deltaMs);//maybe pass now?
}, delay)
}

kill() {
clearInterval(this.interval);
this.interval = null;
}

update(now, deltaMs){
var add = [];

Expand Down
9 changes: 9 additions & 0 deletions cruft/core/View.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default class View {
constructor() {

}

update() {
//render stuff.
}
}
91 changes: 47 additions & 44 deletions cruft/engine.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Emitter from "./core/Emitter";

import Emitter from "./core/Emitter";
import setIntervalMs from "./util/setIntervalMs";
import Actor from "./core/Actor";
import Cache from "./net/Cache";
import {ImageLoader, TextLoader, JsonLoader } from "./net/loaders/loaders.js"
import * as loaders from "./net/loaders/loaders.js"
import Factory from "./core/ActorFactory";
import Network from "./net/Network";
import Scheduler from "./core/Scheduler";
Expand All @@ -11,65 +12,67 @@ import MemoryManager from "./core/MemoryManager"


export class Engine extends Emitter {

constructor() {
super()
this.views = {};
this.scene = null;
this.camera = null;
this.interval = null;
}
}

var engine = new Engine();
var cache = new Cache();
var factory = new Factory();
var network = new Network();
var scheduler = new Scheduler();
var memory = new MemoryManager();
setScene(scene) {
if(this.scene) this.scene.destroy(true);
this.scene = scene;
scene.initialize();
}

addView(name, view) {
this.views[name] = view;
}

var initialize = (config) => {
start(delay) {
if(this.interval) return;

var promises = [];

scheduler.addChild(new Script((now, deltaMs) => {
engine.scene.update(now, deltaMs);
}));
this.interval = setIntervalMs((now, deltaMs)=>{
if(engine.scene){
engine.scene.update(now, deltaMs);
}

cache.register("image", new ImageLoader() );
cache.register("text", new TextLoader() );
cache.register("json", new JsonLoader() );
cache.register("default", cache.loaders.text );//I dont like making TextLoader Twice
scheduler.update(now, deltaMs);

if(config.cache) {
var cacheConfig = config.cache;
for(var key in cacheConfig) {
cache.register(key, cacheConfig[key]);
}
}
//update views
for(let name in this.views){
this.views[name].update(now, deltaMs);
}

if(config.factory) {
let creators = config.factory;
for(let name in creators){
factory.register(name, creators[name]);
}

}, delay)
}

if(config.network){
promises.push(network.initialize(config.network.name, config.network.options));
if(config.network.peer){
promises.push(network.createSession(config.network.peer))
}
kill() {
clearInterval(this.interval);
this.interval = null;
}

engine.scene = instantiate(config.scene || null);
memory.add(engine.scene);

return Promise.all(promises).then(()=>{
engine.scene.initialize();
scheduler.start(config.scheduler || 17);
});

}


var engine = new Engine();

var cache = new Cache();

cache.register(loaders);
cache.register({ default : cache.loaders.text });

var factory = new Factory();
var network = new Network();
var scheduler = new Scheduler();
var memory = new MemoryManager();



var instantiate = (type, config) => {
var actor = factory.create(type, config);
memory.add(actor);
Expand All @@ -78,7 +81,7 @@ var instantiate = (type, config) => {


export default engine;
export {cache, factory, network, scheduler, memory, initialize, instantiate };
export {cache, factory, network, scheduler, memory, instantiate };

//****DEBUG****//
window.engine = engine;
Expand Down
14 changes: 14 additions & 0 deletions cruft/graphics/Camera.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Actor from "../core/Actor";

export default class Camera extends Actor {

constructor() {
super();
}

mouseToWorld(mouse) {
console.warn("mouseToWorld has not been implemented");
return null;
}

}
75 changes: 0 additions & 75 deletions cruft/graphics/Camera2D.js

This file was deleted.

12 changes: 6 additions & 6 deletions cruft/graphics/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import ProgramManager from "./ProgramManager"


export default class Renderer {//todo pass in options for what to enable/disable.
constructor(width, height) {
this.canvas = document.createElement("canvas" , {preserveDrawingBuffer:true});
this.canvas.width = width;
this.canvas.height = height;
constructor({canvas}) {

this.canvas = canvas;

this.plugins = {};

var gl = this.gl = this.canvas.getContext("webgl");
gl.viewport(0, 0, width, height);
var gl = this.gl = this.canvas.getContext("webgl", { preserveDrawingBuffer : true });
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clearColor(0, 0, 0, 1);
gl.disable(gl.DEPTH_TEST)
gl.enable(gl.BLEND);
Expand Down
31 changes: 31 additions & 0 deletions cruft/graphics/cameras/Camera2D.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Camera from "../Camera";
import Transform2D from "../../core/components/Transform2D";
import {vec2} from "../../math/math";

//technically this should maybe be a component? Gah ask matt.

export default class Camera2D extends Camera {

constructor(width, height) {
super();

this.width = width;
this.height = height;

this.addComponent(new Transform2D());
this.getComponent("transform").scale = new vec2(width/2, height/2)
}

mouseToWorld(mouse) {

var transform = this.getComponent("transform");
var position = transform.position;
var scale = transform.scale;

var tmp = new vec2( mouse.x * this.width/2, mouse.y * this.height/2 );
tmp.add(position);

return tmp;
}

}
7 changes: 7 additions & 0 deletions cruft/math/ray2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default class ray2 {
constructor(origin, direction) {
this.origin = origin;
this.direction = direction;
}

}
Loading

0 comments on commit ff5c86e

Please sign in to comment.