forked from micahcowan/tj16
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefs.js
103 lines (91 loc) · 2.93 KB
/
defs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"use strict";
(function(){
var G = (window.FireGame = window.FireGame || {});
var U = MajicUnits;
var MG = MajicGame;
var Bh = MG.behavior;
G.Camera = function(x, y) {
var mg = G.game;
this.x = x;
this.y = y;
function _getAsPx(px) {
return px instanceof U.UnitValue? px.mul( 1 ) : U.pixels( px ).relax();
}
Object.defineProperty(this, 'pos', {
get: function() {
return { x: this.x.mul(1), y: this.y.mul(1) };
}
});
this.getTL = function() {
return {
x: this.x.sub( mg.center.x ).relax()
, y: this.y.sub( mg.center.y ).relax()
};
};
this.getBR = function() {
return {
x: this.x.add( mg.center.x ).relax()
, y: this.y.add( mg.center.y ).relax()
};
};
this.toCamX = function(x) {
x = _getAsPx(x);
return x.sub( this.getTL().x ).relax();
}
this.toCamY = function(y) {
// Convert to U.pixels if necessary
y = _getAsPx(y);
return y.sub( this.getTL().y ).relax();
}
this.toGameX = function(x) {
x = _getAsPx(x);
return x.add( this.getTL().x ).relax();
}
this.toGameY = function(y) {
// Convert to U.pixels if necessary
y = _getAsPx(y);
return y.add( this.getTL().y ).relax();
}
function boundsCheck() {
// Adjust for out-of-bounds
var tl = this.getTL();
var br = this.getBR();
var tDiff = tl.y
var lDiff = tl.x
var bDiff = br.y.sub( G.state.area.h ).relax();
var rDiff = br.x.sub( G.state.area.w ).relax();
if (bDiff > 0) {
this.y = this.y.sub(bDiff);
this.v = U.pixels( 0 ).per.second;
}
if (rDiff > 0) {
this.x = this.x.sub(rDiff);
this.h = U.pixels( 0 ).per.second;
}
if (tDiff < 0) {
this.y = this.y.sub(tDiff);
this.v = U.pixels( 0 ).per.second;
}
if (lDiff < 0) {
this.x = this.x.sub(lDiff);
this.h = U.pixels( 0 ).per.second;
}
};
this.behavior = [
Bh.momentum
, Bh.thrustKeys(
{
forward: ['w', 'ArrowUp']
, back: ['s', 'ArrowDown']
, left: ['a', 'ArrowLeft']
, right: ['d', 'ArrowRight']
}
, U.pixels( 300 ).per.second.per.second
)
, Bh.friction( U.pixels( 100 ).per.second.per.second )
, Bh.speedLimited( U.pixels( 240 ).per.second )
, boundsCheck // (above)
];
};
G.Camera.prototype = MG.spritePrototype;
})();