forked from jakesgordon/javascript-tetris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapBaseView.js
More file actions
71 lines (65 loc) · 2.07 KB
/
MapBaseView.js
File metadata and controls
71 lines (65 loc) · 2.07 KB
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
/**
* Created with JetBrains PhpStorm.
* User: pdietrich
* Date: 10.08.12
* Time: 12:07
* To change this template use File | Settings | File Templates.
*/
var MapBaseView = Backbone.View.extend({
optionDefaults: {
size: 12,
border: 1,
cpsActive: true
},
initialize: function (options) {
_.bindAll(this,"updateFieldSize","getRowColFromXY","getRowFromY","getColFromX","getXYFromRowCol","getXFromCol","getYFromRow","getFieldAtXY","setFieldAtXY","setFieldAtRowCol");
_.defaults(options,this.optionDefaults);
this.options = options;
this.mapViewSettings = new Backbone.Model(options);
this.mapViewSettings.bind("change:size change:border",this.updateFieldSize)
this.updateFieldSize();
},
updateFieldSize: function() {
this.fieldSize = (this.mapViewSettings.get("size") + this.mapViewSettings.get("border"));
//console.log("New fieldsize",this.fieldSize);
},
getRowColFromXY: function (x, y) {
return {
"r": this.getRowFromY(y),
"c": this.getColFromX(x)
}
},
getRowFromY: function (y) {
return Math.floor(y / this.fieldSize);
},
getColFromX: function (x) {
return Math.floor(x / this.fieldSize);
},
getXYFromRowCol: function (r, c) {
return {
"x": this.getXFromCol(c),
"y": this.getYFromRow(r)
}
},
getXFromCol: function(c) {
return (c +.5) * this.fieldSize;
},
getYFromRow: function(r) {
return (r +.5) * this.fieldSize;
},
getFieldAtXY: function (x, y) {
alert("Deprecated");
var rc = this.getRowColFromXY(x, y);
return this.model.getFieldAtRowCol(rc.r,rc.c);
},
setFieldAtXY: function (x, y, field) {
var rc = this.getRowColFromXY(x, y);
var old = this.getFieldAtRowCol(rc.r, rc.c);
if (old != field) {
this.setFieldAtRowCol(rc.r, rc.c, field);
}
},
setFieldAtRowCol: function (r, c, field) {
this.model.setFieldAtRowCol(r, c, field);
}
});