# misc/platformerbody (vanilla-engine 0.4.0)

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

플랫포머 물리 컴포넌트.
- 원본 misc/platformerbody.js 를 옮긴 것이다. 중력 적분, 바닥 접지, 좌우 이동 경계처럼 횡스크롤 게임이 다시 만들던
  최소 물리(운동학)를 담는다. 노드의 로컬 위치를 직접 움직인다. (아래쪽이 +Y)
- 사용:
  ```text
    let body = node_tree[hero_id].add_component(PlatformerBody::new());
    body.set_ground_y(560.0);
    body.set_horizontal_bounds(Some(40.0), Some(920.0));
    // 이동: body.set_velocity_x(±speed), 도약: body.jump(-720.0)
  ```
- 원본과 다른 점:
  경계의 null 은 None 이다. 착지 알림은 FnMut() 콜백이다.

## PlatformerBody (struct) (Default, Component)

플랫포머 물리 컴포넌트.
- tick 마다 integrate 로 다음 위치를 구해 노드 로컬 위치에 넣는다.

예제: 노드에 붙여 떨어뜨리고 바닥에서 뛰기

```rust
use vanilla::{Engine, EngineConfiguration, NodeTree, PlatformerBody, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let hero_id = node_tree.create_child(root_id, "hero");
node_tree[hero_id].set_local_position(Vector2::create(100.0, 0.0));
let body = node_tree[hero_id].add_component(PlatformerBody::new());
body.set_ground_y(560.0);
body.set_horizontal_bounds(Some(40.0), Some(920.0));

for _ in 0..60 {
	node_tree.tick(&engine, 1.0 / 60.0);
}
assert_eq!(node_tree[hero_id].get_local_position().y, 560.0);
let body = node_tree[hero_id].get_component_mut::<PlatformerBody>().unwrap();
assert!(body.is_grounded());
body.set_velocity_x(300.0);
assert!(body.jump(-720.0));
```

- `pub fn new() -> PlatformerBody` (constructor, since 0.3.2): 생성. (중력 초당 2000, 바닥 0, 경계 없음, 공중)
  - returns PlatformerBody: 속도가 0 인 새 플랫포머 물리.
- `pub fn integrate(&mut self, current_position: Vector2, time_delta: f64) -> Vector2` (method, since 0.3.2): 한 걸음 적분. (속도에 중력 → 위치 → 접지 → 경계. 다음 위치를 돌려준다)
  - tick 이 부른다. 노드 없이도 시험할 수 있게 떼어 냈다.
  - 바닥에 닿으면 세로 속도를 0 으로 하고, 공중에 있다가 닿은 순간이면 착지 알림을 부른다.
  - `current_position` Vector2: 지금 위치. (부모 영역 기준 로컬 좌표, 아래쪽이 +Y)
  - `time_delta` f64: 지난 시간. (초)
  - returns Vector2: 다음 위치. (y 는 바닥 높이를 넘지 않고, x 는 좌우 경계 안으로 자른다)

  예제: 노드 없이 한 걸음 적분하기

  ```rust
  use vanilla::{PlatformerBody, Vector2};
  
  let mut body = PlatformerBody::new();
  body.set_ground_y(100.0);
  let next_position = body.integrate(Vector2::create(0.0, 0.0), 0.1);
  assert_eq!(next_position, Vector2::create(0.0, 20.0));
  assert_eq!(body.get_velocity().y, 200.0);
  assert!(!body.is_grounded());
  ```

- `pub fn jump(&mut self, jump_speed: f64) -> bool` (method, since 0.3.2): 도약. (접지 중일 때만, 위쪽이 음수 속도. 뛰었으면 true)
  - `jump_speed` f64: 세로 속도로 넣을 값. (픽셀/초, 위로 뛰려면 음수)
  - returns bool: 접지 중이라 뛰었으면 true. 공중이면 false 이고 속도를 바꾸지 않는다.
- `pub fn set_velocity_x(&mut self, velocity_x: f64)` (method, since 0.3.2): 가로 속도 설정.
  - `velocity_x` f64: 가로 속도. (픽셀/초, 오른쪽이 +)
- `pub fn set_velocity_y(&mut self, velocity_y: f64)` (method, since 0.3.2): 세로 속도 설정.
  - `velocity_y` f64: 세로 속도. (픽셀/초, 아래쪽이 +)
- `pub fn get_velocity(&self) -> Vector2` (method, since 0.3.2): 속도 반환.
  - returns Vector2: 지금 속도. (픽셀/초)
- `pub fn set_gravity(&mut self, gravity: f64)` (method, since 0.3.2): 중력 설정. (초당 속도 변화량)
  - `gravity` f64: 중력. (픽셀/초², 아래쪽이 +, 기본 2000)
- `pub fn get_gravity(&self) -> f64` (method, since 0.3.2): 중력 반환.
  - returns f64: 지금 중력. (픽셀/초²)
- `pub fn set_ground_y(&mut self, ground_y: f64)` (method, since 0.3.2): 바닥 높이 설정. (부모 영역 기준 로컬 Y)
  - `ground_y` f64: 바닥 높이. (로컬 Y, 이 값 아래로 내려가지 않는다)
- `pub fn get_ground_y(&self) -> f64` (method, since 0.3.2): 바닥 높이 반환.
  - returns f64: 지금 바닥 높이. (로컬 Y)
- `pub fn set_horizontal_bounds(&mut self, left_bound: Option<f64>, right_bound: Option<f64>)` (method, since 0.3.2): 좌우 이동 경계 설정. (None 이면 그 쪽은 막지 않는다)
  - `left_bound` Option<f64>: 왼쪽 경계. (로컬 X, 이보다 작아지지 않는다)
  - `right_bound` Option<f64>: 오른쪽 경계. (로컬 X, 이보다 커지지 않는다)
- `pub fn is_grounded(&self) -> bool` (method, since 0.3.2): 접지 중인지.
  - returns bool: 지난 적분에서 바닥에 닿아 있었으면 true. 뛰면 false 가 된다.
- `pub fn set_landed_event(&mut self, landed_event: impl FnMut() + 'static)` (method, since 0.3.2): 착지 알림 설정. (공중에서 바닥에 닿는 순간)
  - `landed_event` impl FnMut() + 'static: 착지할 때 부를 함수. (앞의 알림은 바꾼다)

  예제: 착지 횟수 세기

  ```rust
  use std::cell::Cell;
  use std::rc::Rc;
  use vanilla::{PlatformerBody, Vector2};
  
  let landed_count = Rc::new(Cell::new(0));
  let landed_count_for_event = landed_count.clone();
  let mut body = PlatformerBody::new();
  body.set_ground_y(10.0);
  body.set_landed_event(move || {
  	landed_count_for_event.set(landed_count_for_event.get() + 1);
  });
  let mut position = Vector2::zero();
  for _ in 0..30 {
  	position = body.integrate(position, 0.1);
  }
  assert_eq!(landed_count.get(), 1);
  ```

