blocks-example.cpp
32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
/*
* Copyright (c) 2014 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <sstream>
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <boost/any.hpp>
#include <boost/function.hpp>
#include <dali/dali.h>
#include <dali-toolkit/dali-toolkit.h>
#include "shared/view.h"
using namespace Dali;
using namespace Dali::Toolkit;
using namespace DemoHelper;
namespace
{
const char* BACKGROUND_IMAGE( DALI_IMAGE_DIR "background-blocks.jpg" );
const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
const char* APPLICATION_TITLE( "DALi Blocks" );
const char* BALL_IMAGE = DALI_IMAGE_DIR "blocks-ball.png";
const char* PADDLE_IMAGE = DALI_IMAGE_DIR "blocks-paddle.png";
const char* PADDLE_HANDLE_IMAGE = DALI_IMAGE_DIR "blocks-paddle-handle.png";
const char* BRICK_IMAGE_PATH[] = { DALI_IMAGE_DIR "blocks-brick-1.png",
DALI_IMAGE_DIR "blocks-brick-2.png",
DALI_IMAGE_DIR "blocks-brick-3.png",
DALI_IMAGE_DIR "blocks-brick-4.png" };
const int TOTAL_BRICKS(4); ///< Total bricks in game.
const Vector3 ICON_SIZE(100.0f, 100.0f, 0.0f);
const float SCREEN_MARGIN = 10.0f; ///< Margin indentation around screen
const Vector3 MENU_BUTTON_SIZE = Vector3(0.15f, 0.05f, 1.0f); ///< Standard Menu Buttons.
const float MAX_ANIMATION_DURATION = 60.0f; ///< 60 seconds animations. Long enough for ball to hit an obstacle.
const float BALL_VELOCITY = 300.0f; ///< Ball velocity in pixels/second.
const float MAX_VELOCITY = 500.0f; ///< Max. velocity in pixels/second.
const Vector3 PADDLE_COLLISION_MARGIN(0.0f, 0.0f, 0.0f); ///< Collision margin for ball-paddle detection.
const Vector3 BRICK_COLLISION_MARGIN(0.0f, 0.0f, 0.0f); ///< Collision margin for ball-brick detection.
const Vector3 INITIAL_BALL_DIRECTION(1.0f, 1.0f, 0.0f); ///< Initial ball direction.
const std::string WOBBLE_PROPERTY_NAME("wobble-property"); ///< Wobble property name.
const std::string COLLISION_PROPERTY_NAME("collision-property"); ///< Collision property name.
const Vector2 BRICK_SIZE(0.1f, 0.05f ); ///< Brick size relative to width of stage.
const Vector2 BALL_SIZE( 0.05f, 0.05f ); ///< Ball size relative to width of stage.
const Vector2 PADDLE_SIZE( 0.2f, 0.05f ); ///< Paddle size relative to width of stage.
const Vector2 PADDLE_HANDLE_SIZE( 0.3f, 0.3f ); ///< Paddle handle size relative to width of stage.
const Vector2 BALL_START_POSITION(0.5f, 0.8f); ///< Ball start position relative to stage size.
const Vector2 PADDLE_START_POSITION(0.5f, 0.9f); ///< Paddler start position relative to stage size.
const Vector2 PADDLE_HIT_MARGIN( 0.1, 0.15f ); ///< Extra hit Area for Paddle when touching.
const int TOTAL_LIVES(3); ///< Total lives in game before it's game over!
const int TOTAL_LEVELS(3); ///< 3 Levels total, then repeats.
// constraints ////////////////////////////////////////////////////////////////
/**
* CollisionConstraint generates a collision vector
* between two actors a and b, assuming they're rectangular
* based on their size.
*/
struct CollisionConstraint
{
/**
* Collision Constraint constructor
* The adjust (optional) parameter can be used to add a margin
* to the actors. A +ve size will result in larger collisions,
* while a -ve size will result in tighter collisions.
*
* @param[in] adjust (optional) Adjusts the rectangular size detection
*/
CollisionConstraint(Vector3 adjust = Vector3::ZERO)
: mAdjust(adjust)
{
}
/**
* Generates collision vector indicating whether Actor's A and B
* have overlapped eachother, and the relative position of Actor B to A.
*
* @param[in] current The current collision-property (ignored)
* @param[in] propertyA Actor A's Position property.
* @param[in] propertyB Actor B's Position property.
* @param[in] propertySizeA Actor A's Size property.
* @param[in] propertySizeB Actor B's Size property.
* @return The collision vector is returned.
*/
Vector3 operator()(const Vector3& current,
const PropertyInput& propertyA,
const PropertyInput& propertyB,
const PropertyInput& propertySizeA,
const PropertyInput& propertySizeB)
{
const Vector3& a = propertyA.GetVector3();
const Vector3& b = propertyB.GetVector3();
const Vector3& sizeA = propertySizeA.GetVector3();
const Vector3& sizeB = propertySizeB.GetVector3();
const Vector3 sizeComb = (sizeA + sizeB + mAdjust) * 0.5f;
// get collision relative to a.
Vector3 delta = b - a;
// Check if not overlapping Actors.
if( (fabsf(delta.x) > sizeComb.width) ||
(fabsf(delta.y) > sizeComb.height) )
{
delta = Vector3::ZERO; // not overlapping
}
return delta; // overlapping, return overlap vector relative to actor a.
}
const Vector3 mAdjust; ///< Size Adjustment value
};
/**
* CollisionCircleRectangleConstraint generates a collision vector
* between two actors a (circle) and b (rectangle)
*/
struct CollisionCircleRectangleConstraint
{
/**
* Collision Constraint constructor
* The adjust (optional) parameter can be used to add a margin
* to the actors. A +ve size will result in larger collisions,
* while a -ve size will result in tighter collisions.
*
* @param[in] adjustPosition (optional) Adjusts the position offset of detection
* @param[in] adjustSize (optional) Adjusts the rectangular size of detection
*/
CollisionCircleRectangleConstraint(Vector3 adjustPosition = Vector3::ZERO,
Vector3 adjustSize = Vector3::ZERO)
: mAdjustPosition(adjustPosition),
mAdjustSize(adjustSize)
{
}
/**
* Generates collision vector indicating whether Actor's A and B
* have overlapped eachother, and the relative position of Actor B to A.
*
* @param[in] current The current collision-property (ignored)
* @param[in] propertyA Actor A's Position property.
* @param[in] propertyB Actor B's Position property.
* @param[in] propertySizeA Actor A's Size property.
* @param[in] propertySizeB Actor B's Size property.
* @return The collision vector is returned.
*/
Vector3 operator()(const Vector3& current,
const PropertyInput& propertyA,
const PropertyInput& propertyB,
const PropertyInput& propertySizeA,
const PropertyInput& propertySizeB)
{
const Vector3& a = propertyA.GetVector3();
const Vector3 b = propertyB.GetVector3() + mAdjustPosition;
const Vector3& sizeA = propertySizeA.GetVector3();
const Vector3& sizeB = propertySizeB.GetVector3();
const Vector3 sizeA2 = sizeA * 0.5f; // circle radius
const Vector3 sizeB2 = (sizeB + mAdjustSize) * 0.5f; // rectangle half rectangle.
// get collision relative to a (rectangle).
Vector3 delta = a - b;
// reduce rectangle to 0.
if (delta.x > sizeB2.x)
{
delta.x -= sizeB2.x;
}
else if (delta.x < -sizeB2.x)
{
delta.x += sizeB2.x;
}
else
{
delta.x = 0;
}
if (delta.y > sizeB2.y)
{
delta.y -= sizeB2.y;
}
else if (delta.y < -sizeB2.y)
{
delta.y += sizeB2.y;
}
else
{
delta.y = 0;
}
// now calculate collision vector vs origin. (assume A is a circle, not ellipse)
if(delta.Length() < sizeA2.x)
{
delta.Normalize();
return delta;
}
return Vector3::ZERO;
}
const Vector3 mAdjustPosition; ///< Position Adjustment value
const Vector3 mAdjustSize; ///< Size Adjustment value
};
/**
* WobbleConstraint generates a decaying sinusoidial rotation.
* The result when applied to an Actor, is the Actor rotating left/right
* initially a large amount (deviation degrees, when wobble property is 0.0f)
* then eventually coming to a stop (once wobble property reaches 1.0f)
*/
struct WobbleConstraint
{
/**
* Wobble Constraint constructor
* Generates a sinusoidial rotation that starts with
* high amplitude (deviation), and then decays to zero over input 0.0f to 1.0f
*
* @param[in] deviation The max. deviation of wobble effect in degrees.
*/
WobbleConstraint(float deviation)
: mDeviation(Radian(Degree(deviation)))
{
}
/**
* @param[in] current The current rotation property (ignored)
* @param[in] propertyWobble The wobble property (value from 0.0f to 1.0f)
* @return The rotation (quaternion) is generated.
*/
Quaternion operator()(const Quaternion& current,
const PropertyInput& propertyWobble)
{
const float& wobble = propertyWobble.GetFloat();
float f = sinf(wobble * 10.0f) * (1.0f-wobble);
Quaternion q(mDeviation * f, Vector3::ZAXIS);
return q;
}
const float mDeviation; ///< Deviation factor in radians.
};
} // unnamed namespace
/**
* This example shows how to use PropertyNotifications
*/
class ExampleController : public ConnectionTracker
{
public:
/**
* Constructor
* @param application Application class, stored as reference
*/
ExampleController( Application& application )
: mApplication( application ),
mView()
{
// Connect to the Application's Init and orientation changed signal
mApplication.InitSignal().Connect(this, &ExampleController::Create);
}
/**
* This method gets called once the main loop of application is up and running
* @param[in] application Reference to the application instance
*/
void Create(Application& application)
{
Stage::GetCurrent().KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
// Creates a default view with a default tool bar.
// The view is added to the stage.
Toolkit::ToolBar toolBar;
mContentLayer = DemoHelper::CreateView( application,
mView,
toolBar,
BACKGROUND_IMAGE,
TOOLBAR_IMAGE,
APPLICATION_TITLE );
// Add an extra space on the right to center the title text.
toolBar.AddControl( Actor::New(), DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight );
// Create the content layer, which is where game actors appear.
AddContentLayer();
}
private:
/**
* Adds a new layer to the stage, containing game actors.
*/
void AddContentLayer()
{
Stage stage = Stage::GetCurrent();
const Vector3 stageSize(stage.GetSize());
// Ball setup
mBallStartPosition = stageSize * Vector3( BALL_START_POSITION );
mBall = CreateImage(BALL_IMAGE);
mBall.SetPosition( mBallStartPosition );
mBall.SetSize( BALL_SIZE * stageSize.width );
mContentLayer.Add(mBall);
mBallVelocity = Vector3::ZERO;
// Paddle setup
mPaddleHitMargin = Vector2(stageSize) * PADDLE_HIT_MARGIN;
mPaddle = Actor::New();
mPaddleHandle = CreateImage(PADDLE_HANDLE_IMAGE);
mPaddleImage = CreateImage(PADDLE_IMAGE);
mPaddle.Add( mPaddleHandle );
mPaddle.Add( mPaddleImage );
mPaddleHandle.SetParentOrigin( ParentOrigin::TOP_CENTER );
mPaddleHandle.SetAnchorPoint( AnchorPoint::TOP_CENTER );
mPaddleHandle.SetPosition( 0.0f, stageSize.width * 0.0125f );
mPaddleImage.SetParentOrigin( ParentOrigin::TOP_CENTER );
mPaddleImage.SetAnchorPoint( AnchorPoint::TOP_CENTER );
mPaddle.SetParentOrigin( ParentOrigin::TOP_LEFT );
mPaddle.SetAnchorPoint( AnchorPoint::CENTER );
mPaddleFullSize = PADDLE_SIZE * stageSize.width;
mPaddle.SetSize( mPaddleFullSize + mPaddleHitMargin );
mPaddleHandle.SetSize( PADDLE_HANDLE_SIZE * stageSize.width );
mPaddleImage.SetSize( mPaddleFullSize );
mWobbleProperty = mPaddle.RegisterProperty(WOBBLE_PROPERTY_NAME, 0.0f);
Constraint wobbleConstraint = Constraint::New<Quaternion>( Actor::Property::Rotation,
LocalSource(mWobbleProperty),
WobbleConstraint(10.0f));
mPaddle.ApplyConstraint(wobbleConstraint);
mPaddle.SetPosition( stageSize * Vector3( PADDLE_START_POSITION ) );
mContentLayer.Add(mPaddle);
mPaddle.TouchedSignal().Connect(this, &ExampleController::OnTouchPaddle);
mContentLayer.TouchedSignal().Connect(this, &ExampleController::OnTouchLayer);
const float margin(BALL_SIZE.width * stageSize.width * 0.5f);
// Set up notifications for ball's collisions against walls.
PropertyNotification leftNotification = mBall.AddPropertyNotification( Actor::Property::PositionX, LessThanCondition(margin) );
leftNotification.NotifySignal().Connect( this, &ExampleController::OnHitLeftWall );
PropertyNotification rightNotification = mBall.AddPropertyNotification( Actor::Property::PositionX, GreaterThanCondition(stageSize.width - margin) );
rightNotification.NotifySignal().Connect( this, &ExampleController::OnHitRightWall );
PropertyNotification topNotification = mBall.AddPropertyNotification( Actor::Property::PositionY, LessThanCondition(margin) );
topNotification.NotifySignal().Connect( this, &ExampleController::OnHitTopWall );
PropertyNotification bottomNotification = mBall.AddPropertyNotification( Actor::Property::PositionY, GreaterThanCondition(stageSize.height + margin) );
bottomNotification.NotifySignal().Connect( this, &ExampleController::OnHitBottomWall );
// Set up notification for ball colliding against paddle.
Actor delegate = Actor::New();
stage.Add(delegate);
Property::Index property = delegate.RegisterProperty(COLLISION_PROPERTY_NAME, Vector3::ZERO);
Constraint constraint = Constraint::New<Vector3>( property,
Source(mBall, Actor::Property::Position),
Source(mPaddle, Actor::Property::Position),
Source(mBall, Actor::Property::Size),
Source(mPaddle, Actor::Property::Size),
CollisionCircleRectangleConstraint( -Vector3(0.0f, mPaddleHitMargin.height * 0.575f, 0.0f),-Vector3(mPaddleHitMargin) ));
delegate.ApplyConstraint(constraint);
PropertyNotification paddleNotification = delegate.AddPropertyNotification( property, GreaterThanCondition(0.0f) );
paddleNotification.NotifySignal().Connect( this, &ExampleController::OnHitPaddle );
RestartGame();
}
/**
* Restarts Game
* Resets Lives count and other stats, and loads level
*/
void RestartGame()
{
mLives = TOTAL_LIVES;
mLevel = 0;
mBall.SetPosition( mBallStartPosition );
mBallVelocity = Vector3::ZERO;
mPaddle.SetSize( mPaddleFullSize + mPaddleHitMargin );
mPaddleImage.SetSize( mPaddleFullSize );
LoadLevel(mLevel);
}
/**
* Loads level
* All existing level content is removed, and new bricks
* are added.
* @param[in] level Level index to load.
*/
void LoadLevel(int level)
{
if(mLevelContainer && mLevelContainer.GetParent() == mContentLayer)
{
mContentLayer.Remove( mLevelContainer );
}
mLevelContainer = Actor::New();
mLevelContainer.SetAnchorPoint( AnchorPoint::CENTER );
mLevelContainer.SetParentOrigin( ParentOrigin::CENTER );
mLevelContainer.SetSizeMode( SIZE_EQUAL_TO_PARENT );
mContentLayer.Add( mLevelContainer );
mBrickCount = 0;
switch(level%TOTAL_LEVELS)
{
case 0:
{
GenerateLevel0();
break;
}
case 1:
{
GenerateLevel1();
break;
}
case 2:
{
GenerateLevel2();
break;
}
default:
{
break;
}
} // end switch
}
/**
* Generates level 0
*/
void GenerateLevel0()
{
Vector2 stageSize(Stage::GetCurrent().GetSize());
const Vector2 brickSize(BRICK_SIZE * stageSize.width);
const int columns = (0.85f * stageSize.width) / brickSize.width; // 85 percent of the width of the screen covered with bricks.
const int rows = (0.3f * stageSize.height) / brickSize.height; // 30 percent of the height of the screen covered with bricks.
const Vector2 offset( (stageSize.x - (columns * brickSize.width)) * 0.5f,
stageSize.y * 0.125f );
for(int j = 0; j < rows; j++)
{
for(int i = 0; i < columns; i++)
{
Actor brick = CreateBrick(Vector2(i * brickSize.width + offset.x, j * brickSize.height + offset.y) + (brickSize * 0.5f), j % TOTAL_BRICKS );
mLevelContainer.Add(brick);
mBrickCount++;
}
}
}
/**
* Generates level 1
*/
void GenerateLevel1()
{
Vector2 stageSize(Stage::GetCurrent().GetSize());
const Vector2 brickSize(BRICK_SIZE * stageSize.width);
const int columns = (0.85f * stageSize.width) / brickSize.width; // 85 percent of the width of the screen covered with bricks.
const int rows = (0.3f * stageSize.height) / brickSize.height; // 30 percent of the height of the screen covered with bricks.
const Vector2 offset( (stageSize.x - (columns * brickSize.width)) * 0.5f,
stageSize.y * 0.125f );
for(int j = 0; j < rows; j++)
{
for(int i = 0; i < columns; i++)
{
int i2 = columns - i - 1;
int j2 = rows - j - 1;
int brickIndex = std::min( std::min(i, j), std::min(i2, j2) ) % TOTAL_BRICKS;
Actor brick = CreateBrick(Vector2(i * brickSize.width + offset.x, j * brickSize.height + offset.y) + (brickSize * 0.5f), brickIndex );
mLevelContainer.Add(brick);
mBrickCount++;
}
}
}
/**
* Generates level 2
*/
void GenerateLevel2()
{
Vector2 stageSize(Stage::GetCurrent().GetSize());
const Vector2 brickSize(BRICK_SIZE * stageSize.width);
const int columns = (0.85f * stageSize.width) / brickSize.width; // 85 percent of the width of the screen covered with bricks.
const int rows = (0.3f * stageSize.height) / brickSize.height; // 30 percent of the height of the screen covered with bricks.
const Vector2 offset( (stageSize.x - (columns * brickSize.width)) * 0.5f,
stageSize.y * 0.125f );
// lays down bricks in a spiral formation starting at i,j = (0,0) top left corner
// travelling right di,dj = (1,0) initially
int i = 0;
int j = 0;
int di = 1;
int dj = 0;
// contracting boundaries
int left = 0;
int right = columns - 1;
int top = 2;
int bottom = rows - 1;
// length of current line. we stop laying down bricks when the length is 1 brick or less.
int length = 0;
while(true)
{
Actor brick = CreateBrick(Vector2(i * brickSize.width + offset.x, j * brickSize.height + offset.y) + (brickSize * 0.5f), 0 );
mLevelContainer.Add(brick);
i+=di;
j+=dj;
bool turn(false);
if((i==right) && (di==1))
{
right -= 2;
turn = true;
}
if((j==bottom) && (dj==1))
{
bottom -= 2;
turn = true;
}
if((i==left) && (di==-1))
{
left += 2;
turn = true;
}
if((j==top) && (dj==-1))
{
top += 2;
turn = true;
}
if(turn)
{
// turn 90 degrees clockwise.
std::swap(di, dj);
di = -di;
if (length<=1)
{
break;
}
length = 0;
}
length++;
mBrickCount++;
}
}
/**
* Creates a brick at a specified position on the stage
* @param[in] position the position for the brick
* @param[in] type the type of brick
* @return The Brick Actor is returned.
*/
Actor CreateBrick( const Vector2& position, int type )
{
Vector2 stageSize(Stage::GetCurrent().GetSize());
const Vector2 brickSize(BRICK_SIZE * Vector2(stageSize.x, stageSize.x));
ImageAttributes attr;
attr.SetSize( 128, 64 );
attr.SetScalingMode( ImageAttributes::ScaleToFill );
Image img = ResourceImage::New(BRICK_IMAGE_PATH[type], attr);
ImageActor brick = ImageActor::New(img);
brick.SetParentOrigin(ParentOrigin::TOP_LEFT);
brick.SetAnchorPoint(AnchorPoint::CENTER);
brick.SetSize( brickSize );
brick.SetPosition( Vector3( position ) );
// Add a constraint on the brick between it and the ball generating a collision-property
Property::Index property = brick.RegisterProperty(COLLISION_PROPERTY_NAME, Vector3::ZERO);
Constraint constraint = Constraint::New<Vector3>( property,
Source(mBall, Actor::Property::Position),
Source(brick, Actor::Property::Position),
Source(mBall, Actor::Property::Size),
Source(brick, Actor::Property::Size),
CollisionCircleRectangleConstraint(BRICK_COLLISION_MARGIN));
brick.ApplyConstraint(constraint);
// Now add a notification on this collision-property
PropertyNotification brickNotification = brick.AddPropertyNotification( property, GreaterThanCondition(0.0f) );
brickNotification.NotifySignal().Connect( this, &ExampleController::OnHitBrick );
return brick;
}
/**
* Creates an Image (Helper)
*
* @param[in] filename the path of the image.
*/
ImageActor CreateImage(const std::string& filename)
{
Image img = ResourceImage::New(filename);
ImageActor actor = ImageActor::New(img);
actor.SetParentOrigin(ParentOrigin::TOP_LEFT);
actor.SetAnchorPoint(AnchorPoint::CENTER);
return actor;
}
/**
* Continue animation (based on current velocity)
*/
void ContinueAnimation()
{
if(mBallAnimation)
{
mBallAnimation.Clear();
}
mBallAnimation = Animation::New(MAX_ANIMATION_DURATION);
mBallAnimation.AnimateBy( Property( mBall, Actor::Property::Position ), mBallVelocity * MAX_ANIMATION_DURATION);
mBallAnimation.Play();
}
/**
* Signal invoked whenever user touches the Paddle.
* @param[in] actor The actor touched
* @param[in] event The touch event
*/
bool OnTouchPaddle(Actor actor, const TouchEvent& event)
{
if(event.GetPointCount()>0)
{
const TouchPoint& point = event.GetPoint(0);
if(point.state==TouchPoint::Down) // Commence dragging
{
// Get point where user touched paddle (relative to paddle's center)
mRelativeDragPoint = Vector3(point.screen.x, point.screen.y, 0.0f);
mRelativeDragPoint -= actor.GetCurrentPosition();
mDragActor = actor;
mDragAnimation = Animation::New(0.25f);
mDragAnimation.AnimateTo( Property(mDragActor, Actor::Property::Scale), Vector3(1.1f, 1.1f, 1.0f), AlphaFunctions::EaseOut);
mDragAnimation.AnimateTo( Property(mPaddleHandle, Actor::Property::Color), Vector4(1.0f, 1.0f, 1.0f, 0.0f), AlphaFunctions::EaseOut);
mDragAnimation.Play();
}
}
return false;
}
/**
* Signal invoked whenever user touches anywhere on the screen.
* @param[in] actor The actor touched
* @param[in] event The touch event
*/
bool OnTouchLayer(Actor actor, const TouchEvent& event)
{
if(event.GetPointCount()>0)
{
const TouchPoint& point = event.GetPoint(0);
if(mDragActor)
{
Vector3 position(point.screen.x, point.screen.y, 0.0f);
mPaddle.SetPosition( position - mRelativeDragPoint );
if(point.state==TouchPoint::Up) // Stop dragging
{
mDragAnimation = Animation::New(0.25f);
mDragAnimation.AnimateTo( Property(mDragActor, Actor::Property::Scale), Vector3(1.0f, 1.0f, 1.0f), AlphaFunctions::EaseIn);
mDragAnimation.AnimateTo( Property(mPaddleHandle, Actor::Property::Color), Vector4(1.0f, 1.0f, 1.0f, 1.0f), AlphaFunctions::EaseOut);
mDragAnimation.Play();
mDragActor.Reset();
}
}
}
return false;
}
/**
* Notification: Ball hit left wall
* @param source The notification
*/
void OnHitLeftWall(PropertyNotification& source)
{
mBallVelocity.x = fabsf(mBallVelocity.x);
ContinueAnimation();
}
/**
* Notification: Ball hit right wall
* @param source The notification
*/
void OnHitRightWall(PropertyNotification& source)
{
mBallVelocity.x = -fabsf(mBallVelocity.x);
ContinueAnimation();
}
/**
* Notification: Ball hit top wall
* @param source The notification
*/
void OnHitTopWall(PropertyNotification& source)
{
mBallVelocity.y = fabsf(mBallVelocity.y);
ContinueAnimation();
}
/**
* Notification: Ball hit bottom wall
* @param source The notification
*/
void OnHitBottomWall(PropertyNotification& source)
{
if(mBallAnimation)
{
mBallAnimation.Clear();
}
if(mLives>0)
{
mLives--;
const float f(static_cast<float>(mLives) / TOTAL_LIVES);
mBallVelocity = Vector3::ZERO;
Animation shrink = Animation::New(0.5f);
shrink.AnimateTo( Property(mPaddle, Actor::Property::SizeWidth), mPaddleFullSize.x * f + mPaddleHitMargin.x);
shrink.AnimateTo( Property(mPaddleImage, Actor::Property::SizeWidth), mPaddleFullSize.x * f );
shrink.FinishedSignal().Connect( this, &ExampleController::OnPaddleShrunk );
shrink.Play();
}
}
/**
* Paddle Shrink Animation complete.
* @param[in] source The animation responsible for shrinking the paddle.
*/
void OnPaddleShrunk( Animation &source )
{
// Reposition Ball in start position, and make ball appear.
mBall.SetPosition( mBallStartPosition );
mBall.SetColor( Vector4(1.0f, 1.0f, 1.0f, 0.1f) );
Animation appear = Animation::New(0.5f);
appear.AnimateTo( Property(mBall, Actor::Property::Color), Vector4(1.0f, 1.0f, 1.0f, 1.0f) );
appear.Play();
if(!mLives)
{
RestartGame();
}
}
/**
* Notification: Ball hit paddle
* @param source The notification
*/
void OnHitPaddle(PropertyNotification& source)
{
Actor delegate = Actor::DownCast(source.GetTarget());
Vector3 collisionVector = delegate.GetProperty<Vector3>(source.GetTargetProperty());
Vector3 ballRelativePosition(mBall.GetCurrentPosition() - mPaddle.GetCurrentPosition());
ballRelativePosition.Normalize();
collisionVector.x += ballRelativePosition.x * 0.5f;
if(mBallVelocity.LengthSquared() < Math::MACHINE_EPSILON_1)
{
mBallVelocity += collisionVector * BALL_VELOCITY;
}
else
{
const float normalVelocity = fabsf(mBallVelocity.Dot(collisionVector));
mBallVelocity += collisionVector * normalVelocity * 2.0f;
const float currentSpeed = mBallVelocity.Length();
const float limitedSpeed = std::min( currentSpeed, MAX_VELOCITY );
mBallVelocity = mBallVelocity * limitedSpeed / currentSpeed;
}
ContinueAnimation();
// wobble paddle
mWobbleAnimation = Animation::New(0.5f);
mWobbleAnimation.AnimateTo( Property( mPaddle, mWobbleProperty ), 1.0f );
mWobbleAnimation.Play();
mPaddle.SetProperty(mWobbleProperty, 0.0f);
}
/**
* Notification: Ball hit brick
* @param source The notification
*/
void OnHitBrick(PropertyNotification& source)
{
Actor brick = Actor::DownCast(source.GetTarget());
Vector3 collisionVector = brick.GetProperty<Vector3>(source.GetTargetProperty());
const float normalVelocity = fabsf(mBallVelocity.Dot(collisionVector));
mBallVelocity += collisionVector * normalVelocity * 2.0f;
const float currentSpeed = mBallVelocity.Length();
const float limitedSpeed = std::min( currentSpeed, MAX_VELOCITY );
mBallVelocity = mBallVelocity * limitedSpeed / currentSpeed;
ContinueAnimation();
// remove collision-constraint and notification.
brick.RemovePropertyNotification(source);
brick.RemoveConstraints();
// fade brick (destroy)
Animation destroyAnimation = Animation::New(0.5f);
destroyAnimation.AnimateTo( Property( brick, Actor::Property::ColorAlpha ), 0.0f, AlphaFunctions::EaseIn );
destroyAnimation.Play();
destroyAnimation.FinishedSignal().Connect( this, &ExampleController::OnBrickDestroyed );
mDestroyAnimationMap[destroyAnimation] = brick;
}
/**
* Brick Destruction Animation complete.
* @param[in] source The animation responsible for destroying the brick
*/
void OnBrickDestroyed( Animation& source )
{
// Remove brick from stage, it's constraint and property notification should also remove themselves.
Actor brick = mDestroyAnimationMap[source];
mDestroyAnimationMap.erase(source);
brick.GetParent().Remove(brick);
mBrickCount--;
if(!mBrickCount)
{
mLevel++;
LoadLevel(mLevel);
}
}
/**
* Main key event handler
*/
void OnKeyEvent(const KeyEvent& event)
{
if(event.state == KeyEvent::Down)
{
if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
{
mApplication.Quit();
}
}
}
private:
Application& mApplication; ///< Application instance
Toolkit::View mView; ///< The View instance.
Layer mContentLayer; ///< The content layer (contains game actors)
ImageActor mBall; ///< The Moving ball image.
Vector3 mBallStartPosition; ///< Ball Start position
Vector3 mBallVelocity; ///< Ball's current direction.
Animation mBallAnimation; ///< Ball's animation
Actor mPaddle; ///< The paddle including hit area.
ImageActor mPaddleImage; ///< The paddle's image.
ImageActor mPaddleHandle; ///< The paddle's handle (where the user touches)
Vector2 mPaddleHitMargin; ///< The paddle hit margin.
Animation mWobbleAnimation; ///< Paddle's animation when hit (wobbles)
Property::Index mWobbleProperty; ///< The wobble property (generated from animation)
Actor mLevelContainer; ///< The level container (contains bricks)
// actor - dragging functionality
Animation mDragAnimation; ///< Animation for dragging. (grows - affects ACTOR::SCALE)
Actor mDragActor; ///< The actor which is being dragged (if any)
Vector3 mRelativeDragPoint; ///< The point the user touched, relative to the actor.
std::map<Animation, Actor> mDestroyAnimationMap; ///< Keep track of which actors are to be destroyed.
Vector2 mPaddleFullSize; ///< Initial 100% size of the paddle.
int mLevel; ///< Current level
int mLives; ///< Total lives.
int mBrickCount; ///< Total bricks on screen.
};
void RunTest(Application& app)
{
ExampleController test(app);
app.MainLoop();
}
int main(int argc, char **argv)
{
Application app = Application::New(&argc, &argv);
RunTest(app);
return 0;
}