|
| 1 | +/** |
| 2 | + * House of cards |
| 3 | + */ |
| 4 | + |
| 5 | +//var CARD_HEIGHT = 70; |
| 6 | +//var CARD_WIDTH = 50; |
| 7 | +var CARD_HEIGHT = 120; |
| 8 | +var CARD_WIDTH = 80; |
| 9 | + |
| 10 | +var WIDTH = 800; |
| 11 | +var HEIGHT = 600; |
| 12 | +var BOTTOM = 400; |
| 13 | + |
| 14 | +var TILT = Math.PI/8; |
| 15 | +var PYTH_ANGLE = Math.PI/2 - TILT; |
| 16 | +var TILTED_CARD_HEIGHT = Math.sin(PYTH_ANGLE) * CARD_HEIGHT; |
| 17 | +var TILTED_CARD_WIDTH = Math.cos(PYTH_ANGLE) * CARD_HEIGHT; |
| 18 | +var PYRAMID_WIDTH = TILTED_CARD_WIDTH * 2; |
| 19 | + |
| 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}); |
| 28 | + |
| 29 | +var PILE = 1; |
| 30 | +var HOUSE = 2; |
| 31 | +var current_mode = HOUSE; |
| 32 | + |
| 33 | +var formation_builders = {}; |
| 34 | +formation_builders[PILE] = centered_pile_positions; |
| 35 | +formation_builders[HOUSE] = centered_house_positions; |
| 36 | + |
| 37 | +function create_card(container, index) { |
| 38 | + var card = document.createElement('div'); |
| 39 | + card.className = 'card'; |
| 40 | + card.style.height = CARD_HEIGHT + 'px'; |
| 41 | + card.style.width = CARD_WIDTH + 'px'; |
| 42 | + |
| 43 | + var front = document.createElement('div'); |
| 44 | + front.className = 'front'; |
| 45 | + var xoffset = index % 4; |
| 46 | + var yoffset = Math.floor(index / 4); |
| 47 | + front.style.background = colors[index % colors.length]; |
| 48 | + |
| 49 | + var back = document.createElement('div'); |
| 50 | + back.className = 'back'; |
| 51 | + back.style.background = colors[index % colors.length]; |
| 52 | + |
| 53 | + card.appendChild(front); |
| 54 | + card.appendChild(back); |
| 55 | + container.appendChild(card); |
| 56 | + return card; |
| 57 | +} |
| 58 | + |
| 59 | +// Deck |
| 60 | +var Deck = (function() { |
| 61 | + this.cards = []; |
| 62 | + this.card_index = []; |
| 63 | + |
| 64 | + for(var i=0;i<52;++i) { |
| 65 | + var container = document.getElementById('surface'); |
| 66 | + this.cards.push(create_card(container, i)); |
| 67 | + } |
| 68 | + |
| 69 | + this.next_card = function() { |
| 70 | + if(this.card_index > 51) |
| 71 | + return; |
| 72 | + return this.cards[this.card_index++]; |
| 73 | + }; |
| 74 | + |
| 75 | + this.card_at = function(index) { |
| 76 | + return this.cards[index]; |
| 77 | + }; |
| 78 | + |
| 79 | + this.reset = function() { |
| 80 | + this.card_index = 0; |
| 81 | + }; |
| 82 | + return this; |
| 83 | +})(); |
| 84 | + |
| 85 | +function build_formation(positions) { |
| 86 | + Deck.reset(); |
| 87 | + for(i=0;i<positions.length;++i) { |
| 88 | + snabbt(Deck.next_card(), { |
| 89 | + from_position: positions[i].from_position, |
| 90 | + from_rotation: positions[i].from_rotation, |
| 91 | + position: positions[i].position, |
| 92 | + rotation: positions[i].rotation, |
| 93 | + easing: 'cos', |
| 94 | + perspective: 2000, |
| 95 | + delay: i * 50 |
| 96 | + }); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +function set_mode(mode) { |
| 101 | + if(mode == current_mode) { |
| 102 | + return; |
| 103 | + } |
| 104 | + from_positions = formation_builders[current_mode](); |
| 105 | + positions = formation_builders[mode](); |
| 106 | + 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 | + } |
| 114 | + } |
| 115 | + build_formation(positions); |
| 116 | + current_mode = mode; |
| 117 | +} |
| 118 | + |
| 119 | +function rotate_container() { |
| 120 | + var container = document.getElementById('surface'); |
| 121 | + snabbt(container, { |
| 122 | + rotation: [0, 2*Math.PI, 0], |
| 123 | + duration: 4000 |
| 124 | + }); |
| 125 | +} |
| 126 | + |
| 127 | +function pile_positions() { |
| 128 | + var positions = []; |
| 129 | + Deck.reset(); |
| 130 | + var i=0; |
| 131 | + var card=Deck.next_card(); |
| 132 | + var center = (HEIGHT-CARD_WIDTH)/2; |
| 133 | + while(card) { |
| 134 | + positions.push({ |
| 135 | + position: [center, BOTTOM + i*0.2, 100], |
| 136 | + rotation: [Math.PI/2, 0, 0], |
| 137 | + }); |
| 138 | + ++i; |
| 139 | + card = Deck.next_card(); |
| 140 | + } |
| 141 | + return positions; |
| 142 | +} |
| 143 | + |
| 144 | +function house_positions(floors, x, y, z) { |
| 145 | + Deck.reset(); |
| 146 | + var positions = []; |
| 147 | + var i; |
| 148 | + 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)); |
| 150 | + } |
| 151 | + |
| 152 | + return positions; |
| 153 | +} |
| 154 | + |
| 155 | +function house_row_positions(count, x, y, z) { |
| 156 | + var positions = []; |
| 157 | + var i; |
| 158 | + for(i=0;i<count;++i) { |
| 159 | + card_positions = pyramid_postions(x + i*PYRAMID_WIDTH, y, z); |
| 160 | + positions.push({ |
| 161 | + position: card_positions[0].position, |
| 162 | + rotation: card_positions[0].rotation, |
| 163 | + }); |
| 164 | + positions.push({ |
| 165 | + position: card_positions[1].position, |
| 166 | + rotation: card_positions[1].rotation, |
| 167 | + }); |
| 168 | + } |
| 169 | + for(i=0;i<count-1;++i) { |
| 170 | + positions.push({ |
| 171 | + position: [x + i*PYRAMID_WIDTH + TILTED_CARD_WIDTH, y - TILTED_CARD_HEIGHT/2, z], |
| 172 | + rotation: [Math.PI/2, Math.PI/2, 0], |
| 173 | + }); |
| 174 | + } |
| 175 | + return positions; |
| 176 | +} |
| 177 | + |
| 178 | +function pyramid_postions(x, y, z) { |
| 179 | + var spacing = TILTED_CARD_WIDTH / 2; |
| 180 | + |
| 181 | + return [{ |
| 182 | + position: [x - spacing, y, z], |
| 183 | + rotation: [-TILT, Math.PI/2, 0], |
| 184 | + }, { |
| 185 | + position: [x + spacing, y, z], |
| 186 | + rotation: [TILT, Math.PI/2, 0], |
| 187 | + }]; |
| 188 | +} |
| 189 | + |
| 190 | +function build_house() { |
| 191 | + set_mode(HOUSE); |
| 192 | +} |
| 193 | + |
| 194 | +function build_pile() { |
| 195 | + set_mode(PILE); |
| 196 | +} |
| 197 | + |
| 198 | +function centered_pile_positions() { |
| 199 | + // TODO: Actually center |
| 200 | + return pile_positions(); |
| 201 | +} |
| 202 | +function centered_house_positions() { |
| 203 | + // TODO: Actually center |
| 204 | + return house_positions(5, 200, BOTTOM, -300); |
| 205 | +} |
| 206 | + |
| 207 | +$(function() { |
| 208 | + Deck.reset(); |
| 209 | + build_pile(); |
| 210 | + //build_house(5, 200, 500); |
| 211 | +}); |
0 commit comments