waynemwashuma / chaos-engine

2d game engine
https://waynemwashuma.github.io/chaos-engine/
MIT License
3 stars 0 forks source link
game game-engine html5 html5-canvas javascript javascript-game javascript-library

Chaos-engine

DeepScan grade npm npm NPM GitHub Repo stars

This is a 2d game engine with physics,a basic canvas renderer,AI,audio management through web audio,an event system and many more features.

Features of this game engine.

Usage

Download and installation.

In order to get a copy of the engine ,enter the following command on your node project.

npm i chaos-studio

Import it into your project like this:

import * as CHAOS from "chaos-studio"

OR

Get the umd file from the dist folder of the repository, put it in your root directory and add it to your html page like so:

<head>
  <title>Load Example</title>
  <script src="https://github.com/waynemwashuma/chaos-engine/raw/main/chaos.umd.js"></script>
</head>

This way,the "CHAOS" module name will be available on the window object.

OR

import the library from unpkg.com like this:

<head>
  <script src="https://unpkg.com/chaos-studio@latest/dist/chaos.umd.js"></script>
</head>

Creating an example

Setup game

Now we can create a small demo. Add this to your html file in the body tag for this example to work

<div id="can"></div>

In your JavaScript file,do this:

//creates a new game manager for us to handle the game's entities.
const game = new CHAOS.Manager()

//we need to draw the entities on the screen.
//that is done through a renderer.
const renderer = new CHAOS.Renderer2D()

//this binds the renderer to html.
//the canvas will be a child to the queried  html element(renderer will attach it to the html element with id of "can")
//CHAOS.Renderer2D.bindTo(renderer,"#can")

//This sets the width and height of the renderer to cover the entire screen.
CHAOS.Renderer2D.setViewport(renderer,innerWidth, innerHeight)

//Enables physics for the game.
game.registerPlugin(new CHAOS.Physics2DPlugin({
  //applies gravity to the physics world.
  gravity:new CHAOS.Vector2(0,900)
}))

//Enables rendering to 2D context of canvas
game.registerPlugin(new CHAOS.Renderer2DPlugin(renderer))

Add a box to the game

So far there is nothing on the screen... Lets fix that.

//Creates an entity on which we can components are added to.
game.create({
  //transform contains the position , orientation and scale of the entity
  "transform": new CHAOS.Transform(renderer.width/2, 300),
  //movable contains components to enable movement i.e velocity and rotation
  "movable": new CHAOS.Movable(),
  //bound is used for broadphase collision detection in the physics engine
  "bound": new CHAOS.BoundingBox(),
  //body is used for physics interaction with other bodies.
  "body": new CHAOS.Box(50, 50),
  //sprite is the object renderered onto the canvas.
  "sprite": new CHAOS.Sprite(
    new CHAOS.BoxGeometry(50, 50),
    new CHAOS.BasicMaterial()
  )
})

Adding ground

Now you should see a box falling into nothingness. Lets add ground it can land on.

const body = new CHAOS.Box(400, 100)

//This makes the body immune to gravity and collisions with other bodies.
CHAOS.Box.setType(body, CHAOS.Box.STATIC)

//Creates an entity that will act as ground.
game.create({
  "transform": new CHAOS.Transform(renderer.width/2, renderer.height - 800),
  "movable": new CHAOS.Movable(),
  "bound": new CHAOS.BoundingBox(),
  "body": body,
  "sprite": new CHAOS.Sprite(
    new CHAOS.BoxGeometry(400, 100),
    new CHAOS.BasicMaterial()
  )
})

WARNING

This is not yet a stable version hence dont rely on it to make production apps yet.

FUTURE WORK