// ############################
// # Game Core
// ############################
class Glitchhacked {
constructor() {
this.canvas = document.getElementById('game-canvas');
this.ctx = this.canvas.getContext('2d');
this.logElement = document.getElementById('log');
this.helpMenu = document.getElementById('help-menu');
this.glitchhacksList = document.getElementById('glitchhacks-list');
this.player = {
x: 10,
y: 10,
inventory: []
};
this.map = this.generateMap();
this.items = this.generateItems();
this.codeReplacements = []; // store glitchhack modifications here
this.bindEvents();
this.drawMap();
this.updateLog('Welcome to Glitchhacked!');
this.updateHelpMenu();
}
bindEvents() {
document.addEventListener('keydown', (e) => {
switch (e.key) {
case '7': this.movePlayer(-1, -1); break;
case '8': this.movePlayer(0, -1); break;
case '9': this.movePlayer(1, -1); break;
case '4': this.movePlayer(-1, 0); break;
case '6': this.movePlayer(1, 0); break;
case '1': this.movePlayer(-1, 1); break;
case '2': this.movePlayer(0, 1); break;
case '3': this.movePlayer(1, 1); break;
case 'i': this.showInventory(); break;
case 'u': this.useItem(); break;
case 'd': this.dropItem(); break;
case 'h': this.toggleHelpMenu(); break;
}
});
}
// ############################
// # Game Logic
// ############################
generateMap() {
let map = [];
for (let y = 0; y < 20; y++) {
map[y] = [];
for (let x = 0; x < 20; x++) {
map[y][x] = Math.random() < 0.2 ? 1 : 0; // 1 = wall, 0 = floor
}
}
return map;
}
generateItems() {
let items = [];
for (let i = 0; i < 10; i++) {
items.push({
name: `Glitchhack ${i}`,
description: `A mysterious item...`,
effect: this.getRandomGlitchhack()
});
}
return items;
}
getRandomGlitchhack() {
let glitchhacks = [
() => this.replaceCode('drawMap', 'this.ctx.fillStyle = "#f0f";'), // pink map
() => this.replaceCode('movePlayer', 'this.player.x = Math.random() * 20 | 0;'), // teleport x
() => this.replaceCode('updateLog', 'arguments[0] = "HACKED: " + arguments[0];'), // prefix log messages
() => this.codeReplacements.push(() => this.player.inventory.push(this.items[Math.random() * this.items.length | 0])), // auto-add random item
() => this.replaceCode('drawMap', 'this.ctx.scale(-1, 1);'), // flip map horizontally
() => this.codeReplacements.push(() => console.log('CHEAT ACTIVATED')), // secret console log
() => this.replaceCode('useItem', 'alert("YOU CAN\'T USE ITEMS ANYMORE!");'), // break item usage
() => this.codeReplacements.push(() => this.player.x += 1), // auto-move right
() => this.replaceCode('generateMap', 'return [[]];'), // empty map
() => this.codeReplacements.push(() => document.title = 'GLITCHHACKED') // change title
];
return glitchhacks[Math.random() * glitchhacks.length | 0];
}
replaceCode(funcName, newCode) {
try {
let func = this[funcName].toString();
let newFunc = new Function('return ' + func.replace(/^(function\s*\(.*?\)\s*{)/, `$1${newCode};`))();
this[funcName] = newFunc.bind(this);
this.updateLog(`Glitchhack applied: ${funcName} modified`);
} catch (e) {
this.updateLog(`Glitchhack failed: ${e.message}`);
}
}
applyCodeReplacements() {
this.codeReplacements.forEach((replacement) => replacement());
}
movePlayer(dx, dy) {
this.applyCodeReplacements();
let newX = this.player.x + dx;
let newY = this.player.y + dy;
if (this.map[newY] && this.map[newY][newX] !== 1) {
this.player.x = newX;
this.player.y = newY;
this.checkForItems();
}
this.drawMap();
}
checkForItems() {
let item = this.items.find((item) => item.x === this.player.x && item.y === this.player.y);
if (item) {
this.player.inventory.push(item);
this.items.splice(this.items.indexOf(item), 1);
this.updateLog(`Found item: ${item.name}`);
}
}
useItem() {
if (this.player.inventory.length > 0) {
let item = this.player.inventory.shift();
this.updateLog(`Using item: ${item.name}`);
item.effect(); // execute glitchhack
} else {
this.updateLog('No items to use');
}
}
dropItem() {
if (this.player.inventory.length > 0) {
let item = this.player.inventory.shift();
this.items.push(item);
item.x = this.player.x;
item.y = this.player.y;
this.updateLog(`Dropped item: ${item.name}`);
} else {
this.updateLog('No items to drop');
}
}
// ############################
// # Rendering & UI
// ############################
drawMap() {
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
for (let y = 0; y < this.map.length; y++) {
for (let x = 0; x < this.map[y].length; x++) {
if (this.map[y][x] === 1) {
this.ctx.fillStyle = "#666";
this.ctx.fillRect(x * 20, y * 20, 20, 20);
}
}
}
this.ctx.fillStyle = "#fff";
this.ctx.fillRect(this.player.x * 20, this.player.y * 20, 20, 20);
}
updateLog(message) {
let logEntry = document.createElement('div');
logEntry.textContent = message;
this.logElement.appendChild(logEntry);
this.logElement.scrollTop = this.logElement.scrollHeight;
}
showInventory() {
let inventoryList = this.player.inventory.map((item) => item.name).join(', ');
this.updateLog(`Inventory: ${inventoryList || '(empty)'}`);
}
updateHelpMenu() {
let glitchhacksHtml = this.items.map((item, index) => `<li>${item.name}: ${item.description}</li>`).join('');
this.glitchhacksList.innerHTML = glitchhacksHtml;
}
toggleHelpMenu() {
this.helpMenu.style.display = this.helpMenu.style.display === 'none' ? 'block' : 'none';
}
}
let game = new Glitchhacked();