chuck.js/app/Game/Client/Player.js
logsol 3cb2e39a18 Makes singleton variable name of NotificationCenter lowercase
When we require a singleton, its instance name should be named
by lowercase, since it is not a class.

Relates to #128
2016-10-10 22:11:55 +02:00

120 lines
No EOL
3.2 KiB
JavaScript
Executable file

define([
"Game/Core/Player",
"Lib/Utilities/NotificationCenter",
"Game/Config/Settings"
],
function (Parent, nc, Settings) {
"use strict";
function Player(id, physicsEngine, user, isMe) {
Parent.call(this, id, physicsEngine, user);
this.healthBarView = null;
this.healthBarViewVisibleTimeout = null;
this.healthBarViewVisible = false;
this.initHealthBar();
this.ncTokens = (this.ncTokens || []).concat([
nc.on(nc.ns.client.game.events.render, this.render, this)
]);
}
Player.prototype = Object.create(Parent.prototype);
Player.prototype.setStats = function(stats) {
var oldHealth = this.stats.health;
this.stats = stats;
if(oldHealth != this.stats.health) {
this.onHealthChange();
}
}
Player.prototype.initHealthBar = function() {
var self = this;
this.healthBarViewVisible = false;
var options = {
x: 100,
y: 100,
healthFactor: this.stats.health / 100,
visible: this.healthBarViewVisible
};
var callback = function(healthBarView) {
self.healthBarView = healthBarView;
}
nc.trigger(nc.ns.client.view.healthBar.createAndAdd, callback, options);
};
Player.prototype.onHealthChange = function() {
if(this.stats.health != 100) {
this.setHealthBarVisible(true);
}
};
Player.prototype.spawn = function(x, y) {
Parent.prototype.spawn.call(this, x, y);
this.setHealthBarVisible(false);
};
Player.prototype.setHealthBarVisible = function(visible) {
var self = this;
this.healthBarViewVisible = visible;
if(this.healthBarViewVisibleTimeout) clearTimeout(this.healthBarViewVisibleTimeout);
if(visible) {
var position = this.getPosition();
var options = {
x: position.x * Settings.RATIO,
y: position.y * Settings.RATIO,
healthFactor: this.stats.health / 100,
visible: this.healthBarViewVisible
};
nc.trigger(nc.ns.client.view.healthBar.update, this.healthBarView, options);
this.healthBarViewVisibleTimeout = setTimeout(function() {
self.healthBarViewVisible = false;
nc.trigger(nc.ns.client.view.healthBar.update, self.healthBarView, {visible: self.healthBarViewVisible});
}, Settings.HEALTH_DISPLAY_TIME * 1000);
} else {
nc.trigger(nc.ns.client.view.healthBar.update, this.healthBarView, {visible: this.healthBarViewVisible});
}
};
Player.prototype.render = function() {
if(this.doll) {
this.doll.render();
}
if(this.healthBarViewVisible) {
var position = this.getPosition();
var options = {
healthFactor: this.stats.health / 100,
x: position.x * Settings.RATIO,
y: position.y * Settings.RATIO,
}
nc.trigger(nc.ns.client.view.healthBar.update, this.healthBarView, options);
}
};
Player.prototype.isHoldingSomething = function() {
return !!this.holdingItem;
};
Player.prototype.destroy = function() {
clearTimeout(this.healthBarViewVisibleTimeout);
nc.trigger(nc.ns.client.view.healthBar.remove, this.healthBarView);
nc.off(this.ncTokens);
Parent.prototype.destroy.call(this);
};
return Player;
});