-
Notifications
You must be signed in to change notification settings - Fork 2
/
game.js
224 lines (191 loc) Β· 7.65 KB
/
game.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//function $(q){return document.querySelector(q);}
//var Ο,pi=Ο=Math.PI;
var pi=Math.PI;
if(window.Ralphio){
//var paper=new Ralphio(document.body,"100%","100%");
//default arguments; new is unnesesary;
var paper=Ralphio();
}else{
var paper=new Raphael(document.body,"100%","100%");
}
paper.line = paper.line
//
|| /*
\\ */
function(x1,y1,x2,y2){
return this.path("M"+x1+" "+y1+"L"+x2+" "+y2);
};
/*paper.arc = function(r, centerX, centerY, radius, startAngle, endAngle) {
var startX = centerX+radius*Math.cos((90-startAngle)*Math.PI/180);
var startY = centerY-radius*Math.sin((90-startAngle)*Math.PI/180);
var endX = centerX+radius*Math.cos((90-endAngle)*Math.PI/180);
var endY = centerY-radius*Math.sin((90-endAngle)*Math.PI/180);
return r.path([['M',startX, startY],['A',radius,radius,0,1,1,endX,endY]]);
}*/
var boardSize=4;
var board=[];
var cardTypes=["Put back a F.F.F. card.","Pick up a F.F.F. card.","Lose next turn.","Escape to the nearest cave."];
var deck=[];
var boardSet=paper.set();
var mouse = {};
var center = {};
var boardRadius;
drawBoard();
function drawBoard(){
boardSet.remove();
var centerX=parseFloat(getComputedStyle(document.documentElement).width)/2;
var centerY=parseFloat(getComputedStyle(document.documentElement).height)/2;
boardRadius=Math.min(centerX,centerY)*0.9;
center.x = centerX;
center.y = centerY;
/// draw caves
var d="M"+(centerX+Math.sin(pi*2/3)*boardRadius)+" "+(centerY+Math.cos(pi*2/3)*boardRadius);
for(var s=0; s<6; s++){
var sidex1=centerX+Math.sin(pi*(2+s)/3)*boardRadius;
var sidey1=centerY+Math.cos(pi*(2+s)/3)*boardRadius;
var sidex2=centerX+Math.sin(pi*(1+s)/3)*boardRadius;
var sidey2=centerY+Math.cos(pi*(1+s)/3)*boardRadius;
for(var i=0; i<boardSize; i++){
var x1=sidex1+(sidex2-sidex1)*((i+0.5)/boardSize);
var y1=sidey1+(sidey2-sidey1)*((i+0.5)/boardSize);
boardSet.push(paper.circle(x1,y1,boardRadius/boardSize/2).attr({"fill":"#EEE","stroke":"black"}));
}
d+="L"+sidex1+" "+sidey1;
}
var outerBoard=paper.path(d+"z");
outerBoard.attr({"fill":"#FFE"});
boardSet.push(outerBoard);
/// draw triangles
for(var s=0; s<6; s++){
var sidex1=centerX+Math.sin(pi*(2+s)/3)*boardRadius;
var sidey1=centerY+Math.cos(pi*(2+s)/3)*boardRadius;
var sidex2=centerX+Math.sin(pi*(1+s)/3)*boardRadius;
var sidey2=centerY+Math.cos(pi*(1+s)/3)*boardRadius;
var side2x1=centerX+Math.sin(pi*(3+s)/3)*boardRadius;
var side2y1=centerY+Math.cos(pi*(3+s)/3)*boardRadius;
var side2x2=centerX+Math.sin(pi*(4+s)/3)*boardRadius;
var side2y2=centerY+Math.cos(pi*(4+s)/3)*boardRadius;
for(var i=0; i<=boardSize; i++){
var x1=sidex1+(sidex2-sidex1)*(i/boardSize);
var y1=sidey1+(sidey2-sidey1)*(i/boardSize);
var x2=side2x1+(side2x2-side2x1)*(i/boardSize);
var y2=side2y1+(side2y2-side2y1)*(i/boardSize);
boardSet.push(paper.line(x1,y1,x2,y2));
}
}
for(var yFromTop = 0; yFromTop<getRowCount(); yFromTop++){
for(var xFromCenter = minXAt(yFromTop); xFromCenter < maxXAt(yFromTop); xFromCenter++){
var position = getScreenCoordinates(xFromCenter, yFromTop, centerX, centerY, boardRadius);
if((Math.abs(xFromCenter+yFromTop+0.5))%4<=0.5 && (Math.abs(xFromCenter-yFromTop+0.5))%3<=0.75){
boardSet.push(paper.circle(position.x, position.y, getColumnWidth(boardRadius)/2.0).attr({"fill":"rgba(255,255,155,1)"}));
}else{
boardSet.push(paper.circle(position.x, position.y, getColumnWidth(boardRadius)/2.0).attr({"fill":"rgba(255,155,155,0.5)"}));
}
}
}
// var mouseSpace = getBoardCoordinates(mouse.x, mouse.y, centerX, centerY, boardRadius);
// var position = getScreenCoordinates(mouseSpace.x, mouseSpace.y, centerX, centerY, boardRadius);
// boardSet.push(paper.circle(position.x, position.y, getColumnWidth(boardRadius)/10.0).attr({"fill":"rgba(0,155,155,0.5)"}));
///Make it look all ugly and stuff.
//if(paper.shapes&&paper.shapes[0]&&paper.shapes[0].moveBy){
// var r = Math.sqrt(centerY*centerX)/50;
// for(var i=0;i<paper.shapes.length;i++){
// paper.shapes[i].moveBy((Math.random()-0.5)*r,(Math.random()-0.5)*r,(Math.random()-0.5)*r,(Math.random()-0.5)*r);
// }
//}
}
function drawMouseCircle(){
var mouseSpace = getBoardCoordinates(mouse.x, mouse.y, center.x, center.y, boardRadius);
var position = getScreenCoordinates(mouseSpace.x, mouseSpace.y, center.x, center.y, boardRadius);
var r = Math.random()*255;
var g = Math.random()*255;
var b = Math.random()*255;
boardSet.push(paper.circle(position.x, position.y, getColumnWidth(boardRadius)/10.0).attr({"fill":"rgba(" + r + ","+ g + "," + b +",0.5)"}));
boardSet.push(paper.circle(mouse.x, mouse.y, getColumnWidth(boardRadius)/15.0).attr({"fill":"rgba(" + r + ","+ g + "," + b +",0.5)"}));
}
function minXAt(yFromTop){
return -1*getWidthAt(yFromTop)/2;
}
function maxXAt(yFromTop){
return getWidthAt(yFromTop)/2;
}
function pointsLeft(xFromCenter, yFromTop){
if(((1+ Math.abs(xFromCenter))%2 + Math.abs(yFromTop)%2)%2 ==1 ){
return true;
}
return false;
}
function getRowCount(){
return 4*boardSize;
}
function getColumnCount(){
return boardSize*2;
}
function getRowHeight(boardRadius){
var rowCount = getRowCount();
return 2.0*boardRadius/rowCount;
}
function getColumnWidth(boardRadius){
var equilateralTriangleHeight = Math.sqrt(3)/2.0;
return equilateralTriangleHeight*boardRadius/getColumnCount();
}
function getWidthAt(yFromTop){
var section = Math.floor(yFromTop/boardSize);
var depthInSection = yFromTop%boardSize;
if(section == 1 || section ==2){
return 2*boardSize;
}
if(section == 3){
depthInSection = boardSize -depthInSection;
}
return depthInSection*2;
}
function getScreenCoordinates(xFromCenter, yFromTop, centerX, centerY, boardRadius){
var rowCount = getRowCount();
var columnCount = getColumnCount();
var rowHeight = getRowHeight(boardRadius);
var columnWidth = getColumnWidth(boardRadius);
var xFudge = (2*columnWidth/3)*( pointsLeft(xFromCenter,yFromTop)+ 2.5);
var yTop = centerY-(rowCount/2.0)*rowHeight;
var resultY = yTop + yFromTop*rowHeight;
var resultX = (xFromCenter -0.5)*2*columnWidth + centerX + xFudge;
var result = {};
result.x = resultX;
result.y = resultY;
return result;
}
function getBoardCoordinates(screenX, screenY, centerX, centerY, boardRadius){
var columnWidth = getColumnWidth(boardRadius);
var boardX = 0.5*(screenX - centerX)/columnWidth;
var rowHeight = getRowHeight(boardRadius);
var yTop = centerY - (rowHeight * getRowCount() /2);
var boardY = (screenY - yTop) / rowHeight;
var slope;
var offset = 0;
if(pointsLeft(Math.floor(boardX), Math.floor(boardY))){
slope = 1;
} else {
slope = -1;
offset = 1;
}
var tileX = boardX - Math.floor(boardX);
var tileY = boardY - Math.floor(boardY);
if(tileY > slope*tileX + offset){
boardY = Math.floor(boardY) + 1;
} else {
boardY = Math.floor(boardY);
}
boardX = Math.floor(boardX);
var result = {};
result.x = boardX;
result.y = boardY;
return result;
}
document.body.onmousemove=function(e){
mouse.x = e.pageX;
mouse.y = e.pageY;
drawMouseCircle();
// drawBoard();
}
document.body.onkeypress=function(e){if(e.charCode>=48&&e.charCode<=57)drawBoard(boardSize=e.charCode-48);};
window.onresize=function(){drawBoard();};