Skip to content

Instantly share code, notes, and snippets.

Created January 30, 2013 19:13
Show Gist options
  • Save anonymous/4675866 to your computer and use it in GitHub Desktop.
Save anonymous/4675866 to your computer and use it in GitHub Desktop.
ig.module(
'game.entities.character'
)
.requires(
'game.entities.clickable',
'game.entities.difference',
'plugins.entityExtensions'
)
.defines(function () {
CharacterEntity = ClickableEntity.extend({
char: null,
flip: false,
// wether this character belongs to the player
// or the opponent
mine: false,
// whether the character has been targeted
target: false,
collides: ig.Entity.COLLIDES.PASSIVE,
init: function (x, y, settings) {
this.parent(x, y, settings);
this.newLoc = { x: this.pos.x, y: this.pos.y };
this.size.x = this.char.SpriteSheets['Standing'].FrameSize.X;
this.size.y = this.char.SpriteSheets['Standing'].FrameSize.Y;
this.maxVel.x = 1000;
this.maxVel.y = 1000;
this.font = bs.fonts.get("dw"); // new ig.Font('media/fonts/defaultWhite.png');
// initialize all of our animations from the spritesheet
this.animSheet = new ig.AnimationSheet(this.char.SpriteSheets['Standing'].Sheet, this.size.x, this.size.y);
for (i = 0; i < this.char.SpriteSheets['Standing'].Reels.length; i++) {
var animation = this.addAnim(this.char.SpriteSheets['Standing'].Reels[i].Name, this.char.SpriteSheets['Standing'].Reels[i].Speed, this.char.SpriteSheets['Standing'].Reels[i].Frames);
if (this.flip) {
animation.flip.x = true;
}
animation.stop = this.char.SpriteSheets['Standing'].Reels[i].Stop;
if (this.char.SpriteSheets['Standing'].Reels[i].StartFrame) {
animation.gotoFrame(this.char.SpriteSheets['Standing'].Reels[i].StartFrame);
}
}
var statusX = this.flip ? this.pos.x - 40 : this.pos.x;
},
initializeCharacter: function () {
if (arguments.length === 1) {
this.char = arguments[0];
}
if (this.char.Status.Health.Current < 1) {
this.currentAnim = this.anims.Death;
this.currentAnim.gotoFrame(this.currentAnim.sequence.length);
}
else {
switch (this.char.BusyStatus) {
case 1:
this.busy = true;
this.currentAnim = this.anims.Standing;
break;
default:
// when clicking "next round" after viewing playback
// all of our previously busy characters stay busy
// without this.
this.busy = false;
// default animation is "Standing"
this.currentAnim = this.anims.Standing;
break;
}
}
// if the character cannot act, we set their status to busy as well
this.busy = this.busy || !this.char.CanAct;
},
update: function () {
this.parent();
// make sure the ground stays with us
if (this.ground) {
this.ground.pos.x = this.pos.x;
this.ground.pos.y = this.pos.y + this.size.y;
}
// false this on every update, the pointer collision
// occurs after this and will set it back to true if
// the mouse is active over this entity
this.activeHover = false;
},
draw: function () {
this.parent();
// if we're hovering, show the
// appropriate hover visual
if (this.activeHover) {
this.font.draw("hover", this.pos.x, this.pos.y + 30);
}
this.font.draw(this.char.Id, this.pos.x + this.size.x / 2, this.pos.y + this.size.y - this.font.height);
},
dieIfDead: function () {
if (this.status.status.health.now < 1 && this.currentAnim !== this.anims.Death) {
this.currentAnim = this.anims.Death;
this.currentAnim.rewind();
}
},
clicked: function () {
this.fire("onclick", this);
},
hovered: function () {
this.activeHover = true;
},
kill: function () {
this.parent();
if (this.ground) {
this.ground.kill();
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment