Skip to content

Instantly share code, notes, and snippets.

@gre
Created May 6, 2015 09:07
Show Gist options
  • Save gre/fadd75d340f183444e44 to your computer and use it in GitHub Desktop.
Save gre/fadd75d340f183444e44 to your computer and use it in GitHub Desktop.
import World from "./World";
import Physics from "./Physics";
// expose all the things
window.ENGINE = { World: World, Physics: Physics };
const Physics = {
applyForce: function(x, y) { ... },
processEntity: function(entity) { ... }
};
export default Physics;
const World = {
the_answer_to_everything: 42
};
export default World;
@gre
Copy link
Author

gre commented May 6, 2015

BTW on the other side you can import ENGINE like this:

import { World, Physics } from "./engine"

or also like this:

import ENGINE from "./engine";

// ENGINE.World
// ENGINE.Physics

@rezoner
Copy link

rezoner commented May 6, 2015

It still forces me to write

ENGINE.Physics.applyForce

the idea behind name-spacing was to shorten it to

Physics.applyForce

Which as far as I understand can be achieved by

import * as Physics from "Physics";

Which will result in every Entity definition to start with 100 lines of imports variety stuff before you write a line of logic.

@gre
Copy link
Author

gre commented May 6, 2015

You can explicitely spread the ENGINE object with:

import { World, Physics } from "./engine";

Physics.applyForce(...)

I guess it is the best way you can go with ES6.

It would be great to do import * from "Physics"; but it is not supported yet AFAIK.

@tiborsaas
Copy link

That was what the with statement did and it's really not recommended:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with

You can inherit the ENGINE methods and properties, like this.

var level = ENGINE.extend({
     init: function(){
         this.Physics.appyForce();
     }
});

@rezoner
Copy link

rezoner commented May 6, 2015

It looks great until you take a scope of a real 2 millions characters of code game as an example :)

Of course it would not end up like this, because you would care to copy/paste objects in use to import statement. Nevertheless I still find it counter productive.

import { Ghost, Events, Loader, Components, Animation, Assets, MusicPlayer, Music, Observer, Timeouts, 
MinPool, Application, Collection, GameCollection, Unit, ShuffleBag, Collider, SimplePlayer, sampler, Timer, 
State, Sprite, Particle, Tween, Ejecta, Meteor, Mine, ProximityMine, Smudge, Flare, FlyingText, Explosion, 
Bullet, Capsule, Field, Missile, Bomb, Wave, Beam, StaticBeam, BeamEffect, Weapon, Cooldowns, 
HyperTrail, Trail, Stars, JumpZone, Ship, ShipsImagesFactory, Factory, Lander, ShipManifest, 
ShipWrapper, ShipAi, Turret, Hypercloud, BgDoodle, Robotalk, Face, Menu, Map, Wreck, Wreckage, 
Nebula, Collectible, Astronaut, Planet, StockMarket, Asteroid, ShieldHitEffect, NovaEffect, Console, Tutorial, 
PlayerController, ShopController, Dialogue, AI, Sun, Shipyard, Navimap, DoodlePlanet, DoodleShip, 
Scenario, Creep, DoodleFlare, MysticJumpGate, Generic, Sonar, RepairStation, RepairShip, 
ProductionModule, MapLife, MapLifeView, MapLifeViewShip, ShipsPanel, ServicePanel, EventBubble, 
WaveCharge, Boss1, EquipmentPanel, Dronarium, TiledExplosion, Drone, SpawnerModule, Crew, 
ShipIncomeComponent, TargetInfoComponent, ShipPlanetInfoComponent, TradersGuild, 
GuildsComponent, PlayerControllerComponent, HyperspaceComponent, ShipCrewComponent, 
ShipBusComponent, PiratesGuild, ShipExplorationComponent, PlanetInfoComponent, 
ShipSystemsComponent, TargetingInfoComponent, Encounter, Mission, Storage, Junkyard, Script 
} from "./engine"

@tiborsaas
Copy link

Putting some hierarchy into code design should greatly reduce these imports to 5-10.

@mrspeaker
Copy link

ES6 destructuring makes this nice too - you can pull out bits only when you need them

import Engine from './Engine';
const {Lander, Astronaut, WaveCharge} = Engine;
const {Encouter, Mission, Junkyard} = Engine;

Etc... not sure why Junkyard is part of your engine though ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment