Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by henonchesser

I've got a lot going on in the main.js class and I want to split it up. This biggest offender is my map loader, which spawns infinite random side-scrolling levels. It relies heavily on the resources used by game.main so I felt it was best to just extend game.main. The head of my main.js is fairly basic

ig.module( 
	'game.main' 
)
.requires(
	//core
	'impact.game',
	'impact.font',
	'impact.image',
	'impact.background-map',
	'impact.debug.debug',
	'game.maploader',
	

	
	//entities
	'game.entities.avatar',
	'game.entities.eventHandler',
	
	//levels
	'game.levels.begin'
)
.defines(function(){


MetaGame = ig.Game.extend({
	

As you can see, I'm attempting to require the maploader. But I'm honestly not sure if that's the right way to go about this either. And here's my header for maploader.js

ig.module(
	'game.maploader'
)
.requires(


)
.defines(function(){

ig.MapLoader = MetaGame.extend({ 

Maybe I'm missing this concept entirely. I want to wonder if perhaps it's not possible, but Jessie Freeman's book in on line simply states "you can extend your own game class."

Thoughts? Advice?

1 decade ago by Joncom

While you could extend MetaGame, I'm not sure you would want to...

Let&039;s say you had a game called #PokemonGame, which comes in a few different versions, "Red" and "Blue". Then it might make sense for each color to extend the PokemonGame class. So you'd end up with:

PokemonRed = PokemonGame.extend({
    /* add game components specific to this version */
});

PokemonBlue = PokemonGame.extend({
    /* add game components specific to this version */
});

However, it sounds like you only have one version of your game. And you just want to abstract some logic away from main.js.

The best way to do this would depend on what your MapLoader does. If it adds a lot of logic to ig.game by, for example, overloading a bunch of its pre-existing functions, then you could do something like this:

/* lib/game/map-loader.js */
ig.module('game.map-loader')
.requires('impact.game')
.defines(function(){

    ig.Game.inject({
        loadLevel: function(level) {
            /* Do custom work... */
            this.parent(); // Then call original logic too.
        }
    });

});

If MapLoader doesn&039;t really change #ig.game too much, then it&039;d be best made into its own #ig.Class.

/* lib/game/map-loader.js */
ig.module('game.map-loader')
.requires()
.defines(function(){

    ig.MapLoader = ig.Class.extend({
        specialFunction: function() {
            /* Do custom work... */
        }
    });

});

/* lib/game/main.js */
ig.module('game.main')
.requires( 
    ... // game, font, etc.
    'game.map-loader'
)
.defines(function(){

    MetaGame = ig.Game.extend({
        init: function() {
            ...

            this.mapLoader = new ig.MapLoader();

            // Now you can make calls to 
            // this.mapLoader.specialFunction();
        }
    });

});

1 decade ago by henonchesser

Thanks Joncom! I'm going to sit on this egg for a while and see what hatches. But this is more or less the summary I was hoping for! I'll post back with my results!
Page 1 of 1
« first « previous next › last »