forked from otmb/cocoPuzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameScene.cpp
253 lines (209 loc) · 6.9 KB
/
GameScene.cpp
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
//
// GameScene.cpp
// Puzzle2
//
// Created by otmb on 2014/09/05.
//
//
#include "GameScene.h"
#include "Bullet.h"
#include "UIDialog.h"
#include "DrawLine.h"
USING_NS_CC;
#define MAX_BULLET 45
Scene* GameScene::createScene()
{
//物理演算を使う為にcreateWithPhysicsを使用
auto scene = Scene::createWithPhysics();
//Worldに対して重力をセット
//Vect gravity;
//gravity.setPoint(0, -50);
scene->getPhysicsWorld()->setGravity(Vec2(0, -98.0));
scene->getPhysicsWorld()->setSpeed(4.0f);
//物理オブジェクトにを可視的にしてくれるデバックモード
//scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
auto layer = GameScene::create();
scene->addChild(layer);
return scene;
}
#include <functional>
// on "init" you need to initialize your instance
bool GameScene::init()
{
if (!Layer::init())
return false;
auto winSize = Director::getInstance()->getWinSize();
Vec2 vec[6] =
{
Vec2(winSize.width-1, winSize.height -1),
Vec2(1, winSize.height -1),
Vec2(1, 100),
Vec2(winSize.width/2, 0),
Vec2(winSize.width-1,100),
Vec2(winSize.width-1,winSize.height -1),
};
auto wall = Node::create();
//wall->setPhysicsBody(PhysicsBody::createEdgeChain(vec, 5, PhysicsMaterial(0.1f, 1.0f, 0.0f)));
// 密度、反発、摩擦
wall->setPhysicsBody(PhysicsBody::createEdgeChain(vec, 6, PhysicsMaterial(0.0f, 0.0f, 0.5f)));
wall->setPosition(0, 0);
addChild(wall);
initTouchEvent();
scheduleUpdate();
Button* button = Button::create("Bullet.png");
button->setPosition(Vec2(winSize.width,winSize.height - 50));
button->addTouchEventListener(CC_CALLBACK_2(GameScene::touchEvent, this));
addChild(button);
_bulletVicts = new std::vector<Vec2*>();
_fingerPosition = nullptr;
//_spriteNode = SpriteBatchNode::create("ball.png");
//addChild(_spriteNode);
return true;
}
void GameScene::touchEvent(Ref *pSender, Widget::TouchEventType type)
{
//std::function<void(Ref*,Widget::TouchEventType)> callback = CC_CALLBACK_0(GameScene::dialogClose,this);
Widget::ccWidgetTouchCallback callback = CC_CALLBACK_0(GameScene::dialogClose,this);
_dialog = UIDialog::create(callback);
addChild(_dialog,Z_Dialog,T_Dialog);
}
//void GameScene::dialogClose(Ref *pSender, Widget::TouchEventType type)
void GameScene::dialogClose()
{
auto nodes = getChildren();
for (auto node : nodes)
{
switch (node->getTag())
{
case T_Dialog:
UIDialog* dialog = static_cast<UIDialog*>(node);
dialog->close();
break;
}
}
}
void GameScene::update(float dt)
{
_time += dt;
// 弾の発射判定
if (MAX_BULLET > _bullet) showBullet();
// 弾の座標が物理演算で変わるので対処
//std::vector<Vec2*> * victs = new std::vector<Vec2*>();
for(auto* bullet : _bullets){
_bulletVicts->push_back(new Vec2(bullet->getPosition()));
}
if (_fingerPosition != nullptr)
_bulletVicts->push_back(_fingerPosition);
DrawLineRemove();
// 線を引く
DrawLine* node = DrawLine::create(_bulletVicts);
addChild(node,Z_Line,T_Line);
_bulletVicts->clear();
}
void GameScene::showBullet(){
//auto* sp = Sprite::createWithTexture(_spriteNode->getTexture());
//addChild(sp,Z_Bullet);
auto bullet = Bullet::create();
//srand((unsigned int)time(NULL));
int tagNum = arc4random()%5;
bullet->setColor(_tagColor[tagNum]);
auto winSize = Director::getInstance()->getWinSize();
bullet->setPosition(winSize.width/2, winSize.height - bullet->bulletSize);
PhysicsBody* pBall = bullet->getPhysicsBody();
pBall->setTag(T_Bullet);
addChild(bullet,Z_Bullet,tagNum+2);
_bullet++;
}
void GameScene::initTouchEvent()
{
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan, this);
touchListener->onTouchMoved = CC_CALLBACK_2(GameScene::onTouchMoved, this);
touchListener->onTouchEnded = CC_CALLBACK_2(GameScene::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
}
#define mark - タップイベント
bool GameScene::onTouchBegan(Touch* touch, Event* event)
{
auto location = touch->getLocation();
auto arr = this->getScene()->getPhysicsWorld()->getShapes(location);
for (auto& obj : arr)
{
if ((obj->getBody()->getTag() & T_Bullet) != 0)
{
_tag = static_cast<Bullet*>(obj->getBody()->getNode())->getTag();
break;
}
}
return true;
}
void GameScene::onTouchMoved(Touch* touch, Event* event)
{
auto location = touch->getLocation();
auto arr = this->getScene()->getPhysicsWorld()->getShapes(location);
//_bullets = Vector<Bullet*>();
//std::vector<Vec2*> * _bulletVicts = new std::vector<Vec2*>();
Bullet* bullet = nullptr;
for (auto& obj : arr)
{
if ((obj->getBody()->getTag() & T_Bullet) != 0)
{
bullet = static_cast<Bullet*>(obj->getBody()->getNode());
if (bullet->getTag() == _tag){
break;
} else {
bullet = nullptr;
}
}
}
if (bullet != nullptr
&& bullet->getState() == Bullet::State::Moving){
if (_bullets.empty()){ // 空
_bullets.pushBack(bullet);
} else if (!_bullets.contains(bullet)){ // 配列に入っていない
// 距離の比較
float distance = _bullets.back()->getPosition().distance(bullet->getPosition());
//log("%f",distance);
if (distance < bullet->bulletSize * 3.0f){
_bullets.pushBack(bullet);
}
//} else if (_bullets.back() == bullet) {
// _bullets.erase(_bullets.find(bullet));
}
}
if (_bullets.size() < 2){
_fingerPosition = new Vec2(location);
} else {
_fingerPosition = nullptr;
}
//DrawLine* node = DrawLine::create();
//addChild(node);
}
void GameScene::onTouchEnded(Touch* touch, Event* event)
{
// 2個以上で削除
if (!_bullets.empty() && _bullets.size() > 2){
for (auto* bullet : _bullets){
if (bullet->getState() == Bullet::State::Moving){
bullet->brokenBullet();
_bullet--;
}
}
}
_bullets.clear();
//_bulletVicts->clear();
}
void GameScene::DrawLineRemove()
{
auto nodes = getChildren();
for (auto node : nodes)
{
switch (node->getTag())
{
case T_Line:
DrawLine* line = static_cast<DrawLine*>(node);
line->remove();
break;
}
}
}