Skip to content

Commit 3301a07

Browse files
committed
Camera implementation
1 parent 03242d3 commit 3301a07

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

v3/src/camera/Camera-2.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var Transform = require('../components/experimental-Transform-2');
2+
3+
var Camera = function (x, y, width, height)
4+
{
5+
this.x = x;
6+
this.y = y;
7+
this.width = width;
8+
this.height = height;
9+
this.state = null;
10+
this.statePositionX = 0.0;
11+
this.statePositionY = 0.0;
12+
};
13+
14+
Camera.prototype.setState = function (state)
15+
{
16+
this.state = state;
17+
Transform.updateRoot(this.state.transform);
18+
};
19+
20+
Camera.prototype.preRender = function (interpolation, renderer)
21+
{
22+
var state = this.state;
23+
var stateTransform = state.transform;
24+
25+
this.statePositionX = stateTransform.positionX;
26+
this.statePositionY = stateTransform.positionY;
27+
28+
stateTransform.positionX = this.x;
29+
stateTransform.positionY = this.y;
30+
31+
Transform.updateRoot(stateTransform);
32+
};
33+
34+
Camera.prototype.postRender = function ()
35+
{
36+
var stateTransform = this.state.transform;
37+
stateTransform.positionX = this.statePositionX;
38+
stateTransform.positionY = this.statePositionY;
39+
};
40+
41+
module.exports = Camera;

v3/src/state/Systems.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var Component = require('../components');
1313
// var Camera = require('../camera/Camera');
1414
var Settings = require('./Settings');
1515
var RTree = require('../structs/RTree');
16-
var Transform = require('../components/experimental-Transform-2')
16+
var Camera = require('../camera/Camera-2')
1717

1818
var Systems = function (state, config)
1919
{
@@ -39,7 +39,7 @@ var Systems = function (state, config)
3939
this.tree;
4040

4141
// State properties
42-
// this.camera;
42+
this.camera;
4343
this.children;
4444
this.color;
4545
this.data;
@@ -79,7 +79,7 @@ Systems.prototype = {
7979
this.color = new Component.Color(this.state);
8080
this.data = new Component.Data(this.state);
8181
this.transform = new Component.Transform(this.state);
82-
82+
this.camera = new Camera(0, 0, this.game.config.width, this.game.config.height);
8383
this.inject();
8484
},
8585

@@ -103,6 +103,8 @@ Systems.prototype = {
103103
this.state.state = this.game.state;
104104
this.state.cache = this.game.cache;
105105
this.state.textures = this.game.textures;
106+
107+
this.camera.setState(this.state);
106108
},
107109

108110
// Called just once per frame, regardless of speed
@@ -129,9 +131,10 @@ Systems.prototype = {
129131
render: function (interpolation, renderer)
130132
{
131133
var transform = this.transform;
132-
Transform.updateRoot(transform);
134+
this.camera.preRender();
133135
renderer.render(this.state, transform.flatRenderArray, interpolation);
134136
this.state.render(interpolation);
137+
this.camera.postRender();
135138
},
136139

137140
// Called just once per frame, regardless of speed

0 commit comments

Comments
 (0)