Changed around line 1
+ class Character {
+ constructor(element, x, y) {
+ this.element = element;
+ this.x = x;
+ this.y = y;
+ this.health = 100;
+ this.updatePosition();
+ }
+
+ updatePosition() {
+ this.element.style.left = `${this.x}px`;
+ this.element.style.top = `${this.y}px`;
+ }
+
+ moveBy(dx, dy) {
+ const worldBounds = document.getElementById('gameWorld').getBoundingClientRect();
+ this.x = Math.max(0, Math.min(this.x + dx, worldBounds.width - 32));
+ this.y = Math.max(0, Math.min(this.y + dy, worldBounds.height - 32));
+ this.updatePosition();
+ }
+ }
+
+ class Player extends Character {
+ constructor(element) {
+ super(element, 100, 100);
+ this.score = 0;
+ this.setupControls();
+ }
+
+ setupControls() {
+ document.addEventListener('keydown', (e) => {
+ const speed = 5;
+ switch(e.key) {
+ case 'ArrowUp': this.moveBy(0, -speed); break;
+ case 'ArrowDown': this.moveBy(0, speed); break;
+ case 'ArrowLeft': this.moveBy(-speed, 0); break;
+ case 'ArrowRight': this.moveBy(speed, 0); break;
+ }
+ });
+ }
+ }
+
+ class Enemy extends Character {
+ constructor(element) {
+ super(element, 300, 100);
+ this.patrolPoints = [[300, 100], [500, 100], [500, 300], [300, 300]];
+ this.currentPoint = 0;
+ this.startPatrol();
+ }
+
+ startPatrol() {
+ setInterval(() => {
+ const target = this.patrolPoints[this.currentPoint];
+ const dx = target[0] - this.x;
+ const dy = target[1] - this.y;
+ const distance = Math.sqrt(dx * dx + dy * dy);
+
+ if (distance < 2) {
+ this.currentPoint = (this.currentPoint + 1) % this.patrolPoints.length;
+ } else {
+ this.moveBy(dx/distance * 2, dy/distance * 2);
+ }
+ }, 50);
+ }
+ }
+
+ class NPC extends Character {
+ constructor(element) {
+ super(element, 200, 200);
+ this.setupInteraction();
+ this.startRandomMovement();
+ }
+
+ setupInteraction() {
+ this.element.addEventListener('click', () => {
+ const dialogue = document.getElementById('dialogue');
+ dialogue.textContent = "Hello traveler! Welcome to PixelQuest!";
+ dialogue.style.display = 'block';
+ setTimeout(() => {
+ dialogue.style.display = 'none';
+ }, 3000);
+ });
+ }
+
+ startRandomMovement() {
+ setInterval(() => {
+ const dx = (Math.random() - 0.5) * 4;
+ const dy = (Math.random() - 0.5) * 4;
+ this.moveBy(dx, dy);
+ }, 1000);
+ }
+ }
+
+ // Initialize game
+ document.addEventListener('DOMContentLoaded', () => {
+ const player = new Player(document.getElementById('player'));
+ const enemy = new Enemy(document.getElementById('enemy'));
+ const npc = new NPC(document.getElementById('npc'));
+
+ // Setup attack button
+ document.getElementById('attackBtn').addEventListener('click', () => {
+ const distance = Math.hypot(enemy.x - player.x, enemy.y - player.y);
+ if (distance < 50) {
+ enemy.health -= 20;
+ player.score += 10;
+ document.getElementById('playerScore').textContent = player.score;
+ if (enemy.health <= 30) {
+ enemy.moveBy((enemy.x - player.x) * 2, (enemy.y - player.y) * 2);
+ }
+ }
+ });
+ });