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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // @flow
  2. import * as React from 'react';
  3. import {View} from 'react-native';
  4. import {IconButton, Text, withTheme} from 'react-native-paper';
  5. import MaterialCommunityIcons from "react-native-vector-icons/MaterialCommunityIcons";
  6. import GameLogic from "../logic/GameLogic";
  7. import type {Grid} from "../components/GridComponent";
  8. import GridComponent from "../components/GridComponent";
  9. import Preview from "../components/Preview";
  10. import i18n from "i18n-js";
  11. import MaterialHeaderButtons, {Item} from "../../../components/Overrides/CustomHeaderButton";
  12. import {StackNavigationProp} from "@react-navigation/stack";
  13. import type {CustomTheme} from "../../../managers/ThemeManager";
  14. import type {OptionsDialogButton} from "../../../components/Dialogs/OptionsDialog";
  15. import OptionsDialog from "../../../components/Dialogs/OptionsDialog";
  16. type Props = {
  17. navigation: StackNavigationProp,
  18. theme: CustomTheme,
  19. }
  20. type State = {
  21. grid: Grid,
  22. gameRunning: boolean,
  23. gameTime: number,
  24. gameScore: number,
  25. gameLevel: number,
  26. dialogVisible: boolean,
  27. dialogTitle: string,
  28. dialogMessage: string,
  29. dialogButtons: Array<OptionsDialogButton>,
  30. onDialogDismiss: () => void,
  31. }
  32. class GameMainScreen extends React.Component<Props, State> {
  33. logic: GameLogic;
  34. constructor(props) {
  35. super(props);
  36. this.logic = new GameLogic(20, 10, this.props.theme);
  37. this.state = {
  38. grid: this.logic.getCurrentGrid(),
  39. gameRunning: false,
  40. gameTime: 0,
  41. gameScore: 0,
  42. gameLevel: 0,
  43. dialogVisible: false,
  44. dialogTitle: "",
  45. dialogMessage: "",
  46. dialogButtons: [],
  47. onDialogDismiss: () => {},
  48. };
  49. this.props.navigation.addListener('blur', this.onScreenBlur);
  50. this.props.navigation.addListener('focus', this.onScreenFocus);
  51. }
  52. componentDidMount() {
  53. this.props.navigation.setOptions({
  54. headerRight: this.getRightButton,
  55. });
  56. this.startGame();
  57. }
  58. getRightButton = () => {
  59. return <MaterialHeaderButtons>
  60. <Item title="pause" iconName="pause" onPress={this.togglePause}/>
  61. </MaterialHeaderButtons>;
  62. }
  63. /**
  64. * Remove any interval on un-focus
  65. */
  66. onScreenBlur = () => {
  67. if (!this.logic.isGamePaused())
  68. this.logic.togglePause();
  69. }
  70. onScreenFocus = () => {
  71. if (!this.logic.isGameRunning())
  72. this.startGame();
  73. else if (this.logic.isGamePaused())
  74. this.showPausePopup();
  75. }
  76. getFormattedTime(seconds: number) {
  77. let date = new Date();
  78. date.setHours(0);
  79. date.setMinutes(0);
  80. date.setSeconds(seconds);
  81. let format;
  82. if (date.getHours())
  83. format = date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
  84. else if (date.getMinutes())
  85. format = date.getMinutes() + ':' + date.getSeconds();
  86. else
  87. format = date.getSeconds();
  88. return format;
  89. }
  90. onTick = (score: number, level: number, newGrid: Grid) => {
  91. this.setState({
  92. gameScore: score,
  93. gameLevel: level,
  94. grid: newGrid,
  95. });
  96. }
  97. onClock = (time: number) => {
  98. this.setState({
  99. gameTime: time,
  100. });
  101. }
  102. updateGrid = (newGrid: Grid) => {
  103. this.setState({
  104. grid: newGrid,
  105. });
  106. }
  107. updateGridScore = (newGrid: Grid, score: number) => {
  108. this.setState({
  109. grid: newGrid,
  110. gameScore: score,
  111. });
  112. }
  113. togglePause = () => {
  114. this.logic.togglePause();
  115. if (this.logic.isGamePaused())
  116. this.showPausePopup();
  117. }
  118. onDialogDismiss = () => this.setState({dialogVisible: false});
  119. showPausePopup = () => {
  120. const onDismiss = () => {
  121. this.togglePause();
  122. this.onDialogDismiss();
  123. };
  124. this.setState({
  125. dialogVisible: true,
  126. dialogTitle: i18n.t("screens.game.pause"),
  127. dialogMessage: i18n.t("screens.game.pauseMessage"),
  128. dialogButtons: [
  129. {
  130. title: i18n.t("screens.game.restart.text"),
  131. onPress: this.showRestartConfirm
  132. },
  133. {
  134. title: i18n.t("screens.game.resume"),
  135. onPress: onDismiss
  136. }
  137. ],
  138. onDialogDismiss: onDismiss,
  139. });
  140. }
  141. showRestartConfirm = () => {
  142. this.setState({
  143. dialogVisible: true,
  144. dialogTitle: i18n.t("screens.game.restart.confirm"),
  145. dialogMessage: i18n.t("screens.game.restart.confirmMessage"),
  146. dialogButtons: [
  147. {
  148. title: i18n.t("screens.game.restart.confirmYes"),
  149. onPress: () => {
  150. this.onDialogDismiss();
  151. this.startGame();
  152. }
  153. },
  154. {
  155. title: i18n.t("screens.game.restart.confirmNo"),
  156. onPress: this.showPausePopup
  157. }
  158. ],
  159. onDialogDismiss: this.showPausePopup,
  160. });
  161. }
  162. showGameOverConfirm() {
  163. let message = i18n.t("screens.game.gameOver.score") + this.state.gameScore + '\n';
  164. message += i18n.t("screens.game.gameOver.level") + this.state.gameLevel + '\n';
  165. message += i18n.t("screens.game.gameOver.time") + this.getFormattedTime(this.state.gameTime) + '\n';
  166. const onDismiss = () => {
  167. this.onDialogDismiss();
  168. this.startGame();
  169. };
  170. this.setState({
  171. dialogVisible: true,
  172. dialogTitle: i18n.t("screens.game.gameOver.text"),
  173. dialogMessage: message,
  174. dialogButtons: [
  175. {
  176. title: i18n.t("screens.game.gameOver.exit"),
  177. onPress: () => this.props.navigation.goBack()
  178. },
  179. {
  180. title: i18n.t("screens.game.resume"),
  181. onPress: onDismiss
  182. }
  183. ],
  184. onDialogDismiss: onDismiss,
  185. });
  186. }
  187. startGame = () => {
  188. this.logic.startGame(this.onTick, this.onClock, this.onGameEnd);
  189. this.setState({
  190. gameRunning: true,
  191. });
  192. }
  193. onGameEnd = (time: number, score: number, isRestart: boolean) => {
  194. this.setState({
  195. gameTime: time,
  196. gameScore: score,
  197. gameRunning: false,
  198. });
  199. if (!isRestart)
  200. this.showGameOverConfirm();
  201. }
  202. render() {
  203. const colors = this.props.theme.colors;
  204. return (
  205. <View style={{
  206. width: '100%',
  207. height: '100%',
  208. }}>
  209. <View style={{
  210. flexDirection: 'row',
  211. position: 'absolute',
  212. top: 5,
  213. left: 10,
  214. }}>
  215. <MaterialCommunityIcons
  216. name={'timer'}
  217. color={colors.subtitle}
  218. size={20}/>
  219. <Text style={{
  220. marginLeft: 5,
  221. color: colors.subtitle
  222. }}>{this.getFormattedTime(this.state.gameTime)}</Text>
  223. </View>
  224. <View style={{
  225. flexDirection: 'row',
  226. position: 'absolute',
  227. top: 50,
  228. left: 10,
  229. }}>
  230. <MaterialCommunityIcons
  231. name={'gamepad'}
  232. color={colors.text}
  233. size={20}/>
  234. <Text style={{
  235. marginLeft: 5
  236. }}>{this.state.gameLevel}</Text>
  237. </View>
  238. <View style={{
  239. flexDirection: 'row',
  240. marginRight: 'auto',
  241. marginLeft: 'auto',
  242. }}>
  243. <MaterialCommunityIcons
  244. name={'star'}
  245. color={colors.tetrisScore}
  246. size={30}/>
  247. <Text style={{
  248. marginLeft: 5,
  249. fontSize: 22,
  250. }}>{this.state.gameScore}</Text>
  251. </View>
  252. <GridComponent
  253. width={this.logic.getWidth()}
  254. height={this.logic.getHeight()}
  255. containerMaxHeight={'80%'}
  256. containerMaxWidth={'60%'}
  257. grid={this.state.grid}
  258. backgroundColor={colors.tetrisBackground}
  259. />
  260. <View style={{
  261. position: 'absolute',
  262. top: 50,
  263. right: 5,
  264. }}>
  265. <Preview
  266. items={this.logic.getNextPiecesPreviews()}
  267. />
  268. </View>
  269. <View style={{
  270. position: 'absolute',
  271. bottom: 0,
  272. flexDirection: 'row',
  273. width: '100%',
  274. }}>
  275. <IconButton
  276. icon="rotate-right-variant"
  277. size={40}
  278. onPress={() => this.logic.rotatePressed(this.updateGrid)}
  279. style={{marginRight: 'auto'}}
  280. />
  281. <View style={{
  282. flexDirection: 'row',
  283. }}>
  284. <IconButton
  285. icon="arrow-left"
  286. size={40}
  287. onPress={() => this.logic.pressedOut()}
  288. onPressIn={() => this.logic.leftPressedIn(this.updateGrid)}
  289. />
  290. <IconButton
  291. icon="arrow-right"
  292. size={40}
  293. onPress={() => this.logic.pressedOut()}
  294. onPressIn={() => this.logic.rightPressed(this.updateGrid)}
  295. />
  296. </View>
  297. <IconButton
  298. icon="arrow-down"
  299. size={40}
  300. onPressIn={() => this.logic.downPressedIn(this.updateGridScore)}
  301. onPress={() => this.logic.pressedOut()}
  302. style={{marginLeft: 'auto'}}
  303. color={colors.tetrisScore}
  304. />
  305. </View>
  306. <OptionsDialog
  307. visible={this.state.dialogVisible}
  308. title={this.state.dialogTitle}
  309. message={this.state.dialogMessage}
  310. buttons={this.state.dialogButtons}
  311. onDismiss={this.state.onDialogDismiss}
  312. />
  313. </View>
  314. );
  315. }
  316. }
  317. export default withTheme(GameMainScreen);