-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
181 lines (157 loc) · 5.21 KB
/
main.js
File metadata and controls
181 lines (157 loc) · 5.21 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
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import { Pachisi } from "./src/pachisi.js";
// Basic setup
const scene = new THREE.Scene();
const container = document.getElementById("game-container");
const camera = new THREE.PerspectiveCamera(
60,
container.clientWidth / container.clientHeight,
0.1,
1000
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(container.clientWidth, container.clientHeight);
container.appendChild(renderer.domElement);
// Create Pachisi game
const pachisi = new Pachisi(scene);
pachisi.initialize();
// Position the camera for a good view of the 3D board and pieces
camera.position.set(0, 10, 10);
camera.lookAt(0, 0, 0);
// Add OrbitControls
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true; // Adds smooth damping effect
controls.dampingFactor = 0.25;
controls.screenSpacePanning = false;
controls.minDistance = 5;
controls.maxDistance = 20;
controls.maxPolarAngle = Math.PI / 2;
// Handle window resize
window.addEventListener("resize", onWindowResize, false);
function onWindowResize() {
camera.aspect = container.clientWidth / container.clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(container.clientWidth, container.clientHeight);
}
// Roll dice button functionality
document.getElementById("roll-dice").addEventListener("click", () => {
if (!pachisi.diceRolled) {
const result = pachisi.rollDice();
if (result) {
alert(`You rolled ${result[0]} and ${result[1]}!`);
pachisi.updatePlayerInfo();
}
} else {
alert("You have already rolled the dice. Please move your pieces.");
}
});
//export button
const exportLogsButton = document.createElement("button");
exportLogsButton.id = "export-logs";
exportLogsButton.textContent = "Export Mouse Logs";
exportLogsButton.style.cssText = `
position: fixed;
top: 10px;
right: 10px;
padding: 10px 15px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
z-index: 1000;
`;
document.body.appendChild(exportLogsButton);
// Mouse event logging
let mouseEventLogs = [];
function logMouseEvent(event) {
const log = `${new Date().toISOString()} : ${event.type} at (${
event.clientX
}, ${event.clientY})`;
console.log(log);
mouseEventLogs.push(log);
}
// Add event listeners for mouse events
const mouseEvents = [
"click",
"mousemove",
"mousedown",
"mouseup",
"mouseover",
"mouseout",
];
mouseEvents.forEach((eventType) => {
renderer.domElement.addEventListener(eventType, logMouseEvent);
});
// Export logs functionality
exportLogsButton.addEventListener("click", () => {
const logsJson = JSON.stringify(mouseEventLogs, null, 2);
const blob = new Blob([logsJson], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "mouse_event_logs.txt";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
// Raycaster for piece selection
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
// Add click event listener for piece selection
renderer.domElement.addEventListener("click", onPieceClick, false);
function onPieceClick(event) {
// Calculate mouse position in normalized device coordinates
mouse.x = (event.clientX / container.clientWidth) * 2 - 1;
mouse.y = -(event.clientY / container.clientHeight) * 2 + 1;
// Update the picking ray with the camera and mouse position
raycaster.setFromCamera(mouse, camera);
// Calculate objects intersecting the picking ray
const intersects = raycaster.intersectObjects(scene.children, true);
if (intersects.length > 0) {
// Check if the clicked object is a player piece
const clickedObject = intersects[0].object;
const playerPiece = pachisi.findPieceByMesh(clickedObject);
if (playerPiece && pachisi.canMovePiece(playerPiece)) {
const currentPlayer = pachisi.currentPlayer;
const pieceIndex =
pachisi.players[currentPlayer].pieces.indexOf(playerPiece);
// Ask the player which dice value to use
const diceValueToUse = prompt(
`Choose which dice value to use (${pachisi.diceValues.join(" or ")}):`,
pachisi.diceValues[0]
);
if (pachisi.diceValues.includes(Number(diceValueToUse))) {
pachisi.movePiece(currentPlayer, pieceIndex, Number(diceValueToUse));
// Remove the used dice value
const indexToRemove = pachisi.diceValues.indexOf(
Number(diceValueToUse)
);
if (indexToRemove !== -1) {
pachisi.diceValues.splice(indexToRemove, 1);
}
if (pachisi.movesRemaining === 0) {
alert("Turn ended. Next player's turn.");
} else {
alert(
`You have ${
pachisi.movesRemaining
} move(s) remaining with value(s): ${pachisi.diceValues.join(", ")}`
);
}
} else {
alert("Invalid dice value selected. Please try again.");
}
}
}
}
// Animation loop
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();