-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
273 lines (236 loc) · 7.58 KB
/
script.js
File metadata and controls
273 lines (236 loc) · 7.58 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Game State
let gameState = 'START'; // START, PLAYING, WON, PROPOSAL, END
let score = 0;
const WIN_SCORE = 15; // Hearts needed to fill the meter
let player;
let hearts = [];
let particles = []; // For effects
let animationId;
let loveMeter = document.getElementById('love-fill');
// DOM Elements
const startScreen = document.getElementById('start-screen');
const proposalScreen = document.getElementById('proposal-screen');
const celebrationScreen = document.getElementById('celebration-screen');
const startBtn = document.getElementById('start-btn');
const yesBtn = document.getElementById('yes-btn');
const noBtn = document.getElementById('no-btn');
// Resize Handling
function resize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
if (player) {
player.y = canvas.height - 100;
}
}
window.addEventListener('resize', resize);
// Player Object
class Player {
constructor() {
this.w = 100; // width
this.h = 80; // height
this.x = canvas.width / 2 - this.w / 2;
this.y = canvas.height - 100;
this.speed = 10;
this.dx = 0;
}
draw() {
// Draw a cute basket or cup (using simple shapes for now, can be improved or replaced with image)
ctx.fillStyle = '#ff4d6d';
// Simple semi-circle basket
ctx.beginPath();
ctx.arc(this.x + this.w/2, this.y, this.w/2, 0, Math.PI, false);
ctx.fill();
// Handle
ctx.beginPath();
ctx.strokeStyle = '#c9184a';
ctx.lineWidth = 5;
ctx.arc(this.x + this.w/2, this.y - 10, this.w/2, Math.PI, 0, false);
ctx.stroke();
}
update() {
this.x += this.dx;
// Boundaries
if (this.x < 0) this.x = 0;
if (this.x + this.w > canvas.width) this.x = canvas.width - this.w;
}
}
// Heart Object
class Heart {
constructor() {
this.size = Math.random() * 20 + 20; // 20-40px
this.x = Math.random() * (canvas.width - this.size);
this.y = -this.size;
this.speed = Math.random() * 3 + 2; // 2-5 speed
this.color = `hsl(${Math.random() * 20 + 340}, 100%, 60%)`; // Pinkish/Red variations
}
draw() {
ctx.fillStyle = this.color;
ctx.beginPath();
let topCurveHeight = this.size * 0.3;
ctx.moveTo(this.x, this.y + topCurveHeight);
// top left curve
ctx.bezierCurveTo(
this.x, this.y,
this.x - this.size / 2, this.y,
this.x - this.size / 2, this.y + topCurveHeight
);
// bottom left curve
ctx.bezierCurveTo(
this.x - this.size / 2, this.y + (this.size + topCurveHeight) / 2,
this.x, this.y + (this.size + topCurveHeight) / 2,
this.x, this.y + this.size
);
// bottom right curve
ctx.bezierCurveTo(
this.x, this.y + (this.size + topCurveHeight) / 2,
this.x + this.size / 2, this.y + (this.size + topCurveHeight) / 2,
this.x + this.size / 2, this.y + topCurveHeight
);
// top right curve
ctx.bezierCurveTo(
this.x + this.size / 2, this.y,
this.x, this.y,
this.x, this.y + topCurveHeight
);
ctx.fill();
}
update() {
this.y += this.speed;
}
}
// Particle Effect
class Particle {
constructor(x, y) {
this.x = x;
this.y = y;
this.size = Math.random() * 5 + 2;
this.speedX = (Math.random() - 0.5) * 4;
this.speedY = (Math.random() - 0.5) * 4;
this.life = 100;
this.color = `rgba(255, 255, 255, 0.8)`;
}
update() {
this.x += this.speedX;
this.y += this.speedY;
this.life -= 2;
}
draw() {
ctx.fillStyle = this.color;
ctx.globalAlpha = this.life / 100;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
ctx.globalAlpha = 1;
}
}
// Input Handling
function handleInput(e) {
if (!player) return;
// Mouse / Touch
if (e.type === 'mousemove' || e.type === 'touchmove') {
const clientX = e.type === 'mousemove' ? e.clientX : e.touches[0].clientX;
player.x = clientX - player.w / 2;
}
}
window.addEventListener('mousemove', handleInput);
window.addEventListener('touchmove', handleInput, { passive: false });
// Game Functions
function spawnHeart() {
if (Math.random() < 0.02) {
hearts.push(new Heart());
}
}
function updateGame() {
ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear canvas
if (gameState === 'PLAYING') {
player.update();
player.draw();
spawnHeart();
hearts.forEach((heart, index) => {
heart.update();
heart.draw();
// Collision Detection
// Simple box-ish collision for now
if (
heart.y + heart.size > player.y &&
heart.x > player.x &&
heart.x < player.x + player.w
) {
// Catch!
hearts.splice(index, 1);
score++;
createParticles(heart.x, heart.y);
updateScore();
if (score >= WIN_SCORE) {
triggerProposal();
}
} else if (heart.y > canvas.height) {
hearts.splice(index, 1); // Missed
}
});
particles.forEach((p, idx) => {
p.update();
p.draw();
if (p.life <= 0) particles.splice(idx, 1);
});
}
animationId = requestAnimationFrame(updateGame);
}
function createParticles(x, y) {
for(let i=0; i<5; i++) {
particles.push(new Particle(x, y));
}
}
function updateScore() {
const percentage = (score / WIN_SCORE) * 100;
loveMeter.style.width = `${percentage}%`;
}
function triggerProposal() {
gameState = 'PROPOSAL';
// Small delay to let the particle effect finish or just smooth transition
setTimeout(() => {
proposalScreen.classList.remove('hidden');
proposalScreen.classList.add('active');
// Stop the loop or keep it running for background?
// Let's keep loop for maybe background falling hearts but pause spawning
}, 500);
}
function startGame() {
resize();
player = new Player();
hearts = [];
score = 0;
updateScore();
gameState = 'PLAYING';
startScreen.classList.remove('active');
startScreen.classList.add('hidden');
updateGame();
}
// Event Listeners
startBtn.addEventListener('click', startGame);
yesBtn.addEventListener('click', () => {
proposalScreen.classList.remove('active');
proposalScreen.classList.add('hidden');
celebrationScreen.classList.remove('hidden');
celebrationScreen.classList.add('active');
triggerConfetti(); // Optional: Implement confetti
});
// "No" button runs away
noBtn.addEventListener('mouseover', moveNoButton);
noBtn.addEventListener('touchstart', moveNoButton);
function moveNoButton() {
const x = Math.random() * (window.innerWidth - noBtn.offsetWidth);
const y = Math.random() * (window.innerHeight - noBtn.offsetHeight);
noBtn.style.position = 'fixed';
noBtn.style.left = `${x}px`;
noBtn.style.top = `${y}px`;
}
function triggerConfetti() {
// Simple confetti effect using particles or similar
// We can just reuse the heat/particle engine or add a simple CSS class to body
// For now, let's just let it be.
}
// Initialize
resize();