HTML and CSS Reference
In-Depth Information
z: 10,
attack: 5,
health: 40,
maxHealth: 40,
gold: 0,
xp: 0
}).extend(props));
this.add('player_input, tiled, camera, transition');
this.bind('hit',this,'collision');
this.bind('attack',this,'attack');
this.bind('interact',this,'interact');
this.bind('heal',this,'heal');
},
collision: function(data) {
this.p.x += data.dx * tileSize/2;
this.p.y += data.dy * tileSize/2;
this.p.attacking = true;
},
attack: function(data) {
var damage = Math.round(Math.random() * this.p.attack);
data.sprite.trigger('interact',
{ source: this, damage: damage });
this.p.attacking = false;
this.tiled.setPosition();
},
interact: function(data) {
this.p.health -= data.damage;
if(this.p.health <= 0) {
this.destroy();
}
this.trigger('health');
},
heal: function(data) {
this.p.health += data.amount;
if(this.p.health > this.p.maxHealth) {
this.p.health = this.p.maxHealth;
}
this.trigger('health');
}
});
The Player is sent an initial collision event when it runs into a sprite. (This is handled by the
tiled
com-
ponent earlier.) It reacts to that event by moving half the distance into the square in question and setting the
attacking
property to
true
. The tiled component then sends an
attack
event a short time later. Receiving
this, the Player calculates a random amount of damage based on their
attack
property and sends the
inter-
act
event to whatever it ran into. If it's an enemy, that enemy absorbs the damage and either dies or returns the
attack, triggering an interact event on the player.