134 lines
4.1 KiB
JavaScript
134 lines
4.1 KiB
JavaScript
// Learn cc.Class:
|
|
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
|
|
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
|
|
// Learn Attribute:
|
|
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
|
|
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
|
|
// Learn life-cycle callbacks:
|
|
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
|
|
// - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
|
|
|
|
|
|
// var vfmtPosUvColor = new gfx.VertexFormat([
|
|
// { name: gfx.ATTR_POSITION, type: gfx.ATTR_TYPE_FLOAT32, num: 2 },
|
|
// { name: gfx.ATTR_UV0, type: gfx.ATTR_TYPE_FLOAT32, num: 2 },
|
|
// { name: gfx.ATTR_UV1, type: gfx.ATTR_TYPE_FLOAT32, num: 2 },
|
|
// { name: gfx.ATTR_UV2, type: gfx.ATTR_TYPE_FLOAT32, num: 2 },
|
|
|
|
// ]);
|
|
// vfmtPosUvColor.name = 'vfmtPosUvColor';
|
|
// gfx.VertexFormat.XY_UV_Color = vfmtPosUvColor;
|
|
|
|
|
|
|
|
var ShaderMaterial = require('ShaderMaterial');
|
|
var Utils = require("Utils")
|
|
var ccmat = new ShaderMaterial()
|
|
|
|
|
|
cc.battlegrass = cc.Class({
|
|
extends: cc.Component,
|
|
|
|
properties: {
|
|
chr: {
|
|
default: null,
|
|
type: cc.Node,
|
|
},
|
|
m_sp: {
|
|
default: null,
|
|
type: cc.Sprite,
|
|
},
|
|
m_dis: 100,
|
|
m_pra: 0.3,
|
|
m_speed: 100,
|
|
},
|
|
|
|
// LIFE-CYCLE CALLBACKS:
|
|
|
|
// onLoad () {},
|
|
|
|
start() {
|
|
|
|
|
|
},
|
|
init() {
|
|
this.m_sp = this.getComponent(cc.Sprite)
|
|
this.m_sp.type = 6
|
|
},
|
|
setree(x, y,treenode) {
|
|
this.treenode = treenode
|
|
this.m_sp._updateAssembler()
|
|
this.m_sp.shaderpar1 = this.m_sp.node.width * cc.hdscale
|
|
this.m_sp.shaderpar2 = this.m_sp.node.height * cc.hdscale
|
|
this.m_sp.shaderpar3 = x - this.m_sp.node.width / 2 * cc.hdscale
|
|
this.m_sp.shaderpar4 = y //- this.m_sp.node.height / 2
|
|
|
|
if (!cc.grassdis) {
|
|
cc.grassdis = 0
|
|
}
|
|
this.mat = ccmat
|
|
this.mat.applaySpriteOnce(this.m_sp, "Grass", {
|
|
plus: this.m_pra,
|
|
distance: 0,
|
|
});
|
|
|
|
|
|
|
|
this.pnode = {
|
|
x: this.m_sp.shaderpar3 + this.m_sp.node.width / 2 * cc.hdscale,
|
|
y: this.m_sp.shaderpar4 + this.m_sp.node.height / 2 * cc.hdscale,
|
|
width: this.m_sp.shaderpar1,
|
|
height: this.m_sp.shaderpar2,
|
|
}
|
|
|
|
|
|
},
|
|
|
|
update(dt) {
|
|
if(this.treenode.isdead){
|
|
this.m_sp.shaderpar1 = 0
|
|
return
|
|
}
|
|
|
|
|
|
if (this.mat) {
|
|
var player = cc.gameMgr.watchPlayer
|
|
if (player.needreset) {
|
|
player.ingrass = false
|
|
player.needreset = false
|
|
}
|
|
if (!player.ingrass && Utils.hitTestRectangle(player, this.pnode)) {
|
|
player.ingrass = true
|
|
if (cc.grassdis < this.m_dis) {
|
|
cc.grassdis += dt * this.m_speed
|
|
this.mat.setParamValue('u_dis', cc.grassdis);
|
|
}
|
|
}
|
|
}
|
|
|
|
},
|
|
lateUpdate(dt) {
|
|
if(this.treenode.isdead){
|
|
return
|
|
}
|
|
if (this.mat) {
|
|
var player = cc.gameMgr.watchPlayer
|
|
if (!player.needreset) {
|
|
if (player.ingrass) {
|
|
this.mat.setParamValue('u_cPos', cc.v2(player.x, player.y));
|
|
} else {
|
|
if (cc.grassdis > 0) {
|
|
cc.grassdis -= dt * this.m_speed
|
|
this.mat.setParamValue('u_dis', cc.grassdis);
|
|
}
|
|
this.mat.setParamValue('u_cPos', cc.v2(player.x, player.y));
|
|
// this.mat.setParamValue('u_cPos', cc.v2(99999, 99999));
|
|
// this.mat.setParamValue('u_dis', 0);
|
|
//cc.grassdis = 0
|
|
}
|
|
player.needreset = true
|
|
}
|
|
|
|
}
|
|
}
|
|
}); |