-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_collision_detection.html
More file actions
186 lines (154 loc) · 6.25 KB
/
test_collision_detection.html
File metadata and controls
186 lines (154 loc) · 6.25 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Collision Detection Test</title>
<style>
body {
margin: 0;
padding: 20px;
background: #000;
color: #00ff00;
font-family: monospace;
}
canvas {
border: 1px solid #333;
background: #111;
}
.controls {
margin-top: 10px;
}
button {
background: #333;
color: #00ff00;
border: 1px solid #555;
padding: 5px 10px;
margin: 5px;
cursor: pointer;
}
button:hover {
background: #555;
}
.info {
margin-top: 10px;
font-size: 12px;
}
</style>
</head>
<body>
<h2>Ghost-PacMan Collision Detection Test</h2>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<div class="controls">
<button onclick="testNormalCollision()">Test Normal Mode Collision</button>
<button onclick="testPowerCollision()">Test Power Mode Collision</button>
<button onclick="resetTest()">Reset Test</button>
</div>
<div class="info">
<div>Score: <span id="score">0</span></div>
<div>Lives: <span id="lives">3</span></div>
<div>Power Mode: <span id="powerMode">No</span></div>
<div>Game Status: <span id="gameStatus">playing</span></div>
<div id="collisionLog"></div>
</div>
<!-- Include game modules -->
<script src="js/mazeGraph.js"></script>
<script src="js/pacman.js"></script>
<script src="js/ghost.js"></script>
<script src="js/ghostAI.js"></script>
<script src="js/inputHandler.js"></script>
<script src="js/gameEngine.js"></script>
<script>
let gameEngine;
let canvas;
let ctx;
// Initialize test
function initTest() {
canvas = document.getElementById('gameCanvas');
ctx = canvas.getContext('2d');
// Create game engine
gameEngine = new GameEngine(canvas);
// Create a simple maze for testing
const mazeGraph = new MazeGraph(20, 15);
mazeGraph.createDemoMaze();
// Create input handler
const inputHandler = new InputHandler();
// Initialize entities
gameEngine.initializeEntities(inputHandler, mazeGraph);
// Start the game engine
gameEngine.start();
console.log('Collision detection test initialized');
updateUI();
}
function testNormalCollision() {
console.log('Testing normal mode collision...');
// Ensure power mode is off
gameEngine.gameState.powerPelletActive = false;
// Position PacMan and a ghost at the same location
const pacman = gameEngine.getPacMan();
const ghosts = gameEngine.getGhosts();
if (pacman && ghosts.length > 0) {
const ghost = ghosts[0];
// Set positions to be very close (collision)
pacman.setPosition(10, 10);
ghost.setPosition(10, 10);
// Force collision check
const collisions = gameEngine.getGhostAI().checkCollisions(pacman);
if (collisions.length > 0) {
gameEngine.handleGhostCollisions(collisions);
logCollision(`Normal mode collision detected with ${collisions.length} ghost(s)`);
} else {
logCollision('No collision detected - check collision radius');
}
}
updateUI();
}
function testPowerCollision() {
console.log('Testing power mode collision...');
// Enable power mode
gameEngine.activatePowerPellet(10000);
// Position PacMan and a ghost at the same location
const pacman = gameEngine.getPacMan();
const ghosts = gameEngine.getGhosts();
if (pacman && ghosts.length > 0) {
const ghost = ghosts[0];
// Set positions to be very close (collision)
pacman.setPosition(15, 10);
ghost.setPosition(15, 10);
// Force collision check
const collisions = gameEngine.getGhostAI().checkCollisions(pacman);
if (collisions.length > 0) {
gameEngine.handleGhostCollisions(collisions);
logCollision(`Power mode collision detected with ${collisions.length} ghost(s) - Ghost eaten!`);
} else {
logCollision('No collision detected - check collision radius');
}
}
updateUI();
}
function resetTest() {
console.log('Resetting test...');
gameEngine.resetGame();
clearCollisionLog();
updateUI();
}
function updateUI() {
const gameState = gameEngine.getGameState();
document.getElementById('score').textContent = gameState.score;
document.getElementById('lives').textContent = gameState.lives;
document.getElementById('powerMode').textContent = gameState.powerPelletActive ? 'Yes' : 'No';
document.getElementById('gameStatus').textContent = gameState.gameStatus;
}
function logCollision(message) {
const log = document.getElementById('collisionLog');
const timestamp = new Date().toLocaleTimeString();
log.innerHTML += `<div>[${timestamp}] ${message}</div>`;
}
function clearCollisionLog() {
document.getElementById('collisionLog').innerHTML = '';
}
// Initialize when page loads
window.addEventListener('load', initTest);
</script>
</body>
</html>