Created
August 25, 2014 07:49
-
-
Save kazukitanaka0611/c19b9a8796057392028d to your computer and use it in GitHub Desktop.
Dots game cocos2d-x copy from https://github.com/mihaiserban/DotsSpriteKit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __HELLOWORLD_SCENE_H__ | |
#define __HELLOWORLD_SCENE_H__ | |
#include "cocos2d.h" | |
#include "DotSprite.h" | |
class HelloWorld : public cocos2d::Layer | |
{ | |
public: | |
// there's no 'id' in cpp, so we recommend returning the class instance pointer | |
static cocos2d::Scene* createScene(); | |
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone | |
virtual bool init(); | |
virtual bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event); | |
virtual void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event); | |
virtual void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event); | |
// implement the "static create()" method manually | |
CREATE_FUNC(HelloWorld); | |
private: | |
std::vector<cocos2d::Color3B> possibleColors; | |
std::vector<std::vector<DotSprite*>> collumns; | |
std::vector<DotSprite*> selectedDots; | |
float dotSpacing; | |
cocos2d::DrawNode *shapeLine; | |
cocos2d::Vec2 startPoint; | |
void buildGridView(); | |
DotSprite* createDotSprite(); | |
DotSprite* dotForPoint(cocos2d::Point point); | |
void createDotSpriteSelectAnimationForDotSprite(DotSprite* sprite); | |
bool isDotSprite(DotSprite* newSprite, DotSprite* dotSprite); | |
}; | |
#endif // __HELLOWORLD_SCENE_H__ | |
#include "HelloWorldScene.h" | |
USING_NS_CC; | |
#define kGridItems 7 | |
#define kGridMargins 30 | |
Scene* HelloWorld::createScene() | |
{ | |
// 'scene' is an autorelease object | |
auto scene = Scene::create(); | |
// 'layer' is an autorelease object | |
auto layer = HelloWorld::create(); | |
// add layer as a child to scene | |
scene->addChild(layer); | |
// return the scene | |
return scene; | |
} | |
// on "init" you need to initialize your instance | |
bool HelloWorld::init() | |
{ | |
////////////////////////////// | |
// 1. super init first | |
if ( !Layer::init() ) | |
{ | |
return false; | |
} | |
this->possibleColors.push_back(Color3B::GREEN); | |
this->possibleColors.push_back(Color3B::RED); | |
this->possibleColors.push_back(Color3B::YELLOW); | |
this->possibleColors.push_back(Color3B::MAGENTA); | |
this->possibleColors.push_back(Color3B::ORANGE); | |
// DrawNode | |
this->shapeLine = DrawNode::create(); | |
this->addChild(shapeLine); | |
// grid | |
this->buildGridView(); | |
// イベント | |
auto listener = EventListenerTouchOneByOne::create(); | |
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this); | |
listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this); | |
listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this); | |
auto dispatcher = Director::getInstance()->getEventDispatcher(); | |
dispatcher->addEventListenerWithSceneGraphPriority(listener, this); | |
return true; | |
} | |
void HelloWorld::buildGridView() | |
{ | |
auto winSize = Director::getInstance()->getWinSize(); | |
float gridWidth = winSize.width - 2 * kGridMargins; | |
float startY = (winSize.height - gridWidth) / 2; | |
this->dotSpacing = gridWidth / kGridItems; | |
for (int i = 0; i < kGridItems; i++) | |
{ | |
std::vector<DotSprite*> row; | |
for (int j = 0; j <= kGridItems; j++) | |
{ | |
auto sprite = this->createDotSprite(); | |
sprite->setPosition(Vec2(arc4random() % (int)winSize.width, arc4random() % (int)winSize.height)); | |
this->addChild(sprite); | |
row.push_back(sprite); | |
auto move = MoveTo::create(0.4, Vec2(kGridMargins + (i * this->dotSpacing), | |
j * this->dotSpacing + startY)); | |
sprite->runAction(move); | |
} | |
this->collumns.push_back(row); | |
} | |
} | |
DotSprite* HelloWorld::createDotSprite() | |
{ | |
int randomIndex = arc4random() % this->possibleColors.size(); | |
auto color = this->possibleColors[randomIndex]; | |
auto sprite = DotSprite::create("[email protected]");; | |
sprite->setColor(color); | |
sprite->setSpriteColor(color); | |
return sprite; | |
} | |
DotSprite* HelloWorld::dotForPoint(cocos2d::Point point) | |
{ | |
for (std::vector<DotSprite*> row : this->collumns) | |
{ | |
for (auto sprite : row) | |
{ | |
if (sprite->getBoundingBox().containsPoint(point)) | |
{ | |
return sprite; | |
} | |
} | |
} | |
return nullptr; | |
} | |
void HelloWorld::createDotSpriteSelectAnimationForDotSprite(DotSprite* sprite) | |
{ | |
auto animateSprite = this->createDotSprite(); | |
animateSprite->setColor(sprite->getSpriteColor()); | |
animateSprite->setPosition(Vec2(sprite->getContentSize().width / 2, sprite->getContentSize().height / 2)); | |
auto enlarge = ScaleBy::create(0.15, 1.8); | |
auto fadeOut = FadeTo::create(0.15, 0); | |
animateSprite->runAction(Sequence::create(enlarge, fadeOut, NULL)); | |
sprite->addChild(animateSprite); | |
} | |
// onTouchBegan | |
bool HelloWorld::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event) | |
{ | |
auto sprite = this->dotForPoint(Director::getInstance()->convertToGL(touch->getLocationInView())); | |
if (sprite) | |
{ | |
// add | |
this->selectedDots.push_back(sprite); | |
this->createDotSpriteSelectAnimationForDotSprite(sprite); | |
//log("start point X = %f",sprite->getPosition().x); | |
//log("start point Y = %f",sprite->getPosition().y); | |
this->startPoint = Vec2(sprite->getPosition().x , sprite->getPosition().y); | |
} | |
return true; | |
} | |
// onTouchMoved | |
void HelloWorld::onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event) | |
{ | |
if (this->selectedDots.size()) | |
{ | |
auto touchSprite = this->dotForPoint(Director::getInstance()->convertToGL(touch->getLocationInView())); | |
if (touchSprite) | |
{ | |
if (!(std::find(this->selectedDots.begin(), this->selectedDots.end(), touchSprite) != this->selectedDots.end())) | |
{ | |
auto selectedLastSprite = this->selectedDots[this->selectedDots.size() -1]; | |
if (selectedLastSprite->getSpriteColor().equals(touchSprite->getSpriteColor()) | |
&& this->isDotSprite(touchSprite, selectedLastSprite)) | |
{ | |
// add | |
this->selectedDots.push_back(touchSprite); | |
this->createDotSpriteSelectAnimationForDotSprite(touchSprite); | |
//log("end point X = %f",touchSprite->getPosition().x); | |
//log("end point Y = %f",touchSprite->getPosition().y); | |
auto endPoint = Vec2(touchSprite->getPosition().x, touchSprite->getPosition().y); | |
this->shapeLine->drawSegment(this->startPoint, | |
endPoint, | |
2.0f, | |
Color4F::Color4F(touchSprite->getSpriteColor())); | |
this->startPoint = endPoint; | |
} | |
} | |
} | |
} | |
} | |
// onTouchEnded | |
void HelloWorld::onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event) | |
{ | |
if (this->selectedDots.size() > 1) | |
{ | |
for (auto node : this->selectedDots) | |
{ | |
std::vector<DotSprite*> tempArray; | |
int count = 0; | |
for (std::vector<DotSprite*> row : this->collumns) | |
{ | |
if ((std::find(row.begin(), row.end(), node) != row.end())) | |
{ | |
std::vector<DotSprite *>::iterator it = row.begin(); | |
row.erase(it); | |
this->collumns.erase(this->collumns.begin() + count); | |
this->collumns.insert(this->collumns.begin() + count, row); | |
break; | |
} | |
count++; | |
} | |
} | |
} | |
this->selectedDots.clear(); | |
this->shapeLine->clear(); | |
auto winSize = Director::getInstance()->getWinSize(); | |
float gridWidth = winSize.width - 2 * kGridMargins; | |
float startY = (winSize.height - gridWidth) / 2; | |
for (int i = 0; i < this->collumns.size(); i++) | |
{ | |
std::vector<DotSprite*> row = this->collumns[i]; | |
for (int j = 0; j <= kGridItems; j++) | |
{ | |
DotSprite *sprite = nullptr; | |
if (row.size() > j) | |
{ | |
sprite = row[j]; | |
} | |
else | |
{ | |
sprite = this->createDotSprite(); | |
sprite->setPosition(Vec2(kGridMargins + (i * this->dotSpacing), winSize.height)); | |
this->addChild(sprite, -1); | |
row.push_back(sprite); | |
this->collumns.erase(this->collumns.begin() + i); | |
this->collumns.insert(this->collumns.begin() + i, row); | |
} | |
auto expectedPoint = Vec2(kGridMargins + (i * this->dotSpacing), j * this->dotSpacing + startY); | |
if (sprite->getPosition().x == expectedPoint.x | |
&& sprite->getPosition().y == expectedPoint.y) | |
{ | |
} | |
else | |
{ | |
auto moveToBounce = Vec2(kGridMargins + (i * this->dotSpacing), j * this->dotSpacing + startY - 10); | |
auto moveToNormal = Vec2(kGridMargins + (i * this->dotSpacing), j * this->dotSpacing + startY); | |
auto move = MoveTo::create(0.15, moveToBounce); | |
auto bounce = MoveTo::create(0.15, moveToNormal); | |
sprite->runAction(Sequence::create(move, bounce, NULL)); | |
} | |
} | |
} | |
} | |
bool HelloWorld::isDotSprite(DotSprite* newSprite, DotSprite* dotSprite) | |
{ | |
int rowIdLastObj = 0; | |
int columIdLastObject = 0; | |
for (int i = 0; i < this->collumns.size(); i++) | |
{ | |
std::vector<DotSprite*> row = this->collumns[i]; | |
auto iter = std::find(row.begin(), row.end(), dotSprite); | |
if (iter != row.end()) | |
{ | |
columIdLastObject = i; | |
rowIdLastObj = (int)(iter - row.begin()); | |
break; | |
} | |
} | |
int rowIdTestingObj = 0; | |
int columIdTestingObject = 0; | |
for (int i = 0; i < this->collumns.size(); i++) | |
{ | |
std::vector<DotSprite*> row = this->collumns[i]; | |
auto iter = std::find(row.begin(), row.end(), newSprite); | |
if (iter != row.end()) | |
{ | |
columIdTestingObject = i; | |
rowIdTestingObj = (int)(iter - row.begin()); | |
break; | |
} | |
} | |
bool isNearDot = false; | |
int rowDifference = abs(rowIdLastObj) - abs(rowIdTestingObj); | |
int collumnDifference = abs(columIdLastObject) - abs(columIdTestingObject); | |
if (abs(rowDifference) <= 1 | |
&& abs(collumnDifference) <= 1) | |
{ | |
isNearDot = true; | |
} | |
return isNearDot; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment