-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.android.js
635 lines (622 loc) · 18.8 KB
/
index.android.js
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
import React, {
Component
} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Image,
ScrollView,
Animated,
TouchableOpacity,
Dimensions,
Navigator,
Modal,
Button,
Alert
} from 'react-native';
const windowWidth = Dimensions.get('window').width;
const windowHeight = windowWidth * 1714 / 750;
const SCROLL1TOP = -(Math.floor(windowHeight * 0.3104));
const SCROLL2TOP = -(Math.floor(windowHeight * 0.2174));
const SCROLL3TOP = -(Math.floor(windowHeight * 0.1243));
const SCROLLITEM = Math.floor(windowWidth * 0.2156); //每一个奖章的高度,最后用来计算最终的高度
const WORDPOSITION = Math.floor(windowWidth * 0.83125); //文字层位置
const SCROLL1REF = 'SCROLL1REF';
const SCROLL2REF = 'SCROLL2REF';
const SCROLL3REF = 'SCROLL3REF';
const CONTAINER = 'CONTAINER';
const WORDCONTAINER = 'WORDCONTAINER';
export default class AwesomeProject extends Component {
constructor(props) {
super(props);
this._startShake = this._startShake.bind(this);
this._setChoose = this._setChoose.bind(this);
this._checkChoose = this._checkChoose.bind(this);
this._setGameModel = this._setGameModel.bind(this);
this._setNum = this._setNum.bind(this);
this._showAccount = this._showAccount.bind(this);
this._renderGameImage = this._renderGameImage.bind(this);
this._renderAImage = this._renderAImage.bind(this);
this._renderBImage = this._renderBImage.bind(this);
this._renderCImage = this._renderCImage.bind(this);
this._rotateAnimated = this._rotateAnimated.bind(this);
this._rebackShake = this._rebackShake.bind(this);
this._startScroll = this._startScroll.bind(this);
this._refSet = this._refSet.bind(this); //绑定
this._startScrollActurally = this._startScrollActurally.bind(this);
this._startWordAnimate = this._startWordAnimate.bind(this);
this._getPrize = this._getPrize.bind(this);
this._model = true;
this._account = 2000; //初始金额
this._num = 0;
this._choose = false;
this._rotate = 0; //遥感旋转角度
this._time = null; //计算时间
this._prizetext = ''; //中奖显示的信息
this._prizenum = 3; //竖列次数flag 用来约束点击摇奖
this._prizearr = []; //记录最终摇奖序列
this.state = {
//遥杆动画状态
transformYValue: new Animated.Value(0),
rotateXValue: 0,
};
}
componentDidMount() {
//组件加载完后就让文字动
this._startWordAnimate(0.05);
}
_setNum(num) {
if(this._choose) {
Alert.alert('温馨提示', '你已经下注了请点击摇杆进行游戏');
}
else {
this._num = num;
this.setState({
_num: num
});
}
}
_rebackShake() {
//回去
const nowtime = Date.now();
let detatime = nowtime - this._time;
this._time = nowtime;
this._rotate -= detatime * 0.09;
if (this.state.rotateXValue < 0) {
return;
} else {
this.setState({
rotateXValue: this._rotate
});
requestAnimationFrame(this._rebackShake);
}
}
_rebackTrans() {
//摇完奖后返回
Animated.timing(this.state.transformYValue, {
toValue: 0,
duration: 500,
}).start();
}
_rotateAnimated() {
//按照x轴旋转效果
let nowtime = Date.now();
let detatime = nowtime - this._time;
this._time = nowtime;
this._rotate += detatime * 0.09;
if (this._rotate > 45) {
this._rebackShake();
this._rebackTrans();
return;
} else {
this.setState({
rotateXValue: this._rotate
});
requestAnimationFrame(this._rotateAnimated);
}
}
_setLeft(refer, value) {
this.refs[refer].setNativeProps({
style: {
transform: [{
translateX: value
}]
}
})
}
_startWordAnimate(speed) {
let wordTime = Date.now();
let leftObject = 0;
let wordRefer = WORDCONTAINER;
const self = this;
const wordAnimated = function() {
let nowtime = Date.now();
let detatime = nowtime - wordTime;
wordTime = nowtime;
leftObject -= detatime * speed;
if (Math.abs(leftObject) > 500) {
leftObject = WORDPOSITION;
requestAnimationFrame(wordAnimated);
} else {
self._setLeft(wordRefer, leftObject);
requestAnimationFrame(wordAnimated);
}
};
wordAnimated();
}
_startShake() {
//摇奖点击
if((!this._choose) && this._model == true) {
Alert.alert('温馨提示', '请先选择你要下注的水果');
} else {
if (this._prizenum === 3) {
this._prizenum = 0;
Animated.timing(this.state.transformYValue, {
toValue: 20,
duration: 500,
}).start();
this._time = Date.now();
this._rotateAnimated();
this._startScroll();
}
}
}
_refSet(refer, props) {
//设置文字的位置
this.refs[refer].setNativeProps({
style: {
transform: [{
translateY: props
}]
}
})
}
_getPrize() { //获奖
const num = this._num;
const itemArr1 = ['', 50, 100, 200];
const itemArr2 = {
item1: ["通讯录第六位", "在场名字中带N的", "左手边第三位"],
item2: ["躺在桌子上", "一边唱征服一边", "站在凳子上"],
item3: ["跳小苹果", "一起大喊我是超人", "互相说我是蠢货"]
};
let prizeArr = this._prizearr;
let result = '';
let res = 0;
let add = 0;
if (this._model) {
for(let i=0; i < prizeArr.length; i++) {
if(prizeArr[i] == num){
res++;
}
}
if(res == 1) {
add = 0.5 * itemArr1[num];
} else if(res == 2) {
add = 1 * itemArr1[num];
} else if(res == 3) {
add = 3 * itemArr1[num];
} else {
}
result = '押注中' + res + '个,获得:¥' + add;
Alert.alert(result, '请注意查收余额', [
{text: '确定', onPress: () => console.log('OK Pressed!')},
]);
} else {
result = '和' + itemArr2.item1[prizeArr[0]-1] + itemArr2.item2[prizeArr[1]-1] + itemArr2.item3[prizeArr[2]-1];
}
const addAccount = this._account + add;
this._account = addAccount;
this._prizetext = result;
this._choose = false;
this.setState({
_prizetext: result,
_account: addAccount,
_choose: false
});
//console.warn('中奖序列', this._prizearr);
}
_setGameModel() {
this._prizearr = [];
this._prizetext = '';
const currentModel = this._model;
this._model = !currentModel;
this.setState({
_model: !currentModel,
_prizetext: '',
_prizearr: []
})
}
_startScrollActurally(id, speed, cyclecount) { //抽奖具体执行
let scrollTime = Date.now();
let scrollObject = null;
let scrollRefer = null;
let count = 0;
if (id === 1) {
scrollObject = SCROLL1TOP;
scrollRefer = SCROLL1REF;
}
if (id === 2) {
scrollObject = SCROLL2TOP;
scrollRefer = SCROLL2REF;
}
if (id === 3) {
scrollObject = SCROLL3TOP;
scrollRefer = SCROLL3REF;
}
let scrollOriginalTop = scrollObject;
const self = this;
const scroll = function() {
let nowtime = Date.now();
let detatime = nowtime - scrollTime;
scrollTime = nowtime;
scrollObject += detatime * speed;
if (count > cyclecount) {
let prizeindex = Math.floor(Math.random() * 6); //随机抽
let prizelevel = prizeindex % 3; //跟三取余找下标
self._prizearr.push(prizelevel + 1);
scrollObject = -prizelevel * SCROLLITEM - 22;
self._refSet(scrollRefer, scrollObject);
self._prizenum++;
if (self._prizenum === 3) {
setTimeout(self._getPrize, 1000);
}
return false;
} else {
if (scrollObject >= 0) { //重复
scrollObject = scrollOriginalTop;
count++;
self._refSet(scrollRefer, scrollObject);
} else {
self._refSet(scrollRefer, scrollObject);
}
requestAnimationFrame(scroll);
}
};
scroll();
}
_renderGameImage() {
return (
<View>
<Image source={require('./assets/scroll_item_1.png')} style={styles.prizeLevel} />
<Image source={require('./assets/scroll_item_2.png')} style={styles.prizeLevel} />
<Image source={require('./assets/scroll_item_3.png')} style={styles.prizeLevel} />
<Image source={require('./assets/scroll_item_1.png')} style={styles.prizeLevel} />
<Image source={require('./assets/scroll_item_2.png')} style={styles.prizeLevel} />
<Image source={require('./assets/scroll_item_3.png')} style={styles.prizeLevel} />
</View>
)
}
_renderAImage() {
return (
<View>
<Image source={require('./assets/a1.png')} style={styles.prizeLevel} />
<Image source={require('./assets/a2.png')} style={styles.prizeLevel} />
<Image source={require('./assets/a3.png')} style={styles.prizeLevel} />
<Image source={require('./assets/a1.png')} style={styles.prizeLevel} />
<Image source={require('./assets/a2.png')} style={styles.prizeLevel} />
<Image source={require('./assets/a3.png')} style={styles.prizeLevel} />
</View>
)
}
_renderBImage() {
return (
<View>
<Image source={require('./assets/b1.png')} style={styles.prizeLevel} />
<Image source={require('./assets/b2.png')} style={styles.prizeLevel} />
<Image source={require('./assets/b3.png')} style={styles.prizeLevel} />
<Image source={require('./assets/b1.png')} style={styles.prizeLevel} />
<Image source={require('./assets/b2.png')} style={styles.prizeLevel} />
<Image source={require('./assets/b3.png')} style={styles.prizeLevel} />
</View>
)
}
_renderCImage() {
return (
<View>
<Image source={require('./assets/c1.png')} style={styles.prizeLevel} />
<Image source={require('./assets/c2.png')} style={styles.prizeLevel} />
<Image source={require('./assets/c3.png')} style={styles.prizeLevel} />
<Image source={require('./assets/c1.png')} style={styles.prizeLevel} />
<Image source={require('./assets/c2.png')} style={styles.prizeLevel} />
<Image source={require('./assets/c3.png')} style={styles.prizeLevel} />
</View>
)
}
_startScroll() {
//清掉抽奖组
this._prizearr = [];
this._prizetext = '';
this.setState({
_prizetext: '',
_prizearr: []
});
let cyclecount = Math.floor(Math.random() * 5) + 20;
this._startScrollActurally(1, 4, cyclecount);
cyclecount = Math.floor(Math.random() * 5) + 20;
setTimeout(this._startScrollActurally, 1000, 2, 2, cyclecount);
cyclecount = Math.floor(Math.random() * 5) + 20;
setTimeout(this._startScrollActurally, 2000, 3, 1, cyclecount);
}
_showAccount() {
Alert.alert('当前余额为:¥' + this._account, null, [
{text: '确定', onPress: () => console.log('OK Pressed!')},
]);
}
_checkChoose() {
const arr1 = ['', '你选择了樱桃', '你选择了柠檬', '你选择了菠萝'];
const arr2 = ['', '50', '100', '200'];
if(this._num == 0) {
Alert.alert('温馨提示', '你还没有任何下注');
} else {
if(this._choose){
Alert.alert('温馨提示', '你已经下注了请点击摇杆进行游戏');
}
else {
const self = this;
Alert.alert(arr1[this._num], '将会扣取你¥' + arr2[this._num], [
{text: '取消', onPress: () => console.log('OK Pressed!')},
{text: '确认下注', onPress: () => self._setChoose()},
]);
}
}
}
_setChoose() {
const arr = ['', 50, 100, 200];
const currentAccount = this._account - arr[this._num];
this._choose = true;
this._account = currentAccount;
this.setState({
_choose: true,
_account: currentAccount
});
}
render() {
return (
<View style={styles.background}>
<View style={styles.container}>
<View>
<Text style={styles.title}>疯狂老虎机</Text>
</View>
<View>
<Text style={styles.account} onPress={this._showAccount}>¥{this._account}</Text>
</View>
</View>
<Image source={require('./assets/background.png')} style={styles.scene}>
{ this._model ?
<View>
<View>
<TouchableOpacity onPress={() => this._setNum(1)} activeOpacity={1}>
<Image source={require('./assets/choose_btn_3.png')} style={styles.choosebtn1}/>
</TouchableOpacity>
</View>
<View>
<TouchableOpacity onPress={() => this._setNum(2)} activeOpacity={1}>
<Image source={require('./assets/choose_btn_1.png')} style={styles.choosebtn2}/>
</TouchableOpacity>
</View>
<View>
<TouchableOpacity onPress={() => this._setNum(3)} activeOpacity={1}>
<Image source={require('./assets/choose_btn_2.png')} style={styles.choosebtn3}/>
</TouchableOpacity>
</View>
<View>
{ this._num == 0 &&
<Text style={styles.notice}>
你还没有选择下注的水果
</Text>
}
{ this._num == 1 &&
<Text style={styles.notice}>
你选择了樱桃
</Text>
}
{ this._num == 2 &&
<Text style={styles.notice}>
你选择了柠檬
</Text>
}
{ this._num == 3 &&
<Text style={styles.notice}>
你选择了菠萝
</Text>
}
</View>
<View>
<TouchableOpacity onPress={this._checkChoose} activeOpacity={1}>
<Image source={require('./assets/choose_ok.png')} style={styles.chooseend}/>
</TouchableOpacity>
</View>
</View>
:
<View>
<Text style={styles.notice}>大冒险模式</Text>
</View>
}
<View style={styles.prizeScrollContainer}>
<View style={{flexDirection:'row',justifyContent:'space-between',position:'absolute',left:0,height:40,alignItems:'center',marginRight: 20,backgroundColor:'transparent',transform:[{translateX:WORDPOSITION}]}} ref={WORDCONTAINER}>
<Text style={styles.word}>{this._prizetext}</Text>
<Text style={styles.word}>{this._prizetext}</Text>
<Text style={styles.word}>{this._prizetext}</Text>
<Text style={styles.word}>{this._prizetext}</Text>
<Text style={styles.word}>{this._prizetext}</Text>
</View>
</View>
<TouchableOpacity onPress={this._setGameModel} activeOpacity={1}>
<View style={styles.desc}>{false}</View>
</TouchableOpacity>
<View style={styles.prizeBingo}>
<View style={styles.prizeBingoContainer}>
<Animated.View style={{
transform:[{translateY:SCROLL1TOP}],
position: 'absolute',
flexDirection: 'column',
justifyContent: 'space-between',
width: 69, }}
ref = {SCROLL1REF}
>
{ this._model ?
this._renderGameImage()
:
this._renderAImage()
}
</Animated.View>
</View>
<View style={styles.prizeBingoContainer}>
<Animated.View style={{
transform:[{translateY:SCROLL2TOP}],
position: 'absolute',
flexDirection: 'column',
justifyContent: 'space-between',
width: 69, }}
ref={SCROLL2REF}
>
{ this._model ?
this._renderGameImage()
:
this._renderBImage()
}
</Animated.View>
</View>
<View style={styles.prizeBingoContainer}>
<Animated.View style={{
transform:[{translateY:SCROLL3TOP}],
position: 'absolute',
flexDirection: 'column',
justifyContent: 'space-between',
width: 69, }}
ref={SCROLL3REF}
>
{ this._model ?
this._renderGameImage()
:
this._renderCImage()
}
</Animated.View>
</View>
</View>
<TouchableOpacity onPress={this._startShake} activeOpacity={1}>
<Animated.Image source={require('./assets/btn_start.png')} style={[styles.starbtn,{transform:[{translateY:this.state.transformYValue},{rotateX:this.state.rotateXValue +'deg'}]}]} />
</TouchableOpacity>
</Image>
</View>
);
}
}
const styles = StyleSheet.create({
background: {
backgroundColor: '#282828',
},
container: {
paddingTop: 20,
flexDirection: 'column',
height: 60,
backgroundColor: '#f7b908',
justifyContent: 'center',
},
title: {
alignSelf: 'center',
color: '#FFFFFF',
fontWeight: 'bold',
fontSize: 16
},
account: {
marginTop: -18,
marginRight: 10,
alignSelf: 'flex-end',
color: '#FFFFFF',
fontWeight: 'bold',
fontSize: 16
},
scene: {
width: windowWidth,
height: windowHeight,
},
choosebtn1: {
position: 'absolute',
flexDirection: 'row',
marginTop: 12,
marginLeft: windowWidth * 0.02,
width: windowWidth * 0.30,
height: windowHeight * 0.06,
},
choosebtn2: {
position: 'absolute',
flexDirection: 'row',
marginTop: 12,
left: windowWidth * 0.35,
width: windowWidth * 0.30,
height: windowHeight * 0.06,
},
choosebtn3: {
position: 'absolute',
flexDirection: 'row',
marginTop: 12,
left: windowWidth * 0.68,
width: windowWidth * 0.30,
height: windowHeight * 0.06,
},
notice: {
position: 'relative',
color: '#FFFFFF',
textAlign: 'center',
top: windowHeight * 0.108,
fontSize: 16,
},
chooseend: {
position: 'absolute',
marginTop: windowHeight * 0.146,
width: windowWidth,
height: windowHeight * 0.056,
},
starbtn: {
position: 'absolute',
width: windowWidth * 0.09375,
height: windowHeight * 0.1080,
left: windowWidth * 0.8135,
top: windowHeight * 0.39,
},
prizeBingo: {
position: 'absolute',
flexDirection: 'row',
justifyContent: 'center',
width: windowWidth * 0.6531,
height: windowHeight * 0.2366,
left: windowWidth * 0.1093,
top: windowHeight * 0.3392,
overflow: 'hidden',
},
prizeBingoContainer: {
width: windowWidth * 0.215625,
height: windowHeight * 0.23666,
},
prizeLevel: {
width: windowWidth * 0.215625,
height: windowWidth * 0.215625,
},
desc: {
position: 'absolute',
width: windowWidth * 0.171875,
height: windowHeight * 0.125,
left: windowWidth * 0.78125,
top: windowHeight * 0.2393,
opacity: 0,
},
prizeScrollContainer: {
position: 'absolute',
width: windowWidth * 0.7375,
height: windowHeight * 0.054,
left: windowWidth * 0.0375,
top: windowHeight * 0.23529,
overflow: 'hidden',
},
word: {
marginTop: 4,
marginRight: 20,
fontSize: 15,
color: '#88F1F6',
backgroundColor: 'transparent'
}
});
AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);