MageFlight / merlin-game-engine

GNU General Public License v3.0
1 stars 0 forks source link

Make everything able to be imported from a single module. #3

Open TrueSunGaming opened 1 year ago

TrueSunGaming commented 1 year ago

The problem

Currently, the import statements required to extend the GameState class are the following:

import { GameState } from "merlin-game-engine/dist/gameState";
import { GameObjectTree } from "merlin-game-engine/dist/gameObjects/gameObjectTree";
import { PhysicsEngine } from "merlin-game-engine/dist/physicsEngine/physics";

It would be convenient if you could just import those from one module:

import { GameState, GameObjectTree, PhysicsEngine } from "merlin-game-engine";

How it could be fixed

This could be fixed by importing everything into src/index.ts, then exporting them.

// src/index.ts
import { GameState } from "./GameState";
export { GameState };

import { CollisionData, PhysicsEngine } from "./physicsEngine/physics";
export { CollisionData, PhysicsEngine };

import { GameObjectTree } from "./gameObjects/GameObjectTree";
export { GameObjectTree };

// repeat with the rest of the classes, interfaces, types, and variables.

Now, everything can conveniently be imported from "merlin-game-engine" or "merlin-game-engine/dist":

import { GameState, GameObjectTree, PhysicsEngine } from "merlin-game-engine";
// or
import { GameState, GameObjectTree, PhysicsEngine } from "merlin-game-engine/dist";

This change also allows for everything to be imported into a namespace:

import * as merlin from "merlin-game-engine";
// or
import * as merlin from "merlin-game-engine/dist";
MageFlight commented 1 year ago

The reason why every file currently has to be imported on its own is because I wanted to make the engine highly tree-shakable. My thought process was that there would be parts of the engine that would go unused, so it would not need to be included. If everything was re-exported from an src/index.ts, then the engine wouldn't be tree-shakable, increasing the final bundle size.

As we started using it, however, I realize that, 1, the size of the unused modules is very small, and 2, there aren't many unused modules. As such, this will be implemented.

For the game development club game, the ability for kinematicBody vs. kinematicBody collisions to alter the final velocities of both objects is required. Since this collision feature is required for the game to continue development, it takes priority over this import change.

TL;DR: This will be implemented after #2 is completed.