# core/node/componentnode (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/core/node/componentnode/

계층적 컴포넌트 처리 객체.

## ComponentNode extends Node

계층적 컴포넌트 처리 객체.
- 컴포넌트 기능.
- 주기적 갱신 기능. (컴포넌트 포함)

예제: 컴포넌트 붙이고 찾고 갱신하기

```js
import { ComponentNode, Component } from "./build/vanilla.js";

class Health extends Component {
	constructor() {
		super();
		this.hitPoint = 100;
	}
}

class Poison extends Component {
	require() {
		return [Health];
	}

	tick(timeDelta) {
		const node = this.getNode();
		const health = node.getComponent(Health);
		health.hitPoint -= 10 * timeDelta;
	}
}

const node = new ComponentNode();
node.addComponent(Poison);
const health = node.getComponent(Health);
node.tick(0.5);
const hasHealth = node.hasComponent(Health);
console.log(hasHealth, health.hitPoint);
```

- `constructor()` (constructor, since 0.0.11): 생성.
- `tick(timeDelta)` (method, since 0.0.11): 갱신.
  - `timeDelta` number: 지난 프레임부터의 시간. (초)
- `markDrawBoundsDirty()` (method, since 0.4.0): 하위 트리 그리기 경계가 바뀌었음을 표시. (그리는 노드 TransformNode 가 덮어쓴다, 여기서는 할 일이 없다)
  - 컴포넌트를 더하거나 뺄 때, 컴포넌트를 켜고 끌 때 부른다.
- `getOrAddComponent(componentType)` (method, since 0.2.1): 타입으로 컴포넌트 추가.
  - `componentType` Function: 컴포넌트 클래스. (없으면 붙인다)
  - returns Component | null: 있던 컴포넌트나 새로 붙인 컴포넌트. (componentType 이 없으면 null)
- `addComponent(componentType)` (method, since 0.0.11): 타입으로 컴포넌트 추가.
  - `componentType` Function: 붙일 컴포넌트 클래스. (인자 없이 new 로 만들고, 같은 클래스가 있어도 또 붙인다)
  - returns Component | null: 새로 붙인 컴포넌트. (componentType 이 없으면 null)
- `removeComponent(component)` (method, since 0.0.11): 객체로 컴포넌트 제거.
  - `component` Component: 뗄 컴포넌트. (이 노드에 없으면 아무것도 하지 않는다)
- `hasComponent(componentType)` (method, since 0.0.11): 컴포넌트 보유 여부..
  - `componentType` Function: 찾을 컴포넌트 클래스. (자식 클래스도 맞는다)
  - returns boolean: 있으면 참.
- `getAllComponents()` (method, since 0.0.11): 모든 컴포넌트 목록 반환.
  - returns Component[]: 붙은 컴포넌트 목록. (붙인 차례, 안의 배열 그대로)
- `getComponent(componentType)` (method, since 0.0.11): 타입에 대한 컴포넌트 반환.
  - `componentType` Function: 찾을 컴포넌트 클래스. (자식 클래스도 맞는다)
  - returns Component | null: 처음 맞는 컴포넌트. (없으면 null)
- `getComponents(componentType)` (method, since 0.0.11): 타입에 대한 모든 컴포넌트 반환.
  - `componentType` Function: 찾을 컴포넌트 클래스. (자식 클래스도 맞는다)
  - returns Component[]: 맞는 컴포넌트 모두. (새 배열)
