-
Notifications
You must be signed in to change notification settings - Fork 20
/
Egg.js
149 lines (134 loc) · 3.26 KB
/
Egg.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
import React, { Component } from 'react';
import {
View,
PanResponder,
Animated,
} from 'react-native';
import PropTypes from 'prop-types';
export default class Egg extends Component {
constructor(props) {
super(props);
this.state = {
reset: null,
setp: [],
num: -1,
opacity: new Animated.Value(1),
};
}
componentWillMount() {
this._panResponder = PanResponder.create({
onStartShouldSetPanResponder: (evt, gestureState) => true,
onStartShouldSetPanResponderCapture: (evt, gestureState) => true,
onPanResponderGrant: (evt, gestureState) => {
if (this.props.touchOpacity) {
Animated.timing(
this.state.opacity,
{
toValue: 0.3,
duration: 200,
},
).start();
}
},
onPanResponderRelease: (evt, gestureState) => {
this.gestureSetp(gestureState);
},
onShouldBlockNativeResponder: (evt, gestureState) => {
return true;
},
});
}
componentWillUnmount() {
if (this.state.reset) {
clearTimeout(this.state.reset);
}
}
between = (input, min, max) => {
return input >= min && input <= max;
}
gestureSetp = (gestureState) => {
const { dx, dy } = gestureState;
const isXMove = !this.between(0, dx - 15, dx + 15) && Math.abs(dx) > Math.abs(dy);
const isYMove = !this.between(0, dy - 15, dy + 15) && Math.abs(dy) > Math.abs(dx);
const isTap = !isXMove && !isYMove;
let setp;
if (isXMove) {
if (dx > 0) {
setp = 'R';
} else {
setp = 'L';
}
} else if (isYMove) {
if (dy > 0) {
setp = 'D';
} else {
setp = 'U';
}
} else if (isTap) {
setp = 'T';
}
let newSetp = [...this.state.setp];
if (this.state.num === -1) {
const reset = setTimeout(() => {
this.setState({
setp: [],
num: -1,
});
}, this.props.timeLimit);
this.setState({
reset,
});
}
if (this.props.touchOpacity) {
Animated.timing(
this.state.opacity,
{
toValue: 1,
duration: 200,
},
).start();
}
newSetp[(this.state.num + 1) % 30] = setp;
this.setState({
setp: newSetp,
num: this.state.num + 1,
});
const find = ',';
const re = new RegExp(find, 'g');
let setpString = newSetp.toString().replace(re, '');
setpString = newSetp.length >= 30 ? setpString + setpString : setpString;
if (setpString.indexOf(this.props.setps.toUpperCase()) !== -1) {
this.props.onCatch();
this.setState({
setp: [],
num: -1,
});
}
if (this.props.onAction) {
this.props.onAction(setp)
}
}
render() {
return (
<Animated.View {...this._panResponder.panHandlers}
style={[this.props.style, { opacity: this.state.opacity }]}
>
{this.props.children}
</Animated.View>
);
}
}
Egg.propTypes = {
setps: PropTypes.string,
timeLimit: PropTypes.number,
onCatch: PropTypes.func.isRequired,
onAction: PropTypes.func,
touchOpacity: PropTypes.bool,
};
Egg.defaultProps = {
setps: 'UUDDLRLRTT',
timeLimit: 2000,
onCatch: null,
onAction: null,
touchOpacity: false,
};