# resource/spriteatlas (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/resource/spriteatlas/

찾을 아틀라스 목록. (SpriteAtlas.findFrame 이 앞에서부터 찾는다)

## SpriteAtlas extends Object

스프라이트 아틀라스. (쪽 그림 몇 장에 모은 그림을 이름으로 꺼낸다)
- 같은 쪽 그림을 쓰는 스프라이트는 엔진이 묶어 한 번에 그린다. (Graphic.setBatchingEnabled)
- 읽는 JSON 꼴 둘:
  1. tools/atlas.cjs 가 만든 것. { images: { "이름.png": { x, y, width, height, page } }, meta: { imageFileName, pages: [{ imageFileName }] } }
     page 가 없으면 0 쪽, meta.pages 가 없으면 meta.imageFileName 한 장이다.
  2. 쪽 경로와 [쪽, x, y, 가로, 세로] 표. { pagePaths: ["경로"], frames: { "이름": [쪽, x, y, 가로, 세로] } }
- 그림 이름은 확장자를 뗀 이름으로 찾는다. ("icon-close.png" 는 "icon-close" 로도 "icon-close.png" 로도 찾는다)
- 쪽 경로에 빗금(/)이 없으면 JSON 이 있는 폴더를 기준으로, 있으면 적힌 그대로 읽는다.
- 여러 아틀라스를 addSearchAtlas 로 넣어 두면 findFrame, applyFrame(정적) 이 넣은 차례로 찾는다. (앞의 것이 이긴다)

예제: 찾을 아틀라스에 넣고 스프라이트에 조각 걸기

```js
import { Engine, EngineConfiguration, Scene, WorldNode, Sprite, SpriteAtlas, Vector2 } from "./build/vanilla.js";

const spriteAtlas = new SpriteAtlas();
await spriteAtlas.loadData({
	pagePaths: ["examples/assets/spritesheet.png"],
	frames: {
		"walk-0.png": [0, 0, 0, 200, 200],
		"walk-1.png": [0, 200, 0, 200, 200],
	},
});
SpriteAtlas.addSearchAtlas(spriteAtlas);

class MainScene extends Scene {
	initialize(engine) {
		super.initialize(engine);
		const node = new WorldNode();
		node.setContentSize(Vector2.create(200, 200));
		node.setLocalPosition(Vector2.create(100, 100));
		const root = this.getRoot();
		root.addChild(node);
		const sprite = node.addComponent(Sprite);
		const isFound = SpriteAtlas.applySearchFrame(sprite, "walk-1");
		console.log(isFound);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
```

- `constructor()` (constructor, since 0.4.0): 생성.
- `async load(jsonPath)` (method, since 0.4.0): 아틀라스 JSON 과 쪽 그림 불러오기. (쪽 그림은 한꺼번에 받는다)
  - `jsonPath` string: 아틀라스 JSON 경로.
  - returns Promise<void>: 다 읽으면 풀리는 프로미스. (JSON 을 읽지 못하면 알리고 풀린다, 쪽 그림을 읽지 못하면 거부된다)
- `async loadData(atlasData, baseDirectory = "")` (method, since 0.4.0): 아틀라스 자료로 불러오기. (JSON 을 읽은 객체나 코드에 둔 같은 꼴의 객체, 쪽 경로에 빗금이 없으면 baseDirectory 를 앞에 붙인다)
  - `atlasData` object: 아틀라스 자료. (tools/atlas.cjs 꼴이나 pagePaths, frames 꼴, 다른 꼴이면 알리고 아무것도 하지 않는다)
  - `baseDirectory` string: 빗금 없는 쪽 경로 앞에 붙일 폴더. (끝의 빗금까지, 기본 빈 글)
  - returns Promise<void>: 쪽 그림을 다 읽으면 풀리는 프로미스. (읽지 못하면 거부된다)

  예제: 코드에 둔 아틀라스 자료로 읽기

  ```js
  import { SpriteAtlas } from "./build/vanilla.js";
  
  const spriteAtlas = new SpriteAtlas();
  await spriteAtlas.loadData({
  	pagePaths: ["examples/assets/spritesheet.png"],
  	frames: {
  		"walk-0.png": [0, 0, 0, 200, 200],
  		"walk-1.png": [0, 200, 0, 200, 200],
  	},
  });
  const frame = spriteAtlas.readFrame("walk-1");
  const imageRect = frame.getImageRect();
  console.log(spriteAtlas.getPageCount(), spriteAtlas.getFrameNames(), imageRect.x, spriteAtlas.hasFrame("walk-1.png"));
  ```

- `readFrame(frameName)` (method, since 0.4.0): 조각 읽기. (없으면 null)
  - `frameName` string: 그림 이름. (확장자를 떼어도 된다)
  - returns Frame | null: 조각. (없으면 null)
