Skip to content

Instantly share code, notes, and snippets.

Created August 25, 2015 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/b4b7af0223534cf01c3f to your computer and use it in GitHub Desktop.
Save anonymous/b4b7af0223534cf01c3f to your computer and use it in GitHub Desktop.
ig.module(
'game.entities.character'
)
.requires(
'impact.game',
'impact.entity'
)
.defines(function(){
EntityCharacter = ig.Entity.extend({
animSheet: new ig.AnimationSheet ('media/sprites1.png',50,50),
size: {x: 60, y: 60},
stats: {HP: 100, MP: 4, AP: 2},
name: 0,
gx: 0,
gy: 0,
type: 0,
color: 0,
rosternum: 0,
direction: 0,
moving: false,
aiming: false,
facing: false,
placing: false,
clicked: false,
selected: false,
arrows: 0,
init: function (x,y,n) {
n--;
this.parent(x,y);
var roster = ig.game.playerroster;
this.rosternum = n;
this.name = roster[n].name;
this.stats = roster[n].stats;
this.type = roster[n].type;
this.color = roster[n].color;
this.direction = 2;
var q = this.type*4,
r = this.color*12;
this.addAnim('up', 1, [q+r+0]);
this.addAnim('right', 1, [q+r+1]);
this.addAnim('down', 1, [q+r+2]);
this.addAnim('left', 1, [q+r+3]);
this.addAnim('selected_up', 1, [q+r+60]);
this.addAnim('selected_right', 1, [q+r+61]);
this.addAnim('selected_down', 1, [q+r+62]);
this.addAnim('selected_left', 1, [q+r+63]);
},
update: function () {
var mx = ig.input.mouse.x,
my = ig.input.mouse.y;
if (ig.input.pressed('lmb')) {
//if infocus and not selected, set clicked, and select unit
//otherwise, deselect, declick, deface, and kill arrows if there are any.
if (this.inFocus()) {
if (!this.selected) {
this.clicked = true;
this.selected = true;
console.log('Clicked', this.name);
}
}
else {
this.selected = false;
this.clicked = false;
this.facing = false;
if (this.arrows != 0) {
this.arrows.kill();
}
};
};
if (ig.game.gamestate.placement) {
//if I'm not placing, and unit was clicked, start placing and declick
//if I'm placing, unit follows cursor.
//On click, get grid coordinates from cursor position. If cursor is in starting grid, place character (placing = false), start facing, and spawn arrow entity.
if (!this.placing) {
if (this.clicked) {
this.placing = true;
this.clicked = false;
}
}
else if (this.placing) {
this.pos.x = mx - 25;
this.pos.y = my - 25;
if (ig.input.pressed('lmb')) {
var gridx = Math.floor((mx - 60)/60),
gridy = Math.floor((my - 60)/60);
if (gridx >= 0 && gridx < 4 && gridy >= 6 && gridy < 10) {
this.pos.x = (gridx*60) + 65;
this.pos.y = (gridy*60) + 65;
this.placing = false;
this.facing = true;
this.arrows = ig.game.spawnEntity(EntityArrows, this.pos.x - 5, this.pos.y - 5);
console.log ('Placed', this.name, 'at ', gridx, ',', gridy);
};
};
};
};
if (this.facing) {
if (ig.input.pressed('up')) {
this.direction = 0;
this.facing = false;
this.selected = false;
this.arrows.kill();
console.log(this.name, 'faced up.');
};
if (ig.input.pressed('right')) {
this.direction = 1;
this.facing = false;
this.selected = false;
this.arrows.kill();
console.log(this.name, 'faced right.');
};
if (ig.input.pressed('down')) {
this.direction = 2;
this.facing = false;
this.selected = false;
this.arrows.kill();
console.log(this.name, 'faced down.');
};
if (ig.input.pressed('left')) {
this.direction = 3;
this.facing = false;
this.selected = false;
this.arrows.kill();
console.log(this.name, 'faced left.');
};
};
switch (this.direction) {
case 0:
if (this.selected) {
this.currentAnim = this.anims.selected_up;
}
else {
this.currentAnim = this.anims.up;
}
break;
case 1:
if (this.selected) {
this.currentAnim = this.anims.selected_right;
}
else {
this.currentAnim = this.anims.right;
}
break;
case 2:
if (this.selected) {
this.currentAnim = this.anims.selected_down;
}
else {
this.currentAnim = this.anims.down;
}
break;
case 3:
if (this.selected) {
this.currentAnim = this.anims.selected_left;
}
else {
this.currentAnim = this.anims.left;
}
break;
}
if (this.HP <= 0) {
this.kill();
};
this.parent();
},
inFocus: function() {
return (
(this.pos.x <= (ig.input.mouse.x + ig.game.screen.x)) &&
((ig.input.mouse.x + ig.game.screen.x) <= this.pos.x + this.size.x) &&
(this.pos.y <= (ig.input.mouse.y + ig.game.screen.y)) &&
((ig.input.mouse.y + ig.game.screen.y) <= this.pos.y + this.size.y)
);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment