0.1.1 • Published 17 days ago

react-raycaster v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
17 days ago

banner

React Raycaster (react-raycaster)

NPM Version

A fully customizable raycaster game engine as a React component.

Check out a cool example here.

Installation

npm install react-raycaster

or

yarn add react-raycaster

How to use

import Raycaster from "react-raycaster";

// ...

const map = [
  [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
  [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
  [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  [1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  [1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
  [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
];

const tiles = {
  1: {
    type: "wall",
    src: "https://raw.githubusercontent.com/kingdcreations/react-raycaster/main/example/src/assets/tex/oak_planks.png",
    collision: true,
  },
  2: {
    type: "sprite",
    src: "https://raw.githubusercontent.com/kingdcreations/react-raycaster/main/example/src/assets/tex/barrel.png",
    collision: true,
  },
  3: {
    type: "door",
    src: "https://raw.githubusercontent.com/kingdcreations/react-raycaster/main/example/src/assets/tex/wood.png",
    collision: true,
  },
}

<Raycaster
  map={map}
  tiles={tiles}
  player={{
    x: 5,
    y: 17,
    rotation: -90
  }}
/>

Using the game context

import Raycaster from "react-raycaster";
import { Joystick } from 'react-joystick-component';

// ...

<Raycaster
  map={map}
  tiles={tiles}
  player={player}
>
  {g =>
    <>
      <Joystick
        move={(e) => e.x && e.y && g.joystickMove(e.x, e.y)}
        stop={() => g.joystickMove(0, 0)} />

      <Joystick
        move={(e) => {e.x && e.y && g.joystickCamera(e.x)}}
        stop={() => g.joystickCamera(0)} />
    </>
  }
</Raycaster>

Props

PropTypeDefaultDescription
mapnumber[][]required2D map array containing tiles
tilesTilesrequiredMap tiles definition (See below)
playerPlayerrequiredPlayer initial values
mousebooleanfalseAllows mouse camera rotation
inputsInputs(See below)Sets the rotation speed
widthnumber500Game x resolution in pixels
heightnumber300Game y resulition in pixels
shadingbooleantrueAllows depth shading
bobbingbooleantrueEnables run animation
showFPSbooleanfalseDisplays frames per second
skyboxstringnoneSource from the skybox to display
floorstringnoneSource from the floor to display
ceilingstringnoneSource from the ceiling to display
speednumber20Sets movement speed
rotSpeednumber3Sets the rotation speed

Game context

MethodDescription
joystickMove(x: number, y: number)Changes player x and y position on the map
joystickCamera(x: number)Rotates camera / player view

Types

Tiles

NameTypeDefaultDescription
[number]TilerequiredThe tile identified by a number

Tile

NameTypeDefaultDescription
type"wall" \| "sprite" \| "door"requiredType of the tile
srcstringrequiredSource image of the tile
collisionbooleanfalsePlayer collision against the tile

Player

NameTypeDefaultDescription
xnumberrequiredPlayer x value on map
ynumberrequiredPlayer y value on map
rotationnumber0Player rotation in degree

Inputs

NameTypeDefaultDescription
northstring"ArrowUp"Key code for north mouvement
eaststring"ArrowRight"Key code for east mouvement
southstring"ArrowDown"Key code for south mouvement
weststring"ArrowLeft"Key code for west mouvement
cameraLstringundefinedKey code for left camera rotation
cameraRstringundefinedKey code for right camera rotation
actionstring"Space"Key code for action triggering

TODO

  • Add sounds
  • Add more tile types
  • Add moving sprites
  • Add different walls height

About

I discovered raycasting as a project from 42 in C, this project is inspired by the world-famous Wolfenstein3D game, which was the first FPS ever.

Here is some useful links:

License

MIT

0.1.1

17 days ago

0.1.0

1 month ago

0.0.4

1 month ago

0.0.3

2 months ago

0.0.2

2 months ago

0.0.1

2 months ago

0.0.0

2 months ago