diff --git a/src/game/player.ts b/src/game/player.ts index 61f4970..bf9b7d0 100644 --- a/src/game/player.ts +++ b/src/game/player.ts @@ -64,6 +64,10 @@ export class Player { this.model.position.set(v.x, v.y, v.z + 1) } + findVoxelByPos(x: number, y: number, z: number): Voxel | undefined { + return this.voxels.find(v => v.x === x && v.y === y && v.z === z) + } + reset() { this.voxelStandingOn = this.voxels[this.level.playerPos.voxelIdx] this.setToVoxelPosition(this.voxelStandingOn) @@ -114,13 +118,23 @@ export class Player { } jump() { + if (!this.voxelStandingOn?.neighs) { + this.doneAnimating() + return + } let x0 = this.voxelStandingOn let nextVoxel = this.voxelStandingOn.neighs[this.direction] if (nextVoxel["up"]) { - this.voxelStandingOn = nextVoxel["up"] + const targetX = nextVoxel["up"].x + const targetY = nextVoxel["up"].y + const targetZ = nextVoxel["up"].z + this.voxelStandingOn = this.findVoxelByPos(targetX, targetY, targetZ) || nextVoxel["up"] } if (nextVoxel["down"]) { - this.voxelStandingOn = nextVoxel["down"] + const targetX = nextVoxel["down"].x + const targetY = nextVoxel["down"].y + const targetZ = nextVoxel["down"].z + this.voxelStandingOn = this.findVoxelByPos(targetX, targetY, targetZ) || nextVoxel["down"] } this.actions['Jump'].reset() this.actions['Jump'].play() @@ -157,9 +171,16 @@ export class Player { } forward() { + if (!this.voxelStandingOn?.neighs) { + this.doneAnimating() + return + } let nextVoxel = this.voxelStandingOn.neighs[this.direction] if (nextVoxel["forward"]) { - this.voxelStandingOn = nextVoxel["forward"] + const targetX = nextVoxel["forward"].x + const targetY = nextVoxel["forward"].y + const targetZ = nextVoxel["forward"].z + this.voxelStandingOn = this.findVoxelByPos(targetX, targetY, targetZ) || nextVoxel["forward"] let x = this.model.position.x let y = this.model.position.y this.actions['Walking'].reset()