- `hasFrame(frameName)` (method, since 0.4.0): 조각이 있는지.
  - `frameName` string: 그림 이름.
  - returns boolean: 있으면 참.
- `applyFrame(sprite, frameName)` (method, since 0.4.0): 스프라이트에 조각 걸기. (없는 이름이면 그림을 비우고 false)
  - `sprite` Sprite: 조각을 걸 스프라이트.
  - `frameName` string: 그림 이름.
  - returns boolean: 찾아 걸었으면 참.
- `getPageImages()` (method, since 0.4.0): 쪽 그림 목록 반환.
  - returns HTMLImageElement[]: 쪽 그림 목록. (쪽 번호 차례)
- `getPageCount()` (method, since 0.4.0): 쪽 수.
  - returns number: 쪽 수.
- `getFrameNames()` (method, since 0.4.0): 이름 목록 반환. (확장자를 뗀 이름과 파일 이름이 함께 든다)
  - returns string[]: 이름 목록.
- `isLoaded()` (method, since 0.4.0): 불러왔는지.
  - returns boolean: 불러왔으면 참.
- `static addSearchAtlas(spriteAtlas)` (static, since 0.4.0): 찾을 아틀라스 넣기. (정적, 맨 뒤에 넣는다, 이미 들었으면 그대로)
  - `spriteAtlas` SpriteAtlas: 넣을 아틀라스.
- `static removeSearchAtlas(spriteAtlas)` (static, since 0.4.0): 찾을 아틀라스 빼기. (정적)
  - `spriteAtlas` SpriteAtlas: 뺄 아틀라스. (들어 있지 않으면 아무것도 하지 않는다)
- `static clearSearchAtlases()` (static, since 0.4.0): 찾을 아틀라스 모두 빼기. (정적)
- `static findFrame(frameName)` (static, since 0.4.0): 찾을 아틀라스들에서 조각 찾기. (정적, 넣은 차례로, 없으면 null)
  - `frameName` string: 그림 이름.
  - returns Frame | null: 처음 찾은 조각. (없으면 null)
- `static applySearchFrame(sprite, frameName)` (static, since 0.4.0): 찾을 아틀라스들에서 찾은 조각을 스프라이트에 걸기. (정적, 없으면 그림을 비우고 false)
  - `sprite` Sprite: 조각을 걸 스프라이트.
  - `frameName` string: 그림 이름.
  - returns boolean: 찾아 걸었으면 참.
- `static findFrameInOrder(atlasList, frameName)` (static, since 0.4.0): 넘긴 아틀라스들에서 차례로 조각 찾기. (정적, 앞의 것이 이긴다, 없으면 null, 찾을 아틀라스 목록을 쓰지 않는다)
  - `atlasList` SpriteAtlas[]: 찾을 아틀라스 목록. (앞의 것이 이긴다)
  - `frameName` string: 그림 이름.
  - returns Frame | null: 처음 찾은 조각. (없으면 null)

  예제: 계절 아틀라스를 기본 아틀라스보다 먼저 찾기

  ```js
  import { SpriteAtlas } from "./build/vanilla.js";
  
  const baseAtlas = new SpriteAtlas();
  await baseAtlas.loadData({
  	pagePaths: ["examples/assets/spritesheet.png"],
  	frames: {
  		"walk-0.png": [0, 0, 0, 200, 200],
  		"walk-1.png": [0, 200, 0, 200, 200],
  	},
  });
  const seasonAtlas = new SpriteAtlas();
  await seasonAtlas.loadData({
  	pagePaths: ["examples/assets/spritesheet.png"],
  	frames: {
  		"walk-0": [0, 400, 400, 200, 200],
  	},
  });
  const frame = SpriteAtlas.findFrameInOrder([seasonAtlas, baseAtlas], "walk-0");
  const imageRect = frame.getImageRect();
  const fallbackFrame = SpriteAtlas.findFrameInOrder([seasonAtlas, baseAtlas], "walk-1");
  const fallbackRect = fallbackFrame.getImageRect();
  console.log(imageRect.x, fallbackRect.x);
  ```

- `static applyFrameInOrder(atlasList, sprite, frameName)` (static, since 0.4.0): 넘긴 아틀라스들에서 차례로 찾은 조각을 스프라이트에 걸기. (정적, 없으면 그림을 비우고 false)
  - `atlasList` SpriteAtlas[]: 찾을 아틀라스 목록. (앞의 것이 이긴다)
  - `sprite` Sprite: 조각을 걸 스프라이트.
  - `frameName` string: 그림 이름.
  - returns boolean: 찾아 걸었으면 참.
