Skip to content

Commit

Permalink
fix(laser): additional changes to the chunk render.
Browse files Browse the repository at this point in the history
  • Loading branch information
h0lybyte committed Nov 9, 2024
1 parent 762d8d6 commit b15fc09
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
28 changes: 14 additions & 14 deletions packages/laser/src/lib/phaser/map/mapdatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class MapDatabase extends Dexie {
chunkHeight = 0;
tileWidth = 0;
tileHeight = 0;
chunkSizeX = 10;
chunkSizeY = 10;
scale = 1;
displayedChunks: Set<string> = new Set();

Expand Down Expand Up @@ -917,7 +919,9 @@ class MapDatabase extends Dexie {
this.resetMapSettings();

const mapData = await this.getMap(tilemapKey);
Debug.log(`Map data retrieved for ${tilemapKey}: ${JSON.stringify(mapData)}`);
Debug.log(
`Map data retrieved for ${tilemapKey}: ${JSON.stringify(mapData)}`,
);
if (!mapData) {
Debug.error(`Map data not found for ${tilemapKey}`);
return null;
Expand All @@ -928,23 +932,19 @@ class MapDatabase extends Dexie {
Debug.error(`Parsed JSON data for ${tilemapKey} not found.`);
return null;
} else {
Debug.log(`Loading the chunk data ${tilemapKey}`);
Debug.log(`Loaded the getParsedJsonData chunk data ${tilemapKey}`);
}

if (mapData && jsonData) {
this.tileWidth = jsonData.tilewidth || 32;
this.tileHeight = jsonData.tileheight || 32;
this.scale = mapData.scale || 1;

this.chunkWidth = this.tileWidth * (jsonData.chunkSizeX || 10);
this.chunkHeight = this.tileHeight * (jsonData.chunkSizeY || 10);

this.nbChunksX = Math.ceil(
jsonData.width / (jsonData.chunkSizeX || 10),
);
this.nbChunksY = Math.ceil(
jsonData.height / (jsonData.chunkSizeY || 10),
);

this.chunkWidth = this.tileWidth * this.chunkSizeX;
this.chunkHeight = this.tileHeight * this.chunkSizeY;

this.nbChunksX = Math.ceil(jsonData.width / this.chunkSizeX);
this.nbChunksY = Math.ceil(jsonData.height / this.chunkSizeY);

const tilesetKey = mapData.tilesetKey;
if (!scene.textures.exists(tilesetKey)) {
Expand Down Expand Up @@ -974,11 +974,11 @@ class MapDatabase extends Dexie {
}
return map; // Return the created tilemap
} else {
console.error(`Tileset ${tilesetKey} could not be created.`);
Debug.error(`Tileset ${tilesetKey} could not be created.`);
return null;
}
} else {
console.error(
Debug.error(
`Map data or JSON entry for tilemap key ${tilemapKey} not found.`,
);
return null;
Expand Down
28 changes: 21 additions & 7 deletions packages/laser/src/lib/phaser/player/playercontroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class PlayerController {
private cursor: Phaser.Types.Input.Keyboard.CursorKeys | undefined;
private wasdKeys!: { [key: string]: Phaser.Input.Keyboard.Key; };
private tooltip: Phaser.GameObjects.Text;
private tileSize = 48;


constructor(scene: Scene, gridEngine: any, quadtree: Quadtree) {
Expand All @@ -25,6 +26,7 @@ export class PlayerController {
font: '16px Arial',
backgroundColor: '#000000',
}).setDepth(4).setPadding(3,2,2,3).setVisible(false);
this.tileSize;
}

private initializeWASDKeys() {
Expand Down Expand Up @@ -195,18 +197,30 @@ export class PlayerController {
}

private checkForNearbyObjects() {
const tileSize = 48; // Adjust this based on your game's tile size
const playerPosition = this.gridEngine.getPosition('player') as Point;
const screenX = playerPosition.x * tileSize;
const screenY = playerPosition.y * tileSize;
const screenX = playerPosition.x * this.tileSize;
const screenY = playerPosition.y * this.tileSize;

const foundRanges = this.quadtree.query(playerPosition);

if (foundRanges.length > 0) {
this.tooltip.setPosition(screenX, screenY - 60).setVisible(true);
this.tooltip.setPosition(screenX, screenY - 60).setVisible(true);
} else {
this.tooltip.setVisible(false);
this.tooltip.setVisible(false);
}
}


// Method to get the player's X coordinate in the world
getPlayerCoordsX(): number {
const playerPosition = this.gridEngine.getPosition('player') as Point;
return playerPosition.x;
}

// Method to get the player's Y coordinate in the world
getPlayerCoordsY(): number {
const playerPosition = this.gridEngine.getPosition('player') as Point;
return playerPosition.y;
}

handleMovement() {
Expand Down

0 comments on commit b15fc09

Please sign in to comment.