2
2
* House of cards
3
3
*/
4
4
5
- //var CARD_HEIGHT = 70;
6
- //var CARD_WIDTH = 50;
7
- var CARD_HEIGHT = 120 ;
8
- var CARD_WIDTH = 80 ;
5
+ var CARD_HEIGHT = 100 ;
6
+ var CARD_WIDTH = 60 ;
7
+ var CARD_COUNT = 40 ;
9
8
10
9
var WIDTH = 800 ;
11
10
var HEIGHT = 600 ;
@@ -17,22 +16,17 @@ var TILTED_CARD_HEIGHT = Math.sin(PYTH_ANGLE) * CARD_HEIGHT;
17
16
var TILTED_CARD_WIDTH = Math . cos ( PYTH_ANGLE ) * CARD_HEIGHT ;
18
17
var PYRAMID_WIDTH = TILTED_CARD_WIDTH * 2 ;
19
18
20
- var SPADES = '♠' ;
21
- var HEARTS = '♥' ;
22
- var DIAMONDS = '♦' ;
23
- var CLUBS = '♣' ;
24
- var suits = [ SPADES , CLUBS , HEARTS , DIAMONDS ] ;
25
- //var colors = ['black', 'black', 'red', 'red'];
26
-
27
- var colors = randomColor ( { count : 52 } ) ;
19
+ var COLORS = randomColor ( { count : 40 } ) ;
28
20
29
21
var PILE = 1 ;
30
22
var HOUSE = 2 ;
31
- var current_mode = HOUSE ;
23
+ var WALL = 3 ;
24
+ var current_mode = PILE ;
32
25
33
26
var formation_builders = { } ;
34
- formation_builders [ PILE ] = centered_pile_positions ;
27
+ formation_builders [ PILE ] = pile_positions ;
35
28
formation_builders [ HOUSE ] = centered_house_positions ;
29
+ formation_builders [ WALL ] = wall_positions ;
36
30
37
31
function create_card ( container , index ) {
38
32
var card = document . createElement ( 'div' ) ;
@@ -42,13 +36,11 @@ function create_card(container, index) {
42
36
43
37
var front = document . createElement ( 'div' ) ;
44
38
front . className = 'front' ;
45
- var xoffset = index % 4 ;
46
- var yoffset = Math . floor ( index / 4 ) ;
47
- front . style . background = colors [ index % colors . length ] ;
39
+ front . style . background = COLORS [ index % COLORS . length ] ;
48
40
49
41
var back = document . createElement ( 'div' ) ;
50
42
back . className = 'back' ;
51
- back . style . background = colors [ index % colors . length ] ;
43
+ back . style . background = COLORS [ index % COLORS . length ] ;
52
44
53
45
card . appendChild ( front ) ;
54
46
card . appendChild ( back ) ;
@@ -61,7 +53,7 @@ var Deck = (function() {
61
53
this . cards = [ ] ;
62
54
this . card_index = [ ] ;
63
55
64
- for ( var i = 0 ; i < 52 ; ++ i ) {
56
+ for ( var i = 0 ; i < CARD_COUNT ; ++ i ) {
65
57
var container = document . getElementById ( 'surface' ) ;
66
58
this . cards . push ( create_card ( container , i ) ) ;
67
59
}
@@ -91,7 +83,7 @@ function build_formation(positions) {
91
83
position : positions [ i ] . position ,
92
84
rotation : positions [ i ] . rotation ,
93
85
easing : 'cos' ,
94
- perspective : 2000 ,
86
+ // perspective: 2000,
95
87
delay : i * 50
96
88
} ) ;
97
89
}
@@ -101,17 +93,15 @@ function set_mode(mode) {
101
93
if ( mode == current_mode ) {
102
94
return ;
103
95
}
104
- from_positions = formation_builders [ current_mode ] ( ) ;
96
+
105
97
positions = formation_builders [ mode ] ( ) ;
98
+ from_positions = formation_builders [ current_mode ] ( ) ;
99
+
106
100
for ( var i = 0 ; i < positions . length ; ++ i ) {
107
- if ( from_positions [ i ] ) {
108
- positions [ i ] . from_position = from_positions [ i ] . position ;
109
- positions [ i ] . from_rotation = from_positions [ i ] . rotation ;
110
- } else {
111
- positions [ i ] . from_position = positions [ i ] . position ;
112
- positions [ i ] . from_rotation = positions [ i ] . rotation ;
113
- }
101
+ positions [ i ] . from_position = from_positions [ i ] . position ;
102
+ positions [ i ] . from_rotation = from_positions [ i ] . rotation ;
114
103
}
104
+
115
105
build_formation ( positions ) ;
116
106
current_mode = mode ;
117
107
}
@@ -120,7 +110,8 @@ function rotate_container() {
120
110
var container = document . getElementById ( 'surface' ) ;
121
111
snabbt ( container , {
122
112
rotation : [ 0 , 2 * Math . PI , 0 ] ,
123
- duration : 4000
113
+ duration : 10000 ,
114
+ loop : Infinity
124
115
} ) ;
125
116
}
126
117
@@ -129,10 +120,10 @@ function pile_positions() {
129
120
Deck . reset ( ) ;
130
121
var i = 0 ;
131
122
var card = Deck . next_card ( ) ;
132
- var center = ( HEIGHT - CARD_WIDTH ) / 2 ;
123
+ var center = ( WIDTH - CARD_WIDTH ) / 2 ;
133
124
while ( card ) {
134
125
positions . push ( {
135
- position : [ center , BOTTOM + i * 0.2 , 100 ] ,
126
+ position : [ center , BOTTOM - i * 0.5 , 300 ] ,
136
127
rotation : [ Math . PI / 2 , 0 , 0 ] ,
137
128
} ) ;
138
129
++ i ;
@@ -146,7 +137,7 @@ function house_positions(floors, x, y, z) {
146
137
var positions = [ ] ;
147
138
var i ;
148
139
for ( i = 0 ; i < floors ; ++ i ) {
149
- positions = positions . concat ( house_row_positions ( floors - i , x + i * TILTED_CARD_WIDTH , y - i * CARD_HEIGHT , z ) ) ;
140
+ positions = positions . concat ( house_row_positions ( floors - i , x + i * TILTED_CARD_WIDTH , y - i * TILTED_CARD_HEIGHT , z ) ) ;
150
141
}
151
142
152
143
return positions ;
@@ -155,6 +146,7 @@ function house_positions(floors, x, y, z) {
155
146
function house_row_positions ( count , x , y , z ) {
156
147
var positions = [ ] ;
157
148
var i ;
149
+ // Tilted cards
158
150
for ( i = 0 ; i < count ; ++ i ) {
159
151
card_positions = pyramid_postions ( x + i * PYRAMID_WIDTH , y , z ) ;
160
152
positions . push ( {
@@ -166,6 +158,7 @@ function house_row_positions(count, x, y, z) {
166
158
rotation : card_positions [ 1 ] . rotation ,
167
159
} ) ;
168
160
}
161
+ // Bridge cards
169
162
for ( i = 0 ; i < count - 1 ; ++ i ) {
170
163
positions . push ( {
171
164
position : [ x + i * PYRAMID_WIDTH + TILTED_CARD_WIDTH , y - TILTED_CARD_HEIGHT / 2 , z ] ,
@@ -187,6 +180,25 @@ function pyramid_postions(x, y, z) {
187
180
} ] ;
188
181
}
189
182
183
+ function wall_positions ( ) {
184
+ var positions = [ ] ;
185
+ var start_x = ( WIDTH - 10 * CARD_WIDTH ) / 2 ;
186
+ var start_y = ( HEIGHT - 4 * CARD_HEIGHT ) / 2 ;
187
+ for ( var i = 0 ; i < CARD_COUNT ; ++ i ) {
188
+ var x = ( i % 10 ) * CARD_WIDTH + start_x ;
189
+ var y = ( Math . floor ( i / 10 ) ) * CARD_HEIGHT + start_y ;
190
+ positions . push ( {
191
+ position : [ x , y , 0 ] ,
192
+ rotation : [ 0 , 0 , 0 ]
193
+ } ) ;
194
+ }
195
+ return positions ;
196
+ }
197
+
198
+ function build_wall ( ) {
199
+ set_mode ( WALL ) ;
200
+ }
201
+
190
202
function build_house ( ) {
191
203
set_mode ( HOUSE ) ;
192
204
}
@@ -195,17 +207,32 @@ function build_pile() {
195
207
set_mode ( PILE ) ;
196
208
}
197
209
198
- function centered_pile_positions ( ) {
199
- // TODO: Actually center
200
- return pile_positions ( ) ;
201
- }
202
210
function centered_house_positions ( ) {
203
211
// TODO: Actually center
204
212
return house_positions ( 5 , 200 , BOTTOM , - 300 ) ;
205
213
}
206
214
207
- $ ( function ( ) {
215
+ function init ( ) {
208
216
Deck . reset ( ) ;
209
- build_pile ( ) ;
210
- //build_house(5, 200, 500);
211
- } ) ;
217
+ build_wall ( ) ;
218
+ rotate_container ( ) ;
219
+
220
+ // Event handlers
221
+ var buttons = {
222
+ "pile_button" : PILE ,
223
+ "house_button" : HOUSE ,
224
+ "wall_button" : WALL
225
+ } ;
226
+
227
+ var click_handler = function ( key ) {
228
+ document . getElementById ( 'pile_button' ) . className = '' ;
229
+ document . getElementById ( 'house_button' ) . className = '' ;
230
+ document . getElementById ( 'wall_button' ) . className = '' ;
231
+ document . getElementById ( key ) . className = 'button-primary' ;
232
+ set_mode ( buttons [ key ] ) ;
233
+ } ;
234
+
235
+ for ( var key in buttons ) {
236
+ document . getElementById ( key ) . addEventListener ( 'click' , click_handler . bind ( undefined , key ) ) ;
237
+ }
238
+ }
0 commit comments