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

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

Hierarchical component processing object.

## ComponentNode extends Node

Hierarchical component processing object.
- Component features.
- Periodic update features. (including components)

Example: Attach, find and update components

```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): Constructor.
- `tick(timeDelta)` (method, since 0.0.11): Update.
  - `timeDelta` number: Time since the last frame. (Seconds)
- `markDrawBoundsDirty()` (method, since 0.4.0): Marks that the subtree draw bounds changed. (overridden by the drawing node TransformNode, nothing to do here)
  - Called when a component is added or removed, and when a component is enabled or disabled.
- `getOrAddComponent(componentType)` (method, since 0.2.1): Adds a component by type.
  - `componentType` Function: Component class. (attached if missing)
  - returns Component | null: The existing component or a newly attached one. (null if componentType is missing)
- `addComponent(componentType)` (method, since 0.0.11): Adds a component by type.
  - `componentType` Function: Component class to attach. (created with new and no arguments; attached again even if the same class exists)
  - returns Component | null: The newly attached component. (null if componentType is missing)
- `removeComponent(component)` (method, since 0.0.11): Removes a component by object.
  - `component` Component: Component to detach. (does nothing if not on this node)
- `hasComponent(componentType)` (method, since 0.0.11): Whether it has the component.
  - `componentType` Function: Component class to look for. (subclasses also match)
  - returns boolean: True if present.
- `getAllComponents()` (method, since 0.0.11): Returns the list of all components.
  - returns Component[]: List of attached components. (in attach order, the internal array as is)
- `getComponent(componentType)` (method, since 0.0.11): Returns the component for a type.
  - `componentType` Function: Component class to look for. (subclasses also match)
  - returns Component | null: The first matching component. (null if none)
- `getComponents(componentType)` (method, since 0.0.11): Returns all components for a type.
  - `componentType` Function: Component class to look for. (subclasses also match)
  - returns Component[]: All matching components. (a new array)
