Spaces:
Running
Running
File size: 4,487 Bytes
2a6d2b0 601f069 2a6d2b0 601f069 2a6d2b0 |
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 |
class Character {
constructor(gameState, characterImg) {
this.gameState = gameState;
this.characterImg = characterImg;
this.characterPos = null;
this.previousPos = null;
this.path = [];
this.isMoving = false;
this.moveInterval = null;
this.img = characterImg;
this.speed = 270;
}
getCharacterPosition() {
return this.characterPos;
}
moving_check() {
}
setCharacterPosition(pos) {
this.characterPos = pos;
}
getCurrentRoom() {
if (!this.characterPos) return null;
// Check each room's boundaries to find which room contains the current position
for (const room of this.gameState.map_data.rooms) {
if (this.characterPos.x >= room.start_col &&
this.characterPos.x <= room.end_col &&
this.characterPos.y >= room.start_row &&
this.characterPos.y <= room.end_row) {
return room.name;
}
}
return null; // Return null if not in any room
}
drawPath(CELL_SIZE) {
if (this.path.length > 0 && this.isMoving) {
noFill();
stroke(255, 0, 0);
strokeWeight(2);
line(
this.characterPos.x * CELL_SIZE + CELL_SIZE / 2,
this.characterPos.y * CELL_SIZE + CELL_SIZE / 2,
this.path[0].x * CELL_SIZE + CELL_SIZE / 2,
this.path[0].y * CELL_SIZE + CELL_SIZE / 2
);
for (let i = 0; i < this.path.length - 1; i++) {
line(
this.path[i].x * CELL_SIZE + CELL_SIZE / 2,
this.path[i].y * CELL_SIZE + CELL_SIZE / 2,
this.path[i + 1].x * CELL_SIZE + CELL_SIZE / 2,
this.path[i + 1].y * CELL_SIZE + CELL_SIZE / 2
);
}
strokeWeight(1);
}
}
moveCharacterAlongPath(callback=null) {
if (this.moveInterval) {
clearInterval(this.moveInterval);
}
this.isMoving = true;
this.moveInterval = setInterval(() => {
if (this.path.length === 0) {
this.isMoving = false;
clearInterval(this.moveInterval);
if (callback) callback();
return;
}
this.previousPos = { ...this.characterPos };
const nextPos = this.path.shift();
this.characterPos = nextPos;
this.moving_check();
}, this.speed);
}
findPath(start, end) {
const queue = [[start]];
const visited = new Set();
const key = pos => `${pos.x},${pos.y}`;
visited.add(key(start));
while (queue.length > 0) {
const currentPath = queue.shift();
const current = currentPath[currentPath.length - 1];
if (current.x === end.x && current.y === end.y) {
return currentPath;
}
const neighbors = [
{ x: current.x, y: current.y - 1 },
{ x: current.x + 1, y: current.y },
{ x: current.x, y: current.y + 1 },
{ x: current.x - 1, y: current.y }
];
for (const next of neighbors) {
if (next.x < 0 || next.x >= GRID_COLS || next.y < 0 || next.y >= GRID_ROWS) continue;
if (visited.has(key(next))) continue;
const cell = this.gameState.map_data.grid[next.y][next.x];
if (cell.type === 'wall') continue;
visited.add(key(next));
queue.push([...currentPath, next]);
}
}
return [];
}
moveToPosition(pos,callback=null) {
if (!pos || !this.characterPos) return false;
this.path = this.findPath(this.characterPos, pos);
if (this.path.length > 0) {
this.isMoving = true;
this.moveCharacterAlongPath(callback);
return true;
}
return false;
}
// Movement methods
moveToRoom(roomName) {
const targetRoom = this.gameState.map_data.rooms
.find(room => room.name.toLowerCase() === roomName.toLowerCase());
if (!targetRoom || !this.characterPos) return false;
const destination = targetRoom.label_position;
if (!destination) return false;
this.moveToPosition(destination);
}
} |