# misc/platformerbody (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/misc/platformerbody/

플랫포머 물리 컴포넌트.

## PlatformerBody extends Component

플랫포머 물리 컴포넌트.
- 중력 적분 + 바닥 접지 + 좌우 이동 경계처럼 횡스크롤 게임이 재발명하던
  최소 물리(운동학)를 담는다. 노드의 로컬 위치를 직접 움직인다.
- 사용:
    const body = node.addComponent(PlatformerBody);
    body.setGroundY(560);
    body.setHorizontalBounds(40, 920);
    // 이동: body.setVelocityX(±speed), 도약: body.jump(-720)

예제: 오른쪽으로 걷다가 누르면 뛰어오르기

```js
import { Engine, EngineConfiguration, Scene, WorldNode, Paint, PlatformerBody, Vector2, Pivot } from "./build/vanilla.js";

const playerNode = new WorldNode();
const paint = playerNode.addComponent(Paint);
paint.setColor("#ffcc00");
const platformerBody = playerNode.addComponent(PlatformerBody);
platformerBody.setGroundY(300);
platformerBody.setHorizontalBounds(40, 600);
platformerBody.setVelocityX(120);
platformerBody.setLandedEvent(() => {
	console.log("landed");
});

class MainScene extends Scene {
	initialize(engine) {
		super.initialize(engine);
		playerNode.setPivot(Pivot.bottomCenter);
		playerNode.setLocalPosition(Vector2.create(80, 100));
		playerNode.setContentSize(Vector2.create(32, 48));
		const root = this.getRoot();
		root.addChild(playerNode);
	}

	touchPress(viewInputPosition) {
		super.touchPress(viewInputPosition);
		platformerBody.jump(-720);
	}
}

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

- `constructor()` (constructor, since 0.3.0): 생성.
- `tick(timeDelta)` (method, since 0.3.0): 갱신. (속도 적분 → 중력 → 접지 → 경계)
  - `timeDelta` number: 지난 프레임부터의 시간. (초)
- `jump(jumpSpeed)` (method, since 0.3.0): 도약. (접지 중일 때만, 위쪽이 음수 속도)
  - `jumpSpeed` number: 음수면 위로.
  - returns boolean: 도약 성공 여부.

  예제: 엔진 없이 한 번 뛰어 가장 높이 오른 자리 재기

  ```js
  import { WorldNode, PlatformerBody } from "./build/vanilla.js";
  
  const node = new WorldNode();
  const platformerBody = node.addComponent(PlatformerBody);
  platformerBody.setGroundY(0);
  platformerBody.tick(1 / 60);
  console.log(platformerBody.isGrounded(), platformerBody.jump(-600));
  let highestY = 0;
  for (let frame = 0; frame < 60; ++frame) {
  	platformerBody.tick(1 / 60);
  	const position = node.getLocalPosition();
  	highestY = Math.min(highestY, position.y);
  }
  console.log(highestY.toFixed(1), platformerBody.isGrounded());
  ```

- `setVelocityX(velocityX)` (method, since 0.3.0): 가로 속도 설정. (픽셀 / 초)
  - `velocityX` number: 가로 속도. (픽셀 / 초)
- `setVelocityY(velocityY)` (method, since 0.3.0): 세로 속도 설정. (픽셀 / 초, 아래쪽이 양수)
  - `velocityY` number: 세로 속도. (픽셀 / 초, 위쪽이 음수)
- `getVelocity()` (method, since 0.3.0): 속도 반환. (픽셀 / 초)
  - returns Vector2: 속도. (픽셀 / 초, 아래쪽이 양수)
- `setGravity(gravity)` (method, since 0.3.0): 중력 가속도 설정. (픽셀 / 초², 아래쪽이 양수, 기본 2000)
  - `gravity` number: 중력 가속도. (픽셀 / 초², 아래쪽이 양수)
- `getGravity()` (method, since 0.3.0): 중력 가속도 반환. (픽셀 / 초²)
  - returns number: 중력 가속도. (픽셀 / 초²)
- `setGroundY(groundY)` (method, since 0.3.0): 바닥 높이 설정. (노드가 이 Y 아래로 내려가지 않는다, 기본 0)
  - `groundY` number: 바닥의 Y. (부모 기준 지역 좌표)
- `getGroundY()` (method, since 0.3.0): 바닥 높이 반환.
  - returns number: 바닥의 Y. (부모 기준 지역 좌표)
- `setHorizontalBounds(leftBound, rightBound)` (method, since 0.3.0): 좌우 이동 경계 설정. (null 이면 해당 방향 제한 없음)
  - `leftBound` number | null: 왼쪽 경계 X. (null 이면 제한 없음)
  - `rightBound` number | null: 오른쪽 경계 X. (null 이면 제한 없음)
- `isGrounded()` (method, since 0.3.0): 바닥에 닿아 있는지 여부.
  - returns boolean: 바닥에 닿아 있으면 참.
- `setLandedEvent(landedEvent)` (method, since 0.3.0): 착지 알림 설정. (공중에서 바닥에 닿는 순간)
  - `landedEvent` Function: 공중에서 바닥에 닿는 순간 부를 함수. () => void
