Worked on pathfinding, character animation & rotation, few enhancements

This commit is contained in:
2024-07-29 09:01:54 +02:00
parent dbe25071c7
commit bbeac95512
5 changed files with 188 additions and 164 deletions

View File

@ -1,124 +1,124 @@
import { Character, Zone } from '@prisma/client';
import ZoneRepository from '../repositories/ZoneRepository';
import ZoneService from '../services/ZoneService';
import zoneRepository from '../repositories/ZoneRepository';
import { Character, Zone } from '@prisma/client'
import ZoneRepository from '../repositories/ZoneRepository'
import ZoneService from '../services/ZoneService'
import zoneRepository from '../repositories/ZoneRepository'
type TLoadedZone = {
zone: Zone,
characters: Character[],
zone: Zone
characters: Character[]
grid: number[][]
};
}
class ZoneManager {
private loadedZones: TLoadedZone[] = [];
private loadedZones: TLoadedZone[] = []
// Method to initialize zone manager
public async boot() {
if (!(await ZoneRepository.getById(1))) {
const zoneService = new ZoneService();
await zoneService.createDemoZone();
const zoneService = new ZoneService()
await zoneService.createDemoZone()
}
const zones = await ZoneRepository.getAll();
const zones = await ZoneRepository.getAll()
for (const zone of zones) {
await this.loadZone(zone);
await this.loadZone(zone)
}
console.log('[✅] Zone manager loaded');
console.log('[✅] Zone manager loaded')
}
// Method to handle individual zone loading
public async loadZone(zone: Zone) {
const grid = await this.getGrid(zone.id); // Create the grid for the zone
const grid = await this.getGrid(zone.id) // Create the grid for the zone
this.loadedZones.push({
zone,
characters: [],
grid
});
console.log(`[✅] Zone ID ${zone.id} loaded`);
})
console.log(`[✅] Zone ID ${zone.id} loaded`)
}
// Method to handle individual zone unloading
public unloadZone(zoneId: number) {
this.loadedZones = this.loadedZones.filter((loadedZone) => {
return loadedZone.zone.id !== zoneId;
});
console.log(`[❌] Zone ID ${zoneId} unloaded`);
return loadedZone.zone.id !== zoneId
})
console.log(`[❌] Zone ID ${zoneId} unloaded`)
}
// Getter for loaded zones
public getLoadedZones(): TLoadedZone[] {
return this.loadedZones;
return this.loadedZones
}
// Check if position is walkable
private isPositionWalkable(zoneId: number, x: number, y: number): boolean {
const loadedZone = this.loadedZones.find((lz) => lz.zone.id === zoneId);
return loadedZone ? loadedZone.grid[y][x] === 0 : false;
const loadedZone = this.loadedZones.find((lz) => lz.zone.id === zoneId)
return loadedZone ? loadedZone.grid[y][x] === 0 : false
}
public addCharacterToZone(zoneId: number, character: Character) {
const loadedZone = this.loadedZones.find((loadedZone) => {
return loadedZone.zone.id === zoneId;
});
return loadedZone.zone.id === zoneId
})
if (loadedZone && this.isPositionWalkable(zoneId, character.position_x, character.position_y)) {
loadedZone.characters.push(character);
loadedZone.characters.push(character)
}
}
public removeCharacterFromZone(zoneId: number, character: Character) {
const loadedZone = this.loadedZones.find((loadedZone) => {
return loadedZone.zone.id === zoneId;
});
return loadedZone.zone.id === zoneId
})
if (loadedZone) {
loadedZone.characters = loadedZone.characters.filter((loadedCharacter) => {
return loadedCharacter.id !== character.id;
});
return loadedCharacter.id !== character.id
})
}
}
public updateCharacterInZone(zoneId: number, character: Character) {
const loadedZone = this.loadedZones.find((loadedZone) => {
return loadedZone.zone.id === zoneId;
});
return loadedZone.zone.id === zoneId
})
if (loadedZone) {
const characterIndex = loadedZone.characters.findIndex((loadedCharacter) => {
return loadedCharacter.id === character.id;
});
return loadedCharacter.id === character.id
})
if (characterIndex !== -1) {
loadedZone.characters[characterIndex] = character;
loadedZone.characters[characterIndex] = character
}
}
}
public getCharactersInZone(zoneId: number): Character[] {
const loadedZone = this.loadedZones.find((loadedZone) => {
return loadedZone.zone.id === zoneId;
});
return loadedZone ? loadedZone.characters : [];
return loadedZone.zone.id === zoneId
})
return loadedZone ? loadedZone.characters : []
}
public async getGrid(zoneId: number): Promise<number[][]> {
const zone = this.loadedZones.find((z) => z.zone.id === zoneId);
if (zone) return zone.grid;
const zone = this.loadedZones.find((z) => z.zone.id === zoneId)
if (zone) return zone.grid
const loadedZone = await ZoneRepository.getById(zoneId);
if (!loadedZone) return [];
const loadedZone = await ZoneRepository.getById(zoneId)
if (!loadedZone) return []
let grid: number[][] = Array.from({ length: loadedZone.height }, () => Array.from({ length: loadedZone.width }, () => 0));
let grid: number[][] = Array.from({ length: loadedZone.height }, () => Array.from({ length: loadedZone.width }, () => 0))
const eventTiles = await zoneRepository.getEventTiles(zoneId);
const eventTiles = await zoneRepository.getEventTiles(zoneId)
// Set the grid values based on the event tiles, these are strings
eventTiles.forEach((eventTile) => {
if (eventTile.type === 'BLOCK') {
grid[eventTile.position_y][eventTile.position_x] = 1;
grid[eventTile.position_y][eventTile.position_x] = 1
}
});
})
return grid;
return grid
}
}
export default new ZoneManager();
export default new ZoneManager()