PuzzleSDK
TileMapTest2.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25#include "TileMapTest2.h"
26#include "../testResource.h"
27
28#include "2d/CCFastTMXLayer.h"
29#include "2d/CCFastTMXTiledMap.h"
30
32
33enum
34{
36};
37
38FastTileMapTests::FastTileMapTests()
39{
69}
70
72{
73 // fix bug #486, #419.
74 // "test" is the default value in Director::setGLDefaultValues()
75 // but TransitionTest may setDepthTest(false), we should revert it here
76 Director::getInstance()->getRenderer()->setDepthTest(true);
77 Director::getInstance()->getRenderer()->setDepthWrite(true);
78
79 auto listener = EventListenerTouchAllAtOnce::create();
80 listener->onTouchesMoved = CC_CALLBACK_2(TileDemoNew::onTouchesMoved, this);
81 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
82}
83
85{
86}
87
88std::string TileDemoNew::title() const
89{
90 return "No title";
91}
92
93std::string TileDemoNew::subtitle() const
94{
95 return "drag the screen";
96}
97
99{
100 TestCase::onExit();
101 Director::getInstance()->getRenderer()->setDepthTest(false);
102 Director::getInstance()->getRenderer()->setDepthWrite(false);
103}
104
105void TileDemoNew::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
106{
107 auto touch = touches[0];
108
109 auto diff = touch->getDelta();
110 auto node = getChildByTag(kTagTileMap);
111 auto currentPos = node->getPosition();
112 node->setPosition(currentPos + diff);
113}
114
115//------------------------------------------------------------------
116//
117// TileMapTestNew
118//
119//------------------------------------------------------------------
121{
122 auto map = TileMapAtlas::create(s_TilesPng, s_LevelMapTga, 16, 16);
123 // Convert it to "alias" (GL_LINEAR filtering)
124 map->getTexture()->setAntiAliasTexParameters();
125
126 Size CC_UNUSED s = map->getContentSize();
127 CCLOG("ContentSize: %f, %f", s.width,s.height);
128
129 // If you are not going to use the Map, you can free it now
130 // NEW since v0.7
131 map->releaseMap();
132
133 addChild(map, 0, kTagTileMap);
134
135 map->setAnchorPoint( Vec2(0.0f, 0.5f) );
136
137 auto scale = ScaleBy::create(4, 0.8f);
138 auto scaleBack = scale->reverse();
139
140 auto seq = Sequence::create(scale, scaleBack, nullptr);
141
142 map->runAction(RepeatForever::create(seq));
143}
144
145std::string TileMapTestNew::title() const
146{
147 return "TileMapAtlas";
148}
149
150//------------------------------------------------------------------
151//
152// TileMapEditTestNew
153//
154//------------------------------------------------------------------
156{
157 auto map = TileMapAtlas::create(s_TilesPng, s_LevelMapTga, 16, 16);
158 // Create an Aliased Atlas
159 map->getTexture()->setAliasTexParameters();
160
161 Size CC_UNUSED s = map->getContentSize();
162 CCLOG("ContentSize: %f, %f", s.width,s.height);
163
164 // If you are not going to use the Map, you can free it now
165 // [tilemap releaseMap);
166 // And if you are going to use, it you can access the data with:
167 schedule(CC_SCHEDULE_SELECTOR(TileMapEditTestNew::updateMap), 0.2f);
168
169 addChild(map, 0, kTagTileMap);
170
171 map->setAnchorPoint( Vec2(0, 0) );
172 map->setPosition( Vec2(-20,-200) );
173}
174
176{
177 // IMPORTANT
178 // The only limitation is that you cannot change an empty, or assign an empty tile to a tile
179 // The value 0 not rendered so don't assign or change a tile with value 0
180
181 auto tilemap = (TileMapAtlas*) getChildByTag(kTagTileMap);
182
183 //
184 // For example you can iterate over all the tiles
185 // using this code, but try to avoid the iteration
186 // over all your tiles in every frame. It's very expensive
187 // for(int x=0; x < tilemap.tgaInfo->width; x++) {
188 // for(int y=0; y < tilemap.tgaInfo->height; y++) {
189 // Color3B c =[tilemap tileAt:Size(x,y));
190 // if( c.r != 0 ) {
191 // ////----CCLOG("%d,%d = %d", x,y,c.r);
192 // }
193 // }
194 // }
195
196 // NEW since v0.7
197 Color3B c = tilemap->getTileAt(Vec2(13,21));
198 c.r++;
199 c.r %= 50;
200 if( c.r==0)
201 c.r=1;
202
203 // NEW since v0.7
204 tilemap->setTile(c, Vec2(13,21) );
205}
206
207std::string TileMapEditTestNew::title() const
208{
209 return "Editable TileMapAtlas";
210}
211
212//------------------------------------------------------------------
213//
214// TMXOrthoTestNew
215//
216//------------------------------------------------------------------
218{
219 //
220 // Test orthogonal with 3d camera and anti-alias textures
221 //
222 // it should not flicker. No artifacts should appear
223 //
224 //auto color = LayerColor::create( Color4B(64,64,64,255) );
225 //addChild(color, -1);
226
227 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/orthogonal-test2.tmx");
228
229 addChild(map, 0, kTagTileMap);
230
231 Size CC_UNUSED s = map->getContentSize();
232 CCLOG("ContentSize: %f, %f", s.width,s.height);
233
234 auto scale = ScaleBy::create(10, 0.1f);
235 auto back = scale->reverse();
236 auto seq = Sequence::create(scale, back, nullptr);
237 auto repeat = RepeatForever::create(seq);
238 map->runAction(repeat);
239
240// float x, y, z;
241// map->getCamera()->getEye(&x, &y, &z);
242// map->getCamera()->setEye(x-200, y, z+300);
243}
244
246{
248
249 Director::getInstance()->setProjection(Director::Projection::_3D);
250}
251
253{
254 Director::getInstance()->setProjection(Director::Projection::DEFAULT);
256}
257
258std::string TMXOrthoTestNew::title() const
259{
260 return "TMX Orthogonal test";
261}
262
263//------------------------------------------------------------------
264//
265// TMXOrthoTest2New
266//
267//------------------------------------------------------------------
269{
270 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/orthogonal-test1.tmx");
271 addChild(map, 0, kTagTileMap);
272
273 Size CC_UNUSED s = map->getContentSize();
274 CCLOG("ContentSize: %f, %f", s.width,s.height);
275
276 map->runAction( ScaleBy::create(2, 0.5f) ) ;
277}
278
279std::string TMXOrthoTest2New::title() const
280{
281 return "TMX Ortho test2";
282}
283
284//------------------------------------------------------------------
285//
286// TMXOrthoTest3New
287//
288//------------------------------------------------------------------
290{
291 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/orthogonal-test3.tmx");
292 addChild(map, 0, kTagTileMap);
293
294 Size CC_UNUSED s = map->getContentSize();
295 CCLOG("ContentSize: %f, %f", s.width,s.height);
296
297 map->setScale(0.2f);
298 map->setAnchorPoint( Vec2(0.5f, 0.5f) );
299}
300
301std::string TMXOrthoTest3New::title() const
302{
303 return "TMX anchorPoint test";
304}
305
306//------------------------------------------------------------------
307//
308// TMXOrthoTest4New
309//
310//------------------------------------------------------------------
312{
313 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/orthogonal-test4.tmx");
314 addChild(map, 0, kTagTileMap);
315
316 Size CC_UNUSED s1 = map->getContentSize();
317 CCLOG("ContentSize: %f, %f", s1.width,s1.height);
318
319 map->setAnchorPoint(Vec2(0.0f, 0.0f));
320
321 auto layer = map->getLayer("Layer 0");
322 auto s = layer->getLayerSize();
323
324 Sprite* sprite;
325 sprite = layer->getTileAt(Vec2(0.0f,0.0f));
326 sprite->setScale(2);
327 sprite = layer->getTileAt(Vec2(s.width-1,0.0f));
328 sprite->setScale(2);
329 sprite = layer->getTileAt(Vec2(0.0f,s.height-1));
330 sprite->setScale(2);
331 sprite = layer->getTileAt(Vec2(s.width-1,s.height-1));
332 sprite->setScale(2);
333
334 schedule( CC_SCHEDULE_SELECTOR(TMXOrthoTest4New::removeSprite), 2 );
335
336}
337
339{
340 unschedule(CC_SCHEDULE_SELECTOR(TMXOrthoTest4New::removeSprite));
341
342 auto map = static_cast<cocos2d::FastTMXTiledMap*>( getChildByTag(kTagTileMap) );
343 auto layer = map->getLayer("Layer 0");
344 auto s = layer->getLayerSize();
345
346 auto sprite = layer->getTileAt( Vec2(s.width-1,0.0f) );
347 auto sprite2 = layer->getTileAt(Vec2(s.width-1, s.height-1));
348 layer->removeChild(sprite, true);
349 auto sprite3 = layer->getTileAt(Vec2(2.0f, s.height-1));
350 layer->removeChild(sprite3, true);
351 layer->removeChild(sprite2, true);
352}
353
354std::string TMXOrthoTest4New::title() const
355{
356 return "TMX width/height test";
357}
358
359//------------------------------------------------------------------
360//
361// TMXReadWriteTestNew
362//
363//------------------------------------------------------------------
364enum
365{
370
372{
373 _gid = 0;
374
375 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/orthogonal-test2.tmx");
376 addChild(map, 0, kTagTileMap);
377
378 Size CC_UNUSED s = map->getContentSize();
379 CCLOG("ContentSize: %f, %f", s.width,s.height);
380
381
382 auto layer = map->getLayer("Layer 0");
383
384 map->setScale( 1 );
385
386 auto tile0 = layer->getTileAt(Vec2(1,63));
387 auto tile1 = layer->getTileAt(Vec2(2,63));
388 auto tile2 = layer->getTileAt(Vec2(3,62));//Vec2(1,62));
389 auto tile3 = layer->getTileAt(Vec2(2,62));
390 tile0->setAnchorPoint( Vec2(0.5f, 0.5f) );
391 tile1->setAnchorPoint( Vec2(0.5f, 0.5f) );
392 tile2->setAnchorPoint( Vec2(0.5f, 0.5f) );
393 tile3->setAnchorPoint( Vec2(0.5f, 0.5f) );
394
395 auto move = MoveBy::create(0.5f, Vec2(0.0f,160.0f));
396 auto rotate = RotateBy::create(2, 360);
397 auto scale = ScaleBy::create(2, 5);
398 auto opacity = FadeOut::create(2);
399 auto fadein = FadeIn::create(2);
400 auto scaleback = ScaleTo::create(1, 1);
401 auto finish = CallFuncN::create(CC_CALLBACK_1(TMXReadWriteTestNew::removeSprite, this));
402 auto seq0 = Sequence::create(move, rotate, scale, opacity, fadein, scaleback, finish, nullptr);
403 auto seq1 = seq0->clone();
404 auto seq2 = seq0->clone();
405 auto seq3 = seq0->clone();
406
407 tile0->runAction(seq0);
408 tile1->runAction(seq1);
409 tile2->runAction(seq2);
410 tile3->runAction(seq3);
411
412
413 _gid = layer->getTileGIDAt(Vec2(0,63));
415
416 schedule(CC_SCHEDULE_SELECTOR(TMXReadWriteTestNew::updateCol), 2.0f);
417 schedule(CC_SCHEDULE_SELECTOR(TMXReadWriteTestNew::repaintWithGID), 2.05f);
418 schedule(CC_SCHEDULE_SELECTOR(TMXReadWriteTestNew::removeTiles), 1.0f);
419
422
423 _gid2 = 0;
424}
425
427{
429 auto p = ((Node*)sender)->getParent();
430
431 if (p)
432 {
433 p->removeChild((Node*)sender, true);
434 }
435
437}
438
440{
441 auto map = (cocos2d::FastTMXTiledMap*)getChildByTag(kTagTileMap);
442 auto layer = (cocos2d::FastTMXLayer*)map->getChildByTag(0);
443
446
447
448 auto s = layer->getLayerSize();
449
450 for( int y=0; y< s.height; y++ )
451 {
452 layer->setTileGID(_gid2, Vec2((float)3, (float)y));
453 }
454
455 _gid2 = (_gid2 + 1) % 80;
456}
457
459{
460// unschedule:_cmd);
461
462 auto map = (cocos2d::FastTMXTiledMap*)getChildByTag(kTagTileMap);
463 auto layer = (cocos2d::FastTMXLayer*)map->getChildByTag(0);
464
465 auto s = layer->getLayerSize();
466 for( int x=0; x<s.width;x++)
467 {
468 int y = (int)s.height-1;
469 unsigned int tmpgid = layer->getTileGIDAt( Vec2((float)x, (float)y) );
470 layer->setTileGID(tmpgid+1, Vec2((float)x, (float)y));
471 }
472}
473
475{
476 unschedule(CC_SCHEDULE_SELECTOR(TMXReadWriteTestNew::removeTiles));
477
478 auto map = (cocos2d::FastTMXTiledMap*)getChildByTag(kTagTileMap);
479 auto layer = (cocos2d::FastTMXLayer*)map->getChildByTag(0);
480 auto s = layer->getLayerSize();
481
482 for( int y=0; y< s.height; y++ )
483 {
484 layer->removeTileAt( Vec2(5.0, (float)y) );
485 }
486}
487
488
489
490std::string TMXReadWriteTestNew::title() const
491{
492 return "TMX Read/Write test";
493}
494
495//------------------------------------------------------------------
496//
497// TMXHexTestNew
498//
499//------------------------------------------------------------------
501{
502 auto color = LayerColor::create( Color4B(64,64,64,255) );
503 addChild(color, -1);
504
505 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/hexa-test.tmx");
506 addChild(map, 0, kTagTileMap);
507
508 Size CC_UNUSED s = map->getContentSize();
509 CCLOG("ContentSize: %f, %f", s.width,s.height);
510}
511
512std::string TMXHexTestNew::title() const
513{
514 return "TMX Hex tes";
515}
516
517//------------------------------------------------------------------
518//
519// TMXIsoTestNew
520//
521//------------------------------------------------------------------
523{
524 auto color = LayerColor::create( Color4B(64,64,64,255) );
525 addChild(color, -1);
526
527 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/iso-test.tmx");
528 addChild(map, 0, kTagTileMap);
529
530 // move map to the center of the screen
531 auto ms = map->getMapSize();
532 auto ts = map->getTileSize();
533 map->runAction( MoveTo::create(1.0f, Vec2( -ms.width * ts.width/2, -ms.height * ts.height/2 )) );
534}
535
536std::string TMXIsoTestNew::title() const
537{
538 return "TMX Isometric test 0";
539}
540
541//------------------------------------------------------------------
542//
543// TMXIsoTest1New
544//
545//------------------------------------------------------------------
547{
548 auto color = LayerColor::create( Color4B(64,64,64,255) );
549 addChild(color, -1);
550
551 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/iso-test1.tmx");
552 addChild(map, 0, kTagTileMap);
553
554 Size CC_UNUSED s = map->getContentSize();
555 CCLOG("ContentSize: %f, %f", s.width,s.height);
556
557 map->setAnchorPoint(Vec2(0.5f, 0.5f));
558}
559
560std::string TMXIsoTest1New::title() const
561{
562 return "TMX Isometric test + anchorPoint";
563}
564
565//------------------------------------------------------------------
566//
567// TMXIsoTest2New
568//
569//------------------------------------------------------------------
571{
572 auto color = LayerColor::create( Color4B(64,64,64,255) );
573 addChild(color, -1);
574
575 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/iso-test2.tmx");
576 addChild(map, 0, kTagTileMap);
577
578 Size CC_UNUSED s = map->getContentSize();
579 CCLOG("ContentSize: %f, %f", s.width,s.height);
580
581 // move map to the center of the screen
582 auto ms = map->getMapSize();
583 auto ts = map->getTileSize();
584 map->runAction( MoveTo::create(1.0f, Vec2( -ms.width * ts.width/2, -ms.height * ts.height/2 ) ));
585}
586
587std::string TMXIsoTest2New::title() const
588{
589 return "TMX Isometric test 2";
590}
591
592//------------------------------------------------------------------
593//
594// TMXUncompressedTestNew
595//
596//------------------------------------------------------------------
598{
599 auto color = LayerColor::create( Color4B(64,64,64,255) );
600 addChild(color, -1);
601
602 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/iso-test2-uncompressed.tmx");
603 addChild(map, 0, kTagTileMap);
604
605 Size CC_UNUSED s = map->getContentSize();
606 CCLOG("ContentSize: %f, %f", s.width,s.height);
607
608 // move map to the center of the screen
609 auto ms = map->getMapSize();
610 auto ts = map->getTileSize();
611 map->runAction(MoveTo::create(1.0f, Vec2( -ms.width * ts.width/2, -ms.height * ts.height/2 ) ));
612
613 //unsupported
614// // testing release map
615// TMXLayer* layer;
616//
617// auto& children = map->getChildren();
618// for(const auto &node : children) {
619// layer= static_cast<TMXLayer*>(node);
620// layer->releaseMap();
621// }
622
623}
624
626{
627 return "TMX Uncompressed test";
628}
629
630//------------------------------------------------------------------
631//
632// TMXTilesetTestNew
633//
634//------------------------------------------------------------------
636{
637 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/orthogonal-test5.tmx");
638 addChild(map, 0, kTagTileMap);
639
640 Size CC_UNUSED s = map->getContentSize();
641 CCLOG("ContentSize: %f, %f", s.width,s.height);
642}
643
644std::string TMXTilesetTestNew::title() const
645{
646 return "TMX Tileset test";
647}
648
649//------------------------------------------------------------------
650//
651// TMXOrthoObjectsTestNew
652//
653//------------------------------------------------------------------
655{
656 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/ortho-objects.tmx");
657 addChild(map, -1, kTagTileMap);
658
659 Size CC_UNUSED s = map->getContentSize();
660 CCLOG("ContentSize: %f, %f", s.width,s.height);
661
662 auto group = map->getObjectGroup("Object Group 1");
663 auto& objects = group->getObjects();
664
665 Value objectsVal = Value(objects);
666 CCLOG("%s", objectsVal.getDescription().c_str());
667
668 auto drawNode = DrawNode::create();
669 Color4F color(1.0, 1.0, 1.0, 1.0);
670 for (auto& obj : objects)
671 {
672 ValueMap& dict = obj.asValueMap();
673
674 float x = dict["x"].asFloat();
675 float y = dict["y"].asFloat();
676 float width = dict["width"].asFloat();
677 float height = dict["height"].asFloat();
678
679 drawNode->drawLine(Vec2(x, y), Vec2(x + width, y), color);
680 drawNode->drawLine(Vec2(x + width, y), Vec2(x + width, y + height), color);
681 drawNode->drawLine(Vec2(x + width,y + height), Vec2(x,y + height), color);
682 drawNode->drawLine(Vec2(x,y + height), Vec2(x,y), color);
683 }
684 map->addChild(drawNode);
685}
686
688{
689 return "TMX Ortho object test";
690}
691
693{
694 return "You should see a white box around the 3 platforms";
695}
696
697
698//------------------------------------------------------------------
699//
700// TMXIsoObjectsTestNew
701//
702//------------------------------------------------------------------
703
705{
706 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/iso-test-objectgroup.tmx");
707 addChild(map, -1, kTagTileMap);
708
709 Size CC_UNUSED s = map->getContentSize();
710 CCLOG("ContentSize: %f, %f", s.width,s.height);
711
712 auto group = map->getObjectGroup("Object Group 1");
713
714 auto& objects = group->getObjects();
715
716 Value objectsVal = Value(objects);
717 CCLOG("%s", objectsVal.getDescription().c_str());
718
719 auto drawNode = DrawNode::create();
720 Color4F color(1.0, 1.0, 1.0, 1.0);
721 for (auto& obj : objects)
722 {
723 ValueMap& dict = obj.asValueMap();
724
725 float x = dict["x"].asFloat();
726 float y = dict["y"].asFloat();
727 float width = dict["width"].asFloat();
728 float height = dict["height"].asFloat();
729
730 drawNode->drawLine(Vec2(x, y), Vec2(x + width, y), color);
731 drawNode->drawLine(Vec2(x + width, y), Vec2(x + width, y + height), color);
732 drawNode->drawLine(Vec2(x + width,y + height), Vec2(x,y + height), color);
733 drawNode->drawLine(Vec2(x,y + height), Vec2(x,y), color);
734 }
735 map->addChild(drawNode, 10);
736}
737
739{
740 return "TMX Iso object test";
741}
742
744{
745 return "You need to parse them manually. See bug #810";
746}
747
748
749//------------------------------------------------------------------
750//
751// TMXResizeTestNew
752//
753//------------------------------------------------------------------
754
756{
757 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/orthogonal-test5.tmx");
758 addChild(map, 0, kTagTileMap);
759
760 Size CC_UNUSED s = map->getContentSize();
761 CCLOG("ContentSize: %f, %f", s.width,s.height);
762
763 cocos2d::FastTMXLayer* layer;
764 layer = map->getLayer("Layer 0");
765
766 auto ls = layer->getLayerSize();
767 for (unsigned int y = 0; y < ls.height; y++)
768 {
769 for (unsigned int x = 0; x < ls.width; x++)
770 {
771 layer->setTileGID(1, Vec2((float)x, (float)y ) );
772 }
773 }
774}
775
776std::string TMXResizeTestNew::title() const
777{
778 return "TMX resize test";
779}
780
781std::string TMXResizeTestNew::subtitle() const
782{
783 return "Should not crash. Testing issue #740";
784}
785
786
787//------------------------------------------------------------------
788//
789// TMXIsoZorderNew
790//
791//------------------------------------------------------------------
793{
794 Director::getInstance()->getRenderer()->setDepthTest(false);
795 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/iso-test-zorder.tmx");
796 addChild(map, 0, kTagTileMap);
797
798 auto s = map->getContentSize();
799 CCLOG("ContentSize: %f, %f", s.width,s.height);
800 map->setPosition(Vec2(-s.width/2,0.0f));
801
802 _tamara = Sprite::create(s_pathSister1);
803 map->addChild(_tamara, (int)map->getChildren().size() );
804 _tamara->retain();
805 int mapWidth = map->getMapSize().width * map->getTileSize().width;
806 _tamara->setPosition(CC_POINT_PIXELS_TO_POINTS(Vec2( mapWidth/2.0f,0.0f)));
807 _tamara->setAnchorPoint(Vec2(0.5f,0.0f));
808
809
810 auto move = MoveBy::create(10, Vec2(300.0f,250.0f));
811 auto back = move->reverse();
812 auto seq = Sequence::create(move, back,nullptr);
813 _tamara->runAction( RepeatForever::create(seq) );
814
815 schedule( CC_SCHEDULE_SELECTOR(TMXIsoZorderNew::repositionSprite) );
816}
817
819{
820 _tamara->release();
821}
822
824{
825 unschedule(CC_SCHEDULE_SELECTOR(TMXIsoZorderNew::repositionSprite));
827}
828
830{
831 auto p = _tamara->getPosition();
832 p = CC_POINT_POINTS_TO_PIXELS(p);
833 auto map = getChildByTag(kTagTileMap);
834
835 // there are only 4 layers. (grass and 3 trees layers)
836 // if tamara < 48, z=4
837 // if tamara < 96, z=3
838 // if tamara < 144,z=2
839
840 int newZ = 4 - (p.y / 48);
841 newZ = std::max(newZ,0);
842
843 map->reorderChild(_tamara, newZ);
844}
845
846std::string TMXIsoZorderNew::title() const
847{
848 return "TMX Iso Zorder";
849}
850
851std::string TMXIsoZorderNew::subtitle() const
852{
853 return "Sprite should hide behind the trees";
854}
855
856
857//------------------------------------------------------------------
858//
859// TMXOrthoZorderNew
860//
861//------------------------------------------------------------------
863{
864 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/orthogonal-test-zorder.tmx");
865 addChild(map, 0, kTagTileMap);
866
867 Size CC_UNUSED s = map->getContentSize();
868 CCLOG("ContentSize: %f, %f", s.width,s.height);
869
870 _tamara = Sprite::create(s_pathSister1);
871 map->addChild(_tamara, (int)map->getChildren().size());
872 _tamara->retain();
873 _tamara->setAnchorPoint(Vec2(0.5f,0.0f));
874
875
876 auto move = MoveBy::create(10, Vec2(400.0f,450.0f));
877 auto back = move->reverse();
878 auto seq = Sequence::create(move, back,nullptr);
879 _tamara->runAction( RepeatForever::create(seq));
880
881 schedule( CC_SCHEDULE_SELECTOR(TMXOrthoZorderNew::repositionSprite));
882}
883
885{
886 _tamara->release();
887}
888
890{
891 auto p = _tamara->getPosition();
892 p = CC_POINT_POINTS_TO_PIXELS(p);
893 auto map = getChildByTag(kTagTileMap);
894
895 // there are only 4 layers. (grass and 3 trees layers)
896 // if tamara < 81, z=4
897 // if tamara < 162, z=3
898 // if tamara < 243,z=2
899
900 // -10: customization for this particular sample
901 int newZ = 4 - ( (p.y-10) / 81);
902 newZ = std::max(newZ,0);
903
904 map->reorderChild(_tamara, newZ);
905}
906
907std::string TMXOrthoZorderNew::title() const
908{
909 return "TMX Ortho Zorder";
910}
911
913{
914 return "Sprite should hide behind the trees";
915}
916
917
918//------------------------------------------------------------------
919//
920// TMXIsoVertexZNew
921//
922//------------------------------------------------------------------
924{
925 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/iso-test-vertexz.tmx");
926 addChild(map, 0, kTagTileMap);
927
928 auto s = map->getContentSize();
929 map->setPosition( Vec2(-s.width/2,0.0f) );
930 CCLOG("ContentSize: %f, %f", s.width,s.height);
931
932 // because I'm lazy, I'm reusing a tile as an sprite, but since this method uses vertexZ, you
933 // can use any Sprite and it will work OK.
934 auto layer = map->getLayer("Trees");
935 _tamara = layer->getTileAt( Vec2(29.0f,29.0f) );
936 _tamara->retain();
937
938 auto move = MoveBy::create(10, Vec2(300,250) * (1/CC_CONTENT_SCALE_FACTOR()));
939 auto back = move->reverse();
940 auto seq = Sequence::create(move, back,nullptr);
941 _tamara->runAction( RepeatForever::create(seq) );
942
943 schedule( CC_SCHEDULE_SELECTOR(TMXIsoVertexZNew::repositionSprite));
944
945}
946
948{
949 _tamara->release();
950}
951
953{
954 // tile height is 64x32
955 // map size: 30x30
956 auto p = _tamara->getPosition();
957 p = CC_POINT_POINTS_TO_PIXELS(p);
958 float newZ = -(p.y+32) /16;
959 _tamara->setPositionZ( newZ );
960}
961
963{
965
966 // TIP: 2d projection should be used
967 Director::getInstance()->setProjection(Director::Projection::_2D);
968 Director::getInstance()->getRenderer()->setDepthTest(true);
969 Director::getInstance()->getRenderer()->setDepthWrite(true);
970}
971
973{
974 // At exit use any other projection.
975 Director::getInstance()->setProjection(Director::Projection::DEFAULT);
976 Director::getInstance()->getRenderer()->setDepthTest(false);
977 Director::getInstance()->getRenderer()->setDepthWrite(false);
979}
980
981std::string TMXIsoVertexZNew::title() const
982{
983 return "TMX Iso VertexZ";
984}
985
986std::string TMXIsoVertexZNew::subtitle() const
987{
988 return "Doesn't support yet";
989}
990
991
992//------------------------------------------------------------------
993//
994// TMXOrthoVertexZNew
995//
996//------------------------------------------------------------------
998{
999 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/orthogonal-test-vertexz.tmx");
1000 addChild(map, 0, kTagTileMap);
1001
1002 Size CC_UNUSED s = map->getContentSize();
1003 CCLOG("ContentSize: %f, %f", s.width,s.height);
1004
1005 // because I'm lazy, I'm reusing a tile as an sprite, but since this method uses vertexZ, you
1006 // can use any Sprite and it will work OK.
1007 auto layer = map->getLayer("trees");
1008 _tamara = layer->getTileAt(Vec2(0,11));
1009 CCLOG("%p vertexZ: %f", _tamara, _tamara->getPositionZ());
1010 _tamara->retain();
1011
1012 auto move = MoveBy::create(10, Vec2(400,450) * (1/CC_CONTENT_SCALE_FACTOR()));
1013 auto back = move->reverse();
1014 auto seq = Sequence::create(move, back,nullptr);
1015 _tamara->runAction( RepeatForever::create(seq));
1016
1017 schedule(CC_SCHEDULE_SELECTOR(TMXOrthoVertexZNew::repositionSprite));
1018
1019}
1020
1022{
1023 _tamara->release();
1024}
1025
1027{
1028 // tile height is 101x81
1029 // map size: 12x12
1030 auto p = _tamara->getPosition();
1031 p = CC_POINT_POINTS_TO_PIXELS(p);
1032 _tamara->setPositionZ( -( (p.y+81) /81) );
1033}
1034
1036{
1038
1039 // TIP: 2d projection should be used
1040 Director::getInstance()->setProjection(Director::Projection::_2D);
1041 Director::getInstance()->getRenderer()->setDepthTest(true);
1042 Director::getInstance()->getRenderer()->setDepthWrite(true);
1043}
1044
1046{
1047 // At exit use any other projection.
1048 Director::getInstance()->setProjection(Director::Projection::DEFAULT);
1049 Director::getInstance()->getRenderer()->setDepthTest(false);
1050 Director::getInstance()->getRenderer()->setDepthWrite(false);
1052}
1053
1054std::string TMXOrthoVertexZNew::title() const
1055{
1056 return "TMX Ortho vertexZ";
1057}
1058
1060{
1061 return "Doesn't support yet";
1062}
1063
1064
1065//------------------------------------------------------------------
1066//
1067// TMXIsoMoveLayerNew
1068//
1069//------------------------------------------------------------------
1071{
1072 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/iso-test-movelayer.tmx");
1073 addChild(map, 0, kTagTileMap);
1074
1075 map->setPosition(Vec2(-700.0f,-50.0f));
1076
1077 Size CC_UNUSED s = map->getContentSize();
1078 CCLOG("ContentSize: %f, %f", s.width,s.height);
1079}
1080
1081std::string TMXIsoMoveLayerNew::title() const
1082{
1083 return "TMX Iso Move Layer";
1084}
1085
1087{
1088 return "Trees should be horizontally aligned";
1089}
1090
1091
1092//------------------------------------------------------------------
1093//
1094// TMXOrthoMoveLayerNew
1095//
1096//------------------------------------------------------------------
1098{
1099 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/orthogonal-test-movelayer.tmx");
1100 addChild(map, 0, kTagTileMap);
1101
1102 Size CC_UNUSED s = map->getContentSize();
1103 CCLOG("ContentSize: %f, %f", s.width,s.height);
1104}
1105
1107{
1108 return "TMX Ortho Move Layer";
1109}
1110
1112{
1113 return "Trees should be horizontally aligned";
1114}
1115
1116//------------------------------------------------------------------
1117//
1118// TMXTilePropertyTestNew
1119//
1120//------------------------------------------------------------------
1121
1123{
1124 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/ortho-tile-property.tmx");
1125 addChild(map ,0 ,kTagTileMap);
1126
1127 for(int i=1;i<=20;i++){
1128 for(const auto& value : map->getPropertiesForGID(i).asValueMap())
1129 {
1130 log("GID:%i, Properties:%s, %s", i, value.first.c_str(), value.second.asString().c_str());
1131 }
1132 }
1133}
1134
1136{
1137 return "TMX Tile Property Test";
1138}
1139
1141{
1142 return "In the console you should see tile properties";
1143}
1144
1145//------------------------------------------------------------------
1146//
1147// TMXOrthoFlipTestNew
1148//
1149//------------------------------------------------------------------
1150
1152{
1153 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/ortho-rotation-test.tmx");
1154 addChild(map, 0, kTagTileMap);
1155
1156 Size CC_UNUSED s = map->getContentSize();
1157 log("ContentSize: %f, %f", s.width,s.height);
1158
1159 auto action = ScaleBy::create(2, 0.5f);
1160 map->runAction(action);
1161}
1162
1164{
1165 return "TMX tile flip test";
1166}
1167
1168//------------------------------------------------------------------
1169//
1170// TMXOrthoFlipRunTimeTestNew
1171//
1172//------------------------------------------------------------------
1173
1175{
1176 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/ortho-rotation-test.tmx");
1177 addChild(map, 0, kTagTileMap);
1178
1179 auto s = map->getContentSize();
1180 log("ContentSize: %f, %f", s.width,s.height);
1181
1182 auto action = ScaleBy::create(2, 0.5f);
1183 map->runAction(action);
1184
1185 schedule(CC_SCHEDULE_SELECTOR(TMXOrthoFlipRunTimeTestNew::flipIt), 1.0f);
1186}
1187
1189{
1190 return "TMX tile flip run time test";
1191}
1192
1194{
1195 return "in 2 sec bottom left tiles will flip";
1196}
1197
1199{
1200 auto map = (cocos2d::FastTMXTiledMap*) getChildByTag(kTagTileMap);
1201 auto layer = map->getLayer("Layer 0");
1202
1203 //blue diamond
1204 auto tileCoord = Vec2(1,10);
1205 int flags;
1206 unsigned int GID = layer->getTileGIDAt(tileCoord, (TMXTileFlags*)&flags);
1207 // Vertical
1208 if( flags & kTMXTileVerticalFlag )
1209 flags &= ~kTMXTileVerticalFlag;
1210 else
1211 flags |= kTMXTileVerticalFlag;
1212 layer->setTileGID(GID ,tileCoord, (TMXTileFlags)flags);
1213
1214
1215 tileCoord = Vec2(1,8);
1216 GID = layer->getTileGIDAt(tileCoord, (TMXTileFlags*)&flags);
1217 // Vertical
1218 if( flags & kTMXTileVerticalFlag )
1219 flags &= ~kTMXTileVerticalFlag;
1220 else
1221 flags |= kTMXTileVerticalFlag;
1222 layer->setTileGID(GID ,tileCoord, (TMXTileFlags)flags);
1223
1224
1225 tileCoord = Vec2(2,8);
1226 GID = layer->getTileGIDAt(tileCoord, (TMXTileFlags*)&flags);
1227 // Horizontal
1228 if( flags & kTMXTileHorizontalFlag )
1229 flags &= ~kTMXTileHorizontalFlag;
1230 else
1231 flags |= kTMXTileHorizontalFlag;
1232 layer->setTileGID(GID, tileCoord, (TMXTileFlags)flags);
1233}
1234//------------------------------------------------------------------
1235//
1236// TMXOrthoFromXMLTestNew
1237//
1238//------------------------------------------------------------------
1239
1241{
1242 std::string resources = "TileMaps"; // partial paths are OK as resource paths.
1243 std::string file = resources + "/orthogonal-test1.tmx";
1244
1245 auto fileUtils = FileUtils::getInstance();
1246 std::string str = fileUtils->getStringFromFile(fileUtils->fullPathForFilename(file.c_str()));
1247
1248 auto map = cocos2d::FastTMXTiledMap::createWithXML(str ,resources.c_str());
1249 addChild(map, 0, kTagTileMap);
1250
1251 auto s = map->getContentSize();
1252 log("ContentSize: %f, %f", s.width,s.height);
1253
1254 auto action = ScaleBy::create(2, 0.5f);
1255 map->runAction(action);
1256}
1257
1259{
1260 return "TMX created from XML test";
1261}
1262//------------------------------------------------------------------
1263//
1264// TMXOrthoXMLFormatTestNew
1265//
1266//------------------------------------------------------------------
1267
1269{
1270 // this test tests for:
1271 // 1. load xml format tilemap
1272 // 2. gid lower than firstgid is ignored
1273 // 3. firstgid in tsx is ignored, tile property in tsx loaded correctly.
1274 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/xml-test.tmx");
1275 addChild(map, 0, kTagTileMap);
1276
1277 auto s = map->getContentSize();
1278 log("ContentSize: %f, %f", s.width,s.height);
1279
1280 for(int i=24;i<=26;i++){
1281 log("GID:%i, Properties:%s", i, map->getPropertiesForGID(i).asValueMap()["name"].asString().c_str());
1282 }
1283
1284 auto action = ScaleBy::create(2, 0.5f);
1285 map->runAction(action);
1286}
1287
1289{
1290 return "you should see blue, green and yellow in console.";
1291}
1292
1293//------------------------------------------------------------------
1294//
1295// TMXBug987New
1296//
1297//------------------------------------------------------------------
1299{
1300 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/orthogonal-test6.tmx");
1301 addChild(map, 0, kTagTileMap);
1302
1303 Size CC_UNUSED s1 = map->getContentSize();
1304 CCLOG("ContentSize: %f, %f", s1.width,s1.height);
1305
1306 map->setAnchorPoint(Vec2(0.0f, 0.0f));
1307 auto layer = map->getLayer("Tile Layer 1");
1308 layer->setTileGID(3, Vec2(2,2));
1309}
1310
1311std::string TMXBug987New::title() const
1312{
1313 return "TMX Bug 987";
1314}
1315
1316std::string TMXBug987New::subtitle() const
1317{
1318 return "You should see an square";
1319}
1320
1321//------------------------------------------------------------------
1322//
1323// TMXBug787New
1324//
1325//------------------------------------------------------------------
1327{
1328 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/iso-test-bug787.tmx");
1329 addChild(map, 0, kTagTileMap);
1330
1331 map->setScale(0.25f);
1332}
1333
1334std::string TMXBug787New::title() const
1335{
1336 return "TMX Bug 787";
1337}
1338
1339std::string TMXBug787New::subtitle() const
1340{
1341 return "You should see a map";
1342}
1343
1344//------------------------------------------------------------------
1345//
1346// TMXGIDObjectsTestNew
1347//
1348//------------------------------------------------------------------
1350{
1351 auto map = cocos2d::FastTMXTiledMap::create("TileMaps/test-object-layer.tmx");
1352 addChild(map, -1, kTagTileMap);
1353
1354 Size CC_UNUSED s = map->getContentSize();
1355 CCLOG("Contentsize: %f, %f", s.width, s.height);
1356
1357 CCLOG("----> Iterating over all the group objects");
1358
1359 auto drawNode = DrawNode::create();
1360 Color4F color(1.0, 1.0, 1.0, 1.0);
1361 auto group = map->getObjectGroup("Object Layer 1");
1362 auto objects = group->getObjects();
1363 for (auto& obj : objects)
1364 {
1365 ValueMap& dict = obj.asValueMap();
1366
1367 float x = dict["x"].asFloat();
1368 float y = dict["y"].asFloat();
1369 float width = dict["width"].asFloat();
1370 float height = dict["height"].asFloat();
1371
1372 drawNode->drawLine(Vec2(x, y), Vec2(x + width, y), color);
1373 drawNode->drawLine(Vec2(x + width, y), Vec2(x + width, y + height), color);
1374 drawNode->drawLine(Vec2(x + width,y + height), Vec2(x,y + height), color);
1375 drawNode->drawLine(Vec2(x,y + height), Vec2(x,y), color);
1376 }
1377 map->addChild(drawNode, 10);
1378}
1379
1381{
1382 return "TMX GID objects";
1383}
1384
1386{
1387 return "Tiles are created from an object group";
1388}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
@ kTagTileMap
@ SID_UPDATECOL
@ SID_REMOVETILES
@ SID_REPAINTWITHGID
USING_NS_CC
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string title() const override
void repositionSprite(float dt)
virtual void onEnter() override
cocos2d::Sprite * _tamara
Definition: TileMapTest2.h:221
virtual void onExit() override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
void repositionSprite(float dt)
cocos2d::Sprite * _tamara
Definition: TileMapTest2.h:194
virtual void onExit() override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
void removeSprite(float dt)
virtual std::string title() const override
virtual std::string title() const override
virtual void onExit() override
virtual void onEnter() override
virtual std::string title() const override
void repositionSprite(float dt)
virtual std::string subtitle() const override
cocos2d::Sprite * _tamara
Definition: TileMapTest2.h:236
virtual void onEnter() override
virtual void onExit() override
virtual std::string title() const override
virtual ~TMXOrthoZorderNew()
void repositionSprite(float dt)
virtual std::string subtitle() const override
virtual std::string title() const override
cocos2d::Sprite * _tamara
Definition: TileMapTest2.h:208
void updateCol(float dt)
unsigned int _gid
Definition: TileMapTest2.h:101
void removeTiles(float dt)
void repaintWithGID(float dt)
unsigned int _gid2
Definition: TileMapTest2.h:102
virtual std::string title() const override
void removeSprite(Node *sender)
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string title() const override
virtual void onEnter() override
Definition: BaseTest.cpp:430
virtual void onExit() override
virtual std::string title() const override
virtual std::string subtitle() const override
void onTouchesMoved(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
virtual ~TileDemoNew()
virtual std::string title() const override
void updateMap(float dt)
virtual std::string title() const override
static const char s_TilesPng[]
Definition: testResource.h:63
static const char s_LevelMapTga[]
Definition: testResource.h:64
static const char s_pathSister1[]
Definition: testResource.h:29