Options
All
  • Public
  • Public/Protected
  • All
Menu

The renderer class takes in map data and enemy entities and displays it on the user's screen from the perspective of the player.

It does so by using the browser's Canvas Context Graphics API: See https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D

It uses the RayCaster class to render pseudo 3D using only 2D elements. The enemies are rendered separately, NOT using raycasting, but as whole sprites using trigonometry from MathHelper. The first person weapon is drawn as a whole image aswell.

Hierarchy

  • Renderer

Index

Constructors

constructor

  • new Renderer(canvas: any, resolution: number, drawDistance?: number, focalLen?: number, floorGradient?: ColorGradient, ceilGradient?: ColorGradient, windowMock?: any): Renderer
  • Parameters

    • canvas: any
    • resolution: number

      Amount of vertical bars to raycast. canvas_sizes > resolution.

    • Optional drawDistance: number

      How many units away do we stop drawing walls

    • Optional focalLen: number

      focal length for our drawing calls

    • Optional floorGradient: ColorGradient

      What color (gradient) the floor should be drawn in

    • Optional ceilGradient: ColorGradient

      What color (gradient) the ceiling should be drawn in

    • Optional windowMock: any

      Mocked window object, used exclusively for testing.

    Returns Renderer

Properties

Private barDepthBuffer

barDepthBuffer: number[]

Buffer to record at what distances the individual bars were drawn from the player. This is crucial to determine whether we are allowed to draw a Sprite or Enemy. Is used as a lookup table. Mapping is [x_coor] -> distance_from_player

Private canvas

canvas: any

Canvas DOM element to draw in (reference)

Private ceilingGradient

ceilingGradient: ColorGradient

The color gradient to be shown above (ceiling)

Private context

context: any

Javascript Graphics Context 2D (reference)

Private drawDistance

drawDistance: number

Determines how far away the walls are rendered. 1 means a maximum of 1 block gets rendered in the distance. A lower number means lower work load for the CPU. 1 unit = 1 MapElement

Private fieldOfView

fieldOfView: number

Field of view of the player. Note that this is in radians and set depending on sensor width and the focal length.

Private floorGradient

floorGradient: ColorGradient

The color gradient to be shown below (floor)

Private focalLength

focalLength: number

Determines the field of view. Ranges from 0 to 1. This refers to the y-vector length in the unit circle. The shorter it is,the more you see. https://en.wikipedia.org/wiki/Focal_length

Private halfFieldOfView

halfFieldOfView: number

Shall equal 0.5 * this.fieldOfView. Why as variable?: Because it's used frequently and we want to save ourselves the divisions.

Private originalCeilingNearColor

originalCeilingNearColor: string

The near color (#RRGGBB) for resetting the ceiling

Private originalFloorNearColor

originalFloorNearColor: string

The near color (#RRGGBB) for resetting the floor

Private resolution

resolution: number

Rendering resolution. Determines how many rays are cast. A lower number means lower work load for the CPU. The bigger this is, the more detailed the walls are since they have more bars to represent them.

Private screenRatio

screenRatio: number

Screen width to FOV ratio. Necessary for drawing the enemy on screen on the x-axis.

Private spacing

spacing: number

Bar spacing. Determines how wide the bars are are. Example: If our resolution is 500 and our canvas is 1000, then spacing is 2, so walls are made of vertical bars 2px wide each. Note that the higher this (and thus resolution), the more rays are being cast.

Static Private Readonly CROSSHAIR_COLOR

CROSSHAIR_COLOR: "#a714a2" = '#a714a2'

Color of the crosshair.

Static Private Readonly DEATH_SCREEN_COLOR

DEATH_SCREEN_COLOR: "#a714a2" = '#a714a2'

Color of the death screen (if the Player dies)

Static Private Readonly DEATH_SCREEN_MAX_OPACITY

DEATH_SCREEN_MAX_OPACITY: 0.5 = 0.5

Maximum opacity of the death screen (if the Player dies)

Static Private Readonly FONT_BASE_SIZE

FONT_BASE_SIZE: 64 = 64

Default font size for player tags.

Static Private Readonly FONT_COLOR

FONT_COLOR: "#FFFFFF" = '#FFFFFF'

Default font color for player tag

Static Private Readonly FONT_NAME

FONT_NAME: "ArcadeClassic" = 'ArcadeClassic'

Default font for player tags.

Static Private Readonly SENSORWIDTH

SENSORWIDTH: 1 = 1

Used for calculating this.fieldOfView

Static Private Readonly STANDARD_DRAWDISTANCE

STANDARD_DRAWDISTANCE: 16 = 16

Default value for this.drawDistance

Static Private Readonly STANDARD_FOCAL

STANDARD_FOCAL: 0.75 = 0.75

Default value for this.focalLength

Accessors

getResolution

  • get getResolution(): number

Methods

Private calculateWall

  • calculateWall(height: number, angle: number, distance: number): { height: number; top: number }
  • Calculate bottom, top and height of wall.

    Parameters

    • height: number

      Height of current bar. WARNING: TODO BUG currently will only draw correctly walls as walls, if we add another wall type with the ENUM ID 2, then it may cause issues.

    • angle: number

      Angle of current bar.

    • distance: number

      Distance to the bar.

    Returns { height: number; top: number }

    Top x coordinate where the wall begins and how tall it is (height)

    • height: number
    • top: number

changeResolution

  • changeResolution(delta: number): void
  • Changes the resolution to enable dynamic resolution scaling.

    Parameters

    • delta: number

      By how much we change the current resolution.

    Returns void

deathScreen

  • deathScreen(): void

Private drawBar

  • drawBar(room: Room, bar: number, steps: Step[], rayAngle: number, player: Player): void
  • Draw a single bar. Remember that multiple bars add up to a wall. How "tall" a bar is rendered is determined by how far away the ray has hit (the more steps, the smaller the bar appears).

    Parameters

    • room: Room

      Needed for wall texture resources

    • bar: number

      The n'th bar we're drawing up to this.resolution

    • steps: Step[]

      The individual steps the ray took

    • rayAngle: number

      In radians, what the ray is angled from the player

    • player: Player

    Returns void

Private drawCeiling

  • drawCeiling(): void

Private drawCrosshair

  • drawCrosshair(radius?: number, size?: number, thickness?: number, color?: string): void
  • (Dimensional units in px)

    Parameters

    • radius: number = 15

      How far the horizontal lines are from the center

    • size: number = 10

      How wide the horizontal crosshair line is

    • thickness: number = 3

      How thick the lines are

    • color: string = ...

      Color of the whole crosshair (example: '#AABBCC')

    Returns void

Private drawEnemyList

  • drawEnemyList(enemies: Enemy[], player: Player): void

Private drawFloor

  • drawFloor(): void
  • Draw floor as a gradient rectangle covering the bottom half of the screen. Rendering order is between skybox and walls for desired effect.

    Returns void

Private drawNameTag

  • drawNameTag(position: Vec2D, distanceFactor: number, enemyName: string): void
  • Draws the Enemy name tag on screen.

    Parameters

    • position: Vec2D

      Position of the enemy

    • distanceFactor: number

      Factor for how far away the player is

    • enemyName: string

    Returns void

Private drawSkyBox

  • drawSkyBox(skybox: Image2D, direction: number): void
  • Drawing the skybox which encompasses both sky and floor.

    Parameters

    • skybox: Image2D
    • direction: number

      Viewing angle in radients. (Totally rad)

    Returns void

Private drawSprites

  • Draw sprites stored in a room in respect to the players position, direction, and the renderers drawing distance.

    Parameters

    Returns void

Private drawWalls

  • Draw the walls.

    Parameters

    • room: Room

      Room is the source of walls

    • player: Player

      Player used mainly for positioning and look direction

    Returns void

Private drawWeapon

  • drawWeapon(player: Player, time: number): void
  • Draw the player's weapon with a "bobbing" animation (weapon swaying from left to right to simulate walking from the players perspective).

    Parameters

    • player: Player

      Pass the player to get his weapon

    • time: number

      Needed for time synchronization of the bobbing animation

    Returns void

Private muzzleFlashGradientCheck

  • muzzleFlashGradientCheck(weapon: Weapon): void

render

  • render(room: Room, player: Player, time: number): void
  • Main render call to be called each gameloop tick

    Parameters

    • room: Room

      Room to be drawn with it's skybox, etc

    • player: Player

      player instance

    • time: number

    Returns void

Private setUpCeilingGradient

  • Sets up the color gradient on the graphics context for drawing

    Parameters

    • colFloorGradient: ColorGradient

      Coloring of the floor from close to far away

    Returns void

Private setUpFloorGradient

  • Sets up the color gradient on the graphics context for drawing

    Parameters

    • colFloorGradient: ColorGradient

      Coloring of the floor from close to far away

    Returns void

Generated using TypeDoc