Repository navigation
Expand file tree
/
Copy pathShoalManager.java
More file actions
294 lines (271 loc) · 9.22 KB
/
Copy pathShoalManager.java
File metadata and controls
294 lines (271 loc) · 9.22 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
package com.mypro.manager;
import com.mypro.base.graphics.Matrix;
import com.mypro.base.tools.Log;
import com.mypro.constant.Constant;
import com.mypro.model.GamingInfo;
import com.mypro.model.fish.Fish;
import com.mypro.threads.FishRunThread;
import com.mypro.threads.PicActThread;
import com.mypro.tools.LogTools;
/**
* 鱼群管理器
* @author Leslie Leung
*
*/
public class ShoalManager{
private String[] fish; //可生成的鱼群
private int[] showProbability; //鱼的生成概率
private boolean createable; //是否可生成鱼群
public ShoalManager(){
}
/**
* 启动鱼群管理器
* @param gamePartInfo
*/
public void start(final GamePartInfo gamePartInfo){
this.fish = gamePartInfo.getFishName(); //设置可以创建的鱼群
this.showProbability = gamePartInfo.getShowProbability();
createable = true; //设置可以创建鱼群
new Thread(new Runnable(){
public void run(){
for(int i = 0;i<gamePartInfo.getShoalSumInScreen()&&GamingInfo.getGamingInfo().isGaming();i++){
createShoal();
try {
Thread.sleep((long)(Math.random()*100*gamePartInfo.getShoalSumInScreen()));
} catch (Exception e) {
LogTools.doLogForException(e);
}
}
}
}).start();
}
/**
* 创建一个鱼群
*/
public void createShoal(){
try {
if(!createable){
Log.w("ShoalManager", "不允许创建新鱼群");
return;
}
HeadFish fish = birthHeadFish();
createShoal(fish);
fishRun(fish);
startFishAct(fish.getFish());
setRandomShoalPositionByHeadFish(fish);
} catch (Exception e) {
LogTools.doLogForException(e);
}
}
/**
* 生成一条领头鱼
*/
private HeadFish birthHeadFish(){
//创建一个领头鱼
HeadFish headFish = new HeadFish();
Fish fish = null;
int probability = (int)(Math.random()*100+1); //1~100
int sumProbability = 0;
if(probability<=showProbability[0]){
fish = FishManager.getFishMananger().birthFishByFishName(this.fish[0]);
}else{
sumProbability = showProbability[0];
for(int index=1;index<this.showProbability.length;index++){
sumProbability += this.showProbability[index];
if(probability<=sumProbability){
fish = FishManager.getFishMananger().birthFishByFishName(this.fish[index]);
break;
}
}
}
fish.setCanRun(true);
GamingInfo.getGamingInfo().getFish().add(fish);
headFish.setFish(fish);
fish.setHeadFish(headFish);
//创建了一条领头鱼后,将方向设置到类属性currentFromPoint上,供鱼群使用
headFish.setCurrentFromPoint(getFromPoint(headFish));
return headFish;
}
/**
* 让鱼开始游动
* @param fish 需要游动的鱼
*/
private void fishRun(HeadFish fish){
FishRunThread prt = new FishRunThread(fish); // 创建当前鱼的移动线程
fish.setFishRunThread(prt);
prt.setRun(true); //设置鱼开始移动
prt.start();
}
/**
* 开始播放鱼的动作
* @param fish 需要游动的鱼
*/
private void startFishAct(Fish fish){
//将鱼群的鱼放入图层
GamingInfo.getGamingInfo().getSurface().putDrawablePic(fish.getFishInfo().getFishInLayer(), fish);
PicActThread pat = new PicActThread(fish); // 创建当前鱼的动作线程
fish.setPicActThread(pat);
pat.start();// 鱼的动作控制线程启动
}
/**
* 生成鱼群
* @param fish 领头鱼
*/
private void createShoal(HeadFish fish){
try{
if(GamingInfo.getGamingInfo().isGaming()&&fish.getFish().getFishInfo().getFishShoalMax()!=0){
Fish flagFish; //代表鱼群中的鱼的临时变量
int sum = (int)(Math.random()*fish.getFish().getFishInfo().getFishShoalMax()+1);
for(int i = 0;i<sum;i++){
flagFish = fish.getFish().getFish();
flagFish.setHeadFish(fish);
fish.getShoal().add(flagFish);
GamingInfo.getGamingInfo().getFish().add(flagFish);
}
}
}catch(Exception e){
LogTools.doLogForException(e);
}
}
/**
* 根据领头鱼的位置设置随机位置,并将随机位置的偏移量赋值给给定鱼群
* @param headFish 领头鱼
*/
private void setRandomShoalPositionByHeadFish(final HeadFish headFish){
try{
new Thread(new Runnable(){
public void run() {
try{
float fromY = headFish.getFish_Y();
int shoalIndex = 1;
int sumAtOut = headFish.getShoal().size();//鱼群中还有多少条鱼在屏幕外
Fish fishFlag;
float randomMinY;
float randomMaxY;
float flagFishX,flagFishY;//生成的鱼的坐标
Fish startFish = headFish.getFish();
// TODO Auto-generated method stub
while(GamingInfo.getGamingInfo().isGaming()){
while(!GamingInfo.getGamingInfo().isPause()&&sumAtOut>1){
fishFlag = headFish.getShoal().get(shoalIndex);
randomMinY = (float)(fromY-fishFlag.getPicHeight());
randomMaxY = (float)(fromY+fishFlag.getPicHeight());
if(headFish.getCurrentFromPoint()<=Constant.FROM_BOTTOM_LEFT){
flagFishX = 0-headFish.getFish().getPicWidth();
flagFishY = (float)(randomMinY+Math.random()*(randomMaxY-randomMinY));
}else{
flagFishX = GamingInfo.getGamingInfo().getScreenWidth();
flagFishY = (float)(randomMinY+Math.random()*(randomMaxY-randomMinY));
}
while(!canRun(startFish,flagFishX,flagFishY,headFish)&&GamingInfo.getGamingInfo().isGaming()){
try{
Thread.sleep(50);
}catch(Exception e){
}
}
fishFlag.setDistanceHeadFishX(headFish.getFish_X()-flagFishX);
fishFlag.setDistanceHeadFishY(headFish.getFish_Y()-flagFishY);
fishFlag.setCanRun(true);
startFishAct(fishFlag);
startFish = fishFlag;
shoalIndex++;
sumAtOut--;
try{
Thread.sleep(200);
}catch(Exception e){
}
}
break;
}
}catch(Exception e){}}}).start();
}catch(Exception e){}
}
/**
* 判断鱼是否可以移动了
* 这里的作用是根据参照与的位置,来决定准备移动的鱼是否可以移动了
* @param firstFish 参照鱼
* @param fishFlag 准备移动的鱼
* @param headFish 领头鱼
* @return
*/
private boolean canRun(Fish firstFish,float fishX,float fishY,HeadFish headFish){
//如果起始边在屏幕左边
if(headFish.getCurrentFromPoint()<=Constant.FROM_BOTTOM_LEFT){
if(fishY+firstFish.getPicHeight()<headFish.getFish_Y()-firstFish.getDistanceHeadFishY()
||fishY>headFish.getFish_Y()-firstFish.getDistanceHeadFishY()+firstFish.getPicHeight()
||headFish.getFish_X()-firstFish.getDistanceHeadFishX()>0){
return true;
}
}else{
if(fishY+firstFish.getPicHeight()<headFish.getFish_Y()-firstFish.getDistanceHeadFishY()
||fishY>headFish.getFish_Y()-firstFish.getDistanceHeadFishY()+firstFish.getPicHeight()
||headFish.getFish_X()-firstFish.getDistanceHeadFishX()+firstFish.getPicWidth()<GamingInfo.getGamingInfo().getScreenWidth()){
return true;
}
}
return false;
}
/**
* 通知鱼群管理器,领头鱼已经离开屏幕
*/
public void notifyFishIsOutOfScreen(){
try{
createShoal();
}catch(Exception e){
LogTools.doLogForException(e);
}
}
/**
* 设置鱼的起始坐标 将屏幕分成了四个方位
*/
private int getFromPoint(HeadFish fish) {
int mode = (int) (Math.random() * 4);
Matrix matrix = fish.getFish().getPicMatrix();
switch (mode) {
case Constant.FROM_TOP_LEFT:
// 左上
fish.setFish_X(-(float) fish.getFish().getPicWidth());
fish.setFish_Y((float) Math.random()
* (GamingInfo.getGamingInfo().getScreenHeight() / 2 - fish.getFish().getPicHeight() + 1));
matrix.setTranslate(fish.getFish_X(), fish.getFish_Y());
fish.setLastX(1);
return Constant.FROM_TOP_LEFT;
case Constant.FROM_BOTTOM_LEFT:
// 左下
fish.setFish_X(-(float) fish.getFish().getPicWidth());
fish.setFish_Y((float) (GamingInfo.getGamingInfo().getScreenHeight() / 2 + Math.random()
* (GamingInfo.getGamingInfo().getScreenHeight() - fish.getFish().getPicHeight() + 1 - GamingInfo.getGamingInfo().getScreenHeight() / 2)));
matrix.setTranslate(fish.getFish_X(), fish.getFish_Y());
fish.setLastX(1);
return Constant.FROM_BOTTOM_LEFT;
case Constant.FROM_TOP_RIGHT:
// 右上
fish.setFish_X((float) GamingInfo.getGamingInfo().getScreenWidth());
fish.setFish_Y((float) Math.random()
* (GamingInfo.getGamingInfo().getScreenHeight() / 2 - fish.getFish().getPicHeight() + 1));
matrix.setTranslate(fish.getFish_X(), fish.getFish_Y());
matrix.preRotate(180, fish.getFish().getFishRotatePoint_X(), fish.getFish().getFishRotatePoint_Y());
fish.setCurrentRotate(180);
fish.setLastX(-1);
return Constant.FROM_TOP_RIGHT;
case Constant.FROM_BOTTOM_RIGHT:
// 右下
fish.setFish_X((float) GamingInfo.getGamingInfo().getScreenWidth());
fish.setFish_Y((float) (GamingInfo.getGamingInfo().getScreenHeight() / 2 + Math.random()
* (GamingInfo.getGamingInfo().getScreenHeight() - fish.getFish().getPicHeight() + 1 - GamingInfo.getGamingInfo().getScreenHeight() / 2)));
matrix.setTranslate(fish.getFish_X(), fish.getFish_Y());
matrix.preRotate(180, fish.getFish().getFishRotatePoint_X(), fish.getFish().getFishRotatePoint_Y());
fish.setCurrentRotate(180);
fish.setLastX(-1);
return Constant.FROM_BOTTOM_RIGHT;
}
return 0;
}
/**
* 更新鱼群
* @param fish
*/
public void stop(){
this.createable = false;
}
}