Modules / Physics & movement

volley2dplugin

Bullets in a flat world, none of them a row: each a closed-form function of its volley's age.

Putting it in a scene

List it before render2d. The scene's sim lists the volley2d kind from #plugins/volley2d/volley2d.sim.ts, and the scene has one { "is": "volley2d" } actor, the ledger. A rule fires a volley, which is one entry on the ledger naming a pattern, where it was fired, which way and by whom. Every bullet of it is then a closed-form function of the volley's age, so the server tests it against what it may hit and every page draws it from the same few numbers. A hundred bullets on the screen cost a packet only the entries that fired them. The ledger's prefab holds exactly next, live and frame, and a row field that is none of the three fails.

"plugins": ["loader", "scenemanager", "player", "volley2d", "render2d"],
"world": {
  "loader": { "assets": { "shots": { "type": "json", "url": "/games/mygame/assets/shots.json", "preload": true } } },
  "volley2d": { "patterns": "shots", "bounds": [-16, -16, 336, 256], "layer": "things" }
},
"prefabs": {
  "volley2d": { "next": 1, "live": [], "frame": [0, 0] }
}

The patterns file holds every pattern, each a list of layers. A sheet names a sprite sheet in the same loader catalogue:

{
  "patterns": {
    "fan": [{ "n": 5, "spread": 0.8, "speed": 120, "radius": 3, "sheet": "bullets", "clip": "orb", "orient": true }],
    "spiral": [{ "n": 3, "ring": true, "bursts": 20, "every": 0.1, "turn": 0.3, "speed": 80, "accel": 20, "max": 160, "life": 4, "radius": 2, "sheet": "bullets", "clip": "dot" }],
    "double": [{ "pattern": "fan" }, { "pattern": "fan", "at": [0, 8], "after": 0.5 }]
  }
}

An angle is in radians from the world's x toward its y. In a world whose y grows down the screen, π/2 is straight down and a growing angle turns clockwise on the screen.

Properties

Read from world.volley2d.

.patterns : string

The loader catalogue name of the patterns file, a json entry.

.bounds : [left, top, right, bottom]

A box in the frame, in world units. A bullet outside it is gone.

.layer : string

The render2d layer a bullet is drawn on when its pattern names none.

The patterns file

A layer { "pattern": name } stands for every layer of that pattern, so patterns are built out of patterns. It is fired from at [x, y] off the volley's point, turned by dir radians, and after seconds later, each 0 when left out. A pattern that is built out of itself fails. Every other layer is an emitter, in the world's units and seconds.

KeyMeaning
nBullets in a burst. Default 1. They are fanned evenly across spread radians about the aim (default 0), or with ring evenly round the whole turn, the first on the aim.
bursts, every, afterHow many bursts, default 1; the seconds between them, default 0; and the seconds into the volley the first is fired, default 0.
turnRadians that each burst's aim is turned more than the last's, default 0. A volley of many bursts that turn is a spiral.
offsetRadians added to the volley's aim for this layer, default 0, so one layer can fire behind another.
from[x, y] off the volley's point that the layer's bullets start from, default [0, 0]. It is not turned by the aim.
speed, accel, min, maxSpeed at the start, default 0. accel is added to it every second, default 0, and it is held between min and max once it reaches either. Both are unbounded when left out.
curveRadians that the bullet's heading turns every second, default 0.
lifeThe seconds a bullet flies before it is gone, however far it is from the bounds. A bullet with none flies until it leaves the bounds.
radius, damageHow near a bullet's centre must come to what it may hit to hit it, default 0; and what it deals, default 1.
sheet, clip, orient, layerThe loader sheet a bullet is drawn from, and its clip, frame 0. A bullet whose pattern names no sheet is not drawn. With orient the frame is turned so its down lies along the bullet's heading. layer is the render2d layer it is drawn on, else world.volley2d.layer.

The ledger

Every bullet a volley can ever fire is numbered, layer by layer, burst by burst, bullet by bullet. The entry keeps the numbers of those that are gone, hit or out of the bounds, and of those that have grazed something. Each step, every entry is aged, and every bullet that has flown out of the bounds or past its life is gone. An entry with every bullet gone leaves the row.

FieldMeaning
nextThe id the next entry gets.
liveThe entries, each { id, pattern, side, at, dir, age, gone, grazed }. side is a name the game gives each side that shoots, at the point it was fired from in the frame, dir the aim, and age the seconds since it was fired.
frameThe world point the frame's origin stands on. A bullet's place in the world is the frame's point plus its place in the frame. A rule that scrolls the view moves the frame with it, and every bullet in the air is carried along, as a shooter's bullets are carried with the screen.

Methods

Rules

From #plugins/volley2d/volley2d.sim.ts.

fire( w, pattern, at, dir, side ) : Entry

Adds an entry for a volley fired from the world point at toward dir by side, and hands it back.

move( w, to )

Sets the frame's point to the world point to.

strike( w, side, targets ) : { hits, grazes }

Hits what the bullets of side meet. Each target is a circle { id, pos, r, graze? } in the world. A bullet whose centre comes within r and its own radius of a target's centre is gone, and one within graze more that is not yet gone and has not grazed before grazes. A bullet hits one target at most. It returns every hit { id, damage, entry, k, pos } and every graze { id, entry, k, pos }, with k the bullet's number in the volley, and the rule that asked does what a hit does.

clear( w, side ) : [x, y][]

Takes every bullet of that side out of the air, as a bomb does, and hands back where each was.

points( shots, entry, frame ) : Point[]

Every bullet of an entry in the air at its age, { k, pos, heading, shot }, which the browser half draws. shots is the list that shots(file, name) makes for a pattern.

shots( file, name ) : Shot[]

Every bullet of a pattern, numbered, from the patterns file.

ahead( shot, entry, frame, t ) : { pos, heading } | undefined

A bullet's place and heading at age t of its entry, for a playtester that looks ahead. Undefined before it is fired and after its life.

On the page

Through plugins.volley2d.

fire( pattern, at, dir ) : Entry

Fires a volley of the page's own, from the world point at toward dir, radians from the world's x toward its y. It is drawn from the next frame exactly as an entry of the row is, on no row and sent to no seat, which is what an editor previews a pattern with. It flies in the frame as the row's entries do, with its point taken off the row's frame the first frame it is drawn, so a view that scrolls carries it along. Its age is 0 the first frame it is drawn and grows by the scene's dt each frame after. Bullets that leave the bounds or pass their life are gone, and once every bullet is gone it leaves preview. One whose pattern the file does not have draws nothing and leaves preview that frame.

preview : readonly Entry[]

Every volley of the page's own in the air.

In the guide