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 13KB

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