Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GameMainScreen.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. // @flow
  2. import * as React from 'react';
  3. import {View} from 'react-native';
  4. import {Caption, IconButton, Text, withTheme} from 'react-native-paper';
  5. import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
  6. import i18n from 'i18n-js';
  7. import {StackNavigationProp} from '@react-navigation/stack';
  8. import GameLogic from '../logic/GameLogic';
  9. import type {GridType} from '../components/GridComponent';
  10. import GridComponent from '../components/GridComponent';
  11. import Preview from '../components/Preview';
  12. import MaterialHeaderButtons, {
  13. Item,
  14. } from '../../../components/Overrides/CustomHeaderButton';
  15. import type {CustomThemeType} from '../../../managers/ThemeManager';
  16. import type {OptionsDialogButtonType} from '../../../components/Dialogs/OptionsDialog';
  17. import OptionsDialog from '../../../components/Dialogs/OptionsDialog';
  18. type PropsType = {
  19. navigation: StackNavigationProp,
  20. route: {params: {highScore: number}},
  21. theme: CustomThemeType,
  22. };
  23. type StateType = {
  24. grid: GridType,
  25. gameTime: number,
  26. gameScore: number,
  27. gameLevel: number,
  28. dialogVisible: boolean,
  29. dialogTitle: string,
  30. dialogMessage: string,
  31. dialogButtons: Array<OptionsDialogButtonType>,
  32. onDialogDismiss: () => void,
  33. };
  34. class GameMainScreen extends React.Component<PropsType, StateType> {
  35. static getFormattedTime(seconds: number): string {
  36. const date = new Date();
  37. date.setHours(0);
  38. date.setMinutes(0);
  39. date.setSeconds(seconds);
  40. let format;
  41. if (date.getHours())
  42. format = `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
  43. else if (date.getMinutes())
  44. format = `${date.getMinutes()}:${date.getSeconds()}`;
  45. else format = date.getSeconds().toString();
  46. return format;
  47. }
  48. logic: GameLogic;
  49. highScore: number | null;
  50. constructor(props: PropsType) {
  51. super(props);
  52. this.logic = new GameLogic(20, 10, props.theme);
  53. this.state = {
  54. grid: this.logic.getCurrentGrid(),
  55. gameTime: 0,
  56. gameScore: 0,
  57. gameLevel: 0,
  58. dialogVisible: false,
  59. dialogTitle: '',
  60. dialogMessage: '',
  61. dialogButtons: [],
  62. onDialogDismiss: () => {},
  63. };
  64. if (props.route.params != null)
  65. this.highScore = props.route.params.highScore;
  66. }
  67. componentDidMount() {
  68. const {navigation} = this.props;
  69. navigation.setOptions({
  70. headerRight: this.getRightButton,
  71. });
  72. this.startGame();
  73. }
  74. componentWillUnmount() {
  75. this.logic.endGame(false);
  76. }
  77. getRightButton = (): React.Node => {
  78. return (
  79. <MaterialHeaderButtons>
  80. <Item title="pause" iconName="pause" onPress={this.togglePause} />
  81. </MaterialHeaderButtons>
  82. );
  83. };
  84. onTick = (score: number, level: number, newGrid: GridType) => {
  85. this.setState({
  86. gameScore: score,
  87. gameLevel: level,
  88. grid: newGrid,
  89. });
  90. };
  91. onClock = (time: number) => {
  92. this.setState({
  93. gameTime: time,
  94. });
  95. };
  96. onDialogDismiss = () => {
  97. this.setState({dialogVisible: false});
  98. };
  99. onGameEnd = (time: number, score: number, isRestart: boolean) => {
  100. const {props, state} = this;
  101. this.setState({
  102. gameTime: time,
  103. gameScore: score,
  104. });
  105. if (!isRestart)
  106. props.navigation.replace('game-start', {
  107. score: state.gameScore,
  108. level: state.gameLevel,
  109. time: state.gameTime,
  110. });
  111. };
  112. getStatusIcons(): React.Node {
  113. const {props, state} = this;
  114. return (
  115. <View
  116. style={{
  117. flex: 1,
  118. marginTop: 'auto',
  119. marginBottom: 'auto',
  120. }}>
  121. <View
  122. style={{
  123. marginLeft: 'auto',
  124. marginRight: 'auto',
  125. }}>
  126. <Caption
  127. style={{
  128. marginLeft: 'auto',
  129. marginRight: 'auto',
  130. marginBottom: 5,
  131. }}>
  132. {i18n.t('screens.game.time')}
  133. </Caption>
  134. <View
  135. style={{
  136. flexDirection: 'row',
  137. }}>
  138. <MaterialCommunityIcons
  139. name="timer"
  140. color={props.theme.colors.subtitle}
  141. size={20}
  142. />
  143. <Text
  144. style={{
  145. marginLeft: 5,
  146. color: props.theme.colors.subtitle,
  147. }}>
  148. {GameMainScreen.getFormattedTime(state.gameTime)}
  149. </Text>
  150. </View>
  151. </View>
  152. <View
  153. style={{
  154. marginLeft: 'auto',
  155. marginRight: 'auto',
  156. marginTop: 20,
  157. }}>
  158. <Caption
  159. style={{
  160. marginLeft: 'auto',
  161. marginRight: 'auto',
  162. marginBottom: 5,
  163. }}>
  164. {i18n.t('screens.game.level')}
  165. </Caption>
  166. <View
  167. style={{
  168. flexDirection: 'row',
  169. }}>
  170. <MaterialCommunityIcons
  171. name="gamepad-square"
  172. color={props.theme.colors.text}
  173. size={20}
  174. />
  175. <Text
  176. style={{
  177. marginLeft: 5,
  178. }}>
  179. {state.gameLevel}
  180. </Text>
  181. </View>
  182. </View>
  183. </View>
  184. );
  185. }
  186. getScoreIcon(): React.Node {
  187. const {props, state} = this;
  188. const highScore =
  189. this.highScore == null || state.gameScore > this.highScore
  190. ? state.gameScore
  191. : this.highScore;
  192. return (
  193. <View
  194. style={{
  195. marginTop: 10,
  196. marginBottom: 10,
  197. }}>
  198. <View
  199. style={{
  200. flexDirection: 'row',
  201. marginLeft: 'auto',
  202. marginRight: 'auto',
  203. }}>
  204. <Text
  205. style={{
  206. marginLeft: 5,
  207. fontSize: 20,
  208. }}>
  209. {i18n.t('screens.game.score', {score: state.gameScore})}
  210. </Text>
  211. <MaterialCommunityIcons
  212. name="star"
  213. color={props.theme.colors.tetrisScore}
  214. size={20}
  215. style={{
  216. marginTop: 'auto',
  217. marginBottom: 'auto',
  218. marginLeft: 5,
  219. }}
  220. />
  221. </View>
  222. <View
  223. style={{
  224. flexDirection: 'row',
  225. marginLeft: 'auto',
  226. marginRight: 'auto',
  227. marginTop: 5,
  228. }}>
  229. <Text
  230. style={{
  231. marginLeft: 5,
  232. fontSize: 10,
  233. color: props.theme.colors.textDisabled,
  234. }}>
  235. {i18n.t('screens.game.highScore', {score: highScore})}
  236. </Text>
  237. <MaterialCommunityIcons
  238. name="star"
  239. color={props.theme.colors.tetrisScore}
  240. size={10}
  241. style={{
  242. marginTop: 'auto',
  243. marginBottom: 'auto',
  244. marginLeft: 5,
  245. }}
  246. />
  247. </View>
  248. </View>
  249. );
  250. }
  251. getControlButtons(): React.Node {
  252. const {props} = this;
  253. return (
  254. <View
  255. style={{
  256. height: 80,
  257. flexDirection: 'row',
  258. }}>
  259. <IconButton
  260. icon="rotate-right-variant"
  261. size={40}
  262. onPress={() => {
  263. this.logic.rotatePressed(this.updateGrid);
  264. }}
  265. style={{flex: 1}}
  266. />
  267. <View
  268. style={{
  269. flexDirection: 'row',
  270. flex: 4,
  271. }}>
  272. <IconButton
  273. icon="chevron-left"
  274. size={40}
  275. style={{flex: 1}}
  276. onPress={() => {
  277. this.logic.pressedOut();
  278. }}
  279. onPressIn={() => {
  280. this.logic.leftPressedIn(this.updateGrid);
  281. }}
  282. />
  283. <IconButton
  284. icon="chevron-right"
  285. size={40}
  286. style={{flex: 1}}
  287. onPress={() => {
  288. this.logic.pressedOut();
  289. }}
  290. onPressIn={() => {
  291. this.logic.rightPressed(this.updateGrid);
  292. }}
  293. />
  294. </View>
  295. <IconButton
  296. icon="arrow-down-bold"
  297. size={40}
  298. onPressIn={() => {
  299. this.logic.downPressedIn(this.updateGridScore);
  300. }}
  301. onPress={() => {
  302. this.logic.pressedOut();
  303. }}
  304. style={{flex: 1}}
  305. color={props.theme.colors.tetrisScore}
  306. />
  307. </View>
  308. );
  309. }
  310. updateGrid = (newGrid: GridType) => {
  311. this.setState({
  312. grid: newGrid,
  313. });
  314. };
  315. updateGridScore = (newGrid: GridType, score?: number) => {
  316. this.setState((prevState: StateType): {
  317. grid: GridType,
  318. gameScore: number,
  319. } => ({
  320. grid: newGrid,
  321. gameScore: score != null ? score : prevState.gameScore,
  322. }));
  323. };
  324. togglePause = () => {
  325. this.logic.togglePause();
  326. if (this.logic.isGamePaused()) this.showPausePopup();
  327. };
  328. showPausePopup = () => {
  329. const onDismiss = () => {
  330. this.togglePause();
  331. this.onDialogDismiss();
  332. };
  333. this.setState({
  334. dialogVisible: true,
  335. dialogTitle: i18n.t('screens.game.pause'),
  336. dialogMessage: i18n.t('screens.game.pauseMessage'),
  337. dialogButtons: [
  338. {
  339. title: i18n.t('screens.game.restart.text'),
  340. onPress: this.showRestartConfirm,
  341. },
  342. {
  343. title: i18n.t('screens.game.resume'),
  344. onPress: onDismiss,
  345. },
  346. ],
  347. onDialogDismiss: onDismiss,
  348. });
  349. };
  350. showRestartConfirm = () => {
  351. this.setState({
  352. dialogVisible: true,
  353. dialogTitle: i18n.t('screens.game.restart.confirm'),
  354. dialogMessage: i18n.t('screens.game.restart.confirmMessage'),
  355. dialogButtons: [
  356. {
  357. title: i18n.t('screens.game.restart.confirmYes'),
  358. onPress: () => {
  359. this.onDialogDismiss();
  360. this.startGame();
  361. },
  362. },
  363. {
  364. title: i18n.t('screens.game.restart.confirmNo'),
  365. onPress: this.showPausePopup,
  366. },
  367. ],
  368. onDialogDismiss: this.showPausePopup,
  369. });
  370. };
  371. startGame = () => {
  372. this.logic.startGame(this.onTick, this.onClock, this.onGameEnd);
  373. };
  374. render(): React.Node {
  375. const {props, state} = this;
  376. return (
  377. <View style={{flex: 1}}>
  378. <View
  379. style={{
  380. flex: 1,
  381. flexDirection: 'row',
  382. }}>
  383. {this.getStatusIcons()}
  384. <View style={{flex: 4}}>
  385. {this.getScoreIcon()}
  386. <GridComponent
  387. width={this.logic.getWidth()}
  388. height={this.logic.getHeight()}
  389. grid={state.grid}
  390. style={{
  391. backgroundColor: props.theme.colors.tetrisBackground,
  392. flex: 1,
  393. marginLeft: 'auto',
  394. marginRight: 'auto',
  395. }}
  396. />
  397. </View>
  398. <View style={{flex: 1}}>
  399. <Preview
  400. items={this.logic.getNextPiecesPreviews()}
  401. style={{
  402. marginLeft: 'auto',
  403. marginRight: 'auto',
  404. marginTop: 10,
  405. }}
  406. />
  407. </View>
  408. </View>
  409. {this.getControlButtons()}
  410. <OptionsDialog
  411. visible={state.dialogVisible}
  412. title={state.dialogTitle}
  413. message={state.dialogMessage}
  414. buttons={state.dialogButtons}
  415. onDismiss={state.onDialogDismiss}
  416. />
  417. </View>
  418. );
  419. }
  420. }
  421. export default withTheme(GameMainScreen);