Skip to content

Commit

Permalink
fix: clean URL management
Browse files Browse the repository at this point in the history
  • Loading branch information
Gugustinette committed Aug 31, 2024
1 parent 5876d01 commit a571924
Show file tree
Hide file tree
Showing 33 changed files with 32 additions and 14 deletions.
File renamed without changes
File renamed without changes
File renamed without changes
16 changes: 8 additions & 8 deletions apps/playground-2d/src/level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FSprite } from '@fibbojs/2d'

function addBelowGround(scene: FScene, position: { x: number, y: number }) {
const ground = new FSprite(scene, {
texture: '/fibbo/playground-2d/ground_0122.jpeg',
texture: 'ground_0122.jpeg',
position,
})
ground.initCollider()
Expand All @@ -12,7 +12,7 @@ function addBelowGround(scene: FScene, position: { x: number, y: number }) {

function addGround(scene: FScene, position: { x: number, y: number }) {
const ground = new FSprite(scene, {
texture: '/fibbo/playground-2d/ground_0022.png',
texture: 'ground_0022.png',
position,
})
ground.initCollider()
Expand All @@ -21,7 +21,7 @@ function addGround(scene: FScene, position: { x: number, y: number }) {

function addBackground(scene: FScene, position: { x: number, y: number }) {
const background = new FSprite(scene, {
texture: '/fibbo/playground-2d/background_0000.jpg',
texture: 'background_0000.jpg',
position,
})
background.onLoaded(() => {
Expand All @@ -32,7 +32,7 @@ function addBackground(scene: FScene, position: { x: number, y: number }) {

function addBackgroundDark(scene: FScene, position: { x: number, y: number }) {
const background = new FSprite(scene, {
texture: '/fibbo/playground-2d/background_0016.png',
texture: 'background_0016.png',
position,
})
background.onLoaded(() => {
Expand All @@ -44,12 +44,12 @@ function addBackgroundDark(scene: FScene, position: { x: number, y: number }) {
function addBackgroundVariation(scene: FScene, position: { x: number, y: number }, variation = 0) {
const background = new FSprite(scene, {
texture: variation === 0
? '/fibbo/playground-2d/background_0008.webp'
? 'background_0008.webp'
: variation === 1
? '/fibbo/playground-2d/background_0009.png'
? 'background_0009.png'
: variation === 2
? '/fibbo/playground-2d/background_0010.png'
: '/fibbo/playground-2d/background_0011.png',
? 'background_0010.png'
: 'background_0011.png',
position,
})
background.onLoaded(() => {
Expand Down
2 changes: 1 addition & 1 deletion apps/playground-2d/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import { loadLevel } from './level'

// Create a sprite
const sprite = new FSprite(scene, {
texture: '/fibbo/playground-2d/bunny.png',
texture: 'bunny.png',
position: { x: 2, y: 3 },
})
sprite.onLoaded(() => {
Expand Down
20 changes: 19 additions & 1 deletion packages/2d/src/sprite/FSprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,26 @@ export class FSprite extends FComponent {
* @param texture The path to the texture.
*/
async loadTexture(texture: string) {
// Interpret path function
function interpretPath(path: string) {
// Resource URL (if it starts http, treat as a URL)
if (path.startsWith('http')) {
return path
}
// Absolute path (if it starts with /), add the current domain + path
else if (path.startsWith('/')) {
return `${window.location.href}${path}`
}
// Otherwise, treat as a relative path starting to the assets folder
else {
return `${window.location.href}/assets/${path}`
}
}

// Interpret the path
const path = interpretPath(texture)
// Load the texture
this.texture = await PIXI.Assets.load(texture)
this.texture = await PIXI.Assets.load(path)
this.texture.source.scaleMode = 'nearest'
// Create a sprite
this.container = new PIXI.Sprite(this.texture)
Expand Down
8 changes: 4 additions & 4 deletions packages/3d/src/model/FModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export abstract class FModel extends FComponent {

// Apply default options
const DEFAULT_OPTIONS = {
path: `/models/${options.name}/${options.name}.${options.fileExtension || 'obj'}`,
path: `/assets/${options.name}/${options.name}.${options.fileExtension || 'obj'}`,
textures: {},
fileExtension: 'obj',
}
Expand All @@ -86,9 +86,9 @@ export abstract class FModel extends FComponent {
else if (path.startsWith('/')) {
return `${window.location.href}${path}`
}
// Otherwise, treat as a relative path starting to the models folder
// Otherwise, treat as a relative path starting to the assets folder
else {
return `${window.location.href}/models/${path}`
return `${window.location.href}/assets/${path}`
}
}

Expand Down Expand Up @@ -165,7 +165,7 @@ export abstract class FModel extends FComponent {
}
else {
// Otherwise, use a default path
texturePath = `models/${this.name}/Textures/colormap.png`
texturePath = `assets/${this.name}/Textures/colormap.png`
}
// Load the texture
textureLoader.load(texturePath, (texture) => {
Expand Down

0 comments on commit a571924

Please sign in to comment.