1
0
Fork 0
Browse Source

feat: apply gravity to objects

Signed-off-by: André Jaenisch <andre.jaenisch@posteo.de>
combat-scorched-earth-from-outer-space
André Jaenisch 10 months ago
parent
commit
1763fda79a
Signed by: ryuno-ki
GPG Key ID: 5A668E771F1ED854
  1. BIN
      js13kgames.zip
  2. 56
      src/js/draw.js
  3. 2
      types/draw.d.ts

BIN
js13kgames.zip

Binary file not shown.

56
src/js/draw.js

@ -1,4 +1,9 @@
import { FPS } from './constants.js'
import { computeNormals } from './shape.js'
import { add, rotate, scale } from './vector.js'
/** @typedef {import('./shape').Shape} Shape */
/** @typedef {import('./vector').Vector2D} Vector2D */
/**
* Draws a shape onto the canvas.
@ -11,6 +16,8 @@ export function drawShape (context, shape) {
prepareCanvas(context, shape)
draw(context, shape)
context.restore()
updatePosition(shape)
updateRotation(shape)
}
/**
@ -36,3 +43,52 @@ function draw (context, shape) {
const { W, H } = shape
context.strokeRect(-W / 2, -H / 2, W, H)
}
/**
* Update the position of the shape.
*
* @param {Shape} shape
*/
function updatePosition (shape) {
shape.V = add(shape.V, scale(shape.A, 1 / FPS))
moveShape(shape, scale(shape.V, 1 / FPS))
}
/**
* Move a shape along a vector.
*
* @param {Shape} shape
* @param {Vector2D} v
*/
function moveShape (shape, v) {
shape.C = add(shape.C, v)
shape.X.forEach(function (vertex, index) {
shape.X[index] = add(vertex, v)
})
}
/**
* Update the rotation of the shape.
*
* @param {Shape} shape
*/
function updateRotation (shape) {
shape.v = shape.v + shape.a * 1 / FPS
rotateShape(shape, shape.v * 1 / FPS)
}
/**
* Rotate a shape around its centre.
*
* @param {Shape} shape
* @param {number} angle
*/
function rotateShape (shape, angle) {
shape.G = shape.G + angle
shape.X.forEach(function (vertex, index) {
shape.X[index] = rotate(vertex, shape.C, angle)
computeNormals(shape)
})
}

2
types/draw.d.ts vendored

@ -1,4 +1,5 @@
/** @typedef {import('./shape').Shape} Shape */
/** @typedef {import('./vector').Vector2D} Vector2D */
/**
* Draws a shape onto the canvas.
*
@ -7,3 +8,4 @@
*/
export function drawShape(context: CanvasRenderingContext2D, shape: Shape): void;
export type Shape = import('./shape').Shape;
export type Vector2D = import('./vector').Vector2D;

Loading…
Cancel
Save