2 * Copyright (c) 2017 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 <dali/dali.h>
25 #include <dali-toolkit/dali-toolkit.h>
26 #include "shared/view.h"
29 using namespace Dali::Toolkit;
30 using namespace DemoHelper;
34 const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-blocks.jpg" );
35 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
36 const char* APPLICATION_TITLE( "DALi Blocks" );
37 const char* BALL_IMAGE = DEMO_IMAGE_DIR "blocks-ball.png";
38 const char* PADDLE_IMAGE = DEMO_IMAGE_DIR "blocks-paddle.png";
39 const char* PADDLE_HANDLE_IMAGE = DEMO_IMAGE_DIR "blocks-paddle-handle.png";
41 const char* BRICK_IMAGE_PATH[] = { DEMO_IMAGE_DIR "blocks-brick-1.png",
42 DEMO_IMAGE_DIR "blocks-brick-2.png",
43 DEMO_IMAGE_DIR "blocks-brick-3.png",
44 DEMO_IMAGE_DIR "blocks-brick-4.png" };
46 const int TOTAL_BRICKS(4); ///< Total bricks in game.
47 const Vector3 ICON_SIZE(100.0f, 100.0f, 0.0f);
49 const float SCREEN_MARGIN = 10.0f; ///< Margin indentation around screen
50 const Vector3 MENU_BUTTON_SIZE = Vector3(0.15f, 0.05f, 1.0f); ///< Standard Menu Buttons.
52 const float MAX_ANIMATION_DURATION = 60.0f; ///< 60 seconds animations. Long enough for ball to hit an obstacle.
53 const float BALL_VELOCITY = 300.0f; ///< Ball velocity in pixels/second.
54 const float MAX_VELOCITY = 500.0f; ///< Max. velocity in pixels/second.
55 const Vector3 PADDLE_COLLISION_MARGIN(0.0f, 0.0f, 0.0f); ///< Collision margin for ball-paddle detection.
56 const Vector3 BRICK_COLLISION_MARGIN(0.0f, 0.0f, 0.0f); ///< Collision margin for ball-brick detection.
57 const Vector3 INITIAL_BALL_DIRECTION(1.0f, 1.0f, 0.0f); ///< Initial ball direction.
59 const std::string WOBBLE_PROPERTY_NAME("wobbleProperty"); ///< Wobble property name.
60 const std::string COLLISION_PROPERTY_NAME("collisionProperty"); ///< Collision property name.
62 const Vector2 BRICK_SIZE(0.1f, 0.05f ); ///< Brick size relative to width of stage.
63 const Vector2 BALL_SIZE( 0.05f, 0.05f ); ///< Ball size relative to width of stage.
64 const Vector2 PADDLE_SIZE( 0.2f, 0.05f ); ///< Paddle size relative to width of stage.
65 const Vector2 PADDLE_HANDLE_SIZE( 0.3f, 0.3f ); ///< Paddle handle size relative to width of stage.
66 const Vector2 BALL_START_POSITION(0.5f, 0.8f); ///< Ball start position relative to stage size.
67 const Vector2 PADDLE_START_POSITION(0.5f, 0.9f); ///< Paddler start position relative to stage size.
68 const Vector2 PADDLE_HIT_MARGIN( 0.1, 0.15f ); ///< Extra hit Area for Paddle when touching.
70 const int TOTAL_LIVES(3); ///< Total lives in game before it's game over!
71 const int TOTAL_LEVELS(3); ///< 3 Levels total, then repeats.
73 // constraints ////////////////////////////////////////////////////////////////
76 * CollisionCircleRectangleConstraint generates a collision vector
77 * between two actors a (circle) and b (rectangle)
79 struct CollisionCircleRectangleConstraint
82 * Collision Constraint constructor
83 * The adjust (optional) parameter can be used to add a margin
84 * to the actors. A +ve size will result in larger collisions,
85 * while a -ve size will result in tighter collisions.
87 * @param[in] adjustPosition (optional) Adjusts the position offset of detection
88 * @param[in] adjustSize (optional) Adjusts the rectangular size of detection
90 CollisionCircleRectangleConstraint(Vector3 adjustPosition = Vector3::ZERO,
91 Vector3 adjustSize = Vector3::ZERO)
92 : mAdjustPosition(adjustPosition),
93 mAdjustSize(adjustSize)
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,out] current The current collision-property
102 * @param[in] inputs Contains:
103 * Actor A's Position property.
104 * Actor B's Position property.
105 * Actor A's Size property.
106 * Actor B's Size property.
107 * @return The collision vector is returned.
109 void operator()( Vector3& current, const PropertyInputContainer& inputs )
111 const Vector3& a = inputs[0]->GetVector3();
112 const Vector3 b = inputs[1]->GetVector3() + mAdjustPosition;
113 const Vector3& sizeA = inputs[2]->GetVector3();
114 const Vector3& sizeB = inputs[3]->GetVector3();
115 const Vector3 sizeA2 = sizeA * 0.5f; // circle radius
116 const Vector3 sizeB2 = (sizeB + mAdjustSize) * 0.5f; // rectangle half rectangle.
118 // get collision relative to a (rectangle).
119 Vector3 delta = a - b;
121 // reduce rectangle to 0.
122 if (delta.x > sizeB2.x)
126 else if (delta.x < -sizeB2.x)
135 if (delta.y > sizeB2.y)
139 else if (delta.y < -sizeB2.y)
148 // now calculate collision vector vs origin. (assume A is a circle, not ellipse)
149 if(delta.Length() < sizeA2.x)
156 current = Vector3::ZERO;
160 const Vector3 mAdjustPosition; ///< Position Adjustment value
161 const Vector3 mAdjustSize; ///< Size Adjustment value
165 * WobbleConstraint generates a decaying sinusoidial rotation.
166 * The result when applied to an Actor, is the Actor rotating left/right
167 * initially a large amount (deviation degrees, when wobble property is 0.0f)
168 * then eventually coming to a stop (once wobble property reaches 1.0f)
170 struct WobbleConstraint
173 * Wobble Constraint constructor
174 * Generates a sinusoidial rotation that starts with
175 * high amplitude (deviation), and then decays to zero over input 0.0f to 1.0f
177 * @param[in] deviation The max. deviation of wobble effect in degrees.
179 WobbleConstraint( Degree deviation )
180 : mDeviation( deviation )
186 * @param[in,out] current The current rotation property
187 * @param[in] inputs Contains the wobble property (value from 0.0f to 1.0f)
188 * @return The rotation (quaternion) is generated.
190 void operator()( Quaternion& current, const PropertyInputContainer& inputs )
192 const float& wobble = inputs[0]->GetFloat();
194 float f = sinf(wobble * 10.0f) * (1.0f-wobble);
196 current = Quaternion(mDeviation * f, Vector3::ZAXIS);
199 Radian mDeviation; ///< Deviation factor in radians.
202 } // unnamed namespace
205 * This example shows how to use PropertyNotifications
207 class ExampleController : public ConnectionTracker
213 * @param application Application class, stored as reference
215 ExampleController( Application& application )
216 : mApplication( application ),
220 mBallStartPosition(),
228 mWobbleProperty( Property::INVALID_INDEX ),
233 mRelativeDragPoint(),
234 mDestroyAnimationMap(),
237 mLives( TOTAL_LIVES ),
241 // Connect to the Application's Init and orientation changed signal
242 mApplication.InitSignal().Connect(this, &ExampleController::Create);
246 * This method gets called once the main loop of application is up and running
247 * @param[in] application Reference to the application instance
249 void Create(Application& application)
251 Stage::GetCurrent().KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
253 // Hide the indicator bar
254 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
256 // Creates a default view with a default tool bar.
257 // The view is added to the stage.
258 Toolkit::ToolBar toolBar;
259 mContentLayer = DemoHelper::CreateView( application,
266 // Add an extra space on the right to center the title text.
267 toolBar.AddControl( Actor::New(), DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight );
269 // Create the content layer, which is where game actors appear.
276 * Adds a new layer to the stage, containing game actors.
278 void AddContentLayer()
280 Stage stage = Stage::GetCurrent();
281 const Vector3 stageSize(stage.GetSize());
284 mBallStartPosition = stageSize * Vector3( BALL_START_POSITION );
285 mBall = CreateImage(BALL_IMAGE);
286 mBall.SetPosition( mBallStartPosition );
287 mBall.SetSize( BALL_SIZE * stageSize.width );
288 mContentLayer.Add(mBall);
289 mBallVelocity = Vector3::ZERO;
292 mPaddleHitMargin = Vector2(stageSize) * PADDLE_HIT_MARGIN;
293 mPaddle = Actor::New();
294 mPaddleHandle = CreateImage(PADDLE_HANDLE_IMAGE);
295 mPaddleImage = CreateImage(PADDLE_IMAGE);
296 mPaddle.Add( mPaddleHandle );
297 mPaddle.Add( mPaddleImage );
298 mPaddleHandle.SetParentOrigin( ParentOrigin::TOP_CENTER );
299 mPaddleHandle.SetAnchorPoint( AnchorPoint::TOP_CENTER );
300 mPaddleHandle.SetPosition( 0.0f, stageSize.width * 0.0125f );
301 mPaddleImage.SetParentOrigin( ParentOrigin::TOP_CENTER );
302 mPaddleImage.SetAnchorPoint( AnchorPoint::TOP_CENTER );
303 mPaddle.SetParentOrigin( ParentOrigin::TOP_LEFT );
304 mPaddle.SetAnchorPoint( AnchorPoint::CENTER );
305 mPaddleFullSize = PADDLE_SIZE * stageSize.width;
306 mPaddle.SetSize( mPaddleFullSize + mPaddleHitMargin );
307 mPaddleHandle.SetSize( PADDLE_HANDLE_SIZE * stageSize.width );
308 mPaddleImage.SetSize( mPaddleFullSize );
310 mWobbleProperty = mPaddle.RegisterProperty(WOBBLE_PROPERTY_NAME, 0.0f);
311 Constraint wobbleConstraint = Constraint::New<Quaternion>( mPaddle, Actor::Property::ORIENTATION, WobbleConstraint(Degree( 10.0f )));
312 wobbleConstraint.AddSource( LocalSource(mWobbleProperty) );
313 wobbleConstraint.Apply();
315 mPaddle.SetPosition( stageSize * Vector3( PADDLE_START_POSITION ) );
316 mContentLayer.Add(mPaddle);
317 mPaddle.TouchSignal().Connect(this, &ExampleController::OnTouchPaddle);
318 mContentLayer.TouchSignal().Connect(this, &ExampleController::OnTouchLayer);
320 const float margin(BALL_SIZE.width * stageSize.width * 0.5f);
322 // Set up notifications for ball's collisions against walls.
323 PropertyNotification leftNotification = mBall.AddPropertyNotification( Actor::Property::POSITION_X, LessThanCondition(margin) );
324 leftNotification.NotifySignal().Connect( this, &ExampleController::OnHitLeftWall );
326 PropertyNotification rightNotification = mBall.AddPropertyNotification( Actor::Property::POSITION_X, GreaterThanCondition(stageSize.width - margin) );
327 rightNotification.NotifySignal().Connect( this, &ExampleController::OnHitRightWall );
329 PropertyNotification topNotification = mBall.AddPropertyNotification( Actor::Property::POSITION_Y, LessThanCondition(margin) );
330 topNotification.NotifySignal().Connect( this, &ExampleController::OnHitTopWall );
332 PropertyNotification bottomNotification = mBall.AddPropertyNotification( Actor::Property::POSITION_Y, GreaterThanCondition(stageSize.height + margin) );
333 bottomNotification.NotifySignal().Connect( this, &ExampleController::OnHitBottomWall );
335 // Set up notification for ball colliding against paddle.
336 Actor delegate = Actor::New();
338 Property::Index property = delegate.RegisterProperty(COLLISION_PROPERTY_NAME, Vector3::ZERO);
339 Constraint constraint = Constraint::New<Vector3>( delegate, property, CollisionCircleRectangleConstraint( -Vector3(0.0f, mPaddleHitMargin.height * 0.575f, 0.0f),-Vector3(mPaddleHitMargin) ) );
340 constraint.AddSource( Source(mBall, Actor::Property::POSITION) );
341 constraint.AddSource( Source(mPaddle, Actor::Property::POSITION) );
342 constraint.AddSource( Source(mBall, Actor::Property::SIZE) );
343 constraint.AddSource( Source(mPaddle, Actor::Property::SIZE) );
346 PropertyNotification paddleNotification = delegate.AddPropertyNotification( property, GreaterThanCondition(0.0f) );
347 paddleNotification.NotifySignal().Connect( this, &ExampleController::OnHitPaddle );
354 * Resets Lives count and other stats, and loads level
358 mLives = TOTAL_LIVES;
360 mBall.SetPosition( mBallStartPosition );
361 mBallVelocity = Vector3::ZERO;
362 mPaddle.SetSize( mPaddleFullSize + mPaddleHitMargin );
363 mPaddleImage.SetSize( mPaddleFullSize );
370 * All existing level content is removed, and new bricks
372 * @param[in] level Level index to load.
374 void LoadLevel(int level)
376 if(mLevelContainer && mLevelContainer.GetParent() == mContentLayer)
378 mContentLayer.Remove( mLevelContainer );
381 mLevelContainer = Actor::New();
382 mLevelContainer.SetAnchorPoint( AnchorPoint::CENTER );
383 mLevelContainer.SetParentOrigin( ParentOrigin::CENTER );
384 mLevelContainer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
386 mContentLayer.Add( mLevelContainer );
390 if( mBrickImageMap.Empty() )
392 Vector2 stageSize(Stage::GetCurrent().GetSize());
393 const Vector2 brickSize(BRICK_SIZE * Vector2(stageSize.x, stageSize.x));
395 mBrickImageMap["desiredWidth"] = static_cast<int>( brickSize.width );
396 mBrickImageMap["desiredHeight"] = static_cast<int>( brickSize.height );
397 mBrickImageMap["fittingMode"] = "SCALE_TO_FILL";
398 mBrickImageMap["samplingMode"] = "BOX_THEN_LINEAR";
401 switch(level%TOTAL_LEVELS)
428 void GenerateLevel0()
430 Vector2 stageSize(Stage::GetCurrent().GetSize());
431 const Vector2 brickSize(BRICK_SIZE * stageSize.width);
433 const int columns = (0.85f * stageSize.width) / brickSize.width; // 85 percent of the width of the screen covered with bricks.
434 const int rows = (0.3f * stageSize.height) / brickSize.height; // 30 percent of the height of the screen covered with bricks.
435 const Vector2 offset( (stageSize.x - (columns * brickSize.width)) * 0.5f,
436 stageSize.y * 0.125f );
438 for(int j = 0; j < rows; j++)
440 for(int i = 0; i < columns; i++)
442 Actor brick = CreateBrick(Vector2(i * brickSize.width + offset.x, j * brickSize.height + offset.y) + (brickSize * 0.5f), j % TOTAL_BRICKS );
443 mLevelContainer.Add(brick);
452 void GenerateLevel1()
454 Vector2 stageSize(Stage::GetCurrent().GetSize());
455 const Vector2 brickSize(BRICK_SIZE * stageSize.width);
457 const int columns = (0.85f * stageSize.width) / brickSize.width; // 85 percent of the width of the screen covered with bricks.
458 const int rows = (0.3f * stageSize.height) / brickSize.height; // 30 percent of the height of the screen covered with bricks.
459 const Vector2 offset( (stageSize.x - (columns * brickSize.width)) * 0.5f,
460 stageSize.y * 0.125f );
462 for(int j = 0; j < rows; j++)
464 for(int i = 0; i < columns; i++)
466 int i2 = columns - i - 1;
467 int j2 = rows - j - 1;
468 int brickIndex = std::min( std::min(i, j), std::min(i2, j2) ) % TOTAL_BRICKS;
470 Actor brick = CreateBrick(Vector2(i * brickSize.width + offset.x, j * brickSize.height + offset.y) + (brickSize * 0.5f), brickIndex );
472 mLevelContainer.Add(brick);
481 void GenerateLevel2()
483 Vector2 stageSize(Stage::GetCurrent().GetSize());
484 const Vector2 brickSize(BRICK_SIZE * stageSize.width);
486 const int columns = (0.85f * stageSize.width) / brickSize.width; // 85 percent of the width of the screen covered with bricks.
487 const int rows = (0.3f * stageSize.height) / brickSize.height; // 30 percent of the height of the screen covered with bricks.
488 const Vector2 offset( (stageSize.x - (columns * brickSize.width)) * 0.5f,
489 stageSize.y * 0.125f );
491 // lays down bricks in a spiral formation starting at i,j = (0,0) top left corner
492 // travelling right di,dj = (1,0) initially
498 // contracting boundaries
500 int right = columns - 1;
502 int bottom = rows - 1;
504 // length of current line. we stop laying down bricks when the length is 1 brick or less.
508 Actor brick = CreateBrick(Vector2(i * brickSize.width + offset.x, j * brickSize.height + offset.y) + (brickSize * 0.5f), 0 );
509 mLevelContainer.Add(brick);
513 if((i==right) && (di==1))
518 if((j==bottom) && (dj==1))
523 if((i==left) && (di==-1))
528 if((j==top) && (dj==-1))
535 // turn 90 degrees clockwise.
551 * Creates a brick at a specified position on the stage
552 * @param[in] position the position for the brick
553 * @param[in] type the type of brick
554 * @return The Brick Actor is returned.
556 Actor CreateBrick( const Vector2& position, int type )
558 mBrickImageMap["url"] = BRICK_IMAGE_PATH[type];
559 ImageView brick = ImageView::New();
560 brick.SetProperty( ImageView::Property::IMAGE, mBrickImageMap );
561 brick.SetParentOrigin(ParentOrigin::TOP_LEFT);
562 brick.SetAnchorPoint(AnchorPoint::CENTER);
563 brick.SetPosition( Vector3( position ) );
565 // Add a constraint on the brick between it and the ball generating a collision-property
566 Property::Index property = brick.RegisterProperty(COLLISION_PROPERTY_NAME, Vector3::ZERO);
567 Constraint constraint = Constraint::New<Vector3>( brick, property, CollisionCircleRectangleConstraint(BRICK_COLLISION_MARGIN) );
568 constraint.AddSource( Source(mBall, Actor::Property::POSITION) );
569 constraint.AddSource( Source(brick, Actor::Property::POSITION) );
570 constraint.AddSource( Source(mBall, Actor::Property::SIZE) );
571 constraint.AddSource( Source(brick, Actor::Property::SIZE) );
574 // Now add a notification on this collision-property
576 PropertyNotification brickNotification = brick.AddPropertyNotification( property, GreaterThanCondition(0.0f) );
577 brickNotification.NotifySignal().Connect( this, &ExampleController::OnHitBrick );
583 * Creates an Image (Helper)
585 * @param[in] filename the path of the image.
587 ImageView CreateImage(const std::string& filename)
589 ImageView actor = ImageView::New(filename);
590 actor.SetParentOrigin(ParentOrigin::TOP_LEFT);
591 actor.SetAnchorPoint(AnchorPoint::CENTER);
596 * Continue animation (based on current velocity)
598 void ContinueAnimation()
602 mBallAnimation.Clear();
605 mBallAnimation = Animation::New(MAX_ANIMATION_DURATION);
606 mBallAnimation.AnimateBy( Property( mBall, Actor::Property::POSITION ), mBallVelocity * MAX_ANIMATION_DURATION);
607 mBallAnimation.Play();
611 * Signal invoked whenever user touches the Paddle.
612 * @param[in] actor The actor touched
613 * @param[in] event The touch event
615 bool OnTouchPaddle(Actor actor, const TouchData& event)
617 if(event.GetPointCount()>0)
619 if( event.GetState( 0 ) == PointState::DOWN ) // Commence dragging
621 // Get point where user touched paddle (relative to paddle's center)
622 Vector2 screenPoint = event.GetScreenPosition( 0 );
623 mRelativeDragPoint = screenPoint;
624 mRelativeDragPoint -= actor.GetCurrentPosition();
627 mDragAnimation = Animation::New(0.25f);
628 mDragAnimation.AnimateTo( Property(mDragActor, Actor::Property::SCALE), Vector3(1.1f, 1.1f, 1.0f), AlphaFunction::EASE_OUT);
629 mDragAnimation.AnimateTo( Property(mPaddleHandle, Actor::Property::COLOR), Vector4(1.0f, 1.0f, 1.0f, 0.0f), AlphaFunction::EASE_OUT);
630 mDragAnimation.Play();
637 * Signal invoked whenever user touches anywhere on the screen.
638 * @param[in] actor The actor touched
639 * @param[in] event The touch event
641 bool OnTouchLayer(Actor actor, const TouchData& event)
643 if(event.GetPointCount()>0)
647 Vector3 position( event.GetScreenPosition( 0 ) );
648 mPaddle.SetPosition( position - mRelativeDragPoint );
650 if( event.GetState( 0 ) == PointState::UP ) // Stop dragging
652 mDragAnimation = Animation::New(0.25f);
653 mDragAnimation.AnimateTo( Property(mDragActor, Actor::Property::SCALE), Vector3(1.0f, 1.0f, 1.0f), AlphaFunction::EASE_IN);
654 mDragAnimation.AnimateTo( Property(mPaddleHandle, Actor::Property::COLOR), Vector4(1.0f, 1.0f, 1.0f, 1.0f), AlphaFunction::EASE_OUT);
655 mDragAnimation.Play();
664 * Notification: Ball hit left wall
665 * @param source The notification
667 void OnHitLeftWall(PropertyNotification& source)
669 mBallVelocity.x = fabsf(mBallVelocity.x);
674 * Notification: Ball hit right wall
675 * @param source The notification
677 void OnHitRightWall(PropertyNotification& source)
679 mBallVelocity.x = -fabsf(mBallVelocity.x);
684 * Notification: Ball hit top wall
685 * @param source The notification
687 void OnHitTopWall(PropertyNotification& source)
689 mBallVelocity.y = fabsf(mBallVelocity.y);
694 * Notification: Ball hit bottom wall
695 * @param source The notification
697 void OnHitBottomWall(PropertyNotification& source)
701 mBallAnimation.Clear();
707 const float f(static_cast<float>(mLives) / TOTAL_LIVES);
708 mBallVelocity = Vector3::ZERO;
710 Animation shrink = Animation::New(0.5f);
711 shrink.AnimateTo( Property(mPaddle, Actor::Property::SIZE_WIDTH), mPaddleFullSize.x * f + mPaddleHitMargin.x);
712 shrink.AnimateTo( Property(mPaddleImage, Actor::Property::SIZE_WIDTH), mPaddleFullSize.x * f );
714 shrink.FinishedSignal().Connect( this, &ExampleController::OnPaddleShrunk );
720 * Paddle Shrink Animation complete.
721 * @param[in] source The animation responsible for shrinking the paddle.
723 void OnPaddleShrunk( Animation &source )
725 // Reposition Ball in start position, and make ball appear.
726 mBall.SetPosition( mBallStartPosition );
727 mBall.SetColor( Vector4(1.0f, 1.0f, 1.0f, 0.1f) );
728 Animation appear = Animation::New(0.5f);
729 appear.AnimateTo( Property(mBall, Actor::Property::COLOR), Vector4(1.0f, 1.0f, 1.0f, 1.0f) );
739 * Notification: Ball hit paddle
740 * @param source The notification
742 void OnHitPaddle(PropertyNotification& source)
744 Actor delegate = Actor::DownCast(source.GetTarget());
745 Vector3 collisionVector = delegate.GetCurrentProperty< Vector3 >( source.GetTargetProperty() );
746 Vector3 ballRelativePosition(mBall.GetCurrentPosition() - mPaddle.GetCurrentPosition());
747 ballRelativePosition.Normalize();
749 collisionVector.x += ballRelativePosition.x * 0.5f;
751 if(mBallVelocity.LengthSquared() < Math::MACHINE_EPSILON_1)
753 mBallVelocity += collisionVector * BALL_VELOCITY;
757 const float normalVelocity = fabsf(mBallVelocity.Dot(collisionVector));
758 mBallVelocity += collisionVector * normalVelocity * 2.0f;
759 const float currentSpeed = mBallVelocity.Length();
760 const float limitedSpeed = std::min( currentSpeed, MAX_VELOCITY );
761 mBallVelocity = mBallVelocity * limitedSpeed / currentSpeed;
767 mWobbleAnimation = Animation::New(0.5f);
768 mWobbleAnimation.AnimateTo( Property( mPaddle, mWobbleProperty ), 1.0f );
769 mWobbleAnimation.Play();
770 mPaddle.SetProperty(mWobbleProperty, 0.0f);
774 * Notification: Ball hit brick
775 * @param source The notification
777 void OnHitBrick(PropertyNotification& source)
779 Actor brick = Actor::DownCast(source.GetTarget());
780 Vector3 collisionVector = brick.GetCurrentProperty< Vector3 >( source.GetTargetProperty() );
782 const float normalVelocity = fabsf(mBallVelocity.Dot(collisionVector));
783 mBallVelocity += collisionVector * normalVelocity * 2.0f;
784 const float currentSpeed = mBallVelocity.Length();
785 const float limitedSpeed = std::min( currentSpeed, MAX_VELOCITY );
786 mBallVelocity = mBallVelocity * limitedSpeed / currentSpeed;
790 // remove collision-constraint and notification.
791 brick.RemovePropertyNotification(source);
792 brick.RemoveConstraints();
794 // fade brick (destroy)
795 Animation destroyAnimation = Animation::New(0.5f);
796 destroyAnimation.AnimateTo( Property( brick, Actor::Property::COLOR_ALPHA ), 0.0f, AlphaFunction::EASE_IN );
797 destroyAnimation.Play();
798 destroyAnimation.FinishedSignal().Connect( this, &ExampleController::OnBrickDestroyed );
799 mDestroyAnimationMap[destroyAnimation] = brick;
803 * Brick Destruction Animation complete.
804 * @param[in] source The animation responsible for destroying the brick
806 void OnBrickDestroyed( Animation& source )
808 // Remove brick from stage, it's constraint and property notification should also remove themselves.
809 Actor brick = mDestroyAnimationMap[source];
810 mDestroyAnimationMap.erase(source);
811 brick.GetParent().Remove(brick);
822 * Main key event handler
824 void OnKeyEvent(const KeyEvent& event)
826 if(event.state == KeyEvent::Down)
828 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
837 Application& mApplication; ///< Application instance
838 Toolkit::Control mView; ///< The View instance.
839 Layer mContentLayer; ///< The content layer (contains game actors)
840 ImageView mBall; ///< The Moving ball image.
841 Vector3 mBallStartPosition; ///< Ball Start position
842 Vector3 mBallVelocity; ///< Ball's current direction.
843 Animation mBallAnimation; ///< Ball's animation
844 Actor mPaddle; ///< The paddle including hit area.
845 ImageView mPaddleImage; ///< The paddle's image.
846 ImageView mPaddleHandle; ///< The paddle's handle (where the user touches)
847 Vector2 mPaddleHitMargin; ///< The paddle hit margin.
848 Animation mWobbleAnimation; ///< Paddle's animation when hit (wobbles)
849 Property::Index mWobbleProperty; ///< The wobble property (generated from animation)
850 Actor mLevelContainer; ///< The level container (contains bricks)
851 Property::Map mBrickImageMap; ///< The property map used to load the brick
853 // actor - dragging functionality
855 Animation mDragAnimation; ///< Animation for dragging. (grows - affects ACTOR::SCALE)
856 Actor mDragActor; ///< The actor which is being dragged (if any)
857 Vector3 mRelativeDragPoint; ///< The point the user touched, relative to the actor.
858 std::map<Animation, Actor> mDestroyAnimationMap; ///< Keep track of which actors are to be destroyed.
859 Vector2 mPaddleFullSize; ///< Initial 100% size of the paddle.
860 int mLevel; ///< Current level
861 int mLives; ///< Total lives.
862 int mBrickCount; ///< Total bricks on screen.
865 void RunTest(Application& app)
867 ExampleController test(app);
872 int DALI_EXPORT_API main(int argc, char **argv)
874 Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);