Giter Site home page Giter Site logo

game-engine-in-js's Introduction

Game Engine in JavaScript

Simple engine to create 2D games and animations with JavaScript and canvas object. Strongly inspired by CodingMath series and Unity3D engine.

It's substantially my first time with JS in OOP manner so there are many places where something can be done far better.

Game architecture

This engine based on classic game loop:

while(true)
{
    checkInput();
    updateGameState();
    render();
}

with small improvements. It provides many components and helper classes to make game creating a little easier. The most important thing for me is a fact, that game itself is separated from engine. Every game/animation is created in independent scene.

Components

There are several components in engine that allow to draw shapes, check user's input and so on:

Component Description
Camera Set point of view location and change canvases drawing start position
Game Main game engine class. It initializes all other components and starts the game
Graphics Provides basic functionality for drawing and transforming canvas context
Input Intercepts user's input from mouse and keyboard
KeyMap Contains keyboard keys definitions
Log Simple logging into div's body
Time Calculates all neccessary timing values like timeDelta, timeSinceStartup and so on
Utils Some helper classes

There are also some other classes like particle with physics, 2D vectors, primitive shapes, time accumulator etc.

Empty scene

All scenes have basis:

define(function(){
	
	function Scene() {
	
		var that = this;
		
		// called once at game start
		this.start = function(gameStatus, camera, input) {
			this.gameWidth = gameStatus.getWidth();
			this.gameHeight = gameStatus.getHeight();

			this.screenWidth = camera.getWidth();
			this.screenHeight = camera.getHeight();

			camera.setPointOfViewToCenter(); // set POV to canvas center
		};
		
		// called once every frame before render
		this.update = function(gameStatus, camera, input, time) {
			camera.moveTo(0, 0); // move camera to position
		}
		
		// called once per frame
		this.render = function(graphics, camera) {
			graphics.resetTransformToCamera(camera); // reset view to current camera
		}
	}
	
	return Scene;
})

which can be found in emptyScene file. There are three methods which executes in fixed order. They have suitable components as parameters. If you want to add another scene just copy emptyScene and write your game's logic in these functions.

Sample scenes

During development I've wrote some scenes:

  • Asteroids - well-known asteroids game clone
  • InputTest - scene which test almost all Input component functionalities
  • Rects - tons of random-coloured rectangles, good to test drawing performance
  • ScrollableScene - huge scene which tests camera component (moving camera to position, testing objects visibility etc.)
  • Fireworks - particles which can be emmitted by up arrow key or mouse click (uses objects pooling)

During engine development I will add more scenes.

RequireJS

Game engine is splitted into components with RequireJS. If you want to add new scene, add it to scenes directory and change proper dependency in main.js.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.