This repository has been archived by the owner on Apr 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bowl.js
162 lines (141 loc) · 4.52 KB
/
bowl.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
/*
get that sriracha!
is a javascript game created by:
jennifer tran (jtran1)
michael helmbrecht (mhelmbre)
michelle lew (mlew1)
29 jan 2013
*/
function Bowl(x, y, radius, completion) {
var bowl = this;
this.isInsideBowl = function(x, y) {
// console.log(x + " " + y + " " + radius);
if (Math.sqrt(Math.pow(x-bowl.radius,2) + Math.pow(y-bowl.radius,2)) > bowl.radius) {
return false;
}
else {
return true;
}
};
this.isInsideSriracha = function(x, y, sX, sY, sRadius) {
// console.log(x + " " + y + " " + radius);
if (Math.sqrt(Math.pow(x-sX,2) + Math.pow(y-sY,2)) > sRadius) {
return false;
}
else {
return true;
}
};
this.squareCenter = function(i, j) {
var x = this.radius*2*(i+0.5)/this.gridSize;//-this.radius;
var y = this.radius*2*(j+0.5)/this.gridSize;//-this.radius;
return {x:x, y:y};
};
this.radius = radius;
this.x = x;
this.y = y;
this.srirachaPoints = [];
this.completed = 0;
this.possible = 0;
this.completion = completion;
this.won = false;
this.gridSize = Math.round(radius/10);
this.grid = new Array(this.gridSize);
for (var i = this.grid.length - 1; i >= 0; i--) {
this.grid[i] = new Array(this.gridSize);
for (var j = this.grid[i].length - 1; j >= 0; j--) {
var center = this.squareCenter(i,j);
if (this.isInsideBowl(center.x, center.y)) {
this.possible++;
this.grid[i][j] = false;
}
else {
this.grid[i][j] = true;
}
}
}
console.log(""+this.possible);
this.draw = function() {
var bowlImage = new Image();
bowlImage.src = "bowl-vector.png";
bowlImage.onload = function() {
game.ctx.drawImage(bowlImage, bowl.x, bowl.y, bowl.radius*2, bowl.radius*2);
game.ctx.strokeStyle = "#D23B27";
game.ctx.lineJoin = "round";
game.ctx.lineCap = "round";
game.ctx.lineWidth = 20;
for (var i = 0; i < bowl.srirachaPoints.length; i++) {
if (bowl.srirachaPoints[i].length === 0) { continue; }
game.ctx.beginPath();
game.ctx.moveTo(bowl.srirachaPoints[i][0].x, bowl.srirachaPoints[i][0].y);
if (bowl.srirachaPoints[i].length === 1) {
game.ctx.lineTo(bowl.srirachaPoints[i][0].x-1, bowl.srirachaPoints[i][0].y);
}
else {
for (var j = 1; j < bowl.srirachaPoints[i].length; j++) {
game.ctx.lineTo(bowl.srirachaPoints[i][j].x, bowl.srirachaPoints[i][j].y);
}
}
game.ctx.stroke();
}
};
};
this.getSrirachaed = function(bottle) {
if (bowl.won) { return; }
if (!bottle.open) {
if (bowl.srirachaPoints.length === 0) {
bowl.srirachaPoints.push([]);
}
else if (bowl.srirachaPoints[bowl.srirachaPoints.length-1].length !== 0) {
bowl.srirachaPoints.push([]);
}
return;
}
var x = bottle.x - bowl.x;
var y = bottle.y - bowl.y+ bottle.h;
if (!bowl.isInsideBowl(x,y)) {
// lose points
console.log("Lose points");
bowl.srirachaPoints.push([]);
return;
}
if (bowl.srirachaPoints[bowl.srirachaPoints.length-1].length === 0) {
bowl.srirachaPoints[bowl.srirachaPoints.length-1].push({x:bottle.x, y:bottle.y+bottle.h});
}
else {
var lastPoint = bowl.srirachaPoints[bowl.srirachaPoints.length-1][bowl.srirachaPoints[bowl.srirachaPoints.length-1].length-1];
if ((lastPoint.x !== bottle.x) || (lastPoint.y !== bottle.y+bottle.h)) {
bowl.srirachaPoints[bowl.srirachaPoints.length-1].push({x:bottle.x, y:bottle.y+bottle.h});
}
}
var minX = x - bottle.radius;
var maxX = x + bottle.radius;
var minY = y - bottle.radius;
var maxY = y + bottle.radius;
var minI = Math.round(minX*bowl.gridSize/(bowl.radius*2));
var maxI = Math.round(maxX*bowl.gridSize/(bowl.radius*2));
var minJ = Math.round(minY*bowl.gridSize/(bowl.radius*2));
var maxJ = Math.round(maxY*bowl.gridSize/(bowl.radius*2));
for (var i = minI; i <= maxI; i++) {
for (var j = minJ; j <= maxJ; j++) {
var center = bowl.squareCenter(i,j);
if (bowl.isInsideSriracha(center.x, center.y, x, y, bottle.radius)) {
if (!bowl.grid[i][j]) {
bowl.completed++;
}
bowl.grid[i][j] = true;
}
}
}
console.log("Get that sriracha! "+Math.round(bowl.completed/bowl.possible*1000)/10+" done.");
// if (bowl.completed/bowl.possible > bowl.completion) {
if (bowl.completed/bowl.possible > bowl.completion) {
bowl.won = true;
// alert("You win!");
// var newLevel = level.level;
// var refreshRate = level.refreshRate;
// level = new Level(newLevel+1, refreshRate);
ShowLevelMessage(level.level);
}
};
}