2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include <boost/any.hpp>
25 #include <boost/function.hpp>
26 #include <dali/dali.h>
27 #include <dali-toolkit/dali-toolkit.h>
28 #include "shared/view.h"
31 using namespace Dali::Toolkit;
32 using namespace DemoHelper;
36 const char* BACKGROUND_IMAGE( DALI_IMAGE_DIR "background-blocks.jpg" );
37 const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
38 const char* APPLICATION_TITLE( "DALi Blocks" );
39 const char* BALL_IMAGE = DALI_IMAGE_DIR "blocks-ball.png";
40 const char* PADDLE_IMAGE = DALI_IMAGE_DIR "blocks-paddle.png";
41 const char* PADDLE_HANDLE_IMAGE = DALI_IMAGE_DIR "blocks-paddle-handle.png";
43 const char* BRICK_IMAGE_PATH[] = { DALI_IMAGE_DIR "blocks-brick-1.png",
44 DALI_IMAGE_DIR "blocks-brick-2.png",
45 DALI_IMAGE_DIR "blocks-brick-3.png",
46 DALI_IMAGE_DIR "blocks-brick-4.png" };
48 const int TOTAL_BRICKS(4); ///< Total bricks in game.
49 const Vector3 ICON_SIZE(100.0f, 100.0f, 0.0f);
51 const float SCREEN_MARGIN = 10.0f; ///< Margin indentation around screen
52 const Vector3 MENU_BUTTON_SIZE = Vector3(0.15f, 0.05f, 1.0f); ///< Standard Menu Buttons.
54 const float MAX_ANIMATION_DURATION = 60.0f; ///< 60 seconds animations. Long enough for ball to hit an obstacle.
55 const float BALL_VELOCITY = 300.0f; ///< Ball velocity in pixels/second.
56 const float MAX_VELOCITY = 500.0f; ///< Max. velocity in pixels/second.
57 const Vector3 PADDLE_COLLISION_MARGIN(0.0f, 0.0f, 0.0f); ///< Collision margin for ball-paddle detection.
58 const Vector3 BRICK_COLLISION_MARGIN(0.0f, 0.0f, 0.0f); ///< Collision margin for ball-brick detection.
59 const Vector3 INITIAL_BALL_DIRECTION(1.0f, 1.0f, 0.0f); ///< Initial ball direction.
61 const std::string WOBBLE_PROPERTY_NAME("wobble-property"); ///< Wobble property name.
62 const std::string COLLISION_PROPERTY_NAME("collision-property"); ///< Collision property name.
64 const Vector2 BRICK_SIZE(0.1f, 0.05f ); ///< Brick size relative to width of stage.
65 const Vector2 BALL_SIZE( 0.05f, 0.05f ); ///< Ball size relative to width of stage.
66 const Vector2 PADDLE_SIZE( 0.2f, 0.05f ); ///< Paddle size relative to width of stage.
67 const Vector2 PADDLE_HANDLE_SIZE( 0.3f, 0.3f ); ///< Paddle handle size relative to width of stage.
68 const Vector2 BALL_START_POSITION(0.5f, 0.8f); ///< Ball start position relative to stage size.
69 const Vector2 PADDLE_START_POSITION(0.5f, 0.9f); ///< Paddler start position relative to stage size.
70 const Vector2 PADDLE_HIT_MARGIN( 0.1, 0.15f ); ///< Extra hit Area for Paddle when touching.
72 const int TOTAL_LIVES(3); ///< Total lives in game before it's game over!
73 const int TOTAL_LEVELS(3); ///< 3 Levels total, then repeats.
75 // constraints ////////////////////////////////////////////////////////////////
78 * CollisionConstraint generates a collision vector
79 * between two actors a and b, assuming they're rectangular
80 * based on their size.
82 struct CollisionConstraint
85 * Collision Constraint constructor
86 * The adjust (optional) parameter can be used to add a margin
87 * to the actors. A +ve size will result in larger collisions,
88 * while a -ve size will result in tighter collisions.
90 * @param[in] adjust (optional) Adjusts the rectangular size detection
92 CollisionConstraint(Vector3 adjust = Vector3::ZERO)
98 * Generates collision vector indicating whether Actor's A and B
99 * have overlapped eachother, and the relative position of Actor B to A.
101 * @param[in] current The current collision-property (ignored)
102 * @param[in] propertyA Actor A's Position property.
103 * @param[in] propertyB Actor B's Position property.
104 * @param[in] propertySizeA Actor A's Size property.
105 * @param[in] propertySizeB Actor B's Size property.
106 * @return The collision vector is returned.
108 Vector3 operator()(const Vector3& current,
109 const PropertyInput& propertyA,
110 const PropertyInput& propertyB,
111 const PropertyInput& propertySizeA,
112 const PropertyInput& propertySizeB)
114 const Vector3& a = propertyA.GetVector3();
115 const Vector3& b = propertyB.GetVector3();
116 const Vector3& sizeA = propertySizeA.GetVector3();
117 const Vector3& sizeB = propertySizeB.GetVector3();
118 const Vector3 sizeComb = (sizeA + sizeB + mAdjust) * 0.5f;
120 // get collision relative to a.
121 Vector3 delta = b - a;
123 // Check if not overlapping Actors.
124 if( (fabsf(delta.x) > sizeComb.width) ||
125 (fabsf(delta.y) > sizeComb.height) )
127 delta = Vector3::ZERO; // not overlapping
130 return delta; // overlapping, return overlap vector relative to actor a.
133 const Vector3 mAdjust; ///< Size Adjustment value
137 * CollisionCircleRectangleConstraint generates a collision vector
138 * between two actors a (circle) and b (rectangle)
140 struct CollisionCircleRectangleConstraint
143 * Collision Constraint constructor
144 * The adjust (optional) parameter can be used to add a margin
145 * to the actors. A +ve size will result in larger collisions,
146 * while a -ve size will result in tighter collisions.
148 * @param[in] adjustPosition (optional) Adjusts the position offset of detection
149 * @param[in] adjustSize (optional) Adjusts the rectangular size of detection
151 CollisionCircleRectangleConstraint(Vector3 adjustPosition = Vector3::ZERO,
152 Vector3 adjustSize = Vector3::ZERO)
153 : mAdjustPosition(adjustPosition),
154 mAdjustSize(adjustSize)
159 * Generates collision vector indicating whether Actor's A and B
160 * have overlapped eachother, and the relative position of Actor B to A.
162 * @param[in] current The current collision-property (ignored)
163 * @param[in] propertyA Actor A's Position property.
164 * @param[in] propertyB Actor B's Position property.
165 * @param[in] propertySizeA Actor A's Size property.
166 * @param[in] propertySizeB Actor B's Size property.
167 * @return The collision vector is returned.
169 Vector3 operator()(const Vector3& current,
170 const PropertyInput& propertyA,
171 const PropertyInput& propertyB,
172 const PropertyInput& propertySizeA,
173 const PropertyInput& propertySizeB)
175 const Vector3& a = propertyA.GetVector3();
176 const Vector3 b = propertyB.GetVector3() + mAdjustPosition;
177 const Vector3& sizeA = propertySizeA.GetVector3();
178 const Vector3& sizeB = propertySizeB.GetVector3();
179 const Vector3 sizeA2 = sizeA * 0.5f; // circle radius
180 const Vector3 sizeB2 = (sizeB + mAdjustSize) * 0.5f; // rectangle half rectangle.
182 // get collision relative to a (rectangle).
183 Vector3 delta = a - b;
185 // reduce rectangle to 0.
186 if (delta.x > sizeB2.x)
190 else if (delta.x < -sizeB2.x)
199 if (delta.y > sizeB2.y)
203 else if (delta.y < -sizeB2.y)
212 // now calculate collision vector vs origin. (assume A is a circle, not ellipse)
213 if(delta.Length() < sizeA2.x)
219 return Vector3::ZERO;
222 const Vector3 mAdjustPosition; ///< Position Adjustment value
223 const Vector3 mAdjustSize; ///< Size Adjustment value
227 * WobbleConstraint generates a decaying sinusoidial rotation.
228 * The result when applied to an Actor, is the Actor rotating left/right
229 * initially a large amount (deviation degrees, when wobble property is 0.0f)
230 * then eventually coming to a stop (once wobble property reaches 1.0f)
232 struct WobbleConstraint
235 * Wobble Constraint constructor
236 * Generates a sinusoidial rotation that starts with
237 * high amplitude (deviation), and then decays to zero over input 0.0f to 1.0f
239 * @param[in] deviation The max. deviation of wobble effect in degrees.
241 WobbleConstraint(float deviation)
242 : mDeviation(Radian(Degree(deviation)))
248 * @param[in] current The current rotation property (ignored)
249 * @param[in] propertyWobble The wobble property (value from 0.0f to 1.0f)
250 * @return The rotation (quaternion) is generated.
252 Quaternion operator()(const Quaternion& current,
253 const PropertyInput& propertyWobble)
255 const float& wobble = propertyWobble.GetFloat();
257 float f = sinf(wobble * 10.0f) * (1.0f-wobble);
259 Quaternion q(mDeviation * f, Vector3::ZAXIS);
264 const float mDeviation; ///< Deviation factor in radians.
267 } // unnamed namespace
270 * This example shows how to use PropertyNotifications
272 class ExampleController : public ConnectionTracker
278 * @param application Application class, stored as reference
280 ExampleController( Application& application )
281 : mApplication( application ),
284 // Connect to the Application's Init and orientation changed signal
285 mApplication.InitSignal().Connect(this, &ExampleController::Create);
289 * This method gets called once the main loop of application is up and running
290 * @param[in] application Reference to the application instance
292 void Create(Application& application)
294 Stage::GetCurrent().KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
296 // Creates a default view with a default tool bar.
297 // The view is added to the stage.
298 Toolkit::ToolBar toolBar;
299 mContentLayer = DemoHelper::CreateView( application,
306 // Add an extra space on the right to center the title text.
307 toolBar.AddControl( Actor::New(), DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight );
309 // Create the content layer, which is where game actors appear.
316 * Adds a new layer to the stage, containing game actors.
318 void AddContentLayer()
320 Stage stage = Stage::GetCurrent();
321 const Vector3 stageSize(stage.GetSize());
324 mBallStartPosition = stageSize * Vector3( BALL_START_POSITION );
325 mBall = CreateImage(BALL_IMAGE);
326 mBall.SetPosition( mBallStartPosition );
327 mBall.SetSize( BALL_SIZE * stageSize.width );
328 mContentLayer.Add(mBall);
329 mBallVelocity = Vector3::ZERO;
332 mPaddleHitMargin = Vector2(stageSize) * PADDLE_HIT_MARGIN;
333 mPaddle = Actor::New();
334 mPaddleHandle = CreateImage(PADDLE_HANDLE_IMAGE);
335 mPaddleImage = CreateImage(PADDLE_IMAGE);
336 mPaddle.Add( mPaddleHandle );
337 mPaddle.Add( mPaddleImage );
338 mPaddleHandle.SetParentOrigin( ParentOrigin::TOP_CENTER );
339 mPaddleHandle.SetAnchorPoint( AnchorPoint::TOP_CENTER );
340 mPaddleHandle.SetPosition( 0.0f, stageSize.width * 0.0125f );
341 mPaddleImage.SetParentOrigin( ParentOrigin::TOP_CENTER );
342 mPaddleImage.SetAnchorPoint( AnchorPoint::TOP_CENTER );
343 mPaddle.SetParentOrigin( ParentOrigin::TOP_LEFT );
344 mPaddle.SetAnchorPoint( AnchorPoint::CENTER );
345 mPaddleFullSize = PADDLE_SIZE * stageSize.width;
346 mPaddle.SetSize( mPaddleFullSize + mPaddleHitMargin );
347 mPaddleHandle.SetSize( PADDLE_HANDLE_SIZE * stageSize.width );
348 mPaddleImage.SetSize( mPaddleFullSize );
350 mWobbleProperty = mPaddle.RegisterProperty(WOBBLE_PROPERTY_NAME, 0.0f);
351 Constraint wobbleConstraint = Constraint::New<Quaternion>( Actor::ROTATION,
352 LocalSource(mWobbleProperty),
353 WobbleConstraint(10.0f));
354 mPaddle.ApplyConstraint(wobbleConstraint);
356 mPaddle.SetPosition( stageSize * Vector3( PADDLE_START_POSITION ) );
357 mContentLayer.Add(mPaddle);
358 mPaddle.TouchedSignal().Connect(this, &ExampleController::OnTouchPaddle);
359 mContentLayer.TouchedSignal().Connect(this, &ExampleController::OnTouchLayer);
361 const float margin(BALL_SIZE.width * stageSize.width * 0.5f);
363 // Set up notifications for ball's collisions against walls.
364 PropertyNotification leftNotification = mBall.AddPropertyNotification( Actor::POSITION_X, LessThanCondition(margin) );
365 leftNotification.NotifySignal().Connect( this, &ExampleController::OnHitLeftWall );
367 PropertyNotification rightNotification = mBall.AddPropertyNotification( Actor::POSITION_X, GreaterThanCondition(stageSize.width - margin) );
368 rightNotification.NotifySignal().Connect( this, &ExampleController::OnHitRightWall );
370 PropertyNotification topNotification = mBall.AddPropertyNotification( Actor::POSITION_Y, LessThanCondition(margin) );
371 topNotification.NotifySignal().Connect( this, &ExampleController::OnHitTopWall );
373 PropertyNotification bottomNotification = mBall.AddPropertyNotification( Actor::POSITION_Y, GreaterThanCondition(stageSize.height + margin) );
374 bottomNotification.NotifySignal().Connect( this, &ExampleController::OnHitBottomWall );
376 // Set up notification for ball colliding against paddle.
377 Actor delegate = Actor::New();
379 Property::Index property = delegate.RegisterProperty(COLLISION_PROPERTY_NAME, Vector3::ZERO);
380 Constraint constraint = Constraint::New<Vector3>( property,
381 Source(mBall, Actor::POSITION),
382 Source(mPaddle, Actor::POSITION),
383 Source(mBall, Actor::SIZE),
384 Source(mPaddle, Actor::SIZE),
385 CollisionCircleRectangleConstraint( -Vector3(0.0f, mPaddleHitMargin.height * 0.575f, 0.0f),-Vector3(mPaddleHitMargin) ));
386 delegate.ApplyConstraint(constraint);
388 PropertyNotification paddleNotification = delegate.AddPropertyNotification( property, GreaterThanCondition(0.0f) );
389 paddleNotification.NotifySignal().Connect( this, &ExampleController::OnHitPaddle );
396 * Resets Lives count and other stats, and loads level
400 mLives = TOTAL_LIVES;
402 mBall.SetPosition( mBallStartPosition );
403 mBallVelocity = Vector3::ZERO;
404 mPaddle.SetSize( mPaddleFullSize + mPaddleHitMargin );
405 mPaddleImage.SetSize( mPaddleFullSize );
412 * All existing level content is removed, and new bricks
414 * @param[in] level Level index to load.
416 void LoadLevel(int level)
418 if(mLevelContainer && mLevelContainer.GetParent() == mContentLayer)
420 mContentLayer.Remove( mLevelContainer );
423 mLevelContainer = Actor::New();
424 mLevelContainer.SetAnchorPoint( AnchorPoint::CENTER );
425 mLevelContainer.SetParentOrigin( ParentOrigin::CENTER );
426 mLevelContainer.SetSizeMode( SIZE_EQUAL_TO_PARENT );
427 mContentLayer.Add( mLevelContainer );
431 switch(level%TOTAL_LEVELS)
458 void GenerateLevel0()
460 Vector2 stageSize(Stage::GetCurrent().GetSize());
461 const Vector2 brickSize(BRICK_SIZE * stageSize.width);
463 const int columns = (0.85f * stageSize.width) / brickSize.width; // 85 percent of the width of the screen covered with bricks.
464 const int rows = (0.3f * stageSize.height) / brickSize.height; // 30 percent of the height of the screen covered with bricks.
465 const Vector2 offset( (stageSize.x - (columns * brickSize.width)) * 0.5f,
466 stageSize.y * 0.125f );
468 for(int j = 0; j < rows; j++)
470 for(int i = 0; i < columns; i++)
472 Actor brick = CreateBrick(Vector2(i * brickSize.width + offset.x, j * brickSize.height + offset.y) + (brickSize * 0.5f), j % TOTAL_BRICKS );
473 mLevelContainer.Add(brick);
482 void GenerateLevel1()
484 Vector2 stageSize(Stage::GetCurrent().GetSize());
485 const Vector2 brickSize(BRICK_SIZE * stageSize.width);
487 const int columns = (0.85f * stageSize.width) / brickSize.width; // 85 percent of the width of the screen covered with bricks.
488 const int rows = (0.3f * stageSize.height) / brickSize.height; // 30 percent of the height of the screen covered with bricks.
489 const Vector2 offset( (stageSize.x - (columns * brickSize.width)) * 0.5f,
490 stageSize.y * 0.125f );
492 for(int j = 0; j < rows; j++)
494 for(int i = 0; i < columns; i++)
496 int i2 = columns - i - 1;
497 int j2 = rows - j - 1;
498 int brickIndex = std::min( std::min(i, j), std::min(i2, j2) ) % TOTAL_BRICKS;
500 Actor brick = CreateBrick(Vector2(i * brickSize.width + offset.x, j * brickSize.height + offset.y) + (brickSize * 0.5f), brickIndex );
502 mLevelContainer.Add(brick);
511 void GenerateLevel2()
513 Vector2 stageSize(Stage::GetCurrent().GetSize());
514 const Vector2 brickSize(BRICK_SIZE * stageSize.width);
516 const int columns = (0.85f * stageSize.width) / brickSize.width; // 85 percent of the width of the screen covered with bricks.
517 const int rows = (0.3f * stageSize.height) / brickSize.height; // 30 percent of the height of the screen covered with bricks.
518 const Vector2 offset( (stageSize.x - (columns * brickSize.width)) * 0.5f,
519 stageSize.y * 0.125f );
521 // lays down bricks in a spiral formation starting at i,j = (0,0) top left corner
522 // travelling right di,dj = (1,0) initially
528 // contracting boundaries
530 int right = columns - 1;
532 int bottom = rows - 1;
534 // length of current line. we stop laying down bricks when the length is 1 brick or less.
538 Actor brick = CreateBrick(Vector2(i * brickSize.width + offset.x, j * brickSize.height + offset.y) + (brickSize * 0.5f), 0 );
539 mLevelContainer.Add(brick);
543 if((i==right) && (di==1))
548 if((j==bottom) && (dj==1))
553 if((i==left) && (di==-1))
558 if((j==top) && (dj==-1))
565 // turn 90 degrees clockwise.
581 * Creates a brick at a specified position on the stage
582 * @param[in] position the position for the brick
583 * @param[in] type the type of brick
584 * @return The Brick Actor is returned.
586 Actor CreateBrick( const Vector2& position, int type )
588 Vector2 stageSize(Stage::GetCurrent().GetSize());
589 const Vector2 brickSize(BRICK_SIZE * Vector2(stageSize.x, stageSize.x));
591 ImageAttributes attr;
592 attr.SetSize( 128, 64 );
593 attr.SetScalingMode( ImageAttributes::ScaleToFill );
594 Image img = ResourceImage::New(BRICK_IMAGE_PATH[type], attr);
595 ImageActor brick = ImageActor::New(img);
596 brick.SetParentOrigin(ParentOrigin::TOP_LEFT);
597 brick.SetAnchorPoint(AnchorPoint::CENTER);
598 brick.SetSize( brickSize );
599 brick.SetPosition( Vector3( position ) );
601 // Add a constraint on the brick between it and the ball generating a collision-property
602 Property::Index property = brick.RegisterProperty(COLLISION_PROPERTY_NAME, Vector3::ZERO);
603 Constraint constraint = Constraint::New<Vector3>( property,
604 Source(mBall, Actor::POSITION),
605 Source(brick, Actor::POSITION),
606 Source(mBall, Actor::SIZE),
607 Source(brick, Actor::SIZE),
608 CollisionCircleRectangleConstraint(BRICK_COLLISION_MARGIN));
609 brick.ApplyConstraint(constraint);
611 // Now add a notification on this collision-property
613 PropertyNotification brickNotification = brick.AddPropertyNotification( property, GreaterThanCondition(0.0f) );
614 brickNotification.NotifySignal().Connect( this, &ExampleController::OnHitBrick );
620 * Creates an Image (Helper)
622 * @param[in] filename the path of the image.
624 ImageActor CreateImage(const std::string& filename)
626 Image img = ResourceImage::New(filename);
627 ImageActor actor = ImageActor::New(img);
628 actor.SetParentOrigin(ParentOrigin::TOP_LEFT);
629 actor.SetAnchorPoint(AnchorPoint::CENTER);
634 * Continue animation (based on current velocity)
636 void ContinueAnimation()
640 mBallAnimation.Clear();
643 mBallAnimation = Animation::New(MAX_ANIMATION_DURATION);
644 mBallAnimation.AnimateBy( Property( mBall, Actor::POSITION ), mBallVelocity * MAX_ANIMATION_DURATION);
645 mBallAnimation.Play();
649 * Signal invoked whenever user touches the Paddle.
650 * @param[in] actor The actor touched
651 * @param[in] event The touch event
653 bool OnTouchPaddle(Actor actor, const TouchEvent& event)
655 if(event.GetPointCount()>0)
657 const TouchPoint& point = event.GetPoint(0);
658 if(point.state==TouchPoint::Down) // Commence dragging
660 // Get point where user touched paddle (relative to paddle's center)
661 mRelativeDragPoint = Vector3(point.screen.x, point.screen.y, 0.0f);
662 mRelativeDragPoint -= actor.GetCurrentPosition();
665 mDragAnimation = Animation::New(0.25f);
666 mDragAnimation.AnimateTo( Property(mDragActor, Actor::SCALE), Vector3(1.1f, 1.1f, 1.0f), AlphaFunctions::EaseOut);
667 mDragAnimation.AnimateTo( Property(mPaddleHandle, Actor::COLOR), Vector4(1.0f, 1.0f, 1.0f, 0.0f), AlphaFunctions::EaseOut);
668 mDragAnimation.Play();
675 * Signal invoked whenever user touches anywhere on the screen.
676 * @param[in] actor The actor touched
677 * @param[in] event The touch event
679 bool OnTouchLayer(Actor actor, const TouchEvent& event)
681 if(event.GetPointCount()>0)
683 const TouchPoint& point = event.GetPoint(0);
686 Vector3 position(point.screen.x, point.screen.y, 0.0f);
687 mPaddle.SetPosition( position - mRelativeDragPoint );
689 if(point.state==TouchPoint::Up) // Stop dragging
691 mDragAnimation = Animation::New(0.25f);
692 mDragAnimation.AnimateTo( Property(mDragActor, Actor::SCALE), Vector3(1.0f, 1.0f, 1.0f), AlphaFunctions::EaseIn);
693 mDragAnimation.AnimateTo( Property(mPaddleHandle, Actor::COLOR), Vector4(1.0f, 1.0f, 1.0f, 1.0f), AlphaFunctions::EaseOut);
694 mDragAnimation.Play();
703 * Notification: Ball hit left wall
704 * @param source The notification
706 void OnHitLeftWall(PropertyNotification& source)
708 mBallVelocity.x = fabsf(mBallVelocity.x);
713 * Notification: Ball hit right wall
714 * @param source The notification
716 void OnHitRightWall(PropertyNotification& source)
718 mBallVelocity.x = -fabsf(mBallVelocity.x);
723 * Notification: Ball hit top wall
724 * @param source The notification
726 void OnHitTopWall(PropertyNotification& source)
728 mBallVelocity.y = fabsf(mBallVelocity.y);
733 * Notification: Ball hit bottom wall
734 * @param source The notification
736 void OnHitBottomWall(PropertyNotification& source)
740 mBallAnimation.Clear();
746 const float f(static_cast<float>(mLives) / TOTAL_LIVES);
747 mBallVelocity = Vector3::ZERO;
749 Animation shrink = Animation::New(0.5f);
750 shrink.AnimateTo( Property(mPaddle, Actor::SIZE_WIDTH), mPaddleFullSize.x * f + mPaddleHitMargin.x);
751 shrink.AnimateTo( Property(mPaddleImage, Actor::SIZE_WIDTH), mPaddleFullSize.x * f );
753 shrink.FinishedSignal().Connect( this, &ExampleController::OnPaddleShrunk );
759 * Paddle Shrink Animation complete.
760 * @param[in] source The animation responsible for shrinking the paddle.
762 void OnPaddleShrunk( Animation &source )
764 // Reposition Ball in start position, and make ball appear.
765 mBall.SetPosition( mBallStartPosition );
766 mBall.SetColor( Vector4(1.0f, 1.0f, 1.0f, 0.1f) );
767 Animation appear = Animation::New(0.5f);
768 appear.AnimateTo( Property(mBall, Actor::COLOR), Vector4(1.0f, 1.0f, 1.0f, 1.0f) );
778 * Notification: Ball hit paddle
779 * @param source The notification
781 void OnHitPaddle(PropertyNotification& source)
783 Actor delegate = Actor::DownCast(source.GetTarget());
784 Vector3 collisionVector = delegate.GetProperty<Vector3>(source.GetTargetProperty());
785 Vector3 ballRelativePosition(mBall.GetCurrentPosition() - mPaddle.GetCurrentPosition());
786 ballRelativePosition.Normalize();
788 collisionVector.x += ballRelativePosition.x * 0.5f;
790 if(mBallVelocity.LengthSquared() < Math::MACHINE_EPSILON_1)
792 mBallVelocity += collisionVector * BALL_VELOCITY;
796 const float normalVelocity = fabsf(mBallVelocity.Dot(collisionVector));
797 mBallVelocity += collisionVector * normalVelocity * 2.0f;
798 const float currentSpeed = mBallVelocity.Length();
799 const float limitedSpeed = std::min( currentSpeed, MAX_VELOCITY );
800 mBallVelocity = mBallVelocity * limitedSpeed / currentSpeed;
806 mWobbleAnimation = Animation::New(0.5f);
807 mWobbleAnimation.AnimateTo( Property( mPaddle, mWobbleProperty ), 1.0f );
808 mWobbleAnimation.Play();
809 mPaddle.SetProperty(mWobbleProperty, 0.0f);
813 * Notification: Ball hit brick
814 * @param source The notification
816 void OnHitBrick(PropertyNotification& source)
818 Actor brick = Actor::DownCast(source.GetTarget());
819 Vector3 collisionVector = brick.GetProperty<Vector3>(source.GetTargetProperty());
821 const float normalVelocity = fabsf(mBallVelocity.Dot(collisionVector));
822 mBallVelocity += collisionVector * normalVelocity * 2.0f;
823 const float currentSpeed = mBallVelocity.Length();
824 const float limitedSpeed = std::min( currentSpeed, MAX_VELOCITY );
825 mBallVelocity = mBallVelocity * limitedSpeed / currentSpeed;
829 // remove collision-constraint and notification.
830 brick.RemovePropertyNotification(source);
831 brick.RemoveConstraints();
833 // fade brick (destroy)
834 Animation destroyAnimation = Animation::New(0.5f);
835 destroyAnimation.AnimateTo( Property( brick, Actor::COLOR_ALPHA ), 0.0f, AlphaFunctions::EaseIn );
836 destroyAnimation.Play();
837 destroyAnimation.FinishedSignal().Connect( this, &ExampleController::OnBrickDestroyed );
838 mDestroyAnimationMap[destroyAnimation] = brick;
842 * Brick Destruction Animation complete.
843 * @param[in] source The animation responsible for destroying the brick
845 void OnBrickDestroyed( Animation& source )
847 // Remove brick from stage, it's constraint and property notification should also remove themselves.
848 Actor brick = mDestroyAnimationMap[source];
849 mDestroyAnimationMap.erase(source);
850 brick.GetParent().Remove(brick);
861 * Main key event handler
863 void OnKeyEvent(const KeyEvent& event)
865 if(event.state == KeyEvent::Down)
867 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
876 Application& mApplication; ///< Application instance
877 Toolkit::View mView; ///< The View instance.
878 Layer mContentLayer; ///< The content layer (contains game actors)
879 ImageActor mBall; ///< The Moving ball image.
880 Vector3 mBallStartPosition; ///< Ball Start position
881 Vector3 mBallVelocity; ///< Ball's current direction.
882 Animation mBallAnimation; ///< Ball's animation
883 Actor mPaddle; ///< The paddle including hit area.
884 ImageActor mPaddleImage; ///< The paddle's image.
885 ImageActor mPaddleHandle; ///< The paddle's handle (where the user touches)
886 Vector2 mPaddleHitMargin; ///< The paddle hit margin.
887 Animation mWobbleAnimation; ///< Paddle's animation when hit (wobbles)
888 Property::Index mWobbleProperty; ///< The wobble property (generated from animation)
889 Actor mLevelContainer; ///< The level container (contains bricks)
891 // actor - dragging functionality
893 Animation mDragAnimation; ///< Animation for dragging. (grows - affects ACTOR::SCALE)
894 Actor mDragActor; ///< The actor which is being dragged (if any)
895 Vector3 mRelativeDragPoint; ///< The point the user touched, relative to the actor.
896 std::map<Animation, Actor> mDestroyAnimationMap; ///< Keep track of which actors are to be destroyed.
897 Vector2 mPaddleFullSize; ///< Initial 100% size of the paddle.
898 int mLevel; ///< Current level
899 int mLives; ///< Total lives.
900 int mBrickCount; ///< Total bricks on screen.
903 void RunTest(Application& app)
905 ExampleController test(app);
910 int main(int argc, char **argv)
912 Application app = Application::New(&argc, &argv);