From 6914c9d9024c77e81663d26776bd1c389ce899b5 Mon Sep 17 00:00:00 2001 From: wangshuai Date: Thu, 7 Mar 2013 11:55:57 +0800 Subject: [PATCH] feature #1802:add touches lua test --- .../DrawPrimitivesTest/DrawPrimitivesTest.lua | 209 ++++++++++++++++++ .../luaScript/ParticleTest/ParticleTest.lua | 143 ++++++------ .../Resources/luaScript/TouchesTest/Ball.lua | 95 ++++++++ .../luaScript/TouchesTest/Paddle.lua | 62 ++++++ .../luaScript/TouchesTest/TouchesTest.lua | 125 +++++++++++ .../Resources/luaScript/VisibleRect.lua | 65 ++++++ .../TestLua/Resources/luaScript/extern.lua | 61 +++++ .../Lua/TestLua/Resources/luaScript/tests.lua | 3 +- .../LuaCocos2d.cpp.REMOVED.git-id | 2 +- tools/tolua++/CCEGLViewProtocol.pkg | 6 + 10 files changed, 693 insertions(+), 78 deletions(-) create mode 100644 samples/Lua/TestLua/Resources/luaScript/DrawPrimitivesTest/DrawPrimitivesTest.lua create mode 100644 samples/Lua/TestLua/Resources/luaScript/TouchesTest/Ball.lua create mode 100644 samples/Lua/TestLua/Resources/luaScript/TouchesTest/Paddle.lua create mode 100644 samples/Lua/TestLua/Resources/luaScript/TouchesTest/TouchesTest.lua create mode 100644 samples/Lua/TestLua/Resources/luaScript/VisibleRect.lua create mode 100644 samples/Lua/TestLua/Resources/luaScript/extern.lua diff --git a/samples/Lua/TestLua/Resources/luaScript/DrawPrimitivesTest/DrawPrimitivesTest.lua b/samples/Lua/TestLua/Resources/luaScript/DrawPrimitivesTest/DrawPrimitivesTest.lua new file mode 100644 index 0000000000..454ec68933 --- /dev/null +++ b/samples/Lua/TestLua/Resources/luaScript/DrawPrimitivesTest/DrawPrimitivesTest.lua @@ -0,0 +1,209 @@ + +local SceneIdx = -1 +local MAX_LAYER = 2 + +local background = nil + +local labelAtlas = nil +local titleLabel = nil +local subtitleLabel = nil +local baseLayer_entry = nil + +local s = CCDirector:sharedDirector():getWinSize() + + +local function backAction() + SceneIdx = SceneIdx - 1 + if SceneIdx < 0 then + SceneIdx = SceneIdx + MAX_LAYER + end + + return CreateParticleLayer() +end + +local function restartAction() + return CreateParticleLayer() +end + +local function nextAction() + SceneIdx = SceneIdx + 1 + SceneIdx = math.mod(SceneIdx, MAX_LAYER) + + return CreateDrawPrimitivesTestLayer() +end + +local function backCallback(sender) + local scene = CCScene:create() + + scene:addChild(backAction()) + scene:addChild(CreateBackMenuItem()) + + CCDirector:sharedDirector():replaceScene(scene) +end + +local function restartCallback(sender) + local scene = CCScene:create() + + scene:addChild(restartAction()) + scene:addChild(CreateBackMenuItem()) + + CCDirector:sharedDirector():replaceScene(scene) +end + +local function nextCallback(sender) + local scene = CCScene:create() + + scene:addChild(nextAction()) + scene:addChild(CreateBackMenuItem()) + + CCDirector:sharedDirector():replaceScene(scene) +end + + +local function getBaseLayer() + local layer = CCLayer:create() + + titleLabel = CCLabelTTF:create("", "Arial", 28) + layer:addChild(titleLabel, 100, 1000) + titleLabel:setPosition(s.width / 2, s.height - 50) + + subtitleLabel = CCLabelTTF:create("", "Arial", 16) + layer:addChild(subtitleLabel, 100) + subtitleLabel:setPosition(s.width / 2, s.height - 80) + + + local item1 = CCMenuItemImage:create(s_pPathB1, s_pPathB2) + local item2 = CCMenuItemImage:create(s_pPathR1, s_pPathR2) + local item3 = CCMenuItemImage:create(s_pPathF1, s_pPathF2) + local item4 = CCMenuItemToggle:create(CCMenuItemFont:create("Free Movement")) + item4:addSubItem(CCMenuItemFont:create("Relative Movement")) + item4:addSubItem(CCMenuItemFont:create("Grouped Movement")) + item1:registerScriptTapHandler(backCallback) + item2:registerScriptTapHandler(restartCallback) + item3:registerScriptTapHandler(nextCallback) + + local menu = CCMenu:create() + menu:addChild(item1) + menu:addChild(item2) + menu:addChild(item3) + menu:addChild(item4) + + menu:setPosition(CCPointMake(0, 0)) + item1:setPosition(CCPointMake(s.width/2 - item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + item2:setPosition(CCPointMake(s.width/2, item2:getContentSize().height / 2)) + item3:setPosition(CCPointMake(s.width/2 + item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + item4:setPosition(ccp(0, 100)) + item4:setAnchorPoint(ccp(0, 0)) + + layer:addChild(menu, 100) + + labelAtlas = CCLabelAtlas:create("0000", "fps_images.png", 12, 32, string.byte('.')) + layer:addChild(labelAtlas, 100) + labelAtlas:setPosition(ccp(s.width - 66, 50)) + + layer:setTouchEnabled(false) + + return layer +end + +local function drawPrimitivesTest() + local layer = getBaseLayer() + + ccDrawLine( ccp(0, s.height), ccp(s.width, 0) ); + glLineWidth( 5.0); + ccDrawColor4B(255,0,0,255); + ccDrawLine(ccp(0, 0), ccp(s.width, s.height) ); + +-- ccPointSize(64); +-- ccDrawColor4B(0,0,255,128); +-- ccDrawPoint( VisibleRect::center() ); +-- +-- CHECK_GL_ERROR_DEBUG(); +-- +-- // draw 4 small points +-- CCPoint points[] = { ccp(60,60), ccp(70,70), ccp(60,70), ccp(70,60) }; +-- ccPointSize(4); +-- ccDrawColor4B(0,255,255,255); +-- ccDrawPoints( points, 4); +-- +-- CHECK_GL_ERROR_DEBUG(); +-- +-- // draw a green circle with 10 segments +-- glLineWidth(16); +-- ccDrawColor4B(0, 255, 0, 255); +-- ccDrawCircle( VisibleRect::center(), 100, 0, 10, false); +-- +-- CHECK_GL_ERROR_DEBUG(); +-- +-- // draw a green circle with 50 segments with line to center +-- glLineWidth(2); +-- ccDrawColor4B(0, 255, 255, 255); +-- ccDrawCircle( VisibleRect::center(), 50, CC_DEGREES_TO_RADIANS(90), 50, true); +-- +-- CHECK_GL_ERROR_DEBUG(); +-- +-- // open yellow poly +-- ccDrawColor4B(255, 255, 0, 255); +-- glLineWidth(10); +-- CCPoint vertices[] = { ccp(0,0), ccp(50,50), ccp(100,50), ccp(100,100), ccp(50,100) }; +-- ccDrawPoly( vertices, 5, false); +-- +-- CHECK_GL_ERROR_DEBUG(); +-- +-- // filled poly +-- glLineWidth(1); +-- CCPoint filledVertices[] = { ccp(0,120), ccp(50,120), ccp(50,170), ccp(25,200), ccp(0,170) }; +-- ccDrawSolidPoly(filledVertices, 5, ccc4f(0.5f, 0.5f, 1, 1 ) ); +-- +-- +-- // closed purble poly +-- ccDrawColor4B(255, 0, 255, 255); +-- glLineWidth(2); +-- CCPoint vertices2[] = { ccp(30,130), ccp(30,230), ccp(50,200) }; +-- ccDrawPoly( vertices2, 3, true); +-- +-- CHECK_GL_ERROR_DEBUG(); +-- +-- // draw quad bezier path +-- ccDrawQuadBezier(VisibleRect::leftTop(), VisibleRect::center(), VisibleRect::rightTop(), 50); +-- +-- CHECK_GL_ERROR_DEBUG(); +-- +-- // draw cubic bezier path +-- ccDrawCubicBezier(VisibleRect::center(), ccp(VisibleRect::center().x+30,VisibleRect::center().y+50), ccp(VisibleRect::center().x+60,VisibleRect::center().y-50),VisibleRect::right(),100); +-- +-- CHECK_GL_ERROR_DEBUG(); +-- +-- //draw a solid polygon +-- CCPoint vertices3[] = {ccp(60,160), ccp(70,190), ccp(100,190), ccp(90,160)}; +-- ccDrawSolidPoly( vertices3, 4, ccc4f(1,1,0,1) ); +-- +-- // restore original values +-- glLineWidth(1); +-- ccDrawColor4B(255,255,255,255); +-- ccPointSize(1); +-- +-- CHECK_GL_ERROR_DEBUG(); + return layer +end + +--------------------------------- +-- DrawPrimitives Test +--------------------------------- +function CreateDrawPrimitivesTestLayer() + if SceneIdx == 0 then return drawPrimitivesTest() + end +end + +function DrawPrimitivesTest() + cclog("DrawPrimitivesTest") + local scene = CCScene:create() + + SceneIdx = -1 + scene:addChild(nextAction()) + scene:addChild(CreateBackMenuItem()) + + return scene +end + + diff --git a/samples/Lua/TestLua/Resources/luaScript/ParticleTest/ParticleTest.lua b/samples/Lua/TestLua/Resources/luaScript/ParticleTest/ParticleTest.lua index 5af485b0bb..8953393edb 100644 --- a/samples/Lua/TestLua/Resources/luaScript/ParticleTest/ParticleTest.lua +++ b/samples/Lua/TestLua/Resources/luaScript/ParticleTest/ParticleTest.lua @@ -73,23 +73,22 @@ end local function setEmitterPosition() if emitter ~= nil then - nodeEmitter = tolua.cast(emitter, "CCNode") - nodeEmitter:setPosition(s.width / 2, s.height / 2) + emitter:setPosition(s.width / 2, s.height / 2) end end local function update(dt) if emitter ~= nil then local str = "" .. emitter:getParticleCount() - labelAtlas:setString("" .. str) +-- labelAtlas:setString("" .. str) end end local function baseLayer_onEnterOrExit(tag) local scheduler = CCDirector:sharedDirector():getScheduler() - if tag == 0 then + if tag == "enter" then baseLayer_entry = scheduler:scheduleScriptFunc(update, 0, false) - elseif tag == 1 then + elseif tag == "exit" then scheduler:unscheduleScriptEntry(baseLayer_entry) end end @@ -161,7 +160,7 @@ local function getBaseLayer() end local function onTouch(eventType, x, y) - if eventType == CCTOUCHBEGAN then + if eventType == "began" then return true else return onTouchEnded(x, y) @@ -213,9 +212,9 @@ end local function ParticleReorder_onEnterOrExit(tag) local scheduler = CCDirector:sharedDirector():getScheduler() - if tag == 0 then + if tag == "enter" then ParticleReorder_entry = scheduler:scheduleScriptFunc(reorderParticles, 1.0, false) - elseif tag == 1 then + elseif tag == "exit" then scheduler:unscheduleScriptEntry(ParticleReorder_entry) end end @@ -303,9 +302,9 @@ end local function ParticleBatchHybrid_onEnterOrExit(tag) local scheduler = CCDirector:sharedDirector():getScheduler() - if tag == 0 then + if tag == "enter" then ParticleBatchHybrid_entry = scheduler:scheduleScriptFunc(switchRender, 2.0, false) - elseif tag == 1 then + elseif tag == "exit" then scheduler:unscheduleScriptEntry(ParticleBatchHybrid_entry) --emitter:release() end @@ -378,7 +377,7 @@ local function DemoFlower() emitter = CCParticleFlower:create() -- emitter:retain() - background:addChild(tolua.cast(emitter, "CCNode"), 10) + background:addChild(emitter, 10) emitter:setTexture(CCTextureCache:sharedTextureCache():addImage(s_stars1)) setEmitterPosition() @@ -395,7 +394,7 @@ local function DemoGalaxy() emitter = CCParticleGalaxy:create() -- emitter:retain() - background:addChild(tolua.cast(emitter,"CCNode"), 10) + background:addChild(emitter, 10) emitter:setTexture(CCTextureCache:sharedTextureCache():addImage(s_fire)) @@ -413,7 +412,7 @@ local function DemoFirework() emitter = CCParticleFireworks:create() -- emitter:retain() - background:addChild(tolua.cast(emitter,"CCNode"), 10) + background:addChild(emitter, 10) emitter:setTexture(CCTextureCache:sharedTextureCache():addImage(s_stars1)) @@ -431,7 +430,7 @@ local function DemoSpiral() emitter = CCParticleSpiral:create() -- emitter:retain() - background:addChild(tolua.cast(emitter,"CCNode"), 10) + background:addChild(emitter, 10) emitter:setTexture(CCTextureCache:sharedTextureCache():addImage(s_fire)) @@ -449,7 +448,7 @@ local function DemoSun() emitter = CCParticleSun:create() -- emitter:retain() - background:addChild(tolua.cast(emitter,"CCNode"), 10) + background:addChild(emitter, 10) emitter:setTexture(CCTextureCache:sharedTextureCache():addImage(s_fire)) @@ -467,7 +466,7 @@ local function DemoMeteor() emitter = CCParticleMeteor:create() -- emitter:retain() - background:addChild(tolua.cast(emitter,"CCNode"), 10) + background:addChild(emitter, 10) emitter:setTexture(CCTextureCache:sharedTextureCache():addImage(s_fire)) @@ -485,11 +484,11 @@ local function DemoFire() emitter = CCParticleFire:create() -- emitter:retain() - background:addChild(tolua.cast(emitter,"CCNode"), 10) + background:addChild(emitter, 10) emitter:setTexture(CCTextureCache:sharedTextureCache():addImage(s_fire)) - local pos_x, pos_y = tolua.cast(emitter,"CCNode"):getPosition() - tolua.cast(emitter,"CCNode"):setPosition(pos_x, 100) + local pos_x, pos_y = emitter:getPosition() + emitter:setPosition(pos_x, 100) titleLabel:setString("ParticleFire") return layer @@ -503,11 +502,11 @@ local function DemoSmoke() emitter = CCParticleSmoke:create() -- emitter:retain() - background:addChild(tolua.cast(emitter,"CCNode"), 10) + background:addChild(emitter, 10) emitter:setTexture(CCTextureCache:sharedTextureCache():addImage(s_fire)) - local pos_x, pos_y = tolua.cast(emitter,"CCNode"):getPosition() - tolua.cast(emitter,"CCNode"):setPosition(pos_x, 100) + local pos_x, pos_y = emitter:getPosition() + emitter:setPosition(pos_x, 100) setEmitterPosition() @@ -523,11 +522,11 @@ local function DemoExplosion() emitter = CCParticleExplosion:create() -- emitter:retain() - background:addChild(tolua.cast(emitter,"CCNode"), 10) + background:addChild(emitter, 10) emitter:setTexture(CCTextureCache:sharedTextureCache():addImage(s_stars1)) - tolua.cast(emitter, "CCParticleSystem"):setAutoRemoveOnFinish(true) + emitter:setAutoRemoveOnFinish(true) setEmitterPosition() @@ -543,36 +542,33 @@ local function DemoSnow() emitter = CCParticleSnow:create() -- emitter:retain() - local nodeEmitter = tolua.cast(emitter, "CCNode") - background:addChild(nodeEmitter, 10) + background:addChild(emitter, 10) - local pos_x, pos_y = nodeEmitter:getPosition() - nodeEmitter:setPosition(pos_x, pos_y - 110) - - local systemEmitter = tolua.cast(emitter, "CCParticleSystem") - systemEmitter:setLife(3) - systemEmitter:setLifeVar(1) + local pos_x, pos_y = emitter:getPosition() + emitter:setPosition(pos_x, pos_y - 110) + emitter:setLife(3) + emitter:setLifeVar(1) -- gravity - systemEmitter:setGravity(CCPointMake(0, -10)) + emitter:setGravity(CCPointMake(0, -10)) -- speed of particles - systemEmitter:setSpeed(130) - systemEmitter:setSpeedVar(30) + emitter:setSpeed(130) + emitter:setSpeedVar(30) - local startColor = systemEmitter:getStartColor() + local startColor = emitter:getStartColor() startColor.r = 0.9 startColor.g = 0.9 startColor.b = 0.9 - systemEmitter:setStartColor(startColor) + emitter:setStartColor(startColor) - local startColorVar = systemEmitter:getStartColorVar() + local startColorVar = emitter:getStartColorVar() startColorVar.b = 0.1 - systemEmitter:setStartColorVar(startColorVar) + emitter:setStartColorVar(startColorVar) - systemEmitter:setEmissionRate(systemEmitter:getTotalParticles() / systemEmitter:getLife()) + emitter:setEmissionRate(emitter:getTotalParticles() / emitter:getLife()) - systemEmitter:setTexture(CCTextureCache:sharedTextureCache():addImage(s_snow)) + emitter:setTexture(CCTextureCache:sharedTextureCache():addImage(s_snow)) setEmitterPosition() @@ -588,13 +584,11 @@ local function DemoRain() emitter = CCParticleRain:create() -- emitter:retain() + background:addChild(emitter, 10) - local nodeEmitter = tolua.cast(emitter, "CCNode") - background:addChild(nodeEmitter, 10) - - local pos_x, pos_y = nodeEmitter:getPosition() - nodeEmitter:setPosition(pos_x, pos_y - 100) - tolua.cast(emitter, "CCParticleSystem"):setLife(4) + local pos_x, pos_y = emitter:getPosition() + emitter:setPosition(pos_x, pos_y - 100) + emitter:setLife(4) emitter:setTexture(CCTextureCache:sharedTextureCache():addImage(s_fire)) @@ -827,17 +821,14 @@ local function DemoRing() emitter = CCParticleFlower:create() -- emitter:retain() - local nodeEmitter = tolua.cast(emitter, "CCNode") - background:addChild(nodeEmitter, 10) + background:addChild(emitter, 10) emitter:setTexture(CCTextureCache:sharedTextureCache():addImage(s_stars1)) - - local systemEmitter = tolua.cast(emitter, "CCParticleSystem") - systemEmitter:setLifeVar(0) - systemEmitter:setLife(10) - systemEmitter:setSpeed(100) - systemEmitter:setSpeedVar(0) - systemEmitter:setEmissionRate(10000) + emitter:setLifeVar(0) + emitter:setLife(10) + emitter:setSpeed(100) + emitter:setSpeedVar(0) + emitter:setEmissionRate(10000) setEmitterPosition() @@ -867,12 +858,11 @@ local function ParallaxParticle() -- emitter:retain() emitter:setTexture(CCTextureCache:sharedTextureCache():addImage(s_fire)) - local nodeEmitter = tolua.cast(emitter, "CCNode") - p1:addChild(nodeEmitter, 10) - nodeEmitter:setPosition(250, 200) + p1:addChild(emitter, 10) + emitter:setPosition(250, 200) local par = CCParticleSun:create() - p2:addChild(tolua.cast(par,"CCNode"), 10) + p2:addChild(par, 10) par:setTexture(CCTextureCache:sharedTextureCache():addImage(s_fire)) local move = CCMoveBy:create(4, CCPointMake(300,0)) @@ -1138,9 +1128,9 @@ end local function Issue870_onEnterOrExit(tag) local scheduler = CCDirector:sharedDirector():getScheduler() - if tag == 0 then + if tag == "enter" then Issue870_entry = scheduler:scheduleScriptFunc(updateQuads, 2.0, false) - elseif tag == 1 then + elseif tag == "exit" then scheduler:unscheduleScriptEntry(Issue870_entry) end end @@ -1250,9 +1240,9 @@ end local function AddAndDeleteParticleSystems_onEnterOrExit(tag) local scheduler = CCDirector:sharedDirector():getScheduler() - if tag == 0 then + if tag == "enter" then AddAndDeleteParticleSystems_entry = scheduler:scheduleScriptFunc(removeSystem, 2.0, false) - elseif tag == 1 then + elseif tag == "exit" then scheduler:unscheduleScriptEntry(AddAndDeleteParticleSystems_entry) end end @@ -1308,9 +1298,9 @@ end local function ReorderParticleSystems_onEnterOrExit(tag) local scheduler = CCDirector:sharedDirector():getScheduler() - if tag == 0 then + if tag == "enter" then ReorderParticleSystems_entry = scheduler:scheduleScriptFunc(reorderSystem, 2.0, false) - elseif tag == 1 then + elseif tag == "exit" then scheduler:unscheduleScriptEntry(ReorderParticleSystems_entry) end end @@ -1494,18 +1484,19 @@ function CreateParticleLayer() elseif SceneIdx == 29 then return DemoParticleFromFile("Spiral") elseif SceneIdx == 30 then return DemoParticleFromFile("Galaxy") elseif SceneIdx == 31 then return DemoParticleFromFile("Phoenix") - elseif SceneIdx == 32 then return RadiusMode1() - elseif SceneIdx == 33 then return RadiusMode2() - elseif SceneIdx == 34 then return Issue704() - elseif SceneIdx == 35 then return Issue870() + elseif SceneIdx == 32 then return DemoParticleFromFile("lines") + elseif SceneIdx == 33 then return RadiusMode1() + elseif SceneIdx == 34 then return RadiusMode2() + elseif SceneIdx == 35 then return Issue704() + elseif SceneIdx == 36 then return Issue870() --elseif SceneIdx == 36 then return Issue1201() -- v1.1 tests - elseif SceneIdx == 36 then return MultipleParticleSystems() - elseif SceneIdx == 37 then return MultipleParticleSystemsBatched() - elseif SceneIdx == 38 then return AddAndDeleteParticleSystems() - elseif SceneIdx == 39 then return ReorderParticleSystems() - elseif SceneIdx == 40 then return PremultipliedAlphaTest() - elseif SceneIdx == 41 then return PremultipliedAlphaTest2() + elseif SceneIdx == 37 then return MultipleParticleSystems() + elseif SceneIdx == 38 then return MultipleParticleSystemsBatched() + elseif SceneIdx == 39 then return AddAndDeleteParticleSystems() + elseif SceneIdx == 40 then return ReorderParticleSystems() + elseif SceneIdx == 41 then return PremultipliedAlphaTest() + elseif SceneIdx == 42 then return PremultipliedAlphaTest2() end end diff --git a/samples/Lua/TestLua/Resources/luaScript/TouchesTest/Ball.lua b/samples/Lua/TestLua/Resources/luaScript/TouchesTest/Ball.lua new file mode 100644 index 0000000000..744ba6784c --- /dev/null +++ b/samples/Lua/TestLua/Resources/luaScript/TouchesTest/Ball.lua @@ -0,0 +1,95 @@ + +require "luaScript.extern" +require "luaScript.VisibleRect" +require "luaScript.TouchesTest.Paddle" + +Ball = class("Ball", function(texture) + return CCSprite:createWithTexture(texture) +end) + +Ball.__index = Ball + +Ball.m_velocity = ccp(0,0) + +local M_PI = 3.1415926 + +function Ball:radius() + local size = self:getTexture():getContentSize() + return size.width/2 +end + +function Ball:move(delta) + local getPosition = ccp(self:getPosition()) + local position = ccpMult(self.m_velocity, delta) + self:setPosition( ccpAdd(getPosition, position) ); + + if (getPosition.x > VisibleRect:right().x - self:radius()) then + self:setPosition( ccp( VisibleRect:right().x - self:radius(), getPosition.y) ); + self.m_velocity.x = self.m_velocity.x * -1; + elseif (getPosition.x < VisibleRect:left().x + self:radius()) then + self:setPosition( ccp(VisibleRect:left().x + self:radius(), getPosition.y) ); + self.m_velocity.x = self.m_velocity.x * -1; + end +end + +function Ball:collideWithPaddle(paddle) + local paddleRect = paddle:rect() + local paddleGetPosition = ccp(paddle:getPosition()) + local selfGetPosition = ccp(self:getPosition()) + + paddleRect.origin.x = paddleRect.origin.x + paddleGetPosition.x; + paddleRect.origin.y = paddleRect.origin.y + paddleGetPosition.y; + + local lowY = paddleRect:getMinY(); + local midY = paddleRect:getMidY(); + local highY = paddleRect:getMaxY(); + + local leftX = paddleRect:getMinX(); + local rightX = paddleRect:getMaxX(); + + if (selfGetPosition.x > leftX and selfGetPosition.x < rightX) then + + local hit = false; + local angleOffset = 0.0; + + if (selfGetPosition.y > midY and selfGetPosition.y <= highY + self:radius()) then + self:setPosition( ccp(selfGetPosition.x, highY + self:radius()) ); + hit = true; + angleOffset = M_PI / 2; + elseif (selfGetPosition.y < midY and selfGetPosition.y >= lowY - self:radius()) then + self:setPosition( ccp(selfGetPosition.x, lowY - self:radius()) ); + hit = true; + angleOffset = -M_PI / 2; + end + + if (hit) then + local hitAngle = ccpToAngle(ccpSub(paddleGetPosition, paddleGetPosition)) + angleOffset; + + local scalarVelocity = ccpLength(self.m_velocity) * 1.05; + local velocityAngle = -ccpToAngle(self.m_velocity) + 0.5 * hitAngle; + + self.m_velocity = ccpMult(ccpForAngle(velocityAngle), scalarVelocity); + end + end + +end + +function Ball:setVelocity(velocity) + self.m_velocity = velocity +end + +function Ball:getVelocity() + return self.m_velocity +end + +function Ball.ballWithTexture(aTexture) + + if(aTexture == nil) then + cclog("in ballWithTexture aTexture == nil") + end + + local ball = Ball.new(aTexture) + ball:autorelease() + return ball +end + diff --git a/samples/Lua/TestLua/Resources/luaScript/TouchesTest/Paddle.lua b/samples/Lua/TestLua/Resources/luaScript/TouchesTest/Paddle.lua new file mode 100644 index 0000000000..604a800065 --- /dev/null +++ b/samples/Lua/TestLua/Resources/luaScript/TouchesTest/Paddle.lua @@ -0,0 +1,62 @@ +require "luaScript.extern" +require "luaScript.VisibleRect" + +Paddle = class("Paddle", function(texture) + return CCSprite:createWithTexture(texture) +end) + +Paddle.__index = Paddle + +local kPaddleStateGrabbed = 0 +local kPaddleStateUngrabbed = 1 + +Paddle.m_state = kPaddleStateGrabbed + + +function Paddle:rect() + local s = self:getTexture():getContentSize() + return CCRectMake(-s.width / 2, -s.height / 2, s.width, s.height) +end + +function Paddle:containsTouchLocation(x,y) + local position = ccp(self:getPosition()) + local s = self:getTexture():getContentSize() + local touchRect = CCRectMake(-s.width / 2 + position.x, -s.height / 2 + position.y, s.width, s.height) + local b = touchRect:containsPoint(ccp(x,y)) + return b +end + +function Paddle:ccTouchBegan(x, y) + if (self.m_state ~= kPaddleStateUngrabbed) then + return false + end + + self.m_state = kPaddleStateGrabbed; + return true; +end + +function Paddle:ccTouchMoved(x, y) + self:setPosition( ccp(x,y) ); +end + +function Paddle:ccTouchEnded(x, y) + self.m_state = kPaddleStateUngrabbed; +end + +function Paddle:onTouch(eventType, x, y) + if eventType == "began" then + return self:ccTouchBegan(x,y) + elseif eventType == "moved" then + return self:ccTouchMoved(x,y) + elseif eventType == "ended" then + return self:ccTouchEnded(x, y) + end +end + +function Paddle:paddleWithTexture(aTexture) + local pPaddle = Paddle.new(aTexture); + self.m_state = kPaddleStateUngrabbed; + return pPaddle; +end + + diff --git a/samples/Lua/TestLua/Resources/luaScript/TouchesTest/TouchesTest.lua b/samples/Lua/TestLua/Resources/luaScript/TouchesTest/TouchesTest.lua new file mode 100644 index 0000000000..08c7bda61e --- /dev/null +++ b/samples/Lua/TestLua/Resources/luaScript/TouchesTest/TouchesTest.lua @@ -0,0 +1,125 @@ +require "luaScript/testResource" +require "luaScript/TouchesTest/Ball" +require "luaScript/TouchesTest/Paddle" +require "luaScript/VisibleRect" + +local kHighPlayer = 0 +local kLowPlayer = 1 + +local kStatusBarHeight = 0.0 + +local kSpriteTag = 0 + +local m_ball = nil +local m_ballStartingVelocity = nil +local m_paddles = {} +local layer = nil + +local function backCallback(sender) + local scene = CCScene:create() + scene:addChild(CreateBackMenuItem()) + CCDirector:sharedDirector():replaceScene(scene) +end + + +local function resetAndScoreBallForPlayer(player) + m_ballStartingVelocity = ccpMult(m_ballStartingVelocity, -1.1); + m_ball:setVelocity( m_ballStartingVelocity ); + m_ball:setPosition( VisibleRect:center() ); +end + +local function doStep(delta) + m_ball:move(delta); + + for _,paddle in ipairs(m_paddles) do + + if(paddle == nil) then + break + end + m_ball:collideWithPaddle( paddle ); + end + + local ballPosition = ccp(m_ball:getPosition()) + if (ballPosition.y > VisibleRect:top().y - kStatusBarHeight + m_ball:radius()) then + resetAndScoreBallForPlayer( kLowPlayer ); + elseif (ballPosition.y < VisibleRect:bottom().y-m_ball:radius()) then + resetAndScoreBallForPlayer( kHighPlayer ); + end + m_ball:draw(); +end + +local function onTouch(event, x, y) + + for _,paddle in ipairs(m_paddles) do + if paddle:containsTouchLocation(x,y) == true then + return paddle:onTouch(event, x, y) + end + end + + return true +end + +local function CreateTouchesLayer() + layer = CCLayer:create() + + m_ballStartingVelocity = ccp(20.0, -100.0); + local mgr = CCTextureCache:sharedTextureCache() + local texture = mgr:addImage(s_Ball) + m_ball = Ball.ballWithTexture(texture); + + m_ball:setPosition( VisibleRect:center() ); + m_ball:setVelocity( m_ballStartingVelocity ); + layer:addChild( m_ball ); + m_ball:retain(); + + local paddleTexture = CCTextureCache:sharedTextureCache():addImage(s_Paddle); + + local paddlesM = {} + + local paddle = Paddle:paddleWithTexture(paddleTexture); + paddle:setPosition( ccp(VisibleRect:center().x, VisibleRect:bottom().y + 15) ); + paddlesM[#paddlesM+1] = paddle + + paddle = Paddle:paddleWithTexture( paddleTexture ); + paddle:setPosition( ccp(VisibleRect:center().x, VisibleRect:top().y - kStatusBarHeight - 15) ); + paddlesM[#paddlesM+1] = paddle + + paddle = Paddle:paddleWithTexture( paddleTexture ); + paddle:setPosition( ccp(VisibleRect:center().x, VisibleRect:bottom().y + 100) ); + paddlesM[#paddlesM+1] = paddle + + paddle = Paddle:paddleWithTexture( paddleTexture ); + paddle:setPosition( ccp(VisibleRect:center().x, VisibleRect:top().y - kStatusBarHeight - 100) ); + paddlesM[#paddlesM+1] = paddle + + m_paddles = paddlesM + + for i = 1,#paddlesM do + paddle = paddlesM[i] + + if(paddle == nil) then + break + end + + layer:addChild(paddle); + end + + -- schedule + layer:scheduleUpdateWithPriorityLua(doStep, 0) + layer:setTouchEnabled(true) + layer:registerScriptTouchHandler(onTouch) + + return layer +end + +local function nextAction() + return CreateTouchesLayer() +end + +function TouchesTest() + local scene = CCScene:create() + scene:addChild(nextAction()) + scene:addChild(CreateBackMenuItem()) + return scene +end + diff --git a/samples/Lua/TestLua/Resources/luaScript/VisibleRect.lua b/samples/Lua/TestLua/Resources/luaScript/VisibleRect.lua new file mode 100644 index 0000000000..0c4f2c219c --- /dev/null +++ b/samples/Lua/TestLua/Resources/luaScript/VisibleRect.lua @@ -0,0 +1,65 @@ +require "luaScript/extern" + +VisibleRect = class("VisibleRect") +VisibleRect.__index = VisibleRect + + +VisibleRect.s_visibleRect = CCRect:new() + +function VisibleRect:lazyInit() + if (self.s_visibleRect.size.width == 0.0 and self.s_visibleRect.size.height == 0.0) then + local pEGLView = CCEGLView:sharedOpenGLView(); + self.s_visibleRect.origin = pEGLView:getVisibleOrigin(); + self.s_visibleRect.size = pEGLView:getVisibleSize(); + end +end + +function VisibleRect:getVisibleRect() + self:lazyInit(); + return CCRectMake(self.s_visibleRect.origin.x, self.s_visibleRect.origin.y, self.s_visibleRect.size.width, self.s_visibleRect.size.height); +end + +function VisibleRect:left() + self:lazyInit(); + return ccp(self.s_visibleRect.origin.x, self.s_visibleRect.origin.y+self.s_visibleRect.size.height/2); +end + +function VisibleRect:right() + self:lazyInit(); + return ccp(self.s_visibleRect.origin.x+self.s_visibleRect.size.width, self.s_visibleRect.origin.y+self.s_visibleRect.size.height/2); +end + +function VisibleRect:top() + self:lazyInit(); + return ccp(self.s_visibleRect.origin.x+self.s_visibleRect.size.width/2, self.s_visibleRect.origin.y+self.s_visibleRect.size.height); +end + +function VisibleRect:bottom() + self:lazyInit(); + return ccp(self.s_visibleRect.origin.x+self.s_visibleRect.size.width/2, self.s_visibleRect.origin.y); +end + +function VisibleRect:center() + self:lazyInit(); + return ccp(self.s_visibleRect.origin.x+self.s_visibleRect.size.width/2, self.s_visibleRect.origin.y+self.s_visibleRect.size.height/2); +end + +function VisibleRect:leftTop() + self:lazyInit(); + return ccp(self.s_visibleRect.origin.x, self.s_visibleRect.origin.y+self.s_visibleRect.size.height); +end + +function VisibleRect:rightTop() + self:lazyInit(); + return ccp(self.s_visibleRect.origin.x+self.s_visibleRect.size.width, self.s_visibleRect.origin.y+self.s_visibleRect.size.height); +end + +function VisibleRect:leftBottom() + self:lazyInit(); + return self.s_visibleRect.origin; +end + +function VisibleRect:rightBottom() + self:lazyInit(); + return ccp(self.s_visibleRect.origin.x+self.s_visibleRect.size.width, self.s_visibleRect.origin.y); +end diff --git a/samples/Lua/TestLua/Resources/luaScript/extern.lua b/samples/Lua/TestLua/Resources/luaScript/extern.lua new file mode 100644 index 0000000000..e21d5bc9b4 --- /dev/null +++ b/samples/Lua/TestLua/Resources/luaScript/extern.lua @@ -0,0 +1,61 @@ + +--Create an class. +function class(classname, super) + local superType = type(super) + local cls + + if superType ~= "function" and superType ~= "table" then + superType = nil + super = nil + end + + if superType == "function" or (super and super.__ctype == 1) then + -- inherited from native C++ Object + cls = {} + + if superType == "table" then + -- copy fields from super + for k,v in pairs(super) do cls[k] = v end + cls.__create = super.__create + cls.super = super + else + cls.__create = super + end + + cls.ctor = function() end + cls.__cname = classname + cls.__ctype = 1 + + function cls.new(...) + local instance = cls.__create(...) + -- copy fields from class to native object + for k,v in pairs(cls) do instance[k] = v end + instance.class = cls + instance:ctor(...) + return instance + end + + else + -- inherited from Lua Object + if super then + cls = clone(super) + cls.super = super + else + cls = {ctor = function() end} + end + + cls.__cname = classname + cls.__ctype = 2 -- lua + cls.__index = cls + + function cls.new(...) + local instance = setmetatable({}, cls) + instance.class = cls + instance:ctor(...) + return instance + end + end + + return cls +end + diff --git a/samples/Lua/TestLua/Resources/luaScript/tests.lua b/samples/Lua/TestLua/Resources/luaScript/tests.lua index 668e1b1dc7..7fecfe27b2 100644 --- a/samples/Lua/TestLua/Resources/luaScript/tests.lua +++ b/samples/Lua/TestLua/Resources/luaScript/tests.lua @@ -9,8 +9,9 @@ require "luaScript/RotateWorldTest/RotateWorldTest" require "luaScript/ParticleTest/ParticleTest" require "luaScript/ActionsEaseTest/ActionsEaseTest" require "luaScript/MotionStreakTest/MotionStreakTest" ---require "luaScript/DrawPrimitivesTest/DrawPrimitivesTest" +require "luaScript/DrawPrimitivesTest/DrawPrimitivesTest" require "luaScript/NodeTest/NodeTest" +require "luaScript/TouchesTest/TouchesTest" require "luaScript/PerformanceTest/PerformanceTest" diff --git a/scripting/lua/cocos2dx_support/LuaCocos2d.cpp.REMOVED.git-id b/scripting/lua/cocos2dx_support/LuaCocos2d.cpp.REMOVED.git-id index e18c81c93b..cf2c5c2ef8 100644 --- a/scripting/lua/cocos2dx_support/LuaCocos2d.cpp.REMOVED.git-id +++ b/scripting/lua/cocos2dx_support/LuaCocos2d.cpp.REMOVED.git-id @@ -1 +1 @@ -fff0b7882ef76a67afdca7941683a3cf2e1b44e8 \ No newline at end of file +bd498b6ddf43d256ea4950e3d1a650dbcb6567f2 \ No newline at end of file diff --git a/tools/tolua++/CCEGLViewProtocol.pkg b/tools/tolua++/CCEGLViewProtocol.pkg index 8dd78d0007..29d565e1a9 100644 --- a/tools/tolua++/CCEGLViewProtocol.pkg +++ b/tools/tolua++/CCEGLViewProtocol.pkg @@ -83,3 +83,9 @@ class CCEGLViewProtocol */ float getScaleY() const; }; + +class CCEGLView : public CCEGLViewProtocol +{ + static CCEGLView* sharedOpenGLView(void); +}; +