Skip to content

Commit

Permalink
factor out glmatrix in camera2d
Browse files Browse the repository at this point in the history
  • Loading branch information
mjneil committed Feb 28, 2016
1 parent 51f47c3 commit 12af534
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions cruft/graphics/Camera2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Actor from "engine/core/Actor";
import Renderer from "engine/graphics/Renderer"
import SpriteRenderer from "engine/graphics/plugins/SpriteRenderer";
import Transform2D from "engine/components/Transform2D";
import {vec2} from "engine/lib/gl-matrix";
import {vec2} from "../math/math";
import engine, {scheduler} from "engine/engine";
import Script from "engine/processes/Script";

Expand All @@ -15,7 +15,7 @@ export default class Camera2D extends Actor {//todo set/get widht//height
this._renderer.registerPlugin(new SpriteRenderer());
document.body.appendChild(this._renderer.canvas);//not sure how want to manage access to the dom right now. so tmp here.
this.addComponent(new Transform2D()); //think about using a height variable or somthing? :/ Hmm idk.
this.getComponent("transform").scale = [width/2, height/2] //might want to make variables to like set zoom and stuff idk.
this.getComponent("transform").scale = new vec2(width/2, height/2) //might want to make variables to like set zoom and stuff idk.

//todo track that script so we can delete it.
//also its not in update because I want it to be the last thing that happens
Expand All @@ -40,12 +40,11 @@ export default class Camera2D extends Actor {//todo set/get widht//height
var position = transform.position;
var scale = transform.scale;

var tmp = vec2.create();
tmp[0] = (mouse[0] - window.innerWidth/2);
tmp[1] = (window.innerHeight - mouse[1] - window.innerHeight/2);
var tmp = vec2.zero();
tmp.x = (mouse.x - window.innerWidth/2);
tmp.y = (window.innerHeight - mouse.y - window.innerHeight/2);

tmp[0] += position[0];
tmp[1] += position[1];
tmp.add(position);

return tmp;
}
Expand All @@ -56,14 +55,13 @@ export default class Camera2D extends Actor {//todo set/get widht//height
var transform = this.getComponent("transform");
var world = target.getComponent("transform").getWorldPosition();
var selfWorld = transform.getWorldPosition();
var dif = vec2.create();
vec2.sub(dif, world, selfWorld);
var len = vec2.len(dif);
var scale = (len < 900)? .002:.05;
vec2.scale(dif, dif, .07);
var dif = vec2.subtract(world, selfWorld);
var len = dif.length();
var scale = (len < 900) ? 0.002 : 0.05;
dif.scale(0.07);

var pos = transform.position;
vec2.add(pos, pos, dif);
pos.add(dif)
transform.position = pos;


Expand Down

0 comments on commit 12af534

Please sign in to comment.