-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveControl.cpp
More file actions
198 lines (182 loc) · 6.32 KB
/
MoveControl.cpp
File metadata and controls
198 lines (182 loc) · 6.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
#include"MoveControl.h"
/*float time(Vec2 P1, Vec2 P2, int label)
{
auto distance = sqrtf((P1.x - P2.x) * (P1.x - P2.x) + (P1.y - P2.y)* (P1.y - P2.y)); //¼ÆËãÒÆ¶¯Ê±¼ä
if (label == TypeSodier)
return distance / 50;
if (label == TypeTank)
return distance / 80;
if (label == TypePatrolDog)
return distance / 60;
}*/
// judge if the player wants to cancel previous selection
void isCancelSelection(Vec2 position, EventMouse::MouseButton button)
{
if (button == EventMouse::MouseButton::BUTTON_RIGHT)
{
for (auto i : selectedSprites)
{
i->SetVisible(false);
}
selectedSprites.clear();
isOperating = false;
}
}
//judge if a sprite is in the rectangle area
bool isInRange(Vec2 aimposition, Vec2 startposition, Vec2 endposition)
{
Vec2 delta1 = aimposition - startposition;
Vec2 delta2 = aimposition - endposition;
if (delta1.x * delta2.x <= 0 && delta1.y * delta2.y <= 0)
return true;
return false;
}
//draw a rectangle as the cursor moving
void drawRec(Vec2 position, Layer* layer)
{
if (isDown)
{
isMoved = true;
layer->removeChildByTag(123);
auto draw = DrawNode::create();
draw->setTag(123);
draw->drawRect(startPoint, position, Color4F(100, 100, 100, 100));
layer->addChild(draw);
}
}
//select all the sprites in the rectangle
void selectInRec(int Id,Vec2 position, MyMap* gamemap)
{
if (isMoved && !isOperating)
{
for (auto i : allSprites)
{
if (i->camp==Id&&isInRange(gamemap->convertToWorldSpace(i->getPosition()), startPoint, position))
{
selectedSprites.pushBack(i);
i->SetVisible(true);
}
}
isOperating = true;
SimpleAudioEngine::getInstance()->playEffect("selecttarget.wav");
}
}
//selected the sprite being clicked
void singleSelect(int Id,Vec2 position, MyMap* gamemap)
{
isDown = true;
for (auto i : allSprites)
{
if (i->camp==Id&&isInRange(gamemap->convertToNodeSpace(position), i->getPosition() - i->getContentSize() / 2, i->getPosition() + i->getContentSize() / 2) && !isOperating)
{
SimpleAudioEngine::getInstance()->playEffect("selecttarget.wav");
selectedSprites.pushBack(i);
i->SetVisible(true);
isOperating = true;
break;
}
}
startPoint = position;
}
void FindPath(GameMessageSet* msgs,Vec2 position, MyMap* _tileMap, GridMap* gridmap)
{
vector<GridPoint> path;
Vec2 cusorTilePosition = _tileMap->staggeredCoordForPosition(position);
if (!selectedSprites.empty() && !allSprites.empty() && !isMoved && !isSelecting &&gridmap->gmap[int(cusorTilePosition.x)][int(cusorTilePosition.y)] && isOperating)
{
log("Moved");
for (int i = 0; i < selectedSprites.size() && isMovable(selectedSprites.at(i)); i++)
{
auto temp = selectedSprites.at(i);
Vec2 startTileCoord = _tileMap->staggeredCoordForPosition(_tileMap->convertToWorldSpace(selectedSprites.at(i)->getPosition()));
Vec2 endTileCoord = _tileMap->staggeredCoordForPosition(position);
auto start = GridPoint(startTileCoord.x, startTileCoord.y);
auto end = GridPoint(endTileCoord.x, endTileCoord.y);
if (startTileCoord != endTileCoord)
{
path = searchPathForTile(gridmap, start, end);
}
else
{
path.push_back(start);
}
path.push_back(GridPoint(selectedSprites.size(), 0));
msgs->add_game_message()->genGameMessage(GameMessage::CmdCode::GameMessage_CmdCode_UDP, temp->id,temp->camp,0, path);
path.clear();
}
}
}
//move the sprites being selected with the path generated
/*void setMove(Vec2 position, MyMap* _tileMap, GridMap* gridmap)
{
//Vec2 cusorTilePosition = tileCoordFromPosition(position, _tileMap);
Vec2 cusorTilePosition = _tileMap->staggeredCoordForPosition(position);
if (!selectedSprites.empty() && !allSprites.empty() && !isMoved && !isSelecting &&gridmap->gmap[int(cusorTilePosition.x)][int(cusorTilePosition.y)] && isOperating)
{
log("Moved");
Vec2 contentSize = allSprites.at(0)->getContentSize();
float area = selectedSprites.size() * contentSize.x * contentSize.y * 1.0;
float equalEdge = sqrt(area) / 5;
for (int i = 0; i < selectedSprites.size() && isMovable(selectedSprites.at(i)); i++)
{
srand((unsigned)time(NULL));
Vec2 Position;
Vec2 TilePosition;
do
{
Position.x = ((float)random() / RAND_MAX - 0.5) * equalEdge + position.x;
Position.y = ((float)random() / RAND_MAX - 0.5) * equalEdge + position.y;
//TilePosition = tileCoordFromPosition(Position, _tileMap);
TilePosition = _tileMap->staggeredCoordForPosition(position);
} while (TilePosition.x < _tileMap->getMapSize().height && TilePosition.y < _tileMap->getMapSize().width && TilePosition.x >= 0 && TilePosition.y >= 0 && !gridmap->gmap[int(TilePosition.x)][int(TilePosition.y)]);
positionList.push_back(Position);
}
for (int i = 0; i < selectedSprites.size() && isMovable(selectedSprites.at(i)); i++)
{
int label = selectedSprites.at(i)->getTag();
Vec2 startTileCoord = _tileMap->staggeredCoordForPosition(_tileMap->convertToWorldSpace(selectedSprites.at(i)->getPosition()));
Vec2 endTileCoord = _tileMap->staggeredCoordForPosition(position);
if (startTileCoord != endTileCoord)
{
vector<Vec2> path = searchPathForTile(gridmap, startTileCoord, endTileCoord);
_tileMap->getPositionAtGL(path);
cocos2d::Vector<FiniteTimeAction*> pathAction;
for (int j = 1; j < path.size(); j++)
{
pathAction.pushBack(static_cast<FiniteTimeAction*>(MoveTo::create(time(path[j - 1], path[j], label), path[j])));
}
pathAction.popBack();
pathAction.pushBack(MoveTo::create(time(path[path.size() - 2],_tileMap->convertToNodeSpace(positionList[i]), label), _tileMap->convertToNodeSpace(positionList[i])));
auto sequence = Sequence::create(pathAction);
selectedSprites.at(i)->stopAllActions();
selectedSprites.at(i)->runAction(sequence);
}
}
positionList.clear();
}
}*/
//set the flags to default value
void setDefault(Layer* layer)
{
layer->removeChildByTag(123);
isSelecting = false;
isDown = false;
isMoved = false;
}
bool isMovable(Basement* basement)
{
auto tag = basement->getTag();
if (tag == TypeSodier || tag == TypePatrolDog || tag == TypeTank)
return true;
else
return false;
}
void cancleselected()
{
for(auto i:selectedSprites)
{
i->SetVisible(false);
}
selectedSprites.clear();
isOperating = false;
}