-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRisingTableau.js
156 lines (125 loc) · 3.82 KB
/
RisingTableau.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Copyright 2011 Brandon Lockaby
This file is part of JSPDP.
https://github.com/brandon-lockaby/JSPDP
JSPDP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
JSPDP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with JSPDP. If not, see <http://www.gnu.org/licenses/>.
*/
JSPDP.RisingTableau = function() {
}
var proto = (JSPDP.RisingTableau.prototype = new JSPDP.Tableau());
proto.toppedOut = false;
proto.init = function(width, height) {
JSPDP.Tableau.prototype.init.call(this, width, height);
this.onRise = new JSPDP.Event();
this.onRow = new JSPDP.Event();
this.onTopout = new JSPDP.Event();
this.onCombo.subscribe(this.handleCombo.bind(this));
this.generator = new JSPDP.Generator().init(width);
var rows = this.generator.generateFieldTA(5); // todo
for(var row = 0; row < rows.length; row++) {
for(var col = 0; col < this.dimensions.width; col++) {
if(rows[row][col] != undefined) {
var panel = new JSPDP.Panel();
panel.color = rows[row][col];
panel.type = 1;
this.setPanel(row, col, panel);
}
}
}
this.generator.generateRow(5); // todo: radix
return this;
}
proto.stopTicks = 0;
proto.riseSpeed = (1 / 60) / 10; // todo
proto.riseOffset = 0;
proto.liftSpeed = 1 / 16; // todo
proto.liftJuice = 0; // todo
proto.runTick = function() {
if(this.active) {
this.liftJuice = 0; // todo: this will be optional
}
if(this.liftJuice > 0) {
this.stopTicks = 0;
}
if(this.stopTicks > 0) {
--this.stopTicks;
} else if(!this.active && !this.toppedOut) {
if(this.liftJuice > 0) {
this.riseOffset += this.liftSpeed;
this.liftJuice -= 1 / 16;
if(this.liftJuice < 0) this.liftJuice = 0;
} else {
this.riseOffset += this.riseSpeed;
}
while(this.riseOffset >= 1) {
this.riseOffset -= 1;
// shift everything up a row
for(var row = this.dimensions.height - 1; row > 0; row--) {
for(var col = 0; col < this.dimensions.width; col++) {
var panel = this.getPanel(row, col);
this.setPanel(row, col, this.getPanel(row - 1, col));
this.setPanel(row - 1, col, panel);
}
}
// add panels from generated row
for(var i = 0; i < this.dimensions.width; i++) {
var panel = new JSPDP.Panel();
panel.type = 1;
panel.color = this.generator.current[i];
this.setPanel(0, i, panel);
}
this.needsCheckMatches = true;
this.generator.generateRow(5); // todo: radix
this.liftJuice = 0;
var top_row = this.panels[this.panels.length - 1];
for(var i = 0; i < top_row.length; i++) {
if(!top_row[i].isAir()) {
this.toppedOut = true;
this.riseOffset = 0;
break;
}
}
this.onRow.fire();
}
this.onRise.fire();
}
JSPDP.Tableau.prototype.runTick.call(this);
if(this.toppedOut) {
this.toppedOut = false;
var top_row = this.panels[this.panels.length - 1];
for(var i = 0; i < top_row.length; i++) {
if(!top_row[i].isAir()) {
this.toppedOut = true;
break;
}
}
}
if(this.toppedOut && !this.stopTicks && !this.active) {
this.onTopout.fire();
console.log("topout");
}
};
proto.handleCombo = function(combo) {
// add stoptime
var panel = combo[0];
if(panel.comboSize > 3 || panel.chainIndex > 1) {
this.stopTicks += 60 * (this.stopTicks ? 1 : 5); // todo: more correct stop time
if(this.stopTicks > (60 * 99)) {
this.stopTicks = 60 * 99;
}
}
};
proto.lift = function() {
if(!this.liftJuice) {
this.liftJuice = 1;
}
};