0.2.25 • Published 7 months ago

gdxts v0.2.25

Weekly downloads
-
License
-
Repository
-
Last release
7 months ago

gdxts

Lightweight 2D game development framework, target WebGL, inspired by libgdx.

Installation

$ npm install gdxts

Feature list

  • Texture
  • Mesh
  • Shader
  • SpriteBatch
  • PolygonBatch
  • OrthoCamera
  • Viewport
  • InputHandler
  • ViewportInputHandler
  • TextureRegion
  • TextureAtlas
  • Vector2
  • Vector3
  • Animation
  • ShapeRenderer
  • ScreenManager
  • BitmapFont

The features listed below will go to different packages:

Usage

Game loop

You can create your game loop by calling createGameLoop function.

import { createGameLoop } from 'gdxts';
...
const loop = createGameLoop((delta: number) => {
  // your update and rendering logic goes here!
});
loop.stop(); // stop/pause the game loop
loop.start(); // resume the game loop
loop.getFps(); // get the current FPS of the loop

Viewport

You can start using gdxts easily with the help of Viewport feature. It will provide responsive feature, keep track and update your orthographic camera and help convert coordinates between world/screen space.

import { createViewport, ViewportInputHandler, InputEvent } from 'gdxts';
...
const viewport = createViewport(canvas, WORLD_WIDTH, WORLD_HEIGHT);

// you can get your default camera by using
const camera = viewport.getCamera();

// and your WebGL context by
const gl = viewport.getContext();

// optionally, you can create a ViewportInputHandler
const inputHandler = new ViewportInputHandler(viewport);

InputHandler

You can use InputHandler for handling touch events.

For the event-based approach:

inputHandler.addEventListener(InputEvent.TouchStart, (x, y) => {
  const coord = inputHandler.getTouchedWorldCoord();
});

And the pooling approach:

let touched = false;
// somewhere inside the game loop
if (inputHandler.isTouched()) {
  const coord = inputHandler.getTouchedWorldCoord();
  if (touched) {
    // do something with the coord
  }
  touched = true;
}

Clearing the screen

gl.clearColor(0, 0, 0, 1);
createGameLoop((delta: number) => {
  gl.clear(gl.COLOR_BUFFER_BIT);
  // your rendering logic goes here!
});

Rendering a simple texture

Step 1: Create your SpriteBatch

const batch = new PolygonBatch(gl);

Step 2: Load your texture

const texture = await Texture.load(gl, '<your texture url>');

Step 3: Your typical rendering call

// somewhere inside your game loop
batch.setProjection(camera.combined);
batch.begin();
batch.draw(
  texture,
  x, // the x coord that you want to draw your texture, going from left to right
  y, // the x coord that you want to draw your texture, going from bottom to top
  width,
  height // the size you want to draw your texture, using world space unit
);
// other draws go here!
batch.end();

Using a batch can help you reduce your draw calls. Calling batch.end() will flush the batch and do the actual draw. But changing a uniform (change the projection matrix or use another texture) will flush the batch automatically too. Another case the batch is flushed automatically is when the maximum vertices count is reached.

Step 4: Control your draw (optional) You can rotate and scale your texture while drawing to the screen.

// somewhere inside your game loop
batch.setProjection(camera.combined);
batch.begin();
batch.draw(
  texture,
  x,
  y,
  width,
  height,
  width / 2,
  height / 2, // using the center of the sprite as the origin
  rotation, // an angle in rad
  scaleX,
  scaleY, // negative scale will flip the texture
  u1,
  v1,
  u2,
  v2 // custom UV, change it when you know what it is
);
batch.end();

Using TextureRegion and TextureAtlas

TextureRegion is a logical region of the texture, defined by a rectangle that cover a part of the texture.
You can split a texture into multiple even TextureRegion by calling

const cols = 4;
const rows = 4;
const regions = TextureRegion.splitTexture(texture, cols, rows);
...
// use your region for drawing
regions[0].draw(batch, x, y, width, height);

You can also use region.draw(batch, x, y, width, height, originX, originY, rotation, scaleX, scaleY); just like how you do it with Texture.

A TextureAtlas is the recommended method to work with regions.

const atlas = await TextureAtlas.load(gl, '<.atlas file url>'); // the load function is promise-based

// get a single region with a given name, packer tool usually use -1 index for this type of region
const region: TextureRegion = atlas.findRegion('start_button', -1);

// get multiple regions with the same name
const regions: TextureRegion[] = atlas.findRegion('character_run');

Animation

You can construct a 2D animation with a set of keyframes (TextureRegion) and a frame duration (time in seconds)

const runAnimation = new Animation(
  atlas.findRegions("char_run_full"),
  1 / 30
);

If the duration for each keyframe is different, you can use an array for frame durations.

const runAnimation = new Animation(
  atlas.findRegions("char_run_full"),
  [1 / 10, 1 / 10, 1 / 30] // this array will be filled with the last value
);

Drawing the animation: You can get the current keyframe as a TextureRegion and draw it using the batch

runAnimation.getKeyFrame(stateTime, PlayMode.LOOP).draw(batch, x, y, width, height);

Available PlayModes are LOOP, REVERSED, NORMAL, LOOP_REVERSED, LOOP_PINGPONG

Bitmap font

You can load a bitmapfont by calling

const font = await BitmapFont.load(gl, "./number.fnt", YDOWN, false);

The 3rd argument is flipped, useful for the case you want to choose the coordinate system. Usually, you must set flipped to true if you want to draw with y-down.

To draw some string use the loaded font

font.draw(batch, string, x, y, targetWidth, Align.left);

TODO: More documentation on advanced usage of bitmapfont and glyph.

0.2.25

7 months ago

0.2.24

7 months ago

0.2.23

7 months ago

0.2.22

7 months ago

0.2.21

7 months ago

0.2.20

7 months ago

0.2.19

7 months ago

0.2.18

7 months ago

0.2.17

8 months ago

0.1.45

8 months ago

0.1.46

8 months ago

0.1.47

8 months ago

0.2.16

8 months ago

0.2.15

8 months ago

0.2.14

8 months ago

0.2.13

8 months ago

0.2.12

8 months ago

0.2.11

8 months ago

0.2.10

8 months ago

0.2.1

8 months ago

0.2.0

8 months ago

0.2.7

8 months ago

0.2.6

8 months ago

0.2.9

8 months ago

0.2.8

8 months ago

0.2.3

8 months ago

0.2.2

8 months ago

0.2.5

8 months ago

0.2.4

8 months ago

0.1.41

8 months ago

0.1.42

8 months ago

0.1.43

8 months ago

0.1.44

8 months ago

0.1.40

8 months ago

0.1.39

8 months ago

0.1.37

11 months ago

0.1.38

11 months ago

0.1.36

1 year ago

0.1.34

1 year ago

0.1.35

1 year ago

0.1.30

1 year ago

0.1.31

1 year ago

0.1.32

1 year ago

0.1.33

1 year ago

0.1.27

1 year ago

0.1.28

1 year ago

0.1.29

1 year ago

0.1.22

2 years ago

0.1.23

2 years ago

0.1.24

2 years ago

0.1.25

1 year ago

0.1.26

1 year ago

0.1.21

2 years ago

0.1.20

2 years ago

0.1.19

2 years ago

0.1.18

2 years ago

0.1.17

2 years ago

0.1.16

2 years ago

0.1.15

2 years ago

0.1.14

2 years ago

0.1.13

2 years ago

0.1.12

2 years ago

0.1.11

2 years ago

0.1.10

2 years ago

0.1.9

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago