Skip to content

Instantly share code, notes, and snippets.

@quidmonkey
Created July 5, 2011 15:28
Show Gist options
  • Save quidmonkey/1065058 to your computer and use it in GitHub Desktop.
Save quidmonkey/1065058 to your computer and use it in GitHub Desktop.
NotificationManager for ImpactJS
/*
* Impact Plugin
* NotificationManager
* Written by Abraham Walters
* July 2011
* Jxyzzy Dev Company
* jxyzzy.com
*
* This plugin extends the Font class and allows you to pop-up a
* text Notification (spawnNote()), move it (this.pos) and have
* it fade after a specified amount of time (this.lifetime &
* this.fadetime). The plugin provides a NotificationManager to
* do all your dirty work and track each Notification for updating
* and drawing.
*
* To use the plugin you will need to create an instance of the
* NotificationManager in your ig.game instance (MyGame) like so:
*
* myNoteManager: new NotificationManager(),
*
* You will then need to add myNoteManager.update() to ig.game.update()
* and myNoteManager.draw() to ig.game.draw(). Make sure you add
* myNoteManager.draw() after the this.parent() draw, otherwise your
* Notifications will be drawn over. From there you can spawn a
* Notification within any Entity using the following syntax:
*
* ig.game.myNoteManager.spawnNote('media/font.png', 'string', x, y, settings);
*
* Enjoy!
*
*/
ig.module(
'plugins.notification-manager'
)
.requires(
'impact.impact'
)
.defines(function(){
ig.NotificationManager = ig.Class.extend({
notes: [],
init: function() {},
draw: function() {
for(var i = 0; i < this.notes.length; i++) {
this.notes[i].draw();
}
},
spawnNote: function( font, text, x, y, settings) {
var note = new Notification( font, text, x, y, settings );
this.notes.push( note );
},
update: function() {
for( var i = this.notes.length ; i--; i ) {
this.notes[i].update();
//if note is dead, erase it
if( this.notes[i]._kill ) {
this.notes.splice(i, 1);
}
}
}
});
Notification = ig.Class.extend({
font: null, //font
text: '', //string to draw
pos: { x: null, y: null }, //position
_kill: null, //state
vel: { x: 0, y: -20 }, //velocity - set to 0 if font doesn't move
alpha: 1, //alpha, 0 = translucent, 1 = opaque
lifetime: 1.2, //how long notification should last, set to zero to disable fade
fadetime: 0.4, //how long until note fades
init: function( font, text, x, y, settings ) {
this.font = font;
this.text = text;
this._kill = false;
this.pos.x = x - ig.game.screen.x;
this.pos.y = y - ig.game.screen.y;
ig.merge( this, settings );
this.idleTimer = new ig.Timer();
},
update: function() {
//update position
this.pos.x += (this.vel.x * ig.system.tick);
this.pos.y += (this.vel.y * ig.system.tick);
//if lifetime = 0, skip fade and kill
if( !this.lifetime ) {
return;
}
//if greater than lifetime, kill note
if( this.idleTimer.delta() > this.lifetime ) {
this._kill = true;
return;
}
//do fade - slowly dissipate
this.alpha = this.idleTimer.delta().map(
this.lifetime - this.fadetime, this.lifetime, 1, 0 );
},
draw: function() {
//set system alpha for fade effect
if( this.alpha != 1) {
ig.system.context.globalAlpha = this.alpha;
}
//draw font
this.font.draw( this.text,
ig.system.getDrawPos(this.pos.x), ig.system.getDrawPos(this.pos.y),
ig.Font.ALIGN.LEFT );
//reset system alpha so fade effect doesn't get applied
//to other objects being drawn
if( this.alpha != 1) {
ig.system.context.globalAlpha = 1;
}
}
});
});
@80bit
Copy link

80bit commented Jul 27, 2011

Hey man, thanks so much for this great plugin - I am dying to see this in action, but I'm having some troubles and I was hoping my confusion laced fumbling might be a simple 'fix this you idiot' for you.. Heres the rundown:

Ive been at this for hours and I cant figure out why its not working.. Does the Impact version matter for usage? Im using 1.17.. when the message is supposed to appear Im getting this in the (chrome) dev console:

GET http://localhost/DL/[object%20Object] 404 (Not Found)

Yet i cant figure out what part of the line its talking about. Im only assuming the line in question is the line im calling the function from my entity, but when i look at the line number on the error code, it says this is the line number: [object%20Object]

Im completely bent on why this isnt working... :(

This is what im using to actually call the function at the time of an item 'pickup'...

check: function( other ) {

    if ( other.pickup == true && other.gotcrate == false) {
        other.gotcrate = true;
        this.tik.play();
        ig.game.myNoteManager.spawnNote(ig.game.font, 'my entity message ',this.pos.x, this.pos.y, {vel: { x: 0, y: 0 },  alpha: 0.5, lifetime: 2.2, fadetime: 0.3 });

        this.kill();
    }
},

Any help you could provide would be truly a godsend to me... ive blown my mind 17 different ways trying to figure this out, and im worried the simple fact is that my entire life leading up to this day and everything after it is actually just a bad dream im about to wake up from only to realize im actually a high school janitor in the heart of midwest america and I have no place even dreaming that i should be trying to program anything even remotely resembling a video game. ;)

Infinite thanks for any time you could give me.

@quidmonkey
Copy link
Author

Hey 80bit-

Heh, you're a funny guy. And np on the help.

Typically you get a 404 when it can't find the file. For your font - ig.game.font - did you initialize it properly, and are you sure the image file exists in the /media/ directory? Did you add 'plugins.notification-manager' to your ig.game.requires()? Did you create myNoteManager = new.igNotificationManager()? Did you create a plugins folder in the /lib/ impact directory and dump the NotificationManager.js in there? See the physics example if you need an example are where to put the plugins folder.

Best.

@80bit
Copy link

80bit commented Jul 27, 2011

Yeah that's whats crazy, I've got those lines all in place.. and even tried swapping out the ig.game.font with a new font definition just as it is in the example, but still get the same issue. When i change the font name deliberately to be a font I dont have (ie: pullingyourhairout.png) it actually gives me a specific warning that the specific file cant be found, so i know it's not that..

my requires looks like this:

.requires(
'impact.game',
'impact.font',

'game.entities.player',
'game.entities.crate',

'game.levels.loadingstage',
'game.levels.endingstage',
'game.levels.cave',

'plugins.impact-splash-loader',
'plugins.notification-manager',
'plugins.screen-fader'

)

myGame = ig.Game.extend({

//Load Note Manager
myNoteManager: new ig.NotificationManager(),

    ....

update: function() {

    this.myNoteManager.update();
     ....
     }

draw: function() {

    this.myNoteManager.draw();
    .......
}

Then in my crate.js I have this function that calls it:

check: function( other ) {

    if ( other.pickup == true && other.gotcrate == false) {
        other.gotcrate = true;
        this.tik.play();
        ig.game.myNoteManager.spawnNote(new ig.Font( 'media/fonts/block.png' ), 'my entity message ',this.pos.x, this.pos.y, {vel: { x: 0, y: 0 },  alpha: 0.5, lifetime: 2.2, fadetime: 0.3 });

        this.kill();
    }
},

Let me know if you want direct access to the code, I can give that as well if it helps... I am so thankful for your help. :)

Many cheers,
Nate

@quidmonkey
Copy link
Author

Weird. You're certain that it's not something else? Try commenting out the spawnNote() and see if the error persists.

@80bit
Copy link

80bit commented Jul 27, 2011

Yeah if i comment out the whole spawnNote line i dont get any errors. :(

Could I get your email address to send you a private link to my full source to look at?

@quidmonkey
Copy link
Author

Big thx to 80bit and stahlmanDesign over @ the ImpactJS forums!

We hunted down and found a bug with the Notifications not being translated to the onscreen coordinates nor taking scale into account. Plugin has been updated to reflect these changes.

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