Okay so the game is essentially a gather, build and survive multiplayer game (hopefully it will be soon at least) You can find the editor here if there seems to be something you need to fidget with yourself. I wanted to use a damageDemo script I made to get the player to take damage when colliding with an entity with the script attached, and I added this to the gamePlayManager script to make sure the player is disabled from the network entity when the health has reached 0, however, I guess somewhere in between the lines I messed up or miscalculated somewhere? I have three separate scripts trying to work together to complete the task of hopefully soon what will be PVP, I will list the scripts below:
damageDemo script:
var DamageDemo = pc.createScript('damageDemo');
DamageDemo.prototype.initialize = function() {
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};
DamageDemo.prototype.onCollisionStart = function(result) {
if (result.other && result.other.tags.has('player')) {
result.other.script.health.decreaseHealth(10);
}
};
GamePlayManager script:
var GamePlayManagerScript = pc.createScript('gamePlayManagerScript');
GamePlayManagerScript.attributes.add('mainMenuComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('inputBoxNickNameComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('playButtonComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('chatBoxPanelComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('inputBoxChatMsgComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('scrollContentPanelComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('verticalScrollbarComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('msgHolderImageComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('networkEntity', {type: 'entity'});
GamePlayManagerScript.attributes.add('playerEntity', {type: 'entity'});
GamePlayManagerScript.attributes.add('otherEntity', {type: 'entity'});
// initialize code called once per entity
GamePlayManagerScript.prototype.initialize = function() {
this.app.mouse.disableContextMenu();
this.mainMenuComp.enabled = true;
this.networkEntity.enabled = false;
this.playerEntity.enabled = false;
this.otherEntity.enabled = false;
this.chatBoxPanelComp.enabled = false;
this.gameStarted = false;
this.allowMovement = false;
this.lastMsg = "";
this.nicknameVal = "";
this.playButtonComp.element.on('click', function (evt) {
setTimeout(function(){
this.nicknameVal = this.inputBoxNickNameComp.script.input.getCleanValue();
// console.log('btn clicked ' , this.nicknameVal);
if(this.nicknameVal !== "")
{
this.playerEntity.findByName('playerNickText').element.text = this.nicknameVal;
this.mainMenuComp.enabled = false;
this.networkEntity.enabled = true;
this.playerEntity.enabled = true;
this.chatBoxPanelComp.enabled = true;
this.gameStarted = true;
this.allowMovement = true;
}
else
{
alert("Please enter proper NickName!");
}
}.bind(this),100);
}, this);
};
// update code called every frame
GamePlayManagerScript.prototype.update = function(dt) {
if(this.gameStarted === true)
{
if (this.app.keyboard.wasPressed(pc.KEY_ENTER))
{
// console.log(this.inputBoxChatMsgComp.script.input.checkFocus());
if(this.inputBoxChatMsgComp.script.input.isFocued() === true)
{
var textBoxVal = this.inputBoxChatMsgComp.script.input.getCleanValue();
this.lastMsg = textBoxVal;
this.inputBoxChatMsgComp.script.input.deleteValue();
this.inputBoxChatMsgComp.script.input.blur();
if(this.lastMsg !== "")
{
this.networkEntity.script.network.sendMsg(this.nicknameVal,this.lastMsg);
}
}
else
{
this.inputBoxChatMsgComp.script.input.focus();
}
// console.log("this.lastMsg = ", this.lastMsg);
}
if(this.inputBoxChatMsgComp.script.input.isFocued() === true)
{
this.allowMovement = false;
}
else
{
this.allowMovement = true;
}
}
};
GamePlayManagerScript.prototype.update = function(dt) {
// check for collisions with entities with the damageDemo script
var entities = this.app.root.find(function(node) {
return node.script && node.script.damageDemo;
});
for (var i = 0; i < entities.length; i++) {
if (this.playerEntity.collision.collidesWith(entities[i])) {
// player is hit, decrease health
this.playerEntity.script.playerHealth.takeDamage(10);
// reset the entity that caused the damage
entities[i].script.damageDemo.onCollision();
}
}
};
GamePlayManagerScript.prototype.addMsgInChatBox = function(nicknameVal, actualMsg) {
// console.log("GamePlayManagerScript.prototype.addMsgInChatBox = ",nicknameVal, actualMsg);
var msgHolderImageComp = this.msgHolderImageComp.clone();
msgHolderImageComp.findByName('sentMsgText').element.text = nicknameVal + " : " +actualMsg;
msgHolderImageComp.tags.add('msgBlock');
this.scrollContentPanelComp.addChild(msgHolderImageComp);
msgHolderImageComp.enabled = true;
if(this.scrollContentPanelComp.findByTag('msgBlock').length * msgHolderImageComp.element.height > 260)
{
this.scrollContentPanelComp.element.height = this.scrollContentPanelComp.findByTag('msgBlock').length * msgHolderImageComp.element.height;
this.verticalScrollbarComp.scrollbar.value = 1;
}
};
// swap method called for script hot-reloading
// inherit your script state here
// GamePlayManagerScript.prototype.swap = function(old) { };
// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/
playerHealth script:
var PlayerHealth = pc.createScript('playerHealth');
PlayerHealth.attributes.add('startingHealth', {type: 'number', default: 100});
// initialize code called once per entity
PlayerHealth.prototype.initialize = function() {
this.health = this.startingHealth;
};
// decrease player health by given amount
PlayerHealth.prototype.takeDamage = function(amount) {
this.health -= amount;
if (this.health <= 0) {
// player is dead, disable entity and reload page
this.entity.enabled = false;
window.location.reload();
}
};
// increase player health by given amount
PlayerHealth.prototype.heal = function(amount) {
this.health += amount;
};
// swap method called for script hot-reloading
// inherit your script state here
// PlayerHealth.prototype.swap = function(old) { };