-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcard_game.html
More file actions
644 lines (540 loc) · 24.9 KB
/
card_game.html
File metadata and controls
644 lines (540 loc) · 24.9 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>メンコゲーム - 加速度センサー</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #000;
overflow: hidden;
font-family: Arial, sans-serif;
transition: background-color 0.1s ease;
}
#info {
position: absolute;
top: 10px;
left: 10px;
color: white;
background: rgba(0, 0, 0, 0.7);
padding: 10px;
border-radius: 5px;
font-size: 12px;
z-index: 100;
max-width: 200px;
}
#gameCanvas {
display: block;
}
#permissionDialog {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
padding: 30px;
border-radius: 10px;
text-align: center;
z-index: 1000;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
}
button {
background: #007AFF;
color: white;
border: none;
padding: 15px 30px;
border-radius: 8px;
font-size: 16px;
cursor: pointer;
margin: 10px;
}
button:hover {
background: #0056b3;
}
</style>
</head>
<body>
<div id="permissionDialog">
<h2>メンコ</h2>
<p>目指せパーフェクト!</p>
<button onclick="requestSensorPermission()">ゲーム開始</button>
</div>
<div id="scoreEffect" style="
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 10px;
font-weight: bold;
color: #FFD700;
text-shadow: 2px 2px 4px rgba(0,0,0,0.8);
pointer-events: none;
z-index: 200;
display: none;
"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// ゲーム変数
let scene, camera, renderer, playerCard, floor;
let floorCards = []; // 床に置かれたカード
let flyingCardCount = 0; // 飛んだカードの数
let accelerationData = { x: 0, y: 0, z: 0 };
let orientationData = { alpha: 0, beta: 0, gamma: 0 };
let cardPosition = { x: 0, y: 0, z: 0 };
let cardRotation = { x: 0, y: 0, z: 0 };
let isThrown = false;
let throwVelocity = { x: 0, y: 0, z: 0 };
let throwRotationVelocity = { x: 0, y: 0, z: 0 };
const THROW_THRESHOLD = 50;
const FLOOR_Z = -150; // 床の位置(奥行き方向)
const CARD_SIZE_MULTIPLIER = 3; // カードサイズを倍に
const CARD_WIDTH_RATIO = 0.9; // カードを90%の幅に
const FLOOR_CARD_COUNT = 50; // 床に置くカード数
// テクスチャ
let frontTexture, backTexture;
// Three.js初期化
function initThreeJS() {
// シーン作成
scene = new THREE.Scene();
// カメラ作成
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
// レンダラー作成
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000);
document.body.appendChild(renderer.domElement);
renderer.domElement.id = 'gameCanvas';
// ライト追加
const ambientLight = new THREE.AmbientLight(0x404040, 0.6);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(10, 10, 5);
scene.add(directionalLight);
// プレイヤーカード作成
createPlayerCard();
// 床作成
createFloor();
// 床のカード作成
createFloorCards();
// アニメーションループ開始
animate();
}
function createPlayerCard() {
// プレイヤーカードのジオメトリ(90%の幅)
const aspect = window.innerWidth / window.innerHeight;
const cardWidth = 3 * aspect * CARD_SIZE_MULTIPLIER * CARD_WIDTH_RATIO;
const cardHeight = 3 * CARD_SIZE_MULTIPLIER;
const geometry = new THREE.PlaneGeometry(cardWidth, cardHeight);
const material = new THREE.MeshLambertMaterial({
map: frontTexture,
side: THREE.DoubleSide
});
playerCard = new THREE.Mesh(geometry, material);
playerCard.position.set(0, 0, 0);
scene.add(playerCard);
}
function createFloor() {
// 床の作成(奥行き方向に配置)
const floorGeometry = new THREE.PlaneGeometry(100, 100);
const floorMaterial = new THREE.MeshLambertMaterial({
color: 0x333333,
transparent: true,
opacity: 0.5
});
floor = new THREE.Mesh(floorGeometry, floorMaterial);
// 床を縦に立てて奥行き方向に配置
floor.position.set(0, 0, FLOOR_Z);
scene.add(floor);
}
function createFloorCards() {
floorCards = [];
for (let i = 0; i < FLOOR_CARD_COUNT; i++) {
// カードのジオメトリ
const cardWidth = 4 * CARD_WIDTH_RATIO;
const cardHeight = 4;
const geometry = new THREE.PlaneGeometry(cardWidth, cardHeight);
// 表向きでスタート
const material = new THREE.MeshLambertMaterial({
map: frontTexture,
side: THREE.DoubleSide
});
const floorCard = new THREE.Mesh(geometry, material);
// 床の前面により広い範囲にランダムに配置
const x = (Math.random() - 0.5) * 120;
const y = (Math.random() - 0.5) * 120;
floorCard.position.set(x, y, FLOOR_Z + 1);
// カードの状態を保存
floorCard.userData = {
originalPosition: { x, y, z: FLOOR_Z + 1 },
velocity: { x: 0, y: 0, z: 0 },
rotationVelocity: { x: 0, y: 0, z: 0 },
isFlying: false,
hasLanded: false,
hasFlown: false // 一度でも飛んだかどうか
};
floorCards.push(floorCard);
scene.add(floorCard);
}
}
function updateCard() {
if (!isThrown) {
// Z軸加速度に基づいてカードの位置を更新
const zAccel = Math.abs(accelerationData.z);
cardPosition.z = -zAccel * 0.1; // 近づいたり遠ざかったり
// 角度センサーで視差効果(デバイスの傾きでカメラを移動)
updateCameraWithOrientation();
// 閾値チェック
if (zAccel > THROW_THRESHOLD) {
throwCard();
}
} else {
// 投げられた後の物理演算
cardPosition.x += throwVelocity.x;
cardPosition.y += throwVelocity.y;
cardPosition.z += throwVelocity.z;
cardRotation.x += throwRotationVelocity.x;
cardRotation.y += throwRotationVelocity.y;
cardRotation.z += throwRotationVelocity.z;
// 重力効果
throwVelocity.y -= 0.01;
// 空気抵抗
throwVelocity.x *= 0.99;
throwVelocity.y *= 0.99;
throwVelocity.z *= 0.99;
// 回転の減衰
throwRotationVelocity.x *= 0.98;
throwRotationVelocity.y *= 0.98;
throwRotationVelocity.z *= 0.98;
// 床に当たったかチェック(奥行き方向)
if (cardPosition.z <= FLOOR_Z) {
hitFloor();
}
}
// プレイヤーカードの位置と回転を適用
playerCard.position.set(cardPosition.x, cardPosition.y, cardPosition.z);
playerCard.rotation.set(cardRotation.x, cardRotation.y, cardRotation.z);
// 床のカードの物理演算
updateFloorCards();
}
function throwCard() {
isThrown = true;
// 投げる速度を設定(加速度に基づく)
throwVelocity.x = (accelerationData.x || 0) * 0.02;
throwVelocity.y = (accelerationData.y || 0) * 0.02;
throwVelocity.z = -Math.abs(accelerationData.z) * 0.05; // 奥に向かって
// 回転速度を設定
throwRotationVelocity.x = (Math.random() - 0.5) * 0.3;
throwRotationVelocity.y = (Math.random() - 0.5) * 0.3;
throwRotationVelocity.z = (Math.random() - 0.5) * 0.2;
document.getElementById('gameState').textContent = '投げました!';
}
function hitFloor() {
// 投げ速度を計算(勢い)
const speed = Math.sqrt(
throwVelocity.x * throwVelocity.x +
throwVelocity.y * throwVelocity.y +
throwVelocity.z * throwVelocity.z
);
// カードの角度を計算(床との平行度)
// X軸とY軸の回転が小さいほど床と平行
const angleX = Math.abs(cardRotation.x);
const angleY = Math.abs(cardRotation.y);
const parallelness = Math.max(0, 1 - (angleX + angleY) / Math.PI); // 0-1の範囲
// プレイヤーカードを床で停止
cardPosition.z = FLOOR_Z + 1;
throwVelocity = { x: 0, y: 0, z: 0 };
throwRotationVelocity = { x: 0, y: 0, z: 0 };
// 床のカードを飛ばす(速度と平行度の両方を考慮)
scatterFloorCards(cardPosition.x, cardPosition.y, speed, parallelness);
// カードをリセット
setTimeout(() => {
resetCard();
}, 2000);
}
function scatterFloorCards(impactX, impactY, speed, parallelness) {
// 加速度と平行度に応じた衝撃範囲
const baseRange = 20;
const speedBonus = speed * 2;
const parallelnessBonus = parallelness * 30; // 平行度によるボーナス
const impactRange = Math.min(baseRange + speedBonus + parallelnessBonus, 80);
let newFlyingCards = 0;
floorCards.forEach(card => {
const distance = Math.sqrt(
Math.pow(card.position.x - impactX, 2) +
Math.pow(card.position.y - impactY, 2)
);
// 衝撃範囲内のカードを飛ばす
if (distance < impactRange && !card.userData.isFlying) {
card.userData.isFlying = true;
card.userData.hasLanded = false;
// まだ飛んだことがないカードをカウント
if (!card.userData.hasFlown) {
card.userData.hasFlown = true;
newFlyingCards++;
}
// 飛び散る方向と速度
const direction = {
x: (card.position.x - impactX) / distance,
y: (card.position.y - impactY) / distance
};
const force = Math.max(0.5, (impactRange - distance) / impactRange) * speed * 0.3;
card.userData.velocity.x = direction.x * force;
card.userData.velocity.y = direction.y * force;
card.userData.velocity.z = Math.random() * 2 + 1; // 手前に飛ぶ
// 回転を追加
card.userData.rotationVelocity.x = (Math.random() - 0.5) * 0.2;
card.userData.rotationVelocity.y = (Math.random() - 0.5) * 0.2;
card.userData.rotationVelocity.z = (Math.random() - 0.5) * 0.2;
}
});
// 飛んだカード数を更新
flyingCardCount += newFlyingCards;
updateScoreDisplay();
// スコアエフェクトを表示
if (newFlyingCards > 0) {
showScoreEffect(newFlyingCards);
}
}
function updateScoreDisplay() {
// 全部飛んだらPerfect表示
if (flyingCardCount >= FLOOR_CARD_COUNT) {
showPerfectEffect();
}
}
function showScoreEffect(count) {
const scoreElement = document.getElementById('scoreEffect');
scoreElement.textContent = `+${count}`;
scoreElement.style.display = 'block';
scoreElement.style.fontSize = '10px';
scoreElement.style.opacity = '0';
// アニメーション開始
let scale = 0.1;
let opacity = 1;
const animate = () => {
scale += 0.15;
opacity -= 0.02;
scoreElement.style.fontSize = `${scale * 100}px`;
scoreElement.style.opacity = opacity;
if (opacity > 0 && scale < 5) {
requestAnimationFrame(animate);
} else {
scoreElement.style.display = 'none';
}
};
animate();
}
function showPerfectEffect() {
const scoreElement = document.getElementById('scoreEffect');
scoreElement.textContent = 'Perfect!';
scoreElement.style.display = 'block';
scoreElement.style.fontSize = '20px';
scoreElement.style.opacity = '0';
scoreElement.style.color = '#FF6B6B';
// 派手なアニメーション
let scale = 0.2;
let opacity = 1;
let pulse = 0;
const animate = () => {
scale += 0.1;
opacity -= 0.005;
pulse += 0.2;
const pulseFactor = 1 + Math.sin(pulse) * 0.2;
scoreElement.style.fontSize = `${scale * 100 * pulseFactor}px`;
scoreElement.style.opacity = opacity;
if (opacity > 0 && scale < 8) {
requestAnimationFrame(animate);
} else {
scoreElement.style.display = 'none';
scoreElement.style.color = '#FFD700'; // 元の色に戻す
}
};
animate();
}
function updateFloorCards() {
floorCards.forEach(card => {
if (card.userData.isFlying && !card.userData.hasLanded) {
// 物理演算
card.position.x += card.userData.velocity.x;
card.position.y += card.userData.velocity.y;
card.position.z += card.userData.velocity.z;
card.rotation.x += card.userData.rotationVelocity.x;
card.rotation.y += card.userData.rotationVelocity.y;
card.rotation.z += card.userData.rotationVelocity.z;
// 空気抵抗
card.userData.velocity.x *= 0.98;
card.userData.velocity.y *= 0.98;
card.userData.velocity.z *= 0.98;
card.userData.rotationVelocity.x *= 0.95;
card.userData.rotationVelocity.y *= 0.95;
card.userData.rotationVelocity.z *= 0.95;
// 十分減速したら着地
const totalVelocity = Math.sqrt(
card.userData.velocity.x * card.userData.velocity.x +
card.userData.velocity.y * card.userData.velocity.y +
card.userData.velocity.z * card.userData.velocity.z
);
if (totalVelocity < 0.1) {
card.userData.isFlying = false;
card.userData.hasLanded = true;
card.userData.velocity = { x: 0, y: 0, z: 0 };
card.userData.rotationVelocity = { x: 0, y: 0, z: 0 };
}
}
});
}
function resetCard() {
// カードを元の位置に戻す
cardPosition = { x: 0, y: 0, z: 0 };
cardRotation = { x: 0, y: 0, z: 0 };
throwVelocity = { x: 0, y: 0, z: 0 };
throwRotationVelocity = { x: 0, y: 0, z: 0 };
isThrown = false;
// カメラも元の位置に戻す
camera.position.set(0, 0, 5);
camera.lookAt(0, 0, 0);
// 床のカードをリセット
floorCards.forEach(card => {
card.position.copy(card.userData.originalPosition);
card.rotation.set(0, 0, 0);
card.userData.isFlying = false;
card.userData.hasLanded = false;
card.userData.hasFlown = false;
card.userData.velocity = { x: 0, y: 0, z: 0 };
card.userData.rotationVelocity = { x: 0, y: 0, z: 0 };
});
// スコアリセット
flyingCardCount = 0;
updateScoreDisplay();
}
function updateCameraWithOrientation() {
// 角度センサーの値を使ってカメラの位置を微調整(視差効果)
const maxOffset = 2; // カメラの最大移動距離
// γ(左右の傾き)でX軸移動(逆向き)
const xOffset = -(orientationData.gamma / 90) * maxOffset;
// β(前後の傾き)でY軸移動(逆向き)
const yOffset = -(orientationData.beta / 180) * maxOffset;
// カメラ位置を更新(滑らかに移動)
camera.position.x += (xOffset - camera.position.x) * 0.1;
camera.position.y += (yOffset - camera.position.y) * 0.1;
// カメラを常にカードの方向を向かせる
camera.lookAt(cardPosition.x, cardPosition.y, cardPosition.z);
}
function updateBackgroundColor() {
// 背景色を黒に固定
document.body.style.backgroundColor = 'rgb(0, 0, 0)';
}
function animate() {
requestAnimationFrame(animate);
updateCard();
updateBackgroundColor();
renderer.render(scene, camera);
}
// センサー許可要求
function requestSensorPermission() {
if (typeof DeviceMotionEvent !== 'undefined' &&
typeof DeviceMotionEvent.requestPermission === 'function') {
DeviceMotionEvent.requestPermission()
.then(permissionState => {
if (permissionState === 'granted') {
// 角度センサーの許可も要求
if (typeof DeviceOrientationEvent !== 'undefined' &&
typeof DeviceOrientationEvent.requestPermission === 'function') {
DeviceOrientationEvent.requestPermission()
.then(orientationState => {
startGame();
})
.catch(console.error);
} else {
startGame();
}
} else {
alert('センサーアクセスが許可されませんでした');
}
})
.catch(console.error);
} else {
// Android等
startGame();
}
}
function startGame() {
// 許可ダイアログを隠す
document.getElementById('permissionDialog').style.display = 'none';
// テクスチャを読み込んでからThree.js初期化
loadTextures();
// センサーイベントリスナー追加
window.addEventListener('devicemotion', handleMotion);
window.addEventListener('deviceorientation', handleOrientation);
}
function loadTextures() {
const textureLoader = new THREE.TextureLoader();
let loadedCount = 0;
// 表面テクスチャ
frontTexture = textureLoader.load('assets/card01.png',
function(texture) {
console.log('表面テクスチャ読み込み成功');
loadedCount++;
if (loadedCount === 2) initThreeJS();
},
undefined,
function(error) {
console.log('表面テクスチャ読み込み失敗:', error);
}
);
// 裏面テクスチャ
backTexture = textureLoader.load('assets/card02.png',
function(texture) {
console.log('裏面テクスチャ読み込み成功');
loadedCount++;
if (loadedCount === 2) initThreeJS();
},
undefined,
function(error) {
console.log('裏面テクスチャ読み込み失敗:', error);
}
);
}
function handleMotion(event) {
const acc = event.accelerationIncludingGravity || event.acceleration;
if (acc && acc.x !== null && acc.y !== null && acc.z !== null) {
accelerationData.x = acc.x || 0;
accelerationData.y = acc.y || 0;
accelerationData.z = acc.z || 0;
}
}
function handleOrientation(event) {
orientationData.alpha = event.alpha || 0; // Z軸回転
orientationData.beta = event.beta || 0; // X軸回転(前後の傾き)
orientationData.gamma = event.gamma || 0; // Y軸回転(左右の傾き)
}
// ウィンドウリサイズ対応
window.addEventListener('resize', () => {
if (camera && renderer) {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
});
// ページ読み込み完了時に自動的に許可要求
window.addEventListener('load', () => {
// 少し遅延してから自動で許可要求
setTimeout(() => {
if (document.getElementById('permissionDialog').style.display !== 'none') {
// iOSの場合は手動でボタンを押す必要がある
if (typeof DeviceMotionEvent !== 'undefined' &&
typeof DeviceMotionEvent.requestPermission === 'function') {
// iOSの場合はユーザー操作が必要
return;
} else {
// Androidの場合は自動実行
requestSensorPermission();
}
}
}, 1000);
});
</script>
</body>
</html>