Project rpg on playcanvas

Project rpg on playcanvas
0.0 0

#1

Hello Campers,
i’m going through the camping exercises (slowly lately coz very busy with work) but i’m also working around an rpg project on playcanvas and i have a question about javascript but that is not related with the camping exercises. I want to attach an entity to a bone of another entity, so i tried out this

this.shield=app.root.findByName("shield"); lhand = this.model.findByName('Bip01 I Mano'); this.shield.reparent(lhand); //lhand.addChild(this.shield);

Had to put comment in the last line coz gave an error but the reparent line seems don’t work as well. This is meant to equip a shield on the player … but haven’t found out any clue on internet. Can someone help?
Here a link to the project demo


#2

Try switching lines 3 and 4.


#3

Tried without success, line 4 still give an error if i active that


#4

Have you tried parent.removeChild()?

If you still can’t get it to work, can you paste a larger chunk of code and the errors to give more context?


#5

Full code

pc.script.attribute(“moveSpeed”, “number”, 0.15);
pc.script.attribute(“walkAnimCutoff”, “number”, 0.1);
pc.script.attribute(“blendTime”, “number”, 0.1);
var zone = 100;
var resistence=0;
//var mana = document.getElementById(“container”).contentWindow.mana;
// this lines to be tested to get data from php
//var div = document.getElementById(“dom-target”);
//var myData = div.textContent;
pc.script.create(“player”, function(app) {
var RAY_LENGTH = 50;
var DOUBLE_CLICK_TIME = 0.3;
var targName = “”;
var targPos = “”;
var temp = new pc.Vec3();
var rh=0;

var Player = function (entity) {
    this.entity = entity;
    this.attac=3;
    this.defence=3;
    this.damage=5;
    this.armor=3;
    this.camera = null;
    this.shield=null;
    this.rayStart = new pc.Vec3();
    this.rayEnd = new pc.Vec3();
    this.combat=0;
    this.clickTimer = 0;
    this.clicked = null;
    this.pos = null;
    this.lastPos = new pc.Vec3();
    this.state = "idle";
    this.startPosition = new pc.Vec3();
    this.targetPosition = new pc.Vec3();
    this.moveTimer = 0;
    this.ignoreMouse = false;
};

Player.prototype = {
    
    setCamera: function (cameraName) {
        // Disable the currently active camera
        app.root.findByName('Camera').enabled = false;

        // Enable the newly specified camera
        var activeCamera = app.root.findByName('Player').findByName(cameraName);
        activeCamera.enabled = true;
    },

    initialize: function () {
        this.model = this.entity.findByName("Model");
        
        this.startPosition = this.entity.getPosition();
        this.startRotation = this.entity.getRotation();
        this.entity.rigidbody.syncEntityToBody();
        this.entity.collision.on('collisionstart', this.onCollisionStart, this);
        this.shield=app.root.findByName("shield");
        lhand = this.model.findByName('Bip01 I Mano');
        lhand.removeChild();
        lhand.addChild(this.shield);
        this.shield.reparent(lhand);

    },

    start: function () {
        //context.root.findByName("UI").script.ui.displayInventory(['','','','']);
        app.mouse.on("mousedown", this.onMouseDown, this);
    },

    reset: function () {
        app.mouse.off("mousedown", this.onMouseDown, this);
        this.entity.setPosition(this.startPosition);
        this.entity.setRotation(this.startRotation);
    },

    update: function (dt) {
        this.clickTimer += dt;
        this.moveTimer += dt;

        var pos = this.entity.getPosition();
        var distance = 0;
        
        if (this.state==='dead') {
            //this.timer += dt;
            if (this.model.animation.currentTime >= this.model.animation.duration) {
                this.entity.script.htmlFollow.label.style.display = 'none';
                this.setCamera('CameraD');
            }
            return;
        }

        if(this.state === "walking") {
           
            temp.sub2(this.targetPosition, pos);

            distance = temp.length();
            if (distance < this.walkAnimCutoff) {
                this.entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
            } else {
                temp.normalize().scale(this.moveSpeed);

                this.entity.rigidbody.linearVelocity = temp;
            }
            
            temp.sub2(pos, this.targetPosition);

            if (temp.lengthSq() !== 0) {
                this.targetPosition.y = this.entity.getPosition().y;
                //this.entity.lookAt(pos);
                this.entity.lookAt(this.targetPosition);
                this.entity.rigidbody.syncEntityToBody();
            }
        }

        this.updateAnimation(dt, distance);
        // θυμήσου την τελευταία θέση
        this.lastPos.copy(pos);
    },

    updateAnimation: function (dt, distance) {
        var pos = this.entity.getPosition();
        temp.sub2(this.lastPos, pos);
        var speed = temp.length() / dt;
        var sound= null;
        //alert(mana);
        // var tg = this.entity.findByName(targName).getPosition;
        if (this.state === "walking" && (distance < this.walkAnimCutoff || (this.moveTimer > 0.5 && speed < 0.01))) {
            // the follow code is to enable attack but don't work yet
            //temp.sub2(targPos, pos);
            //var dist = temp.length();
            //var dist = temp.sub2(pos, targPos); //.length();  //this give an error
            //alert(targPos);            // do not use
            if (targPos !== 0 && this.state !== 'attack') {
                this.attack();
                targPos=0;
                return;
            }
            if (targName ==='Blacksmith') {
                // var shop = app.root.findByName('Blacksmith').findByName('Blacklabel');
                // shop.script.texts.setText('Hello');
                if (distance < this.walkAnimCutoff) {
                sound = app.root.findByName('Blacksmith').findByName('Greeting');
                sound.audiosource.play('Bsmith44.wav');
                app.root.findByName('Blacksmith').findByName('BlackShop').script.pcModal.open();
                }
            }
            if (targName ==='InnKeeper') {
                // var shop = app.root.findByName('Blacksmith').findByName('Blacklabel');
                // shop.script.texts.setText('Hello');
                if (distance < this.walkAnimCutoff) {
                sound = app.root.findByName('InnKeeper').findByName('InnGreeting');
                sound.audiosource.play('Bmaid31.wav');
                app.root.findByName('InnKeeper').findByName('InnShop').script.pcModal.open();
                    rh=1;
                }
            }
                //this.model.animation.speed = 0.2;
                //this.model.animation.play("wizard-attack.json", this.blendTime);
                //this.state = "idle";
            //} else {
                //this.model.animation.speed = 0.2;
                //this.model.animation.play("wizard1", this.blendTime);
                //this.state = "idle";
            //} 
            this.model.animation.speed = 0.2;
            this.model.animation.play("stand1.json", this.blendTime);
            this.model.animation.loop = true;
            this.state = "idle";
        } 
        else if (this.state === "walking") {
        this.model.animation.speed = speed;
       }
        
       if (this.state==='attack' && (this.model.animation.currentTime >= this.model.animation.duration)) {
           var EntA=app.root.findByName('enemy');
           var def=EntA.script.enemy.defence;
           this.combat = app.root.findByName('calc').script.maths.damage(this.attac,def);
           if (this.combat>0) {
               var wound=this.combat+this.damage-EntA.script.enemy.armor;// damage calculate
               EntA.script.health.reduce(wound);
               //alert(window.text1);
               EntA.script.htmlFollow.text=wound;
           }
            this.model.animation.speed = 0.2;
            this.model.animation.play("stand1.json", this.blendTime);
            this.model.animation.loop = true;
            this.state = "idle";
       }

    },
    
    fmouse: function(value) {
        rh=0;
    },
    
    attack: function (event) {
        this.state = "attack";
        this.model.animation.play("orc club attack.json", this.blendTime);
        this.model.animation.speed = 0.5;
        this.model.animation.loop = false;
    },

    moveTo: function (position) {
        this.moveTimer = 0.1;

        this.startPosition.copy(this.entity.getPosition());
        this.targetPosition.copy(position);

        this.state = "walking";
        this.model.animation.play("walk-bhand.json", this.blendTime);
        this.model.animation.loop = true;
    },
    die: function () {
        this.state = "dead";
        this.model.animation.play("orc club die.json", this.blendTime);
        this.model.animation.speed = 0.3;
        this.model.animation.loop = false;
    },

    onMouseDown: function (e) {
        if (this.ignoreMouse) {
            this.ignoreMouse = false;
            return;
        }
        
        if (app.keyboard.isPressed(pc.input.KEY_SHIFT) && e.button === pc.MOUSEBUTTON_LEFT) {
            this.attack();
        } else {
            // scale coords to be in canvas resolution
            var device = app.graphicsDevice;
            var _x = e.screenX;
            
            //alert (_x + " " + zone);
            if (_x < zone) return;
            //alert (_x+' '+event.x);
        var camera = app.systems.camera.cameras[0].entity;
        if (rh===0) {
        this.rayStart.copy(camera.getPosition());
        camera.camera.screenToWorld(e.x, e.y, 1, this.rayEnd);
        this.rayEnd.sub(this.rayStart).normalize().scale(RAY_LENGTH).add(this.rayStart);

        app.systems.rigidbody.raycastFirst(this.rayStart, this.rayEnd, this.onRayHit.bind(this));
        }
        }
    },
    
    onCollisionStart: function (result) {
        // var p1 = this.entity.getPosition();
        // var p2 = this.entity.findByName(targName).getPosition();
        // var dist = tempVec.sub2(p1, p2).length();
        // if (dist < 1) {
            // this.model.animation.play("wizard-attack.json", this.blendTime);
            //character1.attack(character2);
        // }
        // if (result.other.rigidbody) {
            // this.entity.audiosource.play("hit");    
        // }
    },

    onRayHit: function (result) {
        result.point.y = this.entity.getPosition().y;
        // get target name?
        targName = result.entity.getName();
        //alert (targName);
        //app.root.findByName('divs').script.htmlHandler.showMessage(targName);  have to try to show the message again later
        if (result.entity.script) {
            if (result.entity.script.enemy) {
                targPos = result.entity.getPosition();
            } else {
                targPos=0;
            }
            if (result.entity.script.shop) {
                targName=result.entity.getName();
            }
        } else {
            targPos=0;
        }
        // alert(result.entity.getPosition());
        this.moveTo(result.point);

        if(result.entity.script && result.entity.script.item) {
            if (this.clickTimer < DOUBLE_CLICK_TIME) {
                clearTimeout(this.clickTimeout);
                result.entity.script.item.onDoubleClick();
                this.clicked = null;
            } else {
                this.clickTimeout = setTimeout(function () {
                    result.entity.script.item.onClick();
                }.bind(this), DOUBLE_CLICK_TIME*1001);
                this.clickTimer = 0;
                this.clicked = result.entity;
            }
        }
    }
 };

return Player;

});


#6

PS in some line the code above refers to other scripts