1
0
Fork 0
Browse Source

refactor: move world elements into dedicated module

Signed-off-by: André Jaenisch <andre.jaenisch@posteo.de>
combat-scorched-earth-from-outer-space
André Jaenisch 10 months ago
parent
commit
fb74ae92fb
Signed by: ryuno-ki
GPG Key ID: 5A668E771F1ED854
  1. 61
      src/js/world.js
  2. 36
      test/js/world.test.js
  3. 24
      types/world.d.ts

61
src/js/world.js

@ -0,0 +1,61 @@
import { RigidShape } from './shape.js'
import { Vec2 } from './vector.js'
/** @typedef {import('./shape').Shape} Shape */
/**
* Creates player avatar.
*
* @returns {Shape}
*/
export function makeAstronaut () {
const center = Vec2(200, 200)
const friction = 20
const restitution = 0
const mass = 400
const bounds = 1
const width = 20
const height = 20
const shape = RigidShape({
center,
mass,
friction,
restitution,
bounds,
width,
height
})
return shape
}
/**
* Creates a boundary at the bottom of the screen.
*
* @param {object} config
* @param {number} config.x
* @param {number} config.y
* @param {number} config.height
* @param {number} config.width
* @returns {Shape}
*/
export function makeBottomBoundary ({ x, y, height, width }) {
const center = Vec2(x + width / 2, y + height / 2)
const friction = 20
const restitution = 0
const mass = 0
const bounds = 1
const shape = RigidShape({
center,
mass,
friction,
restitution,
bounds,
width,
height
})
return shape
}

36
test/js/world.test.js

@ -0,0 +1,36 @@
import { expect } from 'chai'
import { Vec2 } from '../../src/js/vector.js'
import { makeAstronaut, makeBottomBoundary } from '../../src/js/world.js'
describe('World', function () {
describe('makeAstronaut', function () {
it('should make an astronaut shape', function () {
// Arrange
/* nothing to do here */
// Act
const astronaut = makeAstronaut()
// Assert
expect(astronaut).to.be.an('object')
})
})
describe('makeBottomBoundary', function () {
it('should make an immobil shape', function () {
// Arrange
const x = 0
const y = 100
const height = 1
const width = 200
// Act
const boundary = makeBottomBoundary({ x, y, height, width })
// Assert
expect(boundary.X[0]).to.deep.equal(Vec2(x, y))
expect(boundary.X[2]).to.deep.equal(Vec2(x + width, y + height))
})
})
})

24
types/world.d.ts vendored

@ -0,0 +1,24 @@
/** @typedef {import('./shape').Shape} Shape */
/**
* Creates player avatar.
*
* @returns {Shape}
*/
export function makeAstronaut(): Shape;
/**
* Creates a boundary at the bottom of the screen.
*
* @param {object} config
* @param {number} config.x
* @param {number} config.y
* @param {number} config.height
* @param {number} config.width
* @returns {Shape}
*/
export function makeBottomBoundary({ x, y, height, width }: {
x: number;
y: number;
height: number;
width: number;
}): Shape;
export type Shape = import('./shape').Shape;
Loading…
Cancel
Save