Merge pull request #645 from knoxHuang/v1.0

transplant MotionStreak
This commit is contained in:
pandamicro
2016-05-06 18:53:36 +08:00
9 changed files with 1307 additions and 486 deletions
+223 -483
View File
@@ -1,20 +1,18 @@
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
Copyright (c) 2008-2009 Jason Booth
Copyright (c) 2013-2016 Chukong Technologies Inc.
http://www.cocos2d-x.org
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The software or tools in this License Agreement are licensed, not sold.
Chukong Aipu reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
@@ -26,6 +24,7 @@
****************************************************************************/
/**
* !#en
* cc.MotionStreak manages a Ribbon based on it's motion in absolute space. <br/>
* You construct it with a fadeTime, minimum segment size, texture path, texture <br/>
* length and color. The fadeTime controls how long it takes each vertex in <br/>
@@ -33,501 +32,242 @@
* streak will move before adding a new ribbon segment, and the texture <br/>
* length is the how many pixels the texture is stretched across. The texture <br/>
* is vertically aligned along the streak segment.
* @class
* @extends _ccsg.Node
*
* @property {cc.Texture2D} texture - Texture used for the motion streak.
* @property {Boolean} fastMode - Indicate whether use fast mode.
* @property {Boolean} startingPositionInitialized - Indicate whether starting position initialized.
* @example
* //example
* new cc.MotionStreak(2, 3, 32, cc.Color.GREEN, s_streak);
* !#zh 运动轨迹,用于游戏对象的运动轨迹上实现拖尾渐隐效果。
* @class MotionStreak
* @extends Component
*/
cc.MotionStreak = _ccsg.Node.extend(/** @lends cc.MotionStreak# */{
texture:null,
fastMode:false,
startingPositionInitialized:false,
var MotionStreak = cc.Class({
name: 'cc.MotionStreak',
_blendFunc:null,
// To avoid conflict with other render component, we haven't use ComponentUnderSG,
// its implementation also requires some different approach:
// 1.Needed a parent node to make motion streak's position global related.
// 2.Need to update the position in each frame by itself because we don't know
// whether the global position have changed
extends: cc.Component,
_stroke:0,
_fadeDelta:0,
_minSeg:0,
_maxPoints:0,
_nuPoints:0,
_previousNuPoints:0,
/* Pointers */
_pointVertexes:null,
_pointState:null,
// webgl
_vertices:null,
_colorPointer:null,
_texCoords:null,
_verticesBuffer:null,
_colorPointerBuffer:null,
_texCoordsBuffer:null,
_className:"MotionStreak",
/**
* creates and initializes a motion streak with fade in seconds, minimum segments, stroke's width, color, texture filename or texture <br/>
* Constructor of cc.MotionStreak
* @param {Number} fade time to fade
* @param {Number} minSeg minimum segment size
* @param {Number} stroke stroke's width
* @param {Number} color
* @param {string|cc.Texture2D} texture texture filename or texture
*/
ctor: function (fade, minSeg, stroke, color, texture) {
_ccsg.Node.prototype.ctor.call(this);
this._positionR = cc.p(0, 0);
this._blendFunc = new cc.BlendFunc(cc.macro.SRC_ALPHA, cc.macro.ONE_MINUS_SRC_ALPHA);
this.fastMode = false;
this.startingPositionInitialized = false;
this.texture = null;
this._stroke = 0;
this._fadeDelta = 0;
this._minSeg = 0;
this._maxPoints = 0;
this._nuPoints = 0;
this._previousNuPoints = 0;
/** Pointers */
this._pointVertexes = null;
this._pointState = null;
// webgl
this._vertices = null;
this._colorPointer = null;
this._texCoords = null;
this._verticesBuffer = null;
this._colorPointerBuffer = null;
this._texCoordsBuffer = null;
if(texture !== undefined)
this.initWithFade(fade, minSeg, stroke, color, texture);
editor: CC_EDITOR && {
menu: 'i18n:MAIN_MENU.component.others/MotionStreak',
help: 'i18n:COMPONENT.help_url.motionStreak',
playOnFocus: true,
executeInEditMode: true
},
/**
* Gets the texture.
* @return {cc.Texture2D}
*/
getTexture:function () {
return this.texture;
ctor: function() {
this._root = null;
this._motionStreak = null;
},
/**
* Set the texture.
* @param {cc.Texture2D} texture
*/
setTexture:function (texture) {
if (this.texture !== texture)
this.texture = texture;
properties: {
/**
* !#en
* !#zh 在编辑器模式下预览拖尾效果。
* @property {Boolean} preview
* @default false
*/
preview: {
default: false,
editorOnly: true,
notify: CC_EDITOR && function () {
this._motionStreak.reset();
cc.engine.repaintInEditMode();
},
animatable: false
},
/**
* !#en The fade time to fade.
* !#zh 拖尾的渐隐时间,以秒为单位。
* @property fadeTime
* @type {Number}
* @example
* motionStreak.fadeTime = 3;
*/
_fadeTime: 1,
fadeTime: {
get: function () {
return this._fadeTime;
},
set: function (value) {
this._fadeTime = value;
if (this._motionStreak) {
this._motionStreak.setFadeTime(value);
}
},
animatable: false,
tooltip: 'i18n:COMPONENT.motionStreak.fadeTime'
},
/**
* !#en The minimum segment size.
* !#zh 拖尾之间最小距离。
* @property minSeg
* @type {Number}
* @example
* motionStreak.minSeg = 3;
*/
_minSeg: 1,
minSeg: {
get: function () {
return this._minSeg;
},
set: function (value) {
this._minSeg = value;
if (this._motionStreak) {
this._motionStreak.setMinSeg(value);
}
},
animatable: false,
tooltip: 'i18n:COMPONENT.motionStreak.minSeg'
},
/**
* !#en The stroke's width.
* !#zh 拖尾的宽度。
* @property stroke
* @type {Number}
* @example
* motionStreak.stroke = 64;
*/
_stroke: 64,
stroke: {
get: function () {
return this._stroke;
},
set: function (value) {
this._stroke = value;
if (this._motionStreak) {
this._motionStreak.setStroke(value);
}
},
animatable: false,
tooltip: 'i18n:COMPONENT.motionStreak.stroke'
},
/**
* !#en The texture of the MotionStreak.
* !#zh 拖尾的贴图。
* @property texture
* @type {Texture2D}
* @example
* motionStreak.texture = newTexture;
*/
_texture: {
default: '',
url: cc.Texture2D
},
texture: {
get: function () {
return this._texture;
},
set: function (value) {
this._texture = value;
if (this._motionStreak) {
if (cc.js.isString(value))
value = cc.textureCache.addImage(value);
this._motionStreak.setTexture(value);
}
},
url: cc.Texture2D,
animatable: false,
tooltip: 'i18n:COMPONENT.motionStreak.texture'
},
/**
* !#en The fast Mode.
* !#zh 是否启用了快速模式。当启用快速模式,新的点会被更快地添加,但精度较低。
* @property fastMode
* @type {Boolean}
* @default false
* @example
* motionStreak.fastMode = true;
*/
_fastMode: false,
fastMode: {
get: function () {
return this._fastMode;
},
set: function (value) {
this._fastMode = value;
if (this._motionStreak) {
this._motionStreak.setFastMode(value);
}
},
animatable: false,
tooltip: 'i18n:COMPONENT.motionStreak.fastMode'
}
},
/**
* Gets the blend func.
* @return {cc.BlendFunc}
*/
getBlendFunc:function () {
return this._blendFunc;
onFocusInEditor: CC_EDITOR && function () {
if (this.preview) {
this._motionStreak.reset();
}
},
/**
* Set the blend func.
* @param {Number} src
* @param {Number} dst
*/
setBlendFunc:function (src, dst) {
if (dst === undefined) {
this._blendFunc = src;
} else {
this._blendFunc.src = src;
this._blendFunc.dst = dst;
onLostFocusInEditor: CC_EDITOR && function () {
if (this.preview) {
this._motionStreak.reset();
this._motionStreak.update();
cc.engine.repaintInEditMode();
}
},
/**
* Gets opacity.
* @warning cc.MotionStreak.getOpacity has not been supported.
* @returns {number}
* !#en Remove all living segments of the ribbon.
* !#zh 删除当前所有的拖尾片段。
* @method reset
* @example
* // stop particle system.
* myParticleSystem.stopSystem();
*/
getOpacity:function () {
cc.log("cc.MotionStreak.getOpacity has not been supported.");
return 0;
reset: function () {
this._motionStreak.reset();
},
/**
* Set opacity.
* @warning cc.MotionStreak.setOpacity has not been supported.
* @param opacity
*/
setOpacity:function (opacity) {
cc.log("cc.MotionStreak.setOpacity has not been supported.");
},
/**
* set opacity modify RGB.
* @warning cc.MotionStreak.setOpacityModifyRGB has not been supported.
* @param value
*/
setOpacityModifyRGB:function (value) {
},
/**
* Checking OpacityModifyRGB.
* @returns {boolean}
*/
isOpacityModifyRGB:function () {
return false;
},
/**
* Checking fast mode.
* @returns {boolean}
*/
isFastMode:function () {
return this.fastMode;
},
/**
* set fast mode
* @param {Boolean} fastMode
*/
setFastMode:function (fastMode) {
this.fastMode = fastMode;
},
/**
* Checking starting position initialized.
* @returns {boolean}
*/
isStartingPositionInitialized:function () {
return this.startingPositionInitialized;
},
/**
* Set Starting Position Initialized.
* @param {Boolean} startingPositionInitialized
*/
setStartingPositionInitialized:function (startingPositionInitialized) {
this.startingPositionInitialized = startingPositionInitialized;
},
/**
* Get stroke.
* @returns {Number} stroke
*/
getStroke:function () {
return this._stroke;
},
/**
* Set stroke.
* @param {Number} stroke
*/
setStroke:function (stroke) {
this._stroke = stroke;
},
/**
* initializes a motion streak with fade in seconds, minimum segments, stroke's width, color and texture filename or texture
* @param {Number} fade time to fade
* @param {Number} minSeg minimum segment size
* @param {Number} stroke stroke's width
* @param {Number} color
* @param {string|cc.Texture2D} texture texture filename or texture
* @return {Boolean}
*/
initWithFade:function (fade, minSeg, stroke, color, texture) {
if(!texture)
throw new Error("cc.MotionStreak.initWithFade(): Invalid filename or texture");
if (cc.js.isString(texture))
texture = cc.textureCache.addImage(texture);
_ccsg.Node.prototype.setPosition.call(this, cc.p(0,0));
this.anchorX = 0;
this.anchorY = 0;
this.ignoreAnchor = true;
this.startingPositionInitialized = false;
this.fastMode = true;
this._minSeg = (minSeg === -1.0) ? (stroke / 5.0) : minSeg;
this._minSeg *= this._minSeg;
this._stroke = stroke;
this._fadeDelta = 1.0 / fade;
var locMaxPoints = (0 | (fade * 60)) + 2;
this._maxPoints = locMaxPoints;
this._nuPoints = 0;
this._pointState = new Float32Array(locMaxPoints);
this._pointVertexes = new Float32Array(locMaxPoints * 2);
this._vertices = new Float32Array(locMaxPoints * 4);
this._texCoords = new Float32Array(locMaxPoints * 4);
this._colorPointer = new Uint8Array(locMaxPoints * 8);
this._verticesBuffer = gl.createBuffer();
this._texCoordsBuffer = gl.createBuffer();
this._colorPointerBuffer = gl.createBuffer();
// Set blend mode
this._blendFunc.src = gl.SRC_ALPHA;
this._blendFunc.dst = gl.ONE_MINUS_SRC_ALPHA;
this.texture = texture;
this.color = color;
this.scheduleUpdate();
//bind buffer
gl.bindBuffer(gl.ARRAY_BUFFER, this._verticesBuffer);
gl.bufferData(gl.ARRAY_BUFFER, this._vertices, gl.DYNAMIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordsBuffer);
gl.bufferData(gl.ARRAY_BUFFER, this._texCoords, gl.DYNAMIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, this._colorPointerBuffer);
gl.bufferData(gl.ARRAY_BUFFER, this._colorPointer, gl.DYNAMIC_DRAW);
return true;
},
/**
* color used for the tint
* @param {cc.Color} color
*/
tintWithColor:function (color) {
this.color = color;
// Fast assignation
var locColorPointer = this._colorPointer;
for (var i = 0, len = this._nuPoints * 2; i < len; i++) {
locColorPointer[i * 4] = color.r;
locColorPointer[i * 4 + 1] = color.g;
locColorPointer[i * 4 + 2] = color.b;
}
},
/**
* Remove all living segments of the ribbon
*/
reset:function () {
this._nuPoints = 0;
},
/**
* Set the position. <br />
*
* @param {cc.Vec2|Number} position
* @param {Number} [yValue=undefined] If not exists, the first parameter must be cc.Vec2.
*/
setPosition:function (position, yValue) {
this.startingPositionInitialized = true;
if(yValue === undefined){
this._positionR.x = position.x;
this._positionR.y = position.y;
} else {
this._positionR.x = position;
this._positionR.y = yValue;
}
},
/**
* Gets the position.x
* @return {Number}
*/
getPositionX:function () {
return this._positionR.x;
},
/**
* Set the position.x
* @param {Number} x
*/
setPositionX:function (x) {
this._positionR.x = x;
if(!this.startingPositionInitialized)
this.startingPositionInitialized = true;
},
/**
* Gets the position.y
* @return {Number}
*/
getPositionY:function () {
return this._positionR.y;
},
/**
* Set the position.y
* @param {Number} y
*/
setPositionY:function (y) {
this._positionR.y = y;
if(!this.startingPositionInitialized)
this.startingPositionInitialized = true;
},
/**
* <p>schedules the "update" method. <br/>
* It will use the order number 0. This method will be called every frame. <br/>
* Scheduled methods with a lower order value will be called before the ones that have a higher order value.<br/>
* Only one "update" method could be scheduled per node.</p>
* @param {Number} delta
*/
update:function (delta) {
if (!this.startingPositionInitialized)
onLoad: function () {
if (this._root) {
return;
//TODO update the color (need move to render cmd)
this._renderCmd._updateDisplayColor();
delta *= this._fadeDelta;
var newIdx, newIdx2, i, i2;
var mov = 0;
// Update current points
var locNuPoints = this._nuPoints;
var locPointState = this._pointState, locPointVertexes = this._pointVertexes, locVertices = this._vertices;
var locColorPointer = this._colorPointer;
for (i = 0; i < locNuPoints; i++) {
locPointState[i] -= delta;
if (locPointState[i] <= 0)
mov++;
else {
newIdx = i - mov;
if (mov > 0) {
// Move data
locPointState[newIdx] = locPointState[i];
// Move point
locPointVertexes[newIdx * 2] = locPointVertexes[i * 2];
locPointVertexes[newIdx * 2 + 1] = locPointVertexes[i * 2 + 1];
// Move vertices
i2 = i * 2;
newIdx2 = newIdx * 2;
locVertices[newIdx2 * 2] = locVertices[i2 * 2];
locVertices[newIdx2 * 2 + 1] = locVertices[i2 * 2 + 1];
locVertices[(newIdx2 + 1) * 2] = locVertices[(i2 + 1) * 2];
locVertices[(newIdx2 + 1) * 2 + 1] = locVertices[(i2 + 1) * 2 + 1];
// Move color
i2 *= 4;
newIdx2 *= 4;
locColorPointer[newIdx2 + 0] = locColorPointer[i2 + 0];
locColorPointer[newIdx2 + 1] = locColorPointer[i2 + 1];
locColorPointer[newIdx2 + 2] = locColorPointer[i2 + 2];
locColorPointer[newIdx2 + 4] = locColorPointer[i2 + 4];
locColorPointer[newIdx2 + 5] = locColorPointer[i2 + 5];
locColorPointer[newIdx2 + 6] = locColorPointer[i2 + 6];
} else
newIdx2 = newIdx * 8;
var op = locPointState[newIdx] * 255.0;
locColorPointer[newIdx2 + 3] = op;
locColorPointer[newIdx2 + 7] = op;
}
}
locNuPoints -= mov;
// Append new point
var appendNewPoint = true;
if (locNuPoints >= this._maxPoints)
appendNewPoint = false;
else if (locNuPoints > 0) {
var a1 = cc.pDistanceSQ(cc.p(locPointVertexes[(locNuPoints - 1) * 2], locPointVertexes[(locNuPoints - 1) * 2 + 1]),
this._positionR) < this._minSeg;
var a2 = (locNuPoints === 1) ? false : (cc.pDistanceSQ(
cc.p(locPointVertexes[(locNuPoints - 2) * 2], locPointVertexes[(locNuPoints - 2) * 2 + 1]), this._positionR) < (this._minSeg * 2.0));
if (a1 || a2)
appendNewPoint = false;
this._root = new _ccsg.Node();
var motionStreak = new _ccsg.MotionStreak();
if (this._texture) {
motionStreak.initWithFade(this._fadeTime, this._minSeg, this._stroke, this.node.color, this._texture);
}
if (appendNewPoint) {
locPointVertexes[locNuPoints * 2] = this._positionR.x;
locPointVertexes[locNuPoints * 2 + 1] = this._positionR.y;
locPointState[locNuPoints] = 1.0;
// Color assignment
var offset = locNuPoints * 8;
var locDisplayedColor = this.getDisplayedColor();
locColorPointer[offset] = locDisplayedColor.r;
locColorPointer[offset + 1] = locDisplayedColor.g;
locColorPointer[offset + 2] = locDisplayedColor.b;
//*((ccColor3B*)(m_pColorPointer + offset+4)) = this._color;
locColorPointer[offset + 4] = locDisplayedColor.r;
locColorPointer[offset + 5] = locDisplayedColor.g;
locColorPointer[offset + 6] = locDisplayedColor.b;
// Opacity
locColorPointer[offset + 3] = 255;
locColorPointer[offset + 7] = 255;
// Generate polygon
if (locNuPoints > 0 && this.fastMode) {
if (locNuPoints > 1)
cc.vertexLineToPolygon(locPointVertexes, this._stroke, this._vertices, locNuPoints, 1);
else
cc.vertexLineToPolygon(locPointVertexes, this._stroke, this._vertices, 0, 2);
}
locNuPoints++;
motionStreak.setFastMode(this._fastMode);
this._root.addChild(motionStreak);
var sgNode = this.node._sgNode;
if (sgNode) {
sgNode.addChild(this._root, -10);
}
if (!this.fastMode)
cc.vertexLineToPolygon(locPointVertexes, this._stroke, this._vertices, 0, locNuPoints);
// Updated Tex Coords only if they are different than previous step
if (locNuPoints && this._previousNuPoints !== locNuPoints) {
var texDelta = 1.0 / locNuPoints;
var locTexCoords = this._texCoords;
for (i = 0; i < locNuPoints; i++) {
locTexCoords[i * 4] = 0;
locTexCoords[i * 4 + 1] = texDelta * i;
locTexCoords[(i * 2 + 1) * 2] = 1;
locTexCoords[(i * 2 + 1) * 2 + 1] = texDelta * i;
}
this._previousNuPoints = locNuPoints;
}
this._nuPoints = locNuPoints;
this._motionStreak = motionStreak;
// TODO: use sizeProvider when merge to dev branch.
this.node.on('color-changed', this._colorChanged, this);
},
_createRenderCmd: function(){
if(cc._renderType === cc.game.RENDER_TYPE_WEBGL)
return new cc.MotionStreak.WebGLRenderCmd(this);
else
return null; //MotionStreak doesn't support Canvas mode
lateUpdate: function (delta) {
if (CC_EDITOR && !this.preview) {
return;
}
if (this._motionStreak) {
var worldMt = this.node.getNodeToWorldTransform();
this._root.setPosition(-worldMt.tx, -worldMt.ty);
this._motionStreak.setPosition(worldMt.tx, worldMt.ty);
this._motionStreak.update(delta);
}
},
onDestroy: function () {
// TODO: use sizeProvider when merge to dev branch.
this.node.off('color-changed', this._colorChanged, this);
},
_colorChanged: function () {
if (this._motionStreak) {
this._motionStreak.tintWithColor(this.node.color);
}
}
});
/**
* Please use new cc.MotionStreak instead. <br />
* Creates and initializes a motion streak with fade in seconds, minimum segments, stroke's width, color, texture filename or texture
* @deprecated since v3.0 please use new cc.MotionStreak instead.
* @param {Number} fade time to fade
* @param {Number} minSeg minimum segment size
* @param {Number} stroke stroke's width
* @param {Number} color
* @param {string|cc.Texture2D} texture texture filename or texture
* @return {cc.MotionStreak}
* @example
* //example
* new cc.MotionStreak(2, 3, 32, cc.Color.GREEN, s_streak);
*/
cc.MotionStreak.create = function (fade, minSeg, stroke, color, texture) {
return new cc.MotionStreak(fade, minSeg, stroke, color, texture);
};
cc.MotionStreak = module.exports = MotionStreak;
+728
View File
@@ -0,0 +1,728 @@
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
Copyright (c) 2008-2009 Jason Booth
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
function getPositionX () {
return this._positionR.x;
}
function setPositionX (x) {
this._positionR.x = x;
if(!this.startingPositionInitialized)
this.startingPositionInitialized = true;
}
function getPositionY () {
return this._positionR.y;
}
function setPositionY (y) {
this._positionR.y = y;
if(!this.startingPositionInitialized)
this.startingPositionInitialized = true;
}
/**
* converts a line to a polygon
* @param {Float32Array} points
* @param {Number} stroke
* @param {Float32Array} vertices
* @param {Number} offset
* @param {Number} nuPoints
*/
function vertexLineToPolygon (points, stroke, vertices, offset, nuPoints) {
nuPoints += offset;
if (nuPoints <= 1)
return;
stroke *= 0.5;
var idx;
var nuPointsMinus = nuPoints - 1;
for (var i = offset; i < nuPoints; i++) {
idx = i * 2;
var p1 = cc.p(points[i * 2], points[i * 2 + 1]);
var perpVector;
if (i === 0)
perpVector = cc.pPerp(cc.pNormalize(cc.pSub(p1, cc.p(points[(i + 1) * 2], points[(i + 1) * 2 + 1]))));
else if (i === nuPointsMinus)
perpVector = cc.pPerp(cc.pNormalize(cc.pSub(cc.p(points[(i - 1) * 2], points[(i - 1) * 2 + 1]), p1)));
else {
var p0 = cc.p(points[(i - 1) * 2], points[(i - 1) * 2 + 1]);
var p2 = cc.p(points[(i + 1) * 2], points[(i + 1) * 2 + 1]);
var p2p1 = cc.pNormalize(cc.pSub(p2, p1));
var p0p1 = cc.pNormalize(cc.pSub(p0, p1));
// Calculate angle between vectors
var angle = Math.acos(cc.pDot(p2p1, p0p1));
if (angle < cc.degreesToRadians(70))
perpVector = cc.pPerp(cc.pNormalize(cc.pMidpoint(p2p1, p0p1)));
else if (angle < cc.degreesToRadians(170))
perpVector = cc.pNormalize(cc.pMidpoint(p2p1, p0p1));
else
perpVector = cc.pPerp(cc.pNormalize(cc.pSub(p2, p0)));
}
perpVector = cc.pMult(perpVector, stroke);
vertices[idx * 2] = p1.x + perpVector.x;
vertices[idx * 2 + 1] = p1.y + perpVector.y;
vertices[(idx + 1) * 2] = p1.x - perpVector.x;
vertices[(idx + 1) * 2 + 1] = p1.y - perpVector.y;
}
// Validate vertexes
offset = (offset === 0) ? 0 : offset - 1;
for (i = offset; i < nuPointsMinus; i++) {
idx = i * 2;
var idx1 = idx + 2;
var v1 = cc.v2(vertices[idx * 2], vertices[idx * 2 + 1]);
var v2 = cc.v2(vertices[(idx + 1) * 2], vertices[(idx + 1) * 2 + 1]);
var v3 = cc.v2(vertices[idx1 * 2], vertices[idx1 * 2]);
var v4 = cc.v2(vertices[(idx1 + 1) * 2], vertices[(idx1 + 1) * 2 + 1]);
//BOOL fixVertex = !ccpLineIntersect(ccp(p1.x, p1.y), ccp(p4.x, p4.y), ccp(p2.x, p2.y), ccp(p3.x, p3.y), &s, &t);
var fixVertexResult = !vertexLineIntersect(v1.x, v1.y, v4.x, v4.y, v2.x, v2.y, v3.x, v3.y);
if (!fixVertexResult.isSuccess)
if (fixVertexResult.value < 0.0 || fixVertexResult.value > 1.0)
fixVertexResult.isSuccess = true;
if (fixVertexResult.isSuccess) {
vertices[idx1 * 2] = v4.x;
vertices[idx1 * 2 + 1] = v4.y;
vertices[(idx1 + 1) * 2] = v3.x;
vertices[(idx1 + 1) * 2 + 1] = v3.y;
}
}
}
/**
* returns whether or not the line intersects
* @param {Number} Ax
* @param {Number} Ay
* @param {Number} Bx
* @param {Number} By
* @param {Number} Cx
* @param {Number} Cy
* @param {Number} Dx
* @param {Number} Dy
* @return {Object}
*/
function vertexLineIntersect (Ax, Ay, Bx, By, Cx, Cy, Dx, Dy) {
var distAB, theCos, theSin, newX;
// FAIL: Line undefined
if ((Ax === Bx && Ay === By) || (Cx === Dx && Cy === Dy))
return {isSuccess:false, value:0};
// Translate system to make A the origin
Bx -= Ax;
By -= Ay;
Cx -= Ax;
Cy -= Ay;
Dx -= Ax;
Dy -= Ay;
// Length of segment AB
distAB = Math.sqrt(Bx * Bx + By * By);
// Rotate the system so that point B is on the positive X axis.
theCos = Bx / distAB;
theSin = By / distAB;
newX = Cx * theCos + Cy * theSin;
Cy = Cy * theCos - Cx * theSin;
Cx = newX;
newX = Dx * theCos + Dy * theSin;
Dy = Dy * theCos - Dx * theSin;
Dx = newX;
// FAIL: Lines are parallel.
if (Cy === Dy) return {isSuccess:false, value:0};
// Discover the relative position of the intersection in the line AB
var t = (Dx + (Cx - Dx) * Dy / (Dy - Cy)) / distAB;
// Success.
return {isSuccess:true, value:t};
}
/**
* returns wheter or not polygon defined by vertex list is clockwise
* @param {Array} verts
* @return {Boolean}
*/
function vertexListIsClockwise (verts) {
for (var i = 0, len = verts.length; i < len; i++) {
var a = verts[i];
var b = verts[(i + 1) % len];
var c = verts[(i + 2) % len];
if (cc.pCross(cc.pSub(b, a), cc.pSub(c, b)) > 0)
return false;
}
return true;
}
/**
* cc.MotionStreak manages a Ribbon based on it's motion in absolute space. <br/>
* You construct it with a fadeTime, minimum segment size, texture path, texture <br/>
* length and color. The fadeTime controls how long it takes each vertex in <br/>
* the streak to fade out, the minimum segment size it how many pixels the <br/>
* streak will move before adding a new ribbon segment, and the texture <br/>
* length is the how many pixels the texture is stretched across. The texture <br/>
* is vertically aligned along the streak segment.
* @class
* @extends _ccsg.Node
*
* @property {cc.Texture2D} texture - Texture used for the motion streak.
* @property {Boolean} fastMode - Indicate whether use fast mode.
* @property {Boolean} startingPositionInitialized - Indicate whether starting position initialized.
* @example
* //example
* new _ccsg.MotionStreak(2, 3, 32, cc.Color.GREEN, s_streak);
*/
_ccsg.MotionStreak = cc.Class({
name: "MotionStreak",
extends: _ccsg.Node,
properties: {
texture: null,
fastMode: false,
startingPositionInitialized: false,
_blendFunc: null,
_stroke: 0,
_fadeDelta: 0,
_minSeg: 0,
_maxPoints: 0,
_nuPoints: 0,
_previousNuPoints: 0,
/* Pointers */
_pointVertexes: null,
_pointState: null,
// webgl
_vertices: null,
_colorPointer: null,
_texCoords: null,
_verticesBuffer: null,
_colorPointerBuffer: null,
_texCoordsBuffer: null,
_className: "MotionStreak",
x: {
get: getPositionX,
set: setPositionX
},
y: {
get: getPositionY,
set: setPositionY
}
},
/**
* creates and initializes a motion streak with fade in seconds, minimum segments, stroke's width, color, texture filename or texture <br/>
* Constructor of cc.MotionStreak
* @param {Number} fade time to fade
* @param {Number} minSeg minimum segment size
* @param {Number} stroke stroke's width
* @param {Number} color
* @param {string|cc.Texture2D} texture texture filename or texture
*/
ctor: function () {
var fade = arguments[0];
var minSeg = arguments[1];
var stroke = arguments[2];
var color = arguments[3];
var texture = arguments[4];
this._positionR = cc.p(0, 0);
this._blendFunc = new cc.BlendFunc(cc.SRC_ALPHA, cc.ONE_MINUS_SRC_ALPHA);
this.fastMode = false;
this.startingPositionInitialized = false;
this.texture = null;
this._stroke = 0;
this._fadeDelta = 0;
this._minSeg = 0;
this._maxPoints = 0;
this._nuPoints = 0;
this._previousNuPoints = 0;
/** Pointers */
this._pointVertexes = null;
this._pointState = null;
// webgl
this._vertices = null;
this._colorPointer = null;
this._texCoords = null;
this._verticesBuffer = null;
this._colorPointerBuffer = null;
this._texCoordsBuffer = null;
if(texture !== undefined)
this.initWithFade(fade, minSeg, stroke, color, texture);
},
/**
* initializes a motion streak with fade in seconds, minimum segments, stroke's width, color and texture filename or texture
* @param {Number} fade time to fade
* @param {Number} minSeg minimum segment size
* @param {Number} stroke stroke's width
* @param {Number} color
* @param {string|cc.Texture2D} texture texture filename or texture
* @return {Boolean}
*/
initWithFade:function (fade, minSeg, stroke, color, texture) {
if (cc.js.isString(texture))
texture = cc.textureCache.addImage(texture);
this.anchorX = 0;
this.anchorY = 0;
this.ignoreAnchor = true;
this.startingPositionInitialized = false;
this.fastMode = true;
this._stroke = stroke;
this.setMinSeg(minSeg);
this.setFadeTime(fade);
// Set blend mode
this._blendFunc.src = gl.SRC_ALPHA;
this._blendFunc.dst = gl.ONE_MINUS_SRC_ALPHA;
this.setTexture(texture);
this.color = color;
this.scheduleUpdate();
return true;
},
/**
* Gets the texture.
* @return {cc.Texture2D}
*/
getTexture:function () {
return this.texture;
},
/**
* Set the texture.
* @param {cc.Texture2D} texture
*/
setTexture:function (texture) {
if (texture && this.texture !== texture) {
this.texture = texture;
}
},
/**
* Gets the blend func.
* @return {cc.BlendFunc}
*/
getBlendFunc:function () {
return this._blendFunc;
},
/**
* Set the blend func.
* @param {Number} src
* @param {Number} dst
*/
setBlendFunc:function (src, dst) {
if (dst === undefined) {
this._blendFunc = src;
} else {
this._blendFunc.src = src;
this._blendFunc.dst = dst;
}
},
/**
* Gets opacity.
* @warning cc.MotionStreak.getOpacity has not been supported.
* @returns {number}
*/
getOpacity:function () {
cc.log("cc.MotionStreak.getOpacity has not been supported.");
return 0;
},
/**
* Set opacity.
* @warning cc.MotionStreak.setOpacity has not been supported.
* @param opacity
*/
setOpacity:function (opacity) {
cc.log("cc.MotionStreak.setOpacity has not been supported.");
},
/**
* Set opacity modify RGB.
* @warning cc.MotionStreak.setOpacityModifyRGB has not been supported.
* @param value
*/
setOpacityModifyRGB:function (value) {
},
/**
* Checking OpacityModifyRGB.
* @returns {boolean}
*/
isOpacityModifyRGB:function () {
return false;
},
/**
* Get Fade Time.
* @returns {Number}
*/
getFadeTime: function () {
return 1.0 / this._fadeDelta;
},
/**
* Set Fade Time.
* @param {Number} fade
*/
setFadeTime: function (fade) {
this._fadeDelta = 1.0 / fade;
var locMaxPoints = (0 | (fade * 60)) + 2;
this._maxPoints = locMaxPoints;
this._nuPoints = 0;
this._pointState = new Float32Array(locMaxPoints);
this._pointVertexes = new Float32Array(locMaxPoints * 2);
this._vertices = new Float32Array(locMaxPoints * 4);
this._texCoords = new Float32Array(locMaxPoints * 4);
this._colorPointer = new Uint8Array(locMaxPoints * 8);
this._verticesBuffer = gl.createBuffer();
this._texCoordsBuffer = gl.createBuffer();
this._colorPointerBuffer = gl.createBuffer();
//bind buffer
gl.bindBuffer(gl.ARRAY_BUFFER, this._verticesBuffer);
gl.bufferData(gl.ARRAY_BUFFER, this._vertices, gl.DYNAMIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordsBuffer);
gl.bufferData(gl.ARRAY_BUFFER, this._texCoords, gl.DYNAMIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, this._colorPointerBuffer);
gl.bufferData(gl.ARRAY_BUFFER, this._colorPointer, gl.DYNAMIC_DRAW);
},
/**
* Get Minimum Segment Size.
* @return {Number}
*/
getMinSeg: function () {
return this._minSeg;
},
/**
* Set Minimum Segment Size.
* @param {Number} minSeg
*/
setMinSeg: function (minSeg) {
this._minSeg = (minSeg === -1.0) ? (this._stroke / 5.0) : minSeg;
this._minSeg *= this._minSeg;
},
/**
* Checking fast mode.
* @returns {boolean}
*/
isFastMode:function () {
return this.fastMode;
},
/**
* set fast mode
* @param {Boolean} fastMode
*/
setFastMode:function (fastMode) {
this.fastMode = fastMode;
},
/**
* Checking starting position initialized.
* @returns {boolean}
*/
isStartingPositionInitialized:function () {
return this.startingPositionInitialized;
},
/**
* Set Starting Position Initialized.
* @param {Boolean} startingPositionInitialized
*/
setStartingPositionInitialized:function (startingPositionInitialized) {
this.startingPositionInitialized = startingPositionInitialized;
},
/**
* Get stroke.
* @returns {Number} stroke
*/
getStroke:function () {
return this._stroke;
},
/**
* Set stroke.
* @param {Number} stroke
*/
setStroke:function (stroke) {
this._stroke = stroke;
},
/**
* color used for the tint
* @param {cc.Color} color
*/
tintWithColor:function (color) {
this.color = color;
// Fast assignation
var locColorPointer = this._colorPointer;
for (var i = 0, len = this._nuPoints * 2; i < len; i++) {
locColorPointer[i * 4] = color.r;
locColorPointer[i * 4 + 1] = color.g;
locColorPointer[i * 4 + 2] = color.b;
}
},
/**
* Remove all living segments of the ribbon
*/
reset:function () {
this._nuPoints = 0;
},
/**
* Set the position. <br />
*
* @param {cc.Point|Number} position
* @param {Number} [yValue=undefined] If not exists, the first parameter must be cc.Point.
*/
setPosition:function (position, yValue) {
this.startingPositionInitialized = true;
if(yValue === undefined){
this._positionR.x = position.x;
this._positionR.y = position.y;
} else {
this._positionR.x = position;
this._positionR.y = yValue;
}
},
/**
* Gets the position.x
* @return {Number}
*/
getPositionX:function () {
return this._positionR.x;
},
/**
* Set the position.x
* @param {Number} x
*/
setPositionX:function (x) {
this._positionR.x = x;
if(!this.startingPositionInitialized)
this.startingPositionInitialized = true;
},
/**
* Gets the position.y
* @return {Number}
*/
getPositionY:function () {
return this._positionR.y;
},
/**
* Set the position.y
* @param {Number} y
*/
setPositionY:function (y) {
this._positionR.y = y;
if(!this.startingPositionInitialized)
this.startingPositionInitialized = true;
},
/**
* <p>schedules the "update" method. <br/>
* It will use the order number 0. This method will be called every frame. <br/>
* Scheduled methods with a lower order value will be called before the ones that have a higher order value.<br/>
* Only one "update" method could be scheduled per node.</p>
* @param {Number} delta
*/
update:function (delta) {
if (!this.startingPositionInitialized)
return;
delta *= this._fadeDelta;
var newIdx, newIdx2, i, i2;
var mov = 0;
// Update current points
var locNuPoints = this._nuPoints;
var locPointState = this._pointState, locPointVertexes = this._pointVertexes, locVertices = this._vertices;
var locColorPointer = this._colorPointer;
for (i = 0; i < locNuPoints; i++) {
locPointState[i] -= delta;
if (locPointState[i] <= 0)
mov++;
else {
newIdx = i - mov;
if (mov > 0) {
// Move data
locPointState[newIdx] = locPointState[i];
// Move point
locPointVertexes[newIdx * 2] = locPointVertexes[i * 2];
locPointVertexes[newIdx * 2 + 1] = locPointVertexes[i * 2 + 1];
// Move vertices
i2 = i * 2;
newIdx2 = newIdx * 2;
locVertices[newIdx2 * 2] = locVertices[i2 * 2];
locVertices[newIdx2 * 2 + 1] = locVertices[i2 * 2 + 1];
locVertices[(newIdx2 + 1) * 2] = locVertices[(i2 + 1) * 2];
locVertices[(newIdx2 + 1) * 2 + 1] = locVertices[(i2 + 1) * 2 + 1];
// Move color
i2 *= 4;
newIdx2 *= 4;
locColorPointer[newIdx2 + 0] = locColorPointer[i2 + 0];
locColorPointer[newIdx2 + 1] = locColorPointer[i2 + 1];
locColorPointer[newIdx2 + 2] = locColorPointer[i2 + 2];
locColorPointer[newIdx2 + 4] = locColorPointer[i2 + 4];
locColorPointer[newIdx2 + 5] = locColorPointer[i2 + 5];
locColorPointer[newIdx2 + 6] = locColorPointer[i2 + 6];
} else
newIdx2 = newIdx * 8;
var op = locPointState[newIdx] * 255.0;
locColorPointer[newIdx2 + 3] = op;
locColorPointer[newIdx2 + 7] = op;
}
}
locNuPoints -= mov;
// Append new point
var appendNewPoint = true;
if (locNuPoints >= this._maxPoints)
appendNewPoint = false;
else if (locNuPoints > 0) {
var locPoint1 = cc.p(locPointVertexes[(locNuPoints - 1) * 2], locPointVertexes[(locNuPoints - 1) * 2 + 1]);
var a1 = cc.pDistanceSQ(locPoint1, this._positionR) < this._minSeg;
var locPoint2 = cc.p(locPointVertexes[(locNuPoints - 2) * 2], locPointVertexes[(locNuPoints - 2) * 2 + 1]);
var a2 = (locNuPoints === 1) ? false : (cc.pDistanceSQ(locPoint2, this._positionR) < (this._minSeg * 2.0));
if (a1 || a2)
appendNewPoint = false;
}
if (appendNewPoint) {
locPointVertexes[locNuPoints * 2] = this._positionR.x;
locPointVertexes[locNuPoints * 2 + 1] = this._positionR.y;
locPointState[locNuPoints] = 1.0;
// Color assignment
var offset = locNuPoints * 8;
var locDisplayedColor = this.getDisplayedColor();
locColorPointer[offset] = locDisplayedColor.r;
locColorPointer[offset + 1] = locDisplayedColor.g;
locColorPointer[offset + 2] = locDisplayedColor.b;
//*((ccColor3B*)(m_pColorPointer + offset+4)) = this._color;
locColorPointer[offset + 4] = locDisplayedColor.r;
locColorPointer[offset + 5] = locDisplayedColor.g;
locColorPointer[offset + 6] = locDisplayedColor.b;
// Opacity
locColorPointer[offset + 3] = 255;
locColorPointer[offset + 7] = 255;
// Generate polygon
if (locNuPoints > 0 && this.fastMode) {
if (locNuPoints > 1)
vertexLineToPolygon(locPointVertexes, this._stroke, this._vertices, locNuPoints, 1);
else
vertexLineToPolygon(locPointVertexes, this._stroke, this._vertices, 0, 2);
}
locNuPoints++;
}
if (!this.fastMode)
vertexLineToPolygon(locPointVertexes, this._stroke, this._vertices, 0, locNuPoints);
// Updated Tex Coords only if they are different than previous step
if (locNuPoints && this._previousNuPoints !== locNuPoints) {
var texDelta = 1.0 / locNuPoints;
var locTexCoords = this._texCoords;
for (i = 0; i < locNuPoints; i++) {
locTexCoords[i * 4] = 0;
locTexCoords[i * 4 + 1] = texDelta * i;
locTexCoords[(i * 2 + 1) * 2] = 1;
locTexCoords[(i * 2 + 1) * 2 + 1] = texDelta * i;
}
this._previousNuPoints = locNuPoints;
}
this._nuPoints = locNuPoints;
},
_createRenderCmd: function(){
if(cc._renderType === cc.game.RENDER_TYPE_WEBGL)
return new _ccsg.MotionStreak.WebGLRenderCmd(this);
else
return null; //MotionStreak doesn't support Canvas mode
}
});
@@ -0,0 +1,70 @@
/****************************************************************************
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
_ccsg.MotionStreak.WebGLRenderCmd = function(renderableObject){
_ccsg.Node.WebGLRenderCmd.call(this, renderableObject);
this._needDraw = true;
this._shaderProgram = cc.shaderCache.programForKey(cc.macro.SHADER_POSITION_TEXTURECOLOR);
};
_ccsg.MotionStreak.WebGLRenderCmd.prototype = Object.create(_ccsg.Node.WebGLRenderCmd.prototype);
_ccsg.MotionStreak.WebGLRenderCmd.prototype.constructor = _ccsg.Sprite.WebGLRenderCmd;
_ccsg.MotionStreak.WebGLRenderCmd.prototype.rendering = function(ctx){
var node = this._node;
if (node._nuPoints <= 1)
return;
if (node.texture && node.texture.isLoaded()) {
ctx = ctx || cc._renderContext;
// update the color
this._updateDisplayColor();
this._shaderProgram.use();
this._shaderProgram._setUniformForMVPMatrixWithMat4(this._stackMatrix);
cc.gl.enableVertexAttribs(cc.macro.VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
cc.gl.blendFunc(node._blendFunc.src, node._blendFunc.dst);
cc.gl.bindTexture2D(node.texture);
//position
ctx.bindBuffer(ctx.ARRAY_BUFFER, node._verticesBuffer);
ctx.bufferData(ctx.ARRAY_BUFFER, node._vertices, ctx.DYNAMIC_DRAW);
ctx.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_POSITION, 2, ctx.FLOAT, false, 0, 0);
//texcoords
ctx.bindBuffer(ctx.ARRAY_BUFFER, node._texCoordsBuffer);
ctx.bufferData(ctx.ARRAY_BUFFER, node._texCoords, ctx.DYNAMIC_DRAW);
ctx.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_TEX_COORDS, 2, ctx.FLOAT, false, 0, 0);
//colors
ctx.bindBuffer(ctx.ARRAY_BUFFER, node._colorPointerBuffer);
ctx.bufferData(ctx.ARRAY_BUFFER, node._colorPointer, ctx.DYNAMIC_DRAW);
ctx.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_COLOR, 4, ctx.UNSIGNED_BYTE, true, 0, 0);
ctx.drawArrays(ctx.TRIANGLE_STRIP, 0, node._nuPoints * 2);
cc.g_NumberOfDraws++;
}
};
+4
View File
@@ -31,6 +31,10 @@ if (!(CC_EDITOR && Editor.isCoreLevel)) {
require('./cocos2d/particle/CCParticleSystem');
}
if (!(CC_EDITOR && Editor.isCoreLevel)) {
require('./cocos2d/motion-streak/CCMotionStreak');
}
require('./cocos2d/tilemap/CCTiledMapAsset');
if (!(CC_EDITOR && Editor.isCoreLevel)) {
require('./cocos2d/tilemap/CCTiledMap');
+5 -1
View File
@@ -102,7 +102,11 @@ var srcs = [
'./cocos2d/physics/CCPhysicsDebugNode.js',
'./cocos2d/physics/CCPhysicsDebugNodeCanvasRenderCmd.js',
'./cocos2d/physics/CCPhysicsDebugNodeWebGLRenderCmd.js'
'./cocos2d/physics/CCPhysicsDebugNodeWebGLRenderCmd.js',
'./cocos2d/motion-streak/CCSGMotionStreak.js',
'./cocos2d/motion-streak/CCSGMotionStreakWebGLRenderCmd.js'
];
var header = new Buffer('(function (cc, ccui, ccs, cp) {\n');
+2 -1
View File
@@ -200,7 +200,8 @@ window._ccsg = {
Label: cc.Label,
EditBox: cc.EditBox,
TMXTiledMap: cc.TMXTiledMap,
TMXLayer: cc.TMXLayer
TMXLayer: cc.TMXLayer,
MotionStreak: cc.MotionStreak
};
// rename cc.Class to cc._Class
+3 -1
View File
@@ -164,7 +164,9 @@ cc.game.run({
"src/SpritePolygonTest/SpritePolygonTest.js",
"src/NodeEventTest/NodeEventTest.js"
"src/NodeEventTest/NodeEventTest.js",
"src/MotionStreakTest/MotionStreakTest.js"
]
}, function(){
cc.view.enableRetina(false);
@@ -0,0 +1,264 @@
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
Copyright (c) 2008-2009 Jason Booth
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
var TAG_LABEL = 1;
var TAG_SPRITE1 = 2;
var TAG_SPRITE2 = 3;
var sceneIdx = -1;
var MotionStreakTest = cc.Layer.extend({
_streak:null,
title:function () {
return "No title";
},
subtitle:function () {
return "";
},
onEnter:function () {
this._super();
var winSize = cc.director.getWinSize();
var label = new cc.LabelTTF(this.title(), "Arial", 32);
this.addChild(label, 0, TAG_LABEL);
label.x = winSize.width / 2;
label.y = winSize.height - 50;
var subTitle = this.subtitle();
if (subTitle.length > 0) {
var l = new cc.LabelTTF(subTitle, "Arial", 16);
this.addChild(l, 1);
l.x = winSize.width / 2;
l.y = winSize.height - 80;
}
var item1 = new cc.MenuItemImage(s_pathB1, s_pathB2, this.backCallback, this);
var item2 = new cc.MenuItemImage(s_pathR1, s_pathR2, this.restartCallback, this);
var item3 = new cc.MenuItemImage(s_pathF1, s_pathF2, this.nextCallback, this);
var menu = new cc.Menu(item1, item2, item3);
menu.x = 0;
menu.y = 0;
item1.x = cc.visibleRect.center.x - item2.width * 2;
item1.y = cc.visibleRect.bottom.y + item2.height / 2;
item2.x = cc.visibleRect.center.x;
item2.y = cc.visibleRect.bottom.y + item2.height / 2;
item3.x = cc.visibleRect.center.x + item2.width * 2;
item3.y = cc.visibleRect.bottom.y + item2.height / 2;
this.addChild(menu, 1);
var itemMode = new cc.MenuItemToggle(new cc.MenuItemFont("Use High Quality Mode"),
new cc.MenuItemFont("Use Fast Mode"), this.modeCallback, this);
var menuMode = new cc.Menu(itemMode);
this.addChild(menuMode);
menuMode.x = winSize.width / 2;
menuMode.y = winSize.height / 4;
},
restartCallback:function (sender) {
var scene = new MotionStreakTestScene();
scene.addChild(restartMotionAction());
cc.director.runScene(scene);
},
nextCallback:function (sender) {
var scene = new MotionStreakTestScene();
scene.addChild(nextMotionAction());
cc.director.runScene(scene);
},
backCallback:function (sender) {
var scene = new MotionStreakTestScene;
scene.addChild(backMotionAction());
cc.director.runScene(scene);
},
modeCallback:function (sender) {
var fastMode = this._streak.fastMode;
this._streak.fastMode = !fastMode;
}
});
var MotionStreakTest1 = MotionStreakTest.extend({
_root:null,
_target:null,
onEnter:function () {
this._super();
var winSize = cc.director.getWinSize();
// the root object just rotates around
this._root = new _ccsg.Sprite(s_pathR1);
this.addChild(this._root, 1);
this._root.x = winSize.width / 2;
this._root.y = winSize.height / 2;
// the target object is offset from root, and the streak is moved to follow it
this._target = new _ccsg.Sprite(s_pathR1);
this._root.addChild(this._target);
this._target.x = winSize.width / 4;
this._target.y = 0;
// create the streak object and add it to the scene
this._streak = new _ccsg.MotionStreak(2, 3, 32, cc.Color.GREEN, s_streak);
this.addChild(this._streak);
// schedule an update on each frame so we can syncronize the streak with the target
this.schedule(this.onUpdate);
var a1 = cc.rotateBy(2, 360);
var action1 = a1.repeatForever();
var motion = cc.moveBy(2, cc.p(100, 0));
this._root.runAction(cc.sequence(motion, motion.reverse()).repeatForever());
this._root.runAction(action1);
var colorAction = cc.sequence(
cc.tintTo(0.2, 255, 0, 0),
cc.tintTo(0.2, 0, 255, 0),
cc.tintTo(0.2, 0, 0, 255),
cc.tintTo(0.2, 0, 255, 255),
cc.tintTo(0.2, 255, 255, 0),
cc.tintTo(0.2, 255, 0, 255),
cc.tintTo(0.2, 255, 255, 255)
).repeatForever();
this._streak.runAction(colorAction);
},
onUpdate:function (delta) {
var pos = this._target.convertToWorldSpace(cc.p(this._target.width/2, 0));
this._streak.x = pos.x;
this._streak.y = pos.y;
},
title:function () {
return "MotionStreak test 1";
}
});
var MotionStreakTest2 = MotionStreakTest.extend({
_root:null,
_target:null,
onEnter:function () {
this._super();
cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ALL_AT_ONCE,
onTouchesMoved:function (touches, event) {
if (touches.length == 0)
return;
var touch = touches[0];
var touchLocation = touch.getLocation();
var streak = event.getCurrentTarget()._streak;
streak.x = touchLocation.x;
streak.y = touchLocation.y;
}
}, this);
var winSize = cc.director.getWinSize();
// create the streak object and add it to the scene
this._streak = new _ccsg.MotionStreak(3, 3, 64, cc.Color.WHITE, s_streak);
this.addChild(this._streak);
this._streak.x = winSize.width / 2;
this._streak.y = winSize.height / 2;
},
title:function () {
return "MotionStreak test";
}
});
var Issue1358 = MotionStreakTest.extend({
_center:null,
_radius:0,
_angle:0,
title:function () {
return "Issue 1358";
},
subtitle:function () {
return "The tail should use the texture";
},
onEnter:function () {
this._super();
// ask director the the window size
var size = cc.director.getWinSize();
this._streak = new _ccsg.MotionStreak(2.0, 1.0, 50.0, new cc.Color(255, 255, 0), s_image_icon);
this.addChild(this._streak);
this._center = cc.p(size.width / 2, size.height / 2);
this._radius = size.width / 3;
this._angle = 0.0;
this.schedule(this.update, 0);
},
update:function (dt) {
this._angle += 1.0;
var x = this._center.x + Math.cos(this._angle / 180 * Math.PI) * this._radius;
var y = this._center.y + Math.sin(this._angle / 180 * Math.PI) * this._radius;
this._streak.x = this._center.x + Math.cos(this._angle / 180 * Math.PI) * this._radius;
this._streak.y = this._center.y + Math.sin(this._angle / 180 * Math.PI) * this._radius;
}
});
var arrayOfMotionStreakTest = [
MotionStreakTest1,
MotionStreakTest2,
Issue1358
];
var nextMotionAction = function () {
sceneIdx++;
sceneIdx = sceneIdx % arrayOfMotionStreakTest.length;
return new arrayOfMotionStreakTest[sceneIdx]();
};
var backMotionAction = function () {
sceneIdx--;
if (sceneIdx < 0)
sceneIdx += arrayOfMotionStreakTest.length;
return new arrayOfMotionStreakTest[sceneIdx]();
};
var restartMotionAction = function () {
return new arrayOfMotionStreakTest[sceneIdx]();
};
var MotionStreakTestScene = TestScene.extend({
runThisTest:function (num) {
sceneIdx = (num || num == 0) ? (num - 1) : -1;
var pLayer = nextMotionAction();
this.addChild(pLayer);
cc.director.runScene(this);
}
});
+8
View File
@@ -341,6 +341,14 @@ var testNames = [
return new NodeTestScene();
}
},
{
title:"MotionStreak Test",
platforms: PLATFORM_JSB_AND_WEBGL,
linksrc:"src/MotionStreakTest/MotionStreakTest.js",
testScene:function () {
return new MotionStreakTestScene();
}
},
{
title:"OpenGL Test",
resource:g_opengl_resources,