Generate overview based on subproject size

This commit is contained in:
Dennis Schoepf 2021-07-21 15:27:04 +02:00
parent 4053961357
commit 9790361f24
9 changed files with 109 additions and 24 deletions

View file

@ -1 +0,0 @@
export class Api {}

63
src/helpers.ts Normal file
View file

@ -0,0 +1,63 @@
import { mp5 } from '../main';
import { SCREEN_HEIGHT, SCREEN_WIDTH } from './constants/screen';
import { Edge } from './sketchObjects/Edge';
import { Coordinates, SubProject } from './types';
export function getEdgeDimensions({ size }: SubProject): number {
const radius = size * 0.05;
return radius > 150 ? 150 : radius;
}
export function generateRandomEdgeCoordinates(): Coordinates {
return {
x: mp5.random(150, SCREEN_WIDTH - 150),
y: mp5.random(150, SCREEN_HEIGHT - 150),
};
}
export function isColliding(
coordinatesToTest: Coordinates,
existingCoordinates: Coordinates[]
): boolean {
return existingCoordinates.some(
(existingCoordinate) =>
mp5.dist(
existingCoordinate.x,
existingCoordinate.y,
coordinatesToTest.x,
coordinatesToTest.y
) < 300
);
}
export function generateEdgeCoords(existingCoordinates: Coordinates[]): Coordinates {
let newCoords: Coordinates;
do {
newCoords = generateRandomEdgeCoordinates();
} while (isColliding(newCoords, existingCoordinates));
return newCoords;
}
export function generateEdges(subprojects: SubProject[]): Edge[] {
let edgeCoords = [];
subprojects.forEach((subproject) => {
const coordinates = generateEdgeCoords(edgeCoords);
edgeCoords.push({
x: coordinates.x,
y: coordinates.y,
r: getEdgeDimensions(subproject),
});
});
return edgeCoords.map(
(edgeCoord) =>
new Edge({
x: edgeCoord.x,
y: edgeCoord.y,
r: edgeCoord.r,
})
);
}

View file

@ -1,7 +1,8 @@
import { mp5 } from '../../main';
import store from '../store';
import { Scenes } from './scenes';
export class LegacyScene {
export class DetailScene {
constructor() {}
draw() {
@ -9,7 +10,8 @@ export class LegacyScene {
}
onSceneClick() {
console.log('Click on legacy scene');
console.log('Click on detail scene');
console.log('Changing back to overview');
store.setState({ currentScene: Scenes.OVERVIEW });
}
}

View file

@ -2,21 +2,17 @@ import { mp5 } from '../../main';
import { Player } from '../sketchObjects/Player';
import { colors } from '../constants/colors';
import { Edge } from '../sketchObjects/Edge';
import { Scenes } from './scenes';
import store from '../store';
import { generateEdges } from '../helpers';
import { Scenes } from './scenes';
import projectMetadata from '../../metadata/project.json';
export class OverviewScene {
player: Player;
edgeData: Array<{ x: number; y: number; r: number; scene: Scenes }>;
edges: Edge[];
constructor() {
this.edgeData = [
{ x: 100, y: 100, r: 50, scene: Scenes.LEGACY },
{ x: 900, y: 400, r: 100, scene: Scenes.LEGACY },
{ x: 300, y: 600, r: 75, scene: Scenes.LEGACY },
];
this.edges = this.edgeData.map((edge) => new Edge(edge.x, edge.y, edge.r));
this.edges = generateEdges(projectMetadata.subprojects);
this.player = new Player();
}
@ -29,10 +25,10 @@ export class OverviewScene {
}
public onSceneClick() {
this.edgeData.forEach((edge, i) => {
this.edges.forEach((edge, i) => {
const dist = mp5.dist(mp5.mouseX, mp5.mouseY, edge.x, edge.y);
if (dist < edge.r) {
store.setState({ currentScene: edge.scene });
store.setState({ currentScene: Scenes.DETAIL });
}
});
}

View file

@ -1,6 +1,4 @@
export enum Scenes {
OVERVIEW = 'OVERVIEW',
LEGACY = 'LEGACY',
PACKAGES = 'PACKAGES',
CONTRIBUTORS = 'CONTRIBUTORS',
DETAIL = 'DETAIL',
}

View file

@ -6,7 +6,7 @@ export class Edge {
y: number;
r: number;
constructor(x: number, y: number, r: number) {
constructor({ x, y, r }: { x: number; y: number; r: number }) {
this.x = x;
this.y = y;
this.r = r;

11
src/types.ts Normal file
View file

@ -0,0 +1,11 @@
export interface SubProject {
name: string;
path: string;
size: number;
contents: any;
}
export interface Coordinates {
x: number;
y: number;
}