Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
David-Else committed Oct 19, 2020
1 parent 8768a64 commit cdfd2dc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
14 changes: 2 additions & 12 deletions src/World.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
addVectors,
createMultiple,
divideVectors,
Vector2,
} from "./helperFunctions";
import { calculateCenter, createMultiple, Vector2 } from "./helperFunctions";
import {
heroFactory,
zombieFactory,
Expand Down Expand Up @@ -107,16 +102,11 @@ export class World {
return this.entities.get(entityKey)?.[index];
}

centerOfEntityScreenPosition(entity: Entity): Vector2 {
const middleOfEntity = divideVectors(entity.widthHeight, [2, 2]);
return addVectors(entity.position, middleOfEntity);
}

addBullet(): void {
const hero = this.entities.get("hero"); // get possible undefined?
this.entities.get("bullets")?.push(
bulletFactory({
position: this.centerOfEntityScreenPosition(hero?.[0]),
position: calculateCenter(hero?.[0]),
rotation: hero?.[0].rotation,
})
);
Expand Down
8 changes: 8 additions & 0 deletions src/helperFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Entity } from "./World";

export type Vector2 = [number, number];

export const createMultiple = <T>(
Expand All @@ -6,8 +8,14 @@ export const createMultiple = <T>(
): T[] => Array.from({ length: numberOfClasses }, factory);

type VectorFn = (x: Readonly<Vector2>, y: Readonly<Vector2>) => Vector2;

export const addVectors: VectorFn = (x, y) => [x[0] + y[0], x[1] + y[1]];

export const divideVectors: VectorFn = (x, y) => [x[0] / y[0], x[1] / y[1]];

export const calculateCenter = (entity: Readonly<Entity>): Vector2 =>
addVectors(entity.position, divideVectors(entity.widthHeight, [2, 2]));

export const calculateRandomPositionAroundPoint: VectorFn = (
centrePoint,
screenWidthHeight
Expand Down

0 comments on commit cdfd2dc

Please sign in to comment.