Skip to content

Commit

Permalink
Merge pull request #15 from mjneil/graphics
Browse files Browse the repository at this point in the history
Graphics
  • Loading branch information
Squeakrats committed Mar 5, 2016
2 parents 07da43a + 08531df commit dd737a0
Show file tree
Hide file tree
Showing 41 changed files with 580 additions and 290 deletions.
21 changes: 16 additions & 5 deletions cruft/core/Actor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ export default class Actor extends Emitter {
this.components = {};
this.children = {};
this.initialized = false;
this.destroyed = false;
}

addComponent(component) {
this.components[component.constructor.name] = component;
this.components[component.type] = component;
component.actor = this;
this.emit("addComponent", component);
}
Expand All @@ -38,6 +39,8 @@ export default class Actor extends Emitter {
}

child.parent = this;
child.emit("setParent", this);

this.children[child.guid] = child;

if(this.initialized) {
Expand All @@ -48,9 +51,14 @@ export default class Actor extends Emitter {
}

removeChild(child) {//TODO remove the listenr. RIP.
child.parent = null;
delete this.children[child.guid];
this.emit("removeChild", child);
if(child.parent === this){

child.parent = null;
child.emit("removeParent", this);

delete this.children[child.guid];
this.emit("removeChild", child);
}
}

initialize() {
Expand All @@ -59,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 @@ -93,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
1 change: 1 addition & 0 deletions cruft/core/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default class Component extends Emitter {
super();
this.guid = guid || uuid();
this.actor = null;
this.type = "Component";
}

initialize() {
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() {

}

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

export default class Input extends Emitter {

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

addController(controller) {
this.controllers.push(controller);
}

update() {
for(let controller of this.controllers) {
controller.update();
}
}

postUpdate() {
for(let controller of this.controllers) {
controller.postUpdate();
}
}
}
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.
}
}
49 changes: 49 additions & 0 deletions cruft/core/components/Transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Component from "../Component"

export default class Transform extends Component {
constructor() {
super();
this.type = "transform";
}

initialize() {

}

get position () {

}


set position (position) {

}

get rotation () {

}

set rotation (rotation) {

}

getWorldPosition() {

}

setDirection(vec) {

}

get scale() {

}

set scale(scale) {

}

updateMatrix() {

}
}
9 changes: 4 additions & 5 deletions cruft/core/components/Transform2D.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import Component from "../Component"
import Transform from "./Transform"
import {vec2, mat3} from "../../math/math";


var IDENTITY_MATRIX = new mat3();
//mat3.identity(IDENTITY_MATRIX)//DO NOT CHANGE THIS.

export default class Transform2D extends Component {
export default class Transform2D extends Transform {
constructor() {
super();

Expand Down Expand Up @@ -88,7 +87,7 @@ export default class Transform2D extends Component {
this.inverse.copy(matrix).invert();
//TODO z-index? What are we doing with that exactly.
if (parent) {
toWorld.copy(mat3.multiply(parent.getComponent("Transform2D").toWorld, matrix));
toWorld.copy(mat3.multiply(parent.getComponent("transform").toWorld, matrix));
} else {
toWorld.copy(matrix);
}
Expand All @@ -97,7 +96,7 @@ export default class Transform2D extends Component {

var children = this.actor.children;
for (let key in children) {
children[key].updateMatrix();
children[key].getComponent("transform").updateMatrix();
}
}

Expand Down
39 changes: 39 additions & 0 deletions cruft/core/controllers/Gamepad.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import vec2 from "../../math/vec2"
export default class Gamepad {

constructor(index) {
this.capture = {};
this.state = {};
this.index = index;
this.gamepad = navigator.getGamepads()[this.index];

this.axes = {
"horizontal" : 0,
"vertical" : 1
}
}

update() {
this.gamepad = navigator.getGamepads()[this.index];
}


postUpdate() {
//this.capture = {};
}

getAxis(name) {
return this.gamepad.axes[this.axes[name]];
}


getButtonDown(name) {
console.error("Method hasnt's been implemented");
}

getButton(name) {

}


}
88 changes: 88 additions & 0 deletions cruft/core/controllers/MouseKeyboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import vec2 from "../../math/vec2"
export default class MouseKeyboard {

constructor() {
this.capture = {};
this.state = {};

this.mousePosition = new vec2(0, 0);

this.axes = {
horizontal : { negative : "A", positive : "D"},
vertical : { negative : "S", positive : "W"}
};

this.buttons = {}

addEventListener("mousemove", (e) => {
this.mousePosition.x = e.pageX;
this.mousePosition.y = e.pageY;
})

addEventListener("keydown", (e) => {
this.state[e.which] = true;
this.capture[e.which] = true;
})

addEventListener("keyup", (e) => {
this.state[e.which] = false;
})

addEventListener("mousedown", (e) => {
this.state[1000+e.which] = true;
this.capture[1000+e.which] = true;
})

addEventListener("mouseup", (e) => {
this.state[1000+e.which] = false;
})
}

update() {

}


postUpdate() {
this.capture = {};
}

getAxis(name) {
var axis = this.axes[name];
if(!axis) return null;
var isPositive = this.getButton(axis.positive);
var isNegative = this.getButton(axis.negative);

if( !(isNegative || isPositive) || (isNegative && isPositive)) return 0;
if(isNegative) return -1;
if(isPositive) return 1;
}


getButtonDown(name) {
var vb = this.buttons[name];
if(vb){
return !!this.capture[vb];
}else{
return !!this.capture[name.charCodeAt(0)];
}
}

getButton(name) {
var vb = this.buttons[name]
if(vb){
return this.state[vb];
}else{
return !!this.state[name.charCodeAt(0)];
}
}

getMouseButton(which) {
return !!this.state[1000+which];
}

getMouseButtonDown() {
return !!this.capture[1000+which];
}

}
Loading

0 comments on commit dd737a0

Please sign in to comment.