Skip to content
This repository was archived by the owner on Apr 23, 2024. It is now read-only.

scambier/impactjs-component

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

impactjs-component

This plugin aims to bring the component pattern, like in the Unity 3D engine, to Impact Entities.

A component is a piece of reusable code that can be (un)plugged to any Entity, at runtime. The component will then have access to the Entity's attributes.

Installation

Download the file lib/plugins/component.js and drop it in your project.

Usage

Create your components by subclassing the Component class, and require them in your entities, like any other dependency.

Basically, your components are defined like any other Impact ig.Class. You may pass a settings object at initialization that will override default values. The settings object cannot be used to create new attributes or overwrite functions.
Once the instance is created, simply inject it into your entity.

This project is also provided with a simple example: an Entity (a red square) is given a "clickable" component, that outputs a console.log each time the left mouse button is pressed down.

Call the entity from the component

Each component references its linked entity via the delegate attribute. If you have any experience with Unity 3D, the delegate is the equivalent of gameObject.

Link/unlink a component to an entity

var component = new MyComponentSubclass(parameters));
entity.addComponent(component);
entity.removeComponent(component);

Retrieve components

var components = entity.getComponents(MyComponentSubclass); // Retrieve an array containing all components that match the class
var component = entity.getComponents(MyComponentSubclass, true); // Retrieve only the first component to match the class
var component = entity.getCmp(MyComponentSubclass); // Shorthand for the above line

Events

Components have four built-in events.

  • added() is called just after addComponent()
  • update() is called just after the component's update() call
  • draw(), like update(), is called just after the component's draw() call
  • drawOver() will be called after the ig.game draw() function. Useful to draw UI elements like health bars.

About

An ImpactJS plugin to use the Entity-Component-System

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

  •  

Packages

No packages published