├── .gitIgnore ├── README.md ├── actionscript-tetris ├── README.md ├── assets │ ├── atlas │ │ ├── buttons │ │ │ ├── button_cancel_over.png │ │ │ ├── button_cancel_up.png │ │ │ ├── button_config_over.png │ │ │ ├── button_config_up.png │ │ │ ├── button_confirm_over.png │ │ │ ├── button_confirm_up.png │ │ │ ├── button_home_over.png │ │ │ ├── button_home_up.png │ │ │ ├── button_pause_over.png │ │ │ ├── button_pause_up.png │ │ │ ├── button_reset_over.png │ │ │ ├── button_reset_up.png │ │ │ ├── button_resume_over.png │ │ │ ├── button_resume_up.png │ │ │ ├── button_retry_over.png │ │ │ ├── button_retry_up.png │ │ │ ├── button_start_over.png │ │ │ └── button_start_up.png │ │ ├── displays │ │ │ ├── grid.png │ │ │ ├── logo_setzer.png │ │ │ ├── logo_tetris.png │ │ │ └── next_tile.png │ │ ├── extras │ │ │ └── logo_actionscript.png │ │ ├── tetris-starling-atlas-0.png │ │ ├── tetris-starling-atlas.xml │ │ ├── tetris-startling-atlas.tps │ │ ├── tiles.png │ │ └── tiles │ │ │ ├── tile_01.png │ │ │ ├── tile_02.png │ │ │ ├── tile_03.png │ │ │ ├── tile_04.png │ │ │ ├── tile_05.png │ │ │ ├── tile_06.png │ │ │ ├── tile_07.png │ │ │ └── tile_08.png │ └── fonts │ │ ├── SimpleSmalPixel7_.png │ │ ├── SimpleSmallPixel7.fnt │ │ └── SimpleSmallPixel7_0.png ├── bin │ └── actionscript-tetris.swf ├── gif_tetris_as_demo.gif ├── img_cover_tetris_as.png ├── img_game_tetris_as.png ├── img_ss_tetris_as_01.png ├── img_ss_tetris_as_02.png ├── libs │ ├── as3-signals-v0.9-BETA.swc │ ├── flexunit-4.2.0-20140410-as3_4.12.0.swc │ ├── greensock.swc │ ├── robotlegs-extensions-palidor-v1.0.swc │ ├── robotlegs-framework-v2.2.1.swc │ └── starling-2.1.swc ├── src │ └── setzer │ │ └── tetris │ │ ├── Main.as │ │ ├── assets │ │ ├── Assets.as │ │ ├── AssetsInfo.as │ │ └── Embeds.as │ │ ├── commands │ │ ├── CreateLevelCommand.as │ │ ├── GameOverCommand.as │ │ ├── GetNextPieceCommand.as │ │ └── IncreasePointsCommand.as │ │ ├── configs │ │ └── GameConfig.as │ │ ├── events │ │ ├── FlowEvent.as │ │ └── GameEvent.as │ │ ├── factories │ │ ├── StarlingFactory.as │ │ └── TileGroupFactory.as │ │ ├── managers │ │ ├── GameManager.as │ │ └── SharedObjectManager.as │ │ ├── mediators │ │ ├── ConfigViewMediator.as │ │ ├── GameOverPopupMediator.as │ │ ├── GameViewMediator.as │ │ ├── GridComponentMediator.as │ │ ├── HUDGameComponentMediator.as │ │ ├── HomeViewMediator.as │ │ ├── InfoPopupMediator.as │ │ ├── IntroViewMediator.as │ │ ├── NextPieceComponentMediator.as │ │ ├── PausePopupMediator.as │ │ ├── ResetConfirmPopupMediator.as │ │ └── StartingPopupMediator.as │ │ ├── models │ │ ├── GameModel.as │ │ ├── GameStatus.as │ │ ├── Grid.as │ │ ├── Tile.as │ │ ├── TileGroup.as │ │ └── TileGroupType.as │ │ ├── services │ │ ├── FlowService.as │ │ └── GameService.as │ │ ├── utils │ │ ├── Colors.as │ │ ├── GameUtils.as │ │ ├── MagicValues.as │ │ ├── Texts.as │ │ ├── TilePool.as │ │ └── ViewPortSize.as │ │ └── views │ │ ├── ConfigView.as │ │ ├── GameOverPopup.as │ │ ├── GameView.as │ │ ├── HomeView.as │ │ ├── InfoPopup.as │ │ ├── IntroView.as │ │ ├── MainStarlingView.as │ │ ├── PausePopup.as │ │ ├── ResetConfimPopup.as │ │ ├── StartingPopup.as │ │ └── components │ │ ├── DoubleTextField.as │ │ ├── GridComponent.as │ │ ├── HUDGameComponent.as │ │ ├── NextPieceComponent.as │ │ └── TileDisplay.as └── test │ └── setzer │ └── tetris │ ├── factories │ └── TileGroupFactoryTest.as │ └── models │ ├── GameModelTest.as │ ├── GridTest.as │ ├── TileGroupTest.as │ └── TileTest.as ├── gif_tetris_as_demo.gif ├── gif_tetris_ts_demo.gif ├── img_cover_tetris.png ├── img_game_tetris.png └── typescript-tetris ├── .editorconfig ├── .prettierrc ├── README.md ├── assets ├── atlas │ ├── buttons │ │ ├── button_cancel_over.png │ │ ├── button_cancel_up.png │ │ ├── button_config_over.png │ │ ├── button_config_up.png │ │ ├── button_confirm_over.png │ │ ├── button_confirm_up.png │ │ ├── button_home_over.png │ │ ├── button_home_up.png │ │ ├── button_pause_over.png │ │ ├── button_pause_up.png │ │ ├── button_reset_over.png │ │ ├── button_reset_up.png │ │ ├── button_resume_over.png │ │ ├── button_resume_up.png │ │ ├── button_retry_over.png │ │ ├── button_retry_up.png │ │ ├── button_start_over.png │ │ └── button_start_up.png │ ├── displays │ │ ├── grid.png │ │ ├── logo_setzer.png │ │ ├── logo_tetris.png │ │ └── next_tile.png │ ├── tetris-pixijs-atlas.tps │ ├── tiles.png │ └── tiles │ │ ├── tile_01.png │ │ ├── tile_02.png │ │ ├── tile_03.png │ │ ├── tile_04.png │ │ ├── tile_05.png │ │ ├── tile_06.png │ │ ├── tile_07.png │ │ └── tile_08.png ├── fonts │ ├── SimpleSmalPixel7_.png │ ├── SimpleSmallPixel7.fnt │ └── SimpleSmallPixel7_0.png ├── logo_typescript.png ├── tetris-pixijs-atlas.json └── tetris-pixijs-atlas.png ├── media ├── gif_tetris_ts_demo.gif ├── img_cover_tetris_ts.png ├── img_game_tetris_ts.png └── img_ss_tetris_ts_01.png ├── package.json ├── src ├── commands │ ├── CreateLevelCommand.ts │ ├── GameOverCommand.ts │ ├── GetNextPieceCommand.ts │ └── IncreasePointsCommand.ts ├── configs │ ├── GameConfig.ts │ ├── PalidorConfig.ts │ └── ViewsConfig.ts ├── events │ ├── FlowEvent.ts │ └── GameEvent.ts ├── index.ts ├── managers │ └── GameManager.ts ├── mediators │ ├── GameOverPopupMediator.ts │ ├── GameViewMediator.ts │ ├── GridComponentMediator.ts │ ├── HUDGameComponentMediator.ts │ ├── HomeViewMediator.ts │ ├── InfoPopupMediator.ts │ ├── IntroViewMediator.ts │ ├── NextPieceComponentMediator.ts │ ├── OptionsViewMediator.ts │ ├── PausePopupMediator.ts │ ├── ResetConfirmPopupMediator.ts │ └── StartingPopupMediator.ts ├── models │ ├── GameModel.ts │ ├── GameStatus.ts │ ├── Grid.ts │ ├── Tile.ts │ ├── TileGroup.ts │ └── TileGroupType.ts ├── services │ ├── FlowService.ts │ └── GameService.ts ├── utils │ ├── AtlasKeys.ts │ ├── Colors.ts │ ├── GameUtils.ts │ ├── MagicValues.ts │ ├── PixiFactory.ts │ ├── Texts.ts │ ├── TileGroupFactory.ts │ ├── TilePool.ts │ └── ViewPortSize.ts └── views │ ├── GameOverPopup.ts │ ├── GameView.ts │ ├── HomeView.ts │ ├── InfoPopup.ts │ ├── IntroView.ts │ ├── OptionsView.ts │ ├── PausePopup.ts │ ├── ResetConfirmPopup.ts │ ├── StartingPopup.ts │ └── components │ ├── CustomButton.ts │ ├── DoubleTextField.ts │ ├── GridComponent.ts │ ├── HUDGameComponent.ts │ ├── NextPieceComponent.ts │ └── TileDisplay.ts ├── test ├── entry.test.ts ├── models │ ├── GameModel.test.ts │ ├── Grid.test.ts │ ├── Tile.test.ts │ └── TileGroup.test.ts └── utils │ └── TileGroupFactory.test.ts ├── tsconfig.json ├── tslint.json ├── webpack.setzer.config.js └── yarn.lock /.gitIgnore: -------------------------------------------------------------------------------- 1 | # Game-Tetris 2 | 3 | # Build and Release Folders 4 | bin-debug/ 5 | bin-release/ 6 | temp/ 7 | dist/ 8 | node_modules/ 9 | 10 | # Project property files 11 | .actionScriptProperties 12 | .flexLibProperties 13 | .settings/ 14 | .project 15 | 16 | *.iml 17 | *.ipr 18 | *.iws 19 | *.DS_STORE 20 | .idea/ 21 | out/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![cover](img_cover_tetris.png) 2 | 3 | In this repository, you will find the source of the game Tetris, developed in some different programming languages such as Actionscript and TypeScript, following the micro architecture Robotlegs. 4 | 5 | ### Status 6 | 7 | + **[ActionScript](https://ronaldosetzer.github.io/portfolio/open_source/tetris_as/):** Done. 8 | + **Haxe/OpenFl:** in Progress. 9 | + **[TypeScript](https://ronaldosetzer.github.io/portfolio/open_source/tetris_ts/):** Done. 10 | + **C#/Unity:** to do. 11 | 12 | * * * 13 | 14 | ### Gameplay ActionScript 15 | 16 | ![gif_as](gif_tetris_as_demo.gif) 17 | 18 | * * * 19 | 20 | ### Gameplay TypeScript 21 | 22 | ![gif_ts](gif_tetris_ts_demo.gif) 23 | 24 | * * * 25 | 26 | ### Demos 27 | + **[ActionScript](https://ronaldosetzer.github.io/portfolio/open_source/tetris_as/)** 28 | + **[TypeScript](https://ronaldosetzer.github.io/portfolio/open_source/tetris_ts/)** 29 | 30 | * * * 31 | 32 | 33 | ### Screenshots 34 | ![screenshot01](img_game_tetris.png) 35 | * * * 36 | 37 | **Ronaldo Santiago** - Game Developer [ [portfolio](https://ronaldosetzer.github.io/portfolio/) ] -------------------------------------------------------------------------------- /actionscript-tetris/README.md: -------------------------------------------------------------------------------- 1 | ![cover](img_cover_tetris_as.png) 2 | 3 | This is an ActionScript open-source game which was developed to shows how to integrate Starling, Robotlegs, and Palidor. 4 | 5 | + **Category:** Puzzle. 6 | + **Platform:** Web. 7 | + **Language:** Actionscript 3. 8 | + **Technologies:** StarlingFW, Robotlegs, Palidor, FlexUnit. 9 | 10 | 11 | * * * 12 | 13 | ### Gameplay 14 | 15 | ![gif_as](gif_tetris_as_demo.gif) 16 | 17 | * * * 18 | 19 | 20 | ### Dependencies 21 | 22 | + [Starling](https://github.com/Gamua/Starling-Framework) 23 | + [Robotlegs 2](https://github.com/robotlegs/robotlegs-framework) 24 | + [Palidor](https://github.com/RonaldoSetzer/robotlegs-extensions-Palidor) 25 | + [FlexUnit 4](https://flex.apache.org/download-flexunit.html) 26 | 27 | 28 | * * * 29 | 30 | 31 | ### Demos 32 | + **[ActionScript](https://ronaldosetzer.github.io/portfolio/open_source/tetris_as/)** 33 | + **[TypeScript](https://ronaldosetzer.github.io/portfolio/open_source/tetris_ts/)** 34 | 35 | 36 | * * * 37 | 38 | ### Game 39 | 40 | #### Game Flow 41 | 42 | ![screenshot01](img_ss_tetris_as_01.png) 43 | 44 | 45 | #### Views - Managers - Mediators - Commands 46 | 47 | ![screenshot02](img_ss_tetris_as_02.png) 48 | 49 | 50 | ### Palidor Config 51 | #### GameConfig.as 52 | ```as3 53 | public class GameConfig implements IConfig 54 | { 55 | ... 56 | 57 | private function mapCommands():void 58 | { 59 | commandMap.map( GameEvent.CREATE_LEVEL ).toCommand( CreateLevelCommand ); 60 | commandMap.map( GameEvent.GET_NEXT_PIECE ).toCommand( GetNextPieceCommand ); 61 | commandMap.map( GameEvent.INCREASE_POINTS ).toCommand( IncreasePointsCommand ); 62 | commandMap.map( GameEvent.GAME_OVER ).toCommand( GameOverCommand ); 63 | } 64 | private function mapMediators():void 65 | { 66 | //Robotlegs 67 | 68 | mediatorMap.map( IntroView ).toMediator( IntroViewMediator ); 69 | mediatorMap.map( HomeView ).toMediator( HomeViewMediator ); 70 | mediatorMap.map( ConfigView ).toMediator( ConfigViewMediator ); 71 | mediatorMap.map( GameView ).toMediator( GameViewMediator ); 72 | 73 | mediatorMap.map( GridComponent ).toMediator( GridComponentMediator ); 74 | mediatorMap.map( NextPieceComponent ).toMediator( NextPieceComponentMediator ); 75 | mediatorMap.map( HUDGameComponent ).toMediator( HUDGameComponentMediator ); 76 | 77 | mediatorMap.map( StartingPopup ).toMediator( StartingPopupMediator ); 78 | mediatorMap.map( PausePopup ).toMediator( PausePopupMediator ); 79 | mediatorMap.map( GameOverPopup ).toMediator( GameOverPopupMediator ); 80 | mediatorMap.map( ResetConfimPopup ).toMediator( ResetConfirmPopupMediator ); 81 | mediatorMap.map( InfoPopup ).toMediator( InfoPopupMediator ); 82 | } 83 | 84 | private function mapFlowManager():void 85 | { 86 | // Palidor 87 | 88 | flowManager.map( FlowEvent.SHOW_INTRO_VIEW ).toView( IntroView ); 89 | flowManager.map( FlowEvent.SHOW_HOME_VIEW ).toView( HomeView ); 90 | flowManager.map( FlowEvent.SHOW_GAME_VIEW ).toView( GameView ); 91 | flowManager.map( FlowEvent.SHOW_CONIFG_VIEW ).toView( ConfigView ); 92 | 93 | flowManager.map( FlowEvent.SHOW_STARTING_POPUP ).toFloatingView( StartingPopup ); 94 | flowManager.map( FlowEvent.SHOW_PAUSE_POPUP ).toFloatingView( PausePopup ); 95 | flowManager.map( FlowEvent.SHOW_GAME_OVER_POPUP ).toFloatingView( GameOverPopup ); 96 | flowManager.map( FlowEvent.SHOW_RESET_CONFIRM_POPUP ).toFloatingView( ResetConfimPopup ); 97 | flowManager.map( FlowEvent.SHOW_INFO_POPUP ).toFloatingView( InfoPopup ); 98 | } 99 | 100 | ... 101 | } 102 | 103 | ``` 104 | 105 | * * * 106 | 107 | 108 | ### Screenshots 109 | ![screenshot01](img_game_tetris_as.png) 110 | * * * 111 | 112 | **Ronaldo Santiago** - Game Developer [ [portfolio](https://ronaldosetzer.github.io/portfolio/) ] -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/buttons/button_cancel_over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/buttons/button_cancel_over.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/buttons/button_cancel_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/buttons/button_cancel_up.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/buttons/button_config_over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/buttons/button_config_over.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/buttons/button_config_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/buttons/button_config_up.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/buttons/button_confirm_over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/buttons/button_confirm_over.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/buttons/button_confirm_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/buttons/button_confirm_up.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/buttons/button_home_over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/buttons/button_home_over.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/buttons/button_home_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/buttons/button_home_up.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/buttons/button_pause_over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/buttons/button_pause_over.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/buttons/button_pause_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/buttons/button_pause_up.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/buttons/button_reset_over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/buttons/button_reset_over.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/buttons/button_reset_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/buttons/button_reset_up.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/buttons/button_resume_over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/buttons/button_resume_over.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/buttons/button_resume_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/buttons/button_resume_up.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/buttons/button_retry_over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/buttons/button_retry_over.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/buttons/button_retry_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/buttons/button_retry_up.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/buttons/button_start_over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/buttons/button_start_over.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/buttons/button_start_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/buttons/button_start_up.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/displays/grid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/displays/grid.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/displays/logo_setzer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/displays/logo_setzer.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/displays/logo_tetris.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/displays/logo_tetris.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/displays/next_tile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/displays/next_tile.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/extras/logo_actionscript.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/extras/logo_actionscript.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/tetris-starling-atlas-0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/tetris-starling-atlas-0.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/tetris-starling-atlas.xml: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/tiles.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/tiles.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/tiles/tile_01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/tiles/tile_01.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/tiles/tile_02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/tiles/tile_02.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/tiles/tile_03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/tiles/tile_03.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/tiles/tile_04.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/tiles/tile_04.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/tiles/tile_05.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/tiles/tile_05.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/tiles/tile_06.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/tiles/tile_06.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/tiles/tile_07.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/tiles/tile_07.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/atlas/tiles/tile_08.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/atlas/tiles/tile_08.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/fonts/SimpleSmalPixel7_.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/fonts/SimpleSmalPixel7_.png -------------------------------------------------------------------------------- /actionscript-tetris/assets/fonts/SimpleSmallPixel7_0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/assets/fonts/SimpleSmallPixel7_0.png -------------------------------------------------------------------------------- /actionscript-tetris/bin/actionscript-tetris.swf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/bin/actionscript-tetris.swf -------------------------------------------------------------------------------- /actionscript-tetris/gif_tetris_as_demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/gif_tetris_as_demo.gif -------------------------------------------------------------------------------- /actionscript-tetris/img_cover_tetris_as.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/img_cover_tetris_as.png -------------------------------------------------------------------------------- /actionscript-tetris/img_game_tetris_as.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/img_game_tetris_as.png -------------------------------------------------------------------------------- /actionscript-tetris/img_ss_tetris_as_01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/img_ss_tetris_as_01.png -------------------------------------------------------------------------------- /actionscript-tetris/img_ss_tetris_as_02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/img_ss_tetris_as_02.png -------------------------------------------------------------------------------- /actionscript-tetris/libs/as3-signals-v0.9-BETA.swc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/libs/as3-signals-v0.9-BETA.swc -------------------------------------------------------------------------------- /actionscript-tetris/libs/flexunit-4.2.0-20140410-as3_4.12.0.swc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/libs/flexunit-4.2.0-20140410-as3_4.12.0.swc -------------------------------------------------------------------------------- /actionscript-tetris/libs/greensock.swc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/libs/greensock.swc -------------------------------------------------------------------------------- /actionscript-tetris/libs/robotlegs-extensions-palidor-v1.0.swc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/libs/robotlegs-extensions-palidor-v1.0.swc -------------------------------------------------------------------------------- /actionscript-tetris/libs/robotlegs-framework-v2.2.1.swc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/libs/robotlegs-framework-v2.2.1.swc -------------------------------------------------------------------------------- /actionscript-tetris/libs/starling-2.1.swc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/actionscript-tetris/libs/starling-2.1.swc -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/Main.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris 2 | { 3 | import flash.display.Sprite; 4 | import flash.display.StageAlign; 5 | import flash.display.StageScaleMode; 6 | import flash.events.Event; 7 | import flash.geom.Rectangle; 8 | 9 | import robotlegs.bender.bundles.palidor.PalidorBundle; 10 | import robotlegs.bender.extensions.contextView.ContextView; 11 | import robotlegs.bender.framework.api.IContext; 12 | import robotlegs.bender.framework.impl.Context; 13 | 14 | import setzer.tetris.configs.GameConfig; 15 | import setzer.tetris.utils.Colors; 16 | import setzer.tetris.views.MainStarlingView; 17 | 18 | import starling.core.Starling; 19 | 20 | [SWF(width="340", height="480", frameRate="60", backgroundColor="#272f31")] 21 | public class Main extends Sprite 22 | { 23 | public function Main() 24 | { 25 | stage.align = StageAlign.TOP_LEFT; 26 | stage.scaleMode = StageScaleMode.NO_SCALE; 27 | stage.frameRate = 60; 28 | stage.color = Colors.BACKGROUND; 29 | 30 | addEventListener( Event.ADDED_TO_STAGE, tetris_onAddedToStageHandler ); 31 | } 32 | 33 | private function tetris_onAddedToStageHandler( e:Event ):void 34 | { 35 | var starling:Starling = new Starling( MainStarlingView, stage, new Rectangle( 0, 0, stage.stageWidth, stage.stageHeight ) ); 36 | starling.nativeStage.frameRate = 60; 37 | starling.start(); 38 | 39 | const robotlegsContext:IContext = new Context(); 40 | robotlegsContext.install( PalidorBundle ); 41 | robotlegsContext.configure( GameConfig, starling, new ContextView( this ) ); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/assets/Assets.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.assets 2 | { 3 | import starling.text.BitmapFont; 4 | import starling.text.TextField; 5 | import starling.textures.Texture; 6 | import starling.textures.TextureAtlas; 7 | import starling.textures.TextureSmoothing; 8 | 9 | public class Assets 10 | { 11 | private static var _atlas:TextureAtlas; 12 | 13 | public static function init():void 14 | { 15 | var atlasTexture:Texture = Texture.fromEmbeddedAsset( Embeds.ATLAS_IMAGE ); 16 | var atlasXML:XML = XML( new Embeds.ATLAS_XML() ); 17 | _atlas = new TextureAtlas( atlasTexture, atlasXML ); 18 | 19 | var fontTexture:Texture = Texture.fromEmbeddedAsset( Embeds.FONT_IMAGE ); 20 | var font:BitmapFont = new BitmapFont( fontTexture, XML( new Embeds.FONT_XML() ) ); 21 | font.smoothing = TextureSmoothing.NONE; 22 | 23 | TextField.registerBitmapFont( font ); 24 | } 25 | 26 | public static function getTexture( preFix:String ):Texture 27 | { 28 | return _atlas.getTexture( preFix ); 29 | } 30 | 31 | public static function getTileTexture( id:int ):Texture 32 | { 33 | var ids:Array = [ 34 | AssetsInfo.TILE_01, 35 | AssetsInfo.TILE_02, 36 | AssetsInfo.TILE_03, 37 | AssetsInfo.TILE_04, 38 | AssetsInfo.TILE_05, 39 | AssetsInfo.TILE_06, 40 | AssetsInfo.TILE_07, 41 | AssetsInfo.TILE_08, 42 | ]; 43 | return _atlas.getTexture( ids[id] || ids[0] ); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/assets/AssetsInfo.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.assets 2 | { 3 | public class AssetsInfo 4 | { 5 | /* FONT */ 6 | public static const FONT_FAMILY:String = "Small Pixel7"; 7 | 8 | public static const SIZE_DEFAULT:int = 22; 9 | 10 | /* ATLAS PREFIX */ 11 | public static const BUTTON_CANCEL:String = "button_cancel"; 12 | public static const BUTTON_CONFIG:String = "button_config"; 13 | public static const BUTTON_CONFIRM:String = "button_confirm"; 14 | public static const BUTTON_HOME:String = "button_home"; 15 | public static const BUTTON_PAUSE:String = "button_pause"; 16 | public static const BUTTON_RESET:String = "button_reset"; 17 | public static const BUTTON_RESUME:String = "button_resume"; 18 | public static const BUTTON_RETRY:String = "button_retry"; 19 | public static const BUTTON_START:String = "button_start"; 20 | 21 | public static const GRID:String = "grid"; 22 | public static const NEXT_TILE:String = "next_tile"; 23 | public static const LOGO_SETZER:String = "logo_setzer"; 24 | public static const LOGO_TETRIS:String = "logo_tetris"; 25 | 26 | /* TILES */ 27 | public static const TILE_01:String = "tile_01"; 28 | public static const TILE_02:String = "tile_02"; 29 | public static const TILE_03:String = "tile_03"; 30 | public static const TILE_04:String = "tile_04"; 31 | public static const TILE_05:String = "tile_05"; 32 | public static const TILE_06:String = "tile_06"; 33 | public static const TILE_07:String = "tile_07"; 34 | public static const TILE_08:String = "tile_08"; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/assets/Embeds.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.assets 2 | { 3 | public class Embeds 4 | { 5 | [Embed(source="/../assets/atlas/extras/logo_actionscript.png")] 6 | public static const LANGUAGE_IMAGE:Class; 7 | 8 | /* ATLAS */ 9 | [Embed(source="/../assets/atlas/tetris-starling-atlas-0.png")] 10 | public static const ATLAS_IMAGE:Class; 11 | 12 | [Embed(source="/../assets/atlas/tetris-starling-atlas.xml", mimeType="application/octet-stream")] 13 | public static const ATLAS_XML:Class; 14 | 15 | /* FONT */ 16 | [Embed(source="/../assets/fonts/SimpleSmallPixel7.fnt", mimeType="application/octet-stream")] 17 | public static const FONT_XML:Class; 18 | 19 | [Embed(source = "/../assets/fonts/SimpleSmallPixel7_0.png")] 20 | public static const FONT_IMAGE:Class; 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/commands/CreateLevelCommand.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.commands 2 | { 3 | import robotlegs.bender.extensions.commandCenter.api.ICommand; 4 | 5 | import setzer.tetris.factories.TileGroupFactory; 6 | import setzer.tetris.managers.SharedObjectManager; 7 | import setzer.tetris.models.GameModel; 8 | import setzer.tetris.models.GameStatus; 9 | import setzer.tetris.services.FlowService; 10 | import setzer.tetris.services.GameService; 11 | 12 | public class CreateLevelCommand implements ICommand 13 | { 14 | [Inject] 15 | public var model:GameModel; 16 | 17 | [Inject] 18 | public var gameService:GameService; 19 | 20 | [Inject] 21 | public var flowService:FlowService; 22 | 23 | [Inject] 24 | public var sharedObjectManager:SharedObjectManager; 25 | 26 | public function execute():void 27 | { 28 | sharedObjectManager.getExternalData(); 29 | 30 | model.clear(); 31 | model.currentPiece = TileGroupFactory.getRandomTileGroup(); 32 | model.nextPiece = TileGroupFactory.getRandomTileGroup(); 33 | 34 | gameService.clearGrid(); 35 | gameService.updateNextPiece(); 36 | gameService.updateData(); 37 | 38 | if ( model.status ) 39 | flowService.showStartingPopup(); 40 | else 41 | flowService.showInfoPopup(); 42 | 43 | model.status = GameStatus.GAME; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/commands/GameOverCommand.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.commands 2 | { 3 | import robotlegs.bender.extensions.commandCenter.api.ICommand; 4 | 5 | import setzer.tetris.managers.SharedObjectManager; 6 | import setzer.tetris.models.GameModel; 7 | import setzer.tetris.models.GameStatus; 8 | import setzer.tetris.services.FlowService; 9 | 10 | public class GameOverCommand implements ICommand 11 | { 12 | [Inject] 13 | public var model:GameModel; 14 | 15 | [Inject] 16 | public var flowService:FlowService; 17 | 18 | [Inject] 19 | public var sharedObjectManager:SharedObjectManager; 20 | 21 | public function execute():void 22 | { 23 | model.status = GameStatus.GAMEOVER; 24 | 25 | sharedObjectManager.updateHighScore(); 26 | 27 | flowService.showGameOverPopup(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/commands/GetNextPieceCommand.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.commands 2 | { 3 | import robotlegs.bender.extensions.commandCenter.api.ICommand; 4 | 5 | import setzer.tetris.factories.TileGroupFactory; 6 | import setzer.tetris.models.GameModel; 7 | import setzer.tetris.services.GameService; 8 | 9 | public class GetNextPieceCommand implements ICommand 10 | { 11 | [Inject] 12 | public var model:GameModel; 13 | 14 | [Inject] 15 | public var gameService:GameService; 16 | 17 | public function execute():void 18 | { 19 | model.currentPiece = model.nextPiece; 20 | model.nextPiece = TileGroupFactory.getRandomTileGroup(); 21 | 22 | gameService.updateNextPiece(); 23 | gameService.updateData(); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/commands/IncreasePointsCommand.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.commands 2 | { 3 | import robotlegs.bender.extensions.commandCenter.api.ICommand; 4 | 5 | import setzer.tetris.events.GameEvent; 6 | import setzer.tetris.models.GameModel; 7 | import setzer.tetris.services.GameService; 8 | import setzer.tetris.utils.GameUtils; 9 | 10 | public class IncreasePointsCommand implements ICommand 11 | { 12 | 13 | [Inject] 14 | public var model:GameModel; 15 | 16 | [Inject] 17 | public var event:GameEvent; 18 | 19 | [Inject] 20 | public var gameService:GameService; 21 | 22 | public function execute():void 23 | { 24 | model.score += GameUtils.getPointsByLines( event.lines ); 25 | model.level = GameUtils.getCurrentLevel( model.lines ); 26 | model.lines += event.lines; 27 | 28 | gameService.updateData(); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/events/FlowEvent.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.events 2 | { 3 | import starling.events.Event; 4 | 5 | public class FlowEvent extends Event 6 | { 7 | public static const SHOW_INTRO_VIEW:String = "showIntroView"; 8 | public static const SHOW_HOME_VIEW:String = "showHomeView"; 9 | public static const SHOW_GAME_VIEW:String = "showHUDGameView"; 10 | public static const SHOW_CONIFG_VIEW:String = "showConfigView"; 11 | 12 | public static const SHOW_STARTING_POPUP:String = "showStartingPopup"; 13 | public static const SHOW_PAUSE_POPUP:String = "showPausePopup"; 14 | public static const SHOW_GAME_OVER_POPUP:String = "showGameOverPopup"; 15 | public static const SHOW_RESET_CONFIRM_POPUP:String = "showResetConfirmPopup"; 16 | public static const SHOW_INFO_POPUP:String = "showInfoPopup"; 17 | 18 | 19 | public function FlowEvent( type:String, bubbles:Boolean = false, data:Object = null ) 20 | { 21 | super( type, bubbles, data ); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/events/GameEvent.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.events 2 | { 3 | import starling.events.Event; 4 | 5 | public class GameEvent extends Event 6 | { 7 | public static const CREATE_LEVEL:String = "createLevel"; 8 | public static const GAME_OVER:String = "gameOver"; 9 | public static const CLEAR_GRID:String = "clearGrid"; 10 | 11 | public static const RESUME:String = "resume"; 12 | public static const PAUSE:String = "pause"; 13 | 14 | public static const GET_NEXT_PIECE:String = "getNextPiece"; 15 | public static const INCREASE_POINTS:String = "increasePoints"; 16 | 17 | public static const UPDATE_DATA:String = "updateData"; 18 | public static const UPDATE_NEXT_PIECE:String = "updateNextPiece"; 19 | 20 | public var lines:int; 21 | 22 | public function GameEvent( type:String, bubbles:Boolean = false, data:Object = null ) 23 | { 24 | super( type, bubbles, data ); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/factories/StarlingFactory.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.factories 2 | { 3 | import setzer.tetris.assets.Assets; 4 | import setzer.tetris.assets.AssetsInfo; 5 | import setzer.tetris.utils.Colors; 6 | import setzer.tetris.utils.TilePool; 7 | import setzer.tetris.utils.ViewPortSize; 8 | import setzer.tetris.views.components.TileDisplay; 9 | 10 | import starling.display.Button; 11 | import starling.display.Image; 12 | import starling.display.Quad; 13 | import starling.display.Sprite; 14 | import starling.text.TextField; 15 | import starling.text.TextFormat; 16 | import starling.textures.Texture; 17 | import starling.textures.TextureSmoothing; 18 | 19 | public class StarlingFactory 20 | { 21 | 22 | public static function getTextField( width:int, text:String = "", color:uint = Colors.STATIC_TEXT, fontAlign:String = "center" ):TextField 23 | { 24 | var textFormat:TextFormat = new TextFormat( AssetsInfo.FONT_FAMILY, AssetsInfo.SIZE_DEFAULT, color, fontAlign ); 25 | return new TextField( width, AssetsInfo.SIZE_DEFAULT + 2, text, textFormat ); 26 | } 27 | 28 | public static function getImage( assetKey:String ):Image 29 | { 30 | var img:Image = new Image( Assets.getTexture( assetKey ) ); 31 | img.textureSmoothing = TextureSmoothing.NONE; 32 | return img; 33 | } 34 | 35 | public static function getButton( key:String ):Button 36 | { 37 | var up:Texture = Assets.getTexture( key + "_up" ); 38 | var down:Texture = Assets.getTexture( key + "_over" ); 39 | var over:Texture = Assets.getTexture( key + "_over" ); 40 | var button:Button = new Button( up, "", down, over ); 41 | button.scaleWhenDown = .9; 42 | 43 | return button; 44 | } 45 | 46 | public static function getColorBackground( color:uint = Colors.BACKGROUND ):Quad 47 | { 48 | return new Quad( ViewPortSize.MAX_WIDTH, ViewPortSize.MAX_HEIGHT, color ); 49 | } 50 | 51 | public static function getShadowBackground( color:uint = 0x000000, alpha:Number = .6 ):Quad 52 | { 53 | var bg:Quad = new Quad( ViewPortSize.MAX_WIDTH, ViewPortSize.MAX_HEIGHT, color ); 54 | bg.alpha = alpha; 55 | return bg; 56 | } 57 | 58 | public static function getBoardBackground():Sprite 59 | { 60 | var container:Sprite = new Sprite(); 61 | 62 | var bg1:Quad = new Quad( ViewPortSize.MAX_WIDTH, 102, Colors.STATIC_TEXT ); 63 | bg1.alignPivot(); 64 | 65 | var bg2:Quad = new Quad( ViewPortSize.MAX_WIDTH, 100, Colors.BACKGROUND ); 66 | bg2.alignPivot(); 67 | 68 | container.addChild( bg1 ); 69 | container.addChild( bg2 ); 70 | 71 | return container; 72 | } 73 | 74 | public static function getTileDisplay( typeId:int ):TileDisplay 75 | { 76 | return TilePool.getTileDisplay( typeId ); 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/factories/TileGroupFactory.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.factories 2 | { 3 | import setzer.tetris.models.Tile; 4 | import setzer.tetris.models.TileGroup; 5 | import setzer.tetris.models.TileGroupType; 6 | 7 | public class TileGroupFactory 8 | { 9 | public static function getTileGroup( typeId:int ):TileGroup 10 | { 11 | var tiles:Vector. = getTilesByTypeArray( TileGroupType.getTypeArray( typeId )); 12 | return new TileGroup( typeId, tiles ); 13 | } 14 | 15 | public static function getRandomTileGroup():TileGroup 16 | { 17 | var types:Array = [ 18 | TileGroupType.TYPE_I, 19 | TileGroupType.TYPE_Z, 20 | TileGroupType.TYPE_S, 21 | TileGroupType.TYPE_T, 22 | TileGroupType.TYPE_L, 23 | TileGroupType.TYPE_J, 24 | TileGroupType.TYPE_O]; 25 | var rndType:int = Math.floor( Math.random() * types.length ); 26 | return getTileGroup( types[rndType] ); 27 | } 28 | 29 | public static function getTilesByTypeArray( typeArray:Array ):Vector. 30 | { 31 | var tiles:Vector. = new Vector.(); 32 | var tile:Tile; 33 | 34 | for ( var i:int = 0; i < typeArray.length; i++ ) 35 | { 36 | tile = new Tile(); 37 | tile.col = typeArray[i] % 2; 38 | tile.row = int( typeArray[i] / 2 ); 39 | tiles.push( tile ); 40 | } 41 | return tiles; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/managers/GameManager.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.managers 2 | { 3 | import setzer.tetris.models.Grid; 4 | import setzer.tetris.models.Tile; 5 | import setzer.tetris.models.TileGroup; 6 | import setzer.tetris.services.GameService; 7 | 8 | public class GameManager 9 | { 10 | [Inject] 11 | public var gameService:GameService; 12 | 13 | private var _grid:Grid; 14 | private var _currentPiece:TileGroup; 15 | private var _tilesToUpdate:Vector.; 16 | private var _tilesToRemove:Vector.; 17 | 18 | public function createEmpytGrid( cols:uint = 10, rows:uint = 20 ):void 19 | { 20 | _grid = new Grid( cols, rows ); 21 | _tilesToUpdate = new Vector.(); 22 | _tilesToRemove = new Vector.(); 23 | } 24 | 25 | public function addPiece( currentPiece:TileGroup ):void 26 | { 27 | _currentPiece = currentPiece; 28 | _currentPiece.moveHorizontal( 4 ); 29 | addTilesToUpdate( _currentPiece.tiles ); 30 | 31 | validateGameOver(); 32 | } 33 | 34 | private function validateGameOver():void 35 | { 36 | if ( isMovementValid() == false ) 37 | { 38 | gameService.gameOver(); 39 | } 40 | } 41 | 42 | public function tickUpdate():void 43 | { 44 | moveCurrentPieceDown(); 45 | } 46 | 47 | public function solveCompletedRows():void 48 | { 49 | var linesRemoved:int = 0; 50 | 51 | var updateTiles:Vector. = new Vector.(); 52 | var removeTiles:Vector. = new Vector.(); 53 | 54 | var isToRemove:Boolean; 55 | var hasToUpdate:Boolean; 56 | 57 | var tile:Tile; 58 | 59 | for ( var row:int = _grid.maxRows - 1; row > 0; row-- ) 60 | { 61 | isToRemove = true; 62 | for ( var col:int = 0; col < _grid.maxCols; col++ ) 63 | { 64 | tile = _grid.getTile( col, row ); 65 | 66 | if ( tile && hasToUpdate ) 67 | { 68 | tile.setPosition( col, row ); 69 | _tilesToUpdate.push( tile ); 70 | } 71 | 72 | isToRemove &&= tile; 73 | } 74 | if ( isToRemove ) 75 | { 76 | removeTiles = removeTiles.concat( _grid.removeRow( row ) ); 77 | hasToUpdate = true; 78 | row++; 79 | linesRemoved++; 80 | } 81 | } 82 | 83 | if ( linesRemoved > 0 ) 84 | gameService.increasePoints( linesRemoved ); 85 | 86 | _tilesToUpdate = _tilesToUpdate.concat( updateTiles ); 87 | _tilesToRemove = _tilesToRemove.concat( removeTiles ); 88 | } 89 | 90 | public function isMovementValid():Boolean 91 | { 92 | for each ( var tile:Tile in _currentPiece.tiles ) 93 | { 94 | if ( isOffBounds( tile ) || isNotEmptyTile( tile ) ) 95 | return false; 96 | } 97 | return true; 98 | } 99 | 100 | private function isNotEmptyTile( tile:Tile ):Boolean 101 | { 102 | return !_grid.isEmptyTile( tile.col, tile.row ); 103 | } 104 | 105 | private function isOffBounds( tile:Tile ):Boolean 106 | { 107 | return tile.col < 0 || tile.col >= _grid.maxCols || tile.row < 0 || tile.row >= _grid.maxRows; 108 | } 109 | 110 | public function addTilesToUpdate( tiles:Vector. ):void 111 | { 112 | _tilesToUpdate = _tilesToUpdate.concat( tiles ); 113 | } 114 | 115 | public function getTilesToUpdate():Vector. 116 | { 117 | return _tilesToUpdate; 118 | } 119 | 120 | public function getTilesToRemove():Vector. 121 | { 122 | return _tilesToRemove; 123 | } 124 | 125 | public function moveCurrentPieceLeft():void 126 | { 127 | _currentPiece.moveHorizontal( -1 ); 128 | 129 | if ( isMovementValid() == false ) 130 | _currentPiece.rollbackX(); else 131 | addTilesToUpdate( _currentPiece.tiles ); 132 | } 133 | 134 | public function moveCurrentPieceRight():void 135 | { 136 | _currentPiece.moveHorizontal( 1 ); 137 | 138 | if ( isMovementValid() == false ) 139 | _currentPiece.rollbackX(); else 140 | addTilesToUpdate( _currentPiece.tiles ); 141 | } 142 | 143 | public function rotateCurrentPiece():void 144 | { 145 | _currentPiece.rotate(); 146 | 147 | if ( isMovementValid() == false ) 148 | _currentPiece.rollback(); else 149 | addTilesToUpdate( _currentPiece.tiles ); 150 | } 151 | 152 | public function moveCurrentPieceDown():void 153 | { 154 | _currentPiece.moveVertical(); 155 | 156 | if ( isMovementValid() == false ) 157 | { 158 | _currentPiece.rollbackY(); 159 | 160 | for ( var i:int = 0; i < _currentPiece.tiles.length; i++ ) 161 | { 162 | _grid.setTile( _currentPiece.tiles[i], _currentPiece.tiles[i].col, _currentPiece.tiles[i].row ); 163 | } 164 | solveCompletedRows(); 165 | gameService.getNextPiece(); 166 | } else 167 | addTilesToUpdate( _currentPiece.tiles ); 168 | } 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/managers/SharedObjectManager.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.managers 2 | { 3 | import flash.net.SharedObject; 4 | 5 | import setzer.tetris.models.GameModel; 6 | import setzer.tetris.utils.MagicValues; 7 | 8 | public class SharedObjectManager 9 | { 10 | [Inject] 11 | public var model:GameModel; 12 | 13 | public function getExternalData():void 14 | { 15 | var so:SharedObject = SharedObject.getLocal( MagicValues.SHARED_OBJECT_NAME ); 16 | 17 | if ( so.data.hiScore ) 18 | { 19 | model.hiScore = int( so.data.hiScore ); 20 | 21 | trace( "hiScore: " + model.hiScore.toString() ); 22 | } 23 | so.close(); 24 | } 25 | 26 | public function updateHighScore():void 27 | { 28 | var so:SharedObject = SharedObject.getLocal( MagicValues.SHARED_OBJECT_NAME ); 29 | 30 | var currentHiScore:int = int( so.data.hiScore ); 31 | var score:int = model.score; 32 | var hiScore:int = Math.max( currentHiScore, score ); 33 | 34 | if ( so.data.hiScore != null ) 35 | { 36 | trace( "hiScore: " + currentHiScore + " to " + hiScore ); 37 | } 38 | model.hiScore = hiScore; 39 | so.data.hiScore = hiScore; 40 | 41 | so.flush(); 42 | so.close(); 43 | } 44 | 45 | public function clear():void 46 | { 47 | var so:SharedObject = SharedObject.getLocal( MagicValues.SHARED_OBJECT_NAME ); 48 | var currentHiScore:int = int( so.data.hiScore ); 49 | 50 | if ( so.data.hiScore != null ) 51 | { 52 | trace( "hiScore: " + currentHiScore + " to " + 0 ); 53 | } 54 | 55 | model.hiScore = 0; 56 | so.data.hiScore = 0; 57 | 58 | so.flush(); 59 | so.close(); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/mediators/ConfigViewMediator.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.mediators 2 | { 3 | import robotlegs.bender.extensions.palidor.starlingIntegration.starlingViewMap.impl.StarlingMediator; 4 | 5 | import setzer.tetris.services.FlowService; 6 | import setzer.tetris.views.ConfigView; 7 | 8 | import starling.events.Event; 9 | 10 | public class ConfigViewMediator extends StarlingMediator 11 | { 12 | [Inject] 13 | public var view:ConfigView; 14 | 15 | [Inject] 16 | public var service:FlowService; 17 | 18 | override public function initialize():void 19 | { 20 | eventMap.mapListener( view.homeButton, Event.TRIGGERED, homeButton_onTriggeredHandler ) 21 | eventMap.mapListener( view.resetButton, Event.TRIGGERED, resetButton_onTriggeredHandler ) 22 | } 23 | 24 | private function homeButton_onTriggeredHandler( e:Event ):void 25 | { 26 | service.setHomeView(); 27 | } 28 | 29 | private function resetButton_onTriggeredHandler( e:Event ):void 30 | { 31 | service.showResetConfirmPopup(); 32 | } 33 | 34 | override public function destroy():void 35 | { 36 | eventMap.unmapListeners(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/mediators/GameOverPopupMediator.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.mediators 2 | { 3 | import robotlegs.bender.extensions.palidor.starlingIntegration.starlingViewMap.impl.StarlingMediator; 4 | 5 | import setzer.tetris.services.FlowService; 6 | import setzer.tetris.services.GameService; 7 | 8 | import setzer.tetris.views.GameOverPopup; 9 | 10 | import starling.events.Event; 11 | 12 | public class GameOverPopupMediator extends StarlingMediator 13 | { 14 | [Inject] 15 | public var view:GameOverPopup; 16 | 17 | [Inject] 18 | public var flowService:FlowService; 19 | 20 | [Inject] 21 | public var gameService:GameService; 22 | 23 | override public function initialize():void 24 | { 25 | eventMap.mapListener( view.homeButton, Event.TRIGGERED, homeButton_onTriggeredHandler ); 26 | eventMap.mapListener( view.retryButton, Event.TRIGGERED, retryButton_onTriggeredHandler ); 27 | } 28 | 29 | private function homeButton_onTriggeredHandler( e:Event ):void 30 | { 31 | flowService.setHomeView(); 32 | view.removeFromParent(); 33 | } 34 | 35 | private function retryButton_onTriggeredHandler( e:Event ):void 36 | { 37 | gameService.createLevel(); 38 | view.removeFromParent(); 39 | } 40 | 41 | override public function destroy():void 42 | { 43 | eventMap.unmapListeners(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/mediators/GameViewMediator.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.mediators 2 | { 3 | import robotlegs.bender.extensions.palidor.starlingIntegration.starlingViewMap.impl.StarlingMediator; 4 | 5 | import setzer.tetris.services.FlowService; 6 | import setzer.tetris.services.GameService; 7 | import setzer.tetris.views.GameView; 8 | 9 | public class GameViewMediator extends StarlingMediator 10 | { 11 | [Inject] 12 | public var view:GameView; 13 | 14 | [Inject] 15 | public var gameService:GameService; 16 | [Inject] 17 | public var flowService:FlowService; 18 | 19 | override public function initialize():void 20 | { 21 | view.createComponents(); 22 | 23 | gameService.createLevel(); 24 | } 25 | 26 | override public function destroy():void 27 | { 28 | view.destroy(); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/mediators/GridComponentMediator.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.mediators 2 | { 3 | import flash.utils.Dictionary; 4 | 5 | import robotlegs.bender.extensions.palidor.starlingIntegration.starlingViewMap.impl.StarlingMediator; 6 | 7 | import setzer.tetris.events.GameEvent; 8 | import setzer.tetris.factories.StarlingFactory; 9 | import setzer.tetris.managers.GameManager; 10 | import setzer.tetris.models.GameModel; 11 | import setzer.tetris.models.Tile; 12 | import setzer.tetris.services.GameService; 13 | import setzer.tetris.utils.GameUtils; 14 | import setzer.tetris.utils.TilePool; 15 | import setzer.tetris.views.components.GridComponent; 16 | import setzer.tetris.views.components.TileDisplay; 17 | 18 | import starling.display.DisplayObject; 19 | import starling.events.Event; 20 | import starling.events.KeyboardEvent; 21 | 22 | public class GridComponentMediator extends StarlingMediator 23 | { 24 | 25 | [Inject] 26 | public var model:GameModel; 27 | 28 | [Inject] 29 | public var gameService:GameService; 30 | 31 | [Inject] 32 | public var view:GridComponent; 33 | 34 | [Inject] 35 | public var gameManager:GameManager; 36 | 37 | private var _displays:Dictionary; 38 | 39 | private var _tick:int; 40 | 41 | override public function initialize():void 42 | { 43 | eventMap.mapListener( eventDispatcher, GameEvent.UPDATE_NEXT_PIECE, game_onUpdateNextPieceHandler ); 44 | eventMap.mapListener( eventDispatcher, GameEvent.RESUME, game_onResumeGameHandler ); 45 | eventMap.mapListener( eventDispatcher, GameEvent.PAUSE, game_onPauseGameHandler ); 46 | eventMap.mapListener( eventDispatcher, GameEvent.GAME_OVER, game_onGameOVerHandler ); 47 | eventMap.mapListener( eventDispatcher, GameEvent.CLEAR_GRID, game_onClearGridHandler ); 48 | } 49 | 50 | private function game_onClearGridHandler( e:Event ):void 51 | { 52 | _displays = new Dictionary(); 53 | _tick = 0; 54 | 55 | gameManager.createEmpytGrid(); 56 | view.clear(); 57 | } 58 | 59 | private function game_onGameOVerHandler( e:Event ):void 60 | { 61 | eventMap.unmapListener( view, Event.ENTER_FRAME, enterFrame_Handler ); 62 | eventMap.unmapListener( view.stage, KeyboardEvent.KEY_DOWN, keyDown_onMovementHandler ); 63 | } 64 | 65 | private function game_onPauseGameHandler( e:Event ):void 66 | { 67 | eventMap.unmapListener( view, Event.ENTER_FRAME, enterFrame_Handler ); 68 | eventMap.unmapListener( view.stage, KeyboardEvent.KEY_DOWN, keyDown_onMovementHandler ); 69 | } 70 | 71 | private function game_onResumeGameHandler( e:Event ):void 72 | { 73 | eventMap.mapListener( view.stage, KeyboardEvent.KEY_DOWN, keyDown_onMovementHandler ); 74 | eventMap.mapListener( view, Event.ENTER_FRAME, enterFrame_Handler ); 75 | } 76 | 77 | private function keyDown_onMovementHandler( e:KeyboardEvent ):void 78 | { 79 | e.stopImmediatePropagation(); 80 | 81 | var methods:Object = {}; 82 | methods[37] = gameManager.moveCurrentPieceLeft; 83 | methods[65] = gameManager.moveCurrentPieceLeft; 84 | methods[39] = gameManager.moveCurrentPieceRight; 85 | methods[68] = gameManager.moveCurrentPieceRight; 86 | methods[38] = gameManager.rotateCurrentPiece; 87 | methods[87] = gameManager.rotateCurrentPiece; 88 | methods[40] = gameManager.moveCurrentPieceDown; 89 | methods[83] = gameManager.moveCurrentPieceDown; 90 | 91 | if ( methods[e.keyCode] ) methods[e.keyCode](); 92 | 93 | updateDisplays(); 94 | } 95 | 96 | private function enterFrame_Handler( e:Event ):void 97 | { 98 | _tick++; 99 | 100 | if ( _tick > GameUtils.getCurrentSpeed( model.level ) ) 101 | { 102 | gameManager.tickUpdate(); 103 | _tick = 0; 104 | } 105 | 106 | updateDisplays(); 107 | 108 | } 109 | 110 | public function addPiece():void 111 | { 112 | gameManager.addPiece( model.currentPiece ); 113 | 114 | var display:TileDisplay; 115 | for each ( var tile:Tile in model.currentPiece.tiles ) 116 | { 117 | display = StarlingFactory.getTileDisplay( model.currentPiece.typeId ); 118 | addDisplayToStage( tile, display ); 119 | GameUtils.updateDisplayPositionByTile( tile, display ); 120 | } 121 | } 122 | 123 | public function addDisplayToStage( tile:Tile, display:DisplayObject ):void 124 | { 125 | view.addChild( display ); 126 | _displays[tile] = display; 127 | } 128 | 129 | private function updateDisplays():void 130 | { 131 | updateDisplaysPositions( gameManager.getTilesToUpdate() ); 132 | removeDisplays( gameManager.getTilesToRemove() ); 133 | } 134 | 135 | public function updateDisplaysPositions( tiles:Vector. ):void 136 | { 137 | var tile:Tile; 138 | while ( tiles.length > 0 ) 139 | { 140 | tile = tiles.pop(); 141 | GameUtils.updateDisplayPositionByTile( tile, _displays[tile] ); 142 | } 143 | } 144 | 145 | public function removeDisplays( tiles:Vector. ):void 146 | { 147 | var tile:Tile; 148 | var tileDisplay:TileDisplay; 149 | 150 | while ( tiles.length > 0 ) 151 | { 152 | tile = tiles.pop(); 153 | tileDisplay = _displays[tile]; 154 | tileDisplay.removeFromParent(); 155 | 156 | TilePool.back( tileDisplay ); 157 | 158 | delete _displays[tile]; 159 | } 160 | } 161 | 162 | private function game_onUpdateNextPieceHandler( e:GameEvent ):void 163 | { 164 | addPiece(); 165 | } 166 | 167 | override public function destroy():void 168 | { 169 | eventMap.unmapListeners(); 170 | } 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/mediators/HUDGameComponentMediator.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.mediators 2 | { 3 | import robotlegs.bender.extensions.palidor.starlingIntegration.starlingViewMap.impl.StarlingMediator; 4 | 5 | import setzer.tetris.events.GameEvent; 6 | import setzer.tetris.models.GameModel; 7 | import setzer.tetris.services.FlowService; 8 | import setzer.tetris.services.GameService; 9 | import setzer.tetris.views.components.HUDGameComponent; 10 | 11 | import starling.events.Event; 12 | 13 | public class HUDGameComponentMediator extends StarlingMediator 14 | { 15 | [Inject] 16 | public var view:HUDGameComponent; 17 | 18 | [Inject] 19 | public var model:GameModel; 20 | 21 | [Inject] 22 | public var gameService:GameService; 23 | 24 | [Inject] 25 | public var flowService:FlowService; 26 | 27 | override public function initialize():void 28 | { 29 | eventMap.mapListener( view.pauseButton, Event.TRIGGERED, pauseButton_onTriggeredHandler ); 30 | eventMap.mapListener( eventDispatcher, GameEvent.UPDATE_DATA, game_onUpdateHandler ); 31 | } 32 | 33 | private function game_onUpdateHandler( e:Event ):void 34 | { 35 | view.updateData( model ); 36 | } 37 | 38 | private function pauseButton_onTriggeredHandler( e:Event ):void 39 | { 40 | e.stopImmediatePropagation(); 41 | 42 | gameService.pause(); 43 | flowService.showPausePopup(); 44 | } 45 | 46 | override public function destroy():void 47 | { 48 | eventMap.unmapListeners(); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/mediators/HomeViewMediator.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.mediators 2 | { 3 | import robotlegs.bender.extensions.palidor.starlingIntegration.starlingViewMap.impl.StarlingMediator; 4 | 5 | import setzer.tetris.services.FlowService; 6 | import setzer.tetris.views.HomeView; 7 | 8 | import starling.events.Event; 9 | 10 | public class HomeViewMediator extends StarlingMediator 11 | { 12 | [Inject] 13 | public var view:HomeView; 14 | 15 | [Inject] 16 | public var service:FlowService; 17 | 18 | override public function initialize():void 19 | { 20 | eventMap.mapListener( view.configButton, Event.TRIGGERED, configButton_onTriggeredHandler ); 21 | eventMap.mapListener( view.startButton, Event.TRIGGERED, startButton_onTriggeredHandler ); 22 | } 23 | 24 | private function configButton_onTriggeredHandler( e:Event ):void 25 | { 26 | service.setConfigView(); 27 | } 28 | 29 | private function startButton_onTriggeredHandler( e:Event ):void 30 | { 31 | service.setHUDGameView(); 32 | } 33 | 34 | override public function destroy():void 35 | { 36 | eventMap.unmapListeners(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/mediators/InfoPopupMediator.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.mediators 2 | { 3 | import robotlegs.bender.extensions.palidor.starlingIntegration.starlingViewMap.impl.StarlingMediator; 4 | 5 | import setzer.tetris.services.FlowService; 6 | import setzer.tetris.views.InfoPopup; 7 | 8 | import starling.events.Event; 9 | 10 | public class InfoPopupMediator extends StarlingMediator 11 | { 12 | [Inject] 13 | public var view:InfoPopup; 14 | 15 | [Inject] 16 | public var service:FlowService; 17 | 18 | override public function initialize():void 19 | { 20 | eventMap.mapListener( view.closeButton, Event.TRIGGERED, closeButton_onTriggeredHandler ); 21 | } 22 | 23 | private function closeButton_onTriggeredHandler( e:Event ):void 24 | { 25 | service.showStartingPopup(); 26 | view.removeFromParent(); 27 | } 28 | 29 | override public function destroy():void 30 | { 31 | eventMap.unmapListeners(); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/mediators/IntroViewMediator.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.mediators 2 | { 3 | import flash.utils.setTimeout; 4 | 5 | import robotlegs.bender.extensions.palidor.starlingIntegration.starlingViewMap.impl.StarlingMediator; 6 | 7 | import setzer.tetris.services.FlowService; 8 | import setzer.tetris.views.IntroView; 9 | 10 | public class IntroViewMediator extends StarlingMediator 11 | { 12 | [Inject] 13 | public var flowService:FlowService; 14 | 15 | [Inject] 16 | public var view:IntroView; 17 | 18 | override public function initialize():void 19 | { 20 | view.playAnimation(); 21 | setTimeout( onTimerOutHandler, 3000 ); 22 | } 23 | 24 | private function onTimerOutHandler():void 25 | { 26 | flowService.setHomeView(); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/mediators/NextPieceComponentMediator.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.mediators 2 | { 3 | import robotlegs.bender.extensions.palidor.starlingIntegration.starlingViewMap.impl.StarlingMediator; 4 | 5 | import setzer.tetris.events.GameEvent; 6 | import setzer.tetris.factories.StarlingFactory; 7 | import setzer.tetris.models.GameModel; 8 | import setzer.tetris.models.Tile; 9 | import setzer.tetris.services.GameService; 10 | import setzer.tetris.views.components.NextPieceComponent; 11 | 12 | import starling.display.DisplayObject; 13 | 14 | public class NextPieceComponentMediator extends StarlingMediator 15 | { 16 | [Inject] 17 | public var model:GameModel; 18 | 19 | [Inject] 20 | public var gameService:GameService; 21 | 22 | [Inject] 23 | public var view:NextPieceComponent; 24 | 25 | override public function initialize():void 26 | { 27 | eventMap.mapListener( eventDispatcher, GameEvent.UPDATE_NEXT_PIECE, game_updateNextPieceHandler ); 28 | } 29 | 30 | private function game_updateNextPieceHandler( e:GameEvent ):void 31 | { 32 | view.removeChildren(); 33 | 34 | var display:DisplayObject; 35 | for ( var i:int = 0; i < model.nextPiece.tiles.length; i++ ) 36 | { 37 | display = createDisplay( model.nextPiece.typeId ); 38 | display.x = (model.nextPiece.tiles[i].col + 2) * Tile.TILE_WIDTH; 39 | display.y = model.nextPiece.tiles[i].row * Tile.TILE_WIDTH; 40 | display.pivotX = display.width * .5; 41 | view.addChild( display ); 42 | } 43 | } 44 | 45 | private function createDisplay( assetId:int ):DisplayObject 46 | { 47 | return StarlingFactory.getTileDisplay( assetId ); 48 | } 49 | 50 | override public function destroy():void 51 | { 52 | eventMap.unmapListeners(); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/mediators/PausePopupMediator.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.mediators 2 | { 3 | import robotlegs.bender.extensions.palidor.starlingIntegration.starlingViewMap.impl.StarlingMediator; 4 | 5 | import setzer.tetris.services.FlowService; 6 | 7 | import setzer.tetris.services.GameService; 8 | 9 | import setzer.tetris.views.PausePopup; 10 | 11 | import starling.events.Event; 12 | import starling.events.EventDispatcher; 13 | 14 | public class PausePopupMediator extends StarlingMediator 15 | { 16 | [Inject] 17 | public var view:PausePopup; 18 | 19 | [Inject] 20 | public var gameService:GameService; 21 | 22 | [Inject] 23 | public var flowService:FlowService; 24 | 25 | override public function initialize():void 26 | { 27 | eventMap.mapListener( view.resumeButton, Event.TRIGGERED, closeButton_onTriggeredHandler ); 28 | eventMap.mapListener( view.homeButton, Event.TRIGGERED, homeButton_onTriggeredHandler ); 29 | } 30 | 31 | private function homeButton_onTriggeredHandler( e:Event ):void 32 | { 33 | flowService.setHomeView(); 34 | view.removeFromParent(); 35 | } 36 | private function closeButton_onTriggeredHandler( e:Event ):void 37 | { 38 | gameService.resume(); 39 | view.removeFromParent(); 40 | } 41 | 42 | override public function destroy():void 43 | { 44 | eventMap.unmapListeners(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/mediators/ResetConfirmPopupMediator.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.mediators 2 | { 3 | import robotlegs.bender.extensions.palidor.starlingIntegration.starlingViewMap.impl.StarlingMediator; 4 | 5 | import setzer.tetris.managers.SharedObjectManager; 6 | import setzer.tetris.views.ResetConfimPopup; 7 | 8 | import starling.events.Event; 9 | 10 | public class ResetConfirmPopupMediator extends StarlingMediator 11 | { 12 | [Inject] 13 | public var view:ResetConfimPopup; 14 | 15 | [Inject] 16 | public var sharedObjectManager:SharedObjectManager; 17 | 18 | override public function initialize():void 19 | { 20 | eventMap.mapListener( view.confirmButton, Event.TRIGGERED, confirmButton_onTriggeredHandler ); 21 | eventMap.mapListener( view.cancelButton, Event.TRIGGERED, cancelButton_onTriggeredHandler ); 22 | } 23 | 24 | private function confirmButton_onTriggeredHandler( e:Event ):void 25 | { 26 | sharedObjectManager.clear(); 27 | view.removeFromParent(); 28 | } 29 | 30 | private function cancelButton_onTriggeredHandler( e:Event ):void 31 | { 32 | view.removeFromParent(); 33 | } 34 | 35 | override public function destroy():void 36 | { 37 | eventMap.unmapListeners(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/mediators/StartingPopupMediator.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.mediators 2 | { 3 | import robotlegs.bender.extensions.palidor.starlingIntegration.starlingViewMap.impl.StarlingMediator; 4 | 5 | import setzer.tetris.services.GameService; 6 | import setzer.tetris.views.StartingPopup; 7 | 8 | import starling.events.Event; 9 | 10 | public class StartingPopupMediator extends StarlingMediator 11 | { 12 | [Inject] 13 | public var view:StartingPopup; 14 | 15 | [Inject] 16 | public var gameService:GameService; 17 | 18 | override public function initialize():void 19 | { 20 | eventMap.mapListener( view, Event.COMPLETE, view_completeHandler ); 21 | view.start(); 22 | } 23 | 24 | private function view_completeHandler( e:Event ):void 25 | { 26 | view.destroy(); 27 | gameService.resume(); 28 | } 29 | 30 | override public function destroy():void 31 | { 32 | eventMap.unmapListeners(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/models/GameModel.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.models 2 | { 3 | public class GameModel 4 | { 5 | public var score:int; 6 | public var level:int; 7 | public var lines:int; 8 | public var hiScore:int; 9 | 10 | public var currentPiece:TileGroup; 11 | public var nextPiece:TileGroup; 12 | 13 | public var status:String; 14 | 15 | public function GameModel() 16 | { 17 | clear(); 18 | } 19 | 20 | public function clear():void 21 | { 22 | score = 0; 23 | level = 1; 24 | lines = 0; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/models/GameStatus.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.models 2 | { 3 | public class GameStatus 4 | { 5 | public static const GAME:String = "game"; 6 | public static const GAMEOVER:String = "gameOver"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/models/Grid.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.models 2 | { 3 | public class Grid 4 | { 5 | private var _maxCols:uint; 6 | private var _maxRows:uint; 7 | private var _grid:Vector.>; 8 | 9 | public function Grid( cols:uint = 10, rows:uint = 20 ) 10 | { 11 | _grid = new Vector.>(); 12 | _maxCols = cols; 13 | _maxRows = rows; 14 | 15 | generateEmptyGrid(); 16 | } 17 | 18 | private function generateEmptyGrid():void 19 | { 20 | var line:Vector.; 21 | for ( var row:int = 0; row < _maxRows; row++ ) 22 | { 23 | line = new Vector.(); 24 | for ( var col:int = 0; col < _maxCols; col++ ) 25 | { 26 | line.push( null ); 27 | } 28 | _grid.push( line ); 29 | } 30 | } 31 | 32 | public function removeRow( index:uint ):Vector. 33 | { 34 | var removedRow:Vector. = getRow( index ); 35 | _grid.splice( index, 1 ); 36 | 37 | insertNewEmpytRow(0); 38 | 39 | return removedRow; 40 | } 41 | 42 | private function insertNewEmpytRow( index:int ):void 43 | { 44 | var newRow:Vector. = new Vector.(); 45 | for ( var i:int = 0; i < _maxCols; i++ ) 46 | { 47 | newRow.push(null); 48 | } 49 | _grid.splice(index,0,newRow); 50 | } 51 | 52 | public function getRow( index:uint ):Vector. 53 | { 54 | return _grid[index]; 55 | } 56 | 57 | public function get maxCols():uint 58 | { 59 | return _maxCols; 60 | } 61 | 62 | public function get maxRows():uint 63 | { 64 | return _maxRows; 65 | } 66 | 67 | public function isEmptyTile( col:uint, row:uint ):Boolean 68 | { 69 | return (_grid[row][col] == null); 70 | } 71 | 72 | public function setTile( tile:Tile, col:int, row:int ):void 73 | { 74 | _grid[row][col] = tile; 75 | } 76 | 77 | public function getTile( col:int, row:int ):Tile 78 | { 79 | return _grid[row][col] || null; 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/models/Tile.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.models 2 | { 3 | public class Tile 4 | { 5 | public static const TILE_WIDTH:int = 18; 6 | 7 | public var row:int; 8 | public var col:int; 9 | public var bRow:int; 10 | public var bCol:int; 11 | 12 | public function Tile( col:int = 0, row:int = 0 ) 13 | { 14 | this.col = col; 15 | this.row = row; 16 | bCol = col; 17 | bRow = row; 18 | } 19 | 20 | public function clone():Tile 21 | { 22 | var tile:Tile = new Tile(); 23 | tile.col = col; 24 | tile.row = row; 25 | tile.bCol = bCol; 26 | tile.bRow = bRow; 27 | 28 | return tile; 29 | } 30 | 31 | public function setPosition( col:int, row:int ):void 32 | { 33 | this.col = col; 34 | this.row = row; 35 | } 36 | 37 | public function toString():String 38 | { 39 | return "tile_col_" + col + "_row_" + row; 40 | } 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/models/TileGroup.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.models 2 | { 3 | public class TileGroup 4 | { 5 | private var _tiles:Vector.; 6 | private var _typeId:int; 7 | 8 | public function TileGroup( typeId:int, tiles:Vector. ) 9 | { 10 | _typeId = typeId; 11 | _tiles = tiles; 12 | } 13 | 14 | public function moveVertical( direction:int = 1 ):void 15 | { 16 | for ( var i:int = 0; i < tiles.length; i++ ) 17 | { 18 | _tiles[i].bRow = _tiles[i].row; 19 | _tiles[i].row += direction; 20 | } 21 | } 22 | 23 | public function moveHorizontal( direction:int ):void 24 | { 25 | for ( var i:int = 0; i < tiles.length; i++ ) 26 | { 27 | _tiles[i].bCol = _tiles[i].col; 28 | _tiles[i].col += direction; 29 | } 30 | } 31 | 32 | public function rotate():void 33 | { 34 | var anchorTile:Tile = _tiles[1]; 35 | var newTile:Tile = new Tile(); 36 | for ( var i:int = 0; i < _tiles.length; i++ ) 37 | { 38 | _tiles[i].bCol = _tiles[i].col; 39 | _tiles[i].bRow = _tiles[i].row; 40 | newTile.col = _tiles[i].row - anchorTile.row; 41 | newTile.row = _tiles[i].col - anchorTile.col; 42 | _tiles[i].col = anchorTile.col - newTile.col; 43 | _tiles[i].row = anchorTile.row + newTile.row; 44 | } 45 | } 46 | 47 | public function rollback():void 48 | { 49 | for ( var i:int = 0; i < _tiles.length; i++ ) 50 | { 51 | _tiles[i].col = _tiles[i].bCol; 52 | _tiles[i].row = _tiles[i].bRow; 53 | } 54 | } 55 | 56 | public function rollbackX():void 57 | { 58 | for ( var i:int = 0; i < _tiles.length; i++ ) 59 | { 60 | _tiles[i].col = _tiles[i].bCol; 61 | } 62 | } 63 | 64 | public function rollbackY():void 65 | { 66 | for ( var i:int = 0; i < _tiles.length; i++ ) 67 | { 68 | _tiles[i].row = _tiles[i].bRow; 69 | } 70 | } 71 | 72 | public function get tiles():Vector. 73 | { 74 | return _tiles; 75 | } 76 | 77 | public function get typeId():int 78 | { 79 | return _typeId; 80 | } 81 | } 82 | } -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/models/TileGroupType.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.models 2 | { 3 | import flash.utils.Dictionary; 4 | 5 | public class TileGroupType 6 | { 7 | public static const TYPE_I:int = 0; 8 | public static const TYPE_Z:int = 1; 9 | public static const TYPE_S:int = 2; 10 | public static const TYPE_T:int = 3; 11 | public static const TYPE_L:int = 4; 12 | public static const TYPE_J:int = 5; 13 | public static const TYPE_O:int = 6; 14 | 15 | private static const TYPE_I_ARRAY:Array = [1, 3, 5, 7]; 16 | private static const TYPE_Z_ARRAY:Array = [2, 4, 5, 7]; 17 | private static const TYPE_S_ARRAY:Array = [3, 5, 4, 6]; 18 | private static const TYPE_T_ARRAY:Array = [3, 5, 4, 7]; 19 | private static const TYPE_L_ARRAY:Array = [2, 3, 5, 7]; 20 | private static const TYPE_J_ARRAY:Array = [3, 5, 7, 6]; 21 | private static const TYPE_O_ARRAY:Array = [2, 3, 4, 5]; 22 | 23 | public static function getTypeArray( type:int ):Array 24 | { 25 | var dic:Dictionary = new Dictionary(); 26 | dic[TYPE_I] = TYPE_I_ARRAY; 27 | dic[TYPE_Z] = TYPE_Z_ARRAY; 28 | dic[TYPE_S] = TYPE_S_ARRAY; 29 | dic[TYPE_T] = TYPE_T_ARRAY; 30 | dic[TYPE_L] = TYPE_L_ARRAY; 31 | dic[TYPE_J] = TYPE_J_ARRAY; 32 | dic[TYPE_O] = TYPE_O_ARRAY; 33 | 34 | return dic[type]; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/services/FlowService.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.services 2 | { 3 | import setzer.tetris.events.FlowEvent; 4 | 5 | import starling.events.EventDispatcher; 6 | 7 | public class FlowService 8 | { 9 | [Inject] 10 | public var eventDispatcher:EventDispatcher; 11 | 12 | //Views 13 | public function setHUDGameView():void 14 | { 15 | eventDispatcher.dispatchEventWith( FlowEvent.SHOW_GAME_VIEW ); 16 | } 17 | 18 | public function setHomeView():void 19 | { 20 | eventDispatcher.dispatchEventWith( FlowEvent.SHOW_HOME_VIEW ); 21 | } 22 | 23 | public function setConfigView():void 24 | { 25 | eventDispatcher.dispatchEventWith( FlowEvent.SHOW_CONIFG_VIEW ); 26 | } 27 | 28 | //Floating Views 29 | public function showStartingPopup():void 30 | { 31 | eventDispatcher.dispatchEventWith( FlowEvent.SHOW_STARTING_POPUP ); 32 | } 33 | 34 | public function showGameOverPopup():void 35 | { 36 | eventDispatcher.dispatchEventWith( FlowEvent.SHOW_GAME_OVER_POPUP ); 37 | } 38 | 39 | public function showResetConfirmPopup():void 40 | { 41 | eventDispatcher.dispatchEventWith( FlowEvent.SHOW_RESET_CONFIRM_POPUP ); 42 | } 43 | 44 | public function showPausePopup():void 45 | { 46 | eventDispatcher.dispatchEventWith( FlowEvent.SHOW_PAUSE_POPUP ); 47 | } 48 | 49 | public function showInfoPopup():void 50 | { 51 | eventDispatcher.dispatchEventWith( FlowEvent.SHOW_INFO_POPUP ); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/services/GameService.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.services 2 | { 3 | import setzer.tetris.events.GameEvent; 4 | 5 | import starling.events.EventDispatcher; 6 | 7 | public class GameService 8 | { 9 | [Inject] 10 | public var dispatcher:EventDispatcher; 11 | 12 | public function createLevel():void 13 | { 14 | dispatcher.dispatchEventWith( GameEvent.CREATE_LEVEL ); 15 | } 16 | 17 | public function pause():void 18 | { 19 | dispatcher.dispatchEventWith( GameEvent.PAUSE ); 20 | } 21 | 22 | public function resume():void 23 | { 24 | dispatcher.dispatchEventWith( GameEvent.RESUME ); 25 | } 26 | 27 | public function gameOver():void 28 | { 29 | dispatcher.dispatchEventWith( GameEvent.GAME_OVER ); 30 | } 31 | 32 | public function clearGrid():void 33 | { 34 | dispatcher.dispatchEventWith( GameEvent.CLEAR_GRID ); 35 | } 36 | 37 | 38 | public function getNextPiece():void 39 | { 40 | dispatcher.dispatchEventWith( GameEvent.GET_NEXT_PIECE ); 41 | } 42 | 43 | public function increasePoints( linesRemoved:int ):void 44 | { 45 | var event:GameEvent = new GameEvent( GameEvent.INCREASE_POINTS ); 46 | event.lines = linesRemoved; 47 | dispatcher.dispatchEvent( event ); 48 | } 49 | 50 | 51 | public function updateData():void 52 | { 53 | dispatcher.dispatchEvent( new GameEvent( GameEvent.UPDATE_DATA ) ); 54 | } 55 | 56 | public function updateNextPiece():void 57 | { 58 | dispatcher.dispatchEvent( new GameEvent( GameEvent.UPDATE_NEXT_PIECE, true ) ); 59 | } 60 | 61 | 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/utils/Colors.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.utils 2 | { 3 | public class Colors 4 | { 5 | public static const BACKGROUND:int = 0x272f31; 6 | 7 | public static const STATIC_TEXT:int = 0x5c6d72; 8 | public static const DYNAMIC_TEXT:int = 0x94adb4; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/utils/GameUtils.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.utils 2 | { 3 | import setzer.tetris.models.Tile; 4 | import setzer.tetris.views.components.TileDisplay; 5 | 6 | public class GameUtils 7 | { 8 | public static function getCurrentLevel( lines:int ):int 9 | { 10 | return Math.floor( lines / 10 + 1 ); 11 | } 12 | 13 | public static function getCurrentSpeed( level:int ):int 14 | { 15 | return Math.max( 4, 19 - level ); 16 | } 17 | 18 | public static function getPointsByLines( lines:int ):int 19 | { 20 | var bonus:int = Math.floor( lines * .9 ); 21 | return ( (lines + bonus) * 100); 22 | } 23 | 24 | public static function updateDisplayPositionByTile( tile:Tile, display:TileDisplay ):void 25 | { 26 | if ( display == null || tile == null ) return; 27 | 28 | display.x = tile.col * Tile.TILE_WIDTH; 29 | display.y = tile.row * Tile.TILE_WIDTH; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/utils/MagicValues.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.utils 2 | { 3 | public class MagicValues 4 | { 5 | public static const BORDER_OFFSET:int = 18; 6 | public static const SHARED_OBJECT_NAME:String = "TetrisPalidor"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/utils/Texts.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.utils 2 | { 3 | public class Texts 4 | { 5 | public static const DEVELOPER:String = "RONALDO SANTIAGO"; 6 | 7 | public static const TETRIS:String = "TETRIS"; 8 | public static const CONFIG:String = "CONFIG"; 9 | 10 | public static const LINES:String = "lines"; 11 | public static const SCORE:String = "score"; 12 | public static const LEVEL:String = "level"; 13 | public static const HI_SCORE:String = "hiScore"; 14 | 15 | public static const COMMANDS:String = "USE THE ARROWS\nTO MOVE\nAND ROTATE"; 16 | public static const CONFIRM_RESET:String = "WOULD LIKE TO RESET\nTHE HI-SCORE?"; 17 | public static const GAME_OVER:String = "GAME OVER"; 18 | public static const PAUSED:String = "PAUSED"; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/utils/TilePool.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.utils 2 | { 3 | import flash.utils.Dictionary; 4 | 5 | import setzer.tetris.assets.Assets; 6 | import setzer.tetris.views.components.TileDisplay; 7 | 8 | public class TilePool 9 | { 10 | public static var tileTypeLists:Dictionary; 11 | 12 | public static function init():void 13 | { 14 | tileTypeLists = new Dictionary(); 15 | } 16 | 17 | public static function getTileDisplay( typeId:int ):TileDisplay 18 | { 19 | if ( tileTypeLists[typeId] == null ) tileTypeLists[typeId] = new Vector.(); 20 | 21 | var list:Vector. = tileTypeLists[typeId]; 22 | var tileDisplay:TileDisplay = list.shift() || new TileDisplay( Assets.getTileTexture( typeId ), typeId ); 23 | tileDisplay.visible = true; 24 | 25 | return tileDisplay; 26 | } 27 | 28 | public static function back( tile:TileDisplay ):void 29 | { 30 | var list:Vector. = tileTypeLists[tile.typeId]; 31 | tile.visible = false; 32 | 33 | if ( list.indexOf( tile ) == -1 ) 34 | list.push( tile ); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/utils/ViewPortSize.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.utils 2 | { 3 | public class ViewPortSize 4 | { 5 | public static const MAX_WIDTH:int = 340; 6 | public static const MAX_HEIGHT:int = 480; 7 | public static const HALF_WIDTH:int = 170; 8 | public static const HALF_HEIGHT:int = 240; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/views/ConfigView.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.views 2 | { 3 | import setzer.tetris.assets.AssetsInfo; 4 | import setzer.tetris.factories.StarlingFactory; 5 | import setzer.tetris.utils.Colors; 6 | import setzer.tetris.utils.MagicValues; 7 | import setzer.tetris.utils.Texts; 8 | import setzer.tetris.utils.ViewPortSize; 9 | 10 | import starling.display.Button; 11 | import starling.display.Sprite; 12 | import starling.text.TextField; 13 | 14 | public class ConfigView extends Sprite 15 | { 16 | private var _homeButton:Button; 17 | private var _resetButton:Button; 18 | 19 | public function ConfigView() 20 | { 21 | 22 | addChild( StarlingFactory.getColorBackground() ); 23 | 24 | var title:TextField = StarlingFactory.getTextField( 100, Texts.CONFIG, Colors.DYNAMIC_TEXT, "left" ); 25 | title.x = MagicValues.BORDER_OFFSET; 26 | title.y = 24; 27 | addChild( title ); 28 | 29 | var hiScore:TextField = StarlingFactory.getTextField( 150, Texts.HI_SCORE + ":", Colors.DYNAMIC_TEXT, "left" ); 30 | hiScore.x = MagicValues.BORDER_OFFSET; 31 | hiScore.y = 108; 32 | addChild( hiScore ); 33 | 34 | _resetButton = StarlingFactory.getButton( AssetsInfo.BUTTON_RESET ); 35 | _resetButton.pivotX = _resetButton.width; 36 | _resetButton.x = ViewPortSize.MAX_WIDTH - MagicValues.BORDER_OFFSET; 37 | _resetButton.y = 100; 38 | addChild( _resetButton ); 39 | 40 | _homeButton = StarlingFactory.getButton( AssetsInfo.BUTTON_HOME ); 41 | _homeButton.pivotX = _homeButton.width; 42 | _homeButton.x = ViewPortSize.MAX_WIDTH - MagicValues.BORDER_OFFSET; 43 | _homeButton.y = MagicValues.BORDER_OFFSET; 44 | addChild( _homeButton ); 45 | } 46 | 47 | public function get resetButton():Button 48 | { 49 | return _resetButton; 50 | } 51 | 52 | public function get homeButton():Button 53 | { 54 | return _homeButton; 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/views/GameOverPopup.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.views 2 | { 3 | import setzer.tetris.assets.AssetsInfo; 4 | import setzer.tetris.factories.StarlingFactory; 5 | import setzer.tetris.utils.Colors; 6 | import setzer.tetris.utils.Texts; 7 | import setzer.tetris.utils.ViewPortSize; 8 | 9 | import starling.display.Button; 10 | import starling.display.Sprite; 11 | import starling.text.TextField; 12 | 13 | public class GameOverPopup extends Sprite 14 | { 15 | private var _homeButton:Button; 16 | private var _retryButton:Button; 17 | 18 | public function GameOverPopup() 19 | { 20 | addChild( StarlingFactory.getShadowBackground() ); 21 | 22 | var boardBackground:Sprite = StarlingFactory.getBoardBackground(); 23 | boardBackground.x = ViewPortSize.HALF_WIDTH; 24 | boardBackground.y = ViewPortSize.HALF_HEIGHT; 25 | addChild( boardBackground ); 26 | 27 | var commands:TextField = StarlingFactory.getTextField( 150, Texts.GAME_OVER, Colors.DYNAMIC_TEXT ); 28 | commands.x = ViewPortSize.HALF_WIDTH; 29 | commands.y = ViewPortSize.HALF_HEIGHT - 30; 30 | commands.scaleX = 1.2; 31 | commands.scaleY = 1.2; 32 | commands.height = 50; 33 | commands.alignPivot(); 34 | addChild( commands ); 35 | 36 | _retryButton = StarlingFactory.getButton( AssetsInfo.BUTTON_RETRY ); 37 | _retryButton.alignPivot(); 38 | _retryButton.x = ViewPortSize.HALF_WIDTH - _retryButton.width * .5 - 4; 39 | _retryButton.y = ViewPortSize.HALF_HEIGHT + 25; 40 | addChild( _retryButton ); 41 | 42 | _homeButton = StarlingFactory.getButton( AssetsInfo.BUTTON_HOME ); 43 | _homeButton.alignPivot(); 44 | _homeButton.x = ViewPortSize.HALF_WIDTH + _homeButton.width * .5 + 4; 45 | _homeButton.y = ViewPortSize.HALF_HEIGHT + 25; 46 | addChild( _homeButton ); 47 | } 48 | 49 | public function get retryButton():Button 50 | { 51 | return _retryButton; 52 | } 53 | 54 | public function get homeButton():Button 55 | { 56 | return _homeButton; 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/views/GameView.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.views 2 | { 3 | import setzer.tetris.assets.AssetsInfo; 4 | import setzer.tetris.factories.StarlingFactory; 5 | import setzer.tetris.utils.MagicValues; 6 | import setzer.tetris.utils.ViewPortSize; 7 | import setzer.tetris.views.components.GridComponent; 8 | import setzer.tetris.views.components.HUDGameComponent; 9 | import setzer.tetris.views.components.NextPieceComponent; 10 | 11 | import starling.display.Image; 12 | import starling.display.Sprite; 13 | 14 | public class GameView extends Sprite 15 | { 16 | private var _gridComponent:GridComponent; 17 | private var _nextPieceComponent:NextPieceComponent; 18 | private var _hudComponent:HUDGameComponent; 19 | 20 | public function GameView() 21 | { 22 | createBackground(); 23 | } 24 | 25 | private function createBackground():void 26 | { 27 | addChild( StarlingFactory.getColorBackground() ); 28 | 29 | var grid:Image = StarlingFactory.getImage( AssetsInfo.GRID ); 30 | grid.alignPivot(); 31 | grid.x = MagicValues.BORDER_OFFSET + grid.pivotX; 32 | grid.y = 76 + grid.pivotY; 33 | addChild( grid ); 34 | 35 | var nextPieceBg:Image = StarlingFactory.getImage( AssetsInfo.NEXT_TILE ); 36 | nextPieceBg.x = ViewPortSize.MAX_WIDTH - nextPieceBg.width - MagicValues.BORDER_OFFSET; 37 | nextPieceBg.y = 76; 38 | addChild( nextPieceBg ); 39 | } 40 | 41 | public function destroy():void 42 | { 43 | removeChild( _gridComponent ); 44 | removeChild( _nextPieceComponent ); 45 | removeChild( _hudComponent ); 46 | 47 | _gridComponent = null; 48 | _nextPieceComponent = null; 49 | _hudComponent = null; 50 | } 51 | 52 | public function createComponents():void 53 | { 54 | _nextPieceComponent = new NextPieceComponent(); 55 | _nextPieceComponent.x = ViewPortSize.MAX_WIDTH - 91 - MagicValues.BORDER_OFFSET + 1; 56 | _nextPieceComponent.y = 76 + 1; 57 | addChild( _nextPieceComponent ); 58 | 59 | _gridComponent = new GridComponent(); 60 | _gridComponent.x = MagicValues.BORDER_OFFSET; 61 | _gridComponent.y = 76; 62 | addChild( _gridComponent ); 63 | 64 | _hudComponent = new HUDGameComponent(); 65 | addChild( _hudComponent ); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/views/HomeView.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.views 2 | { 3 | import setzer.tetris.assets.AssetsInfo; 4 | import setzer.tetris.factories.StarlingFactory; 5 | import setzer.tetris.utils.MagicValues; 6 | import setzer.tetris.utils.ViewPortSize; 7 | 8 | import starling.display.Button; 9 | import starling.display.Image; 10 | import starling.display.Sprite; 11 | 12 | public class HomeView extends Sprite 13 | { 14 | private var _startButton:Button; 15 | private var _configButton:Button; 16 | 17 | public function HomeView() 18 | { 19 | addChild( StarlingFactory.getColorBackground() ); 20 | 21 | var logoTetris:Image = StarlingFactory.getImage( AssetsInfo.LOGO_TETRIS ); 22 | logoTetris.x = ViewPortSize.HALF_WIDTH; 23 | logoTetris.y = ViewPortSize.MAX_HEIGHT * .3; 24 | logoTetris.alignPivot(); 25 | addChild( logoTetris ); 26 | 27 | _startButton = StarlingFactory.getButton( AssetsInfo.BUTTON_START ); 28 | _startButton.x = ViewPortSize.HALF_WIDTH; 29 | _startButton.y = ViewPortSize.MAX_HEIGHT * .6; 30 | _startButton.alignPivot(); 31 | addChild( _startButton ); 32 | 33 | _configButton = StarlingFactory.getButton( AssetsInfo.BUTTON_CONFIG ); 34 | _configButton.x = ViewPortSize.HALF_WIDTH; 35 | _configButton.y = _startButton.y + 10 + _configButton.height; 36 | _configButton.alignPivot(); 37 | addChild( _configButton ); 38 | 39 | var logoSetzer:Image = StarlingFactory.getImage( AssetsInfo.LOGO_SETZER ); 40 | logoSetzer.x = MagicValues.BORDER_OFFSET; 41 | logoSetzer.y = ViewPortSize.MAX_HEIGHT - MagicValues.BORDER_OFFSET - logoSetzer.height; 42 | addChild( logoSetzer ); 43 | } 44 | 45 | public function get startButton():Button 46 | { 47 | return _startButton; 48 | } 49 | 50 | public function get configButton():Button 51 | { 52 | return _configButton; 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/views/InfoPopup.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.views 2 | { 3 | import setzer.tetris.assets.AssetsInfo; 4 | import setzer.tetris.factories.StarlingFactory; 5 | import setzer.tetris.utils.Colors; 6 | import setzer.tetris.utils.MagicValues; 7 | import setzer.tetris.utils.Texts; 8 | import setzer.tetris.utils.ViewPortSize; 9 | 10 | import starling.display.Button; 11 | import starling.display.Sprite; 12 | import starling.text.TextField; 13 | 14 | public class InfoPopup extends Sprite 15 | { 16 | private var _closeButton:Button; 17 | 18 | public function InfoPopup() 19 | { 20 | addChild( StarlingFactory.getShadowBackground() ); 21 | 22 | var boardBackground:Sprite = StarlingFactory.getBoardBackground(); 23 | boardBackground.x = ViewPortSize.HALF_WIDTH; 24 | boardBackground.y = ViewPortSize.HALF_HEIGHT; 25 | addChild( boardBackground ); 26 | 27 | _closeButton = StarlingFactory.getButton( AssetsInfo.BUTTON_CANCEL ); 28 | _closeButton.pivotX = _closeButton.width; 29 | _closeButton.x = ViewPortSize.MAX_WIDTH - MagicValues.BORDER_OFFSET; 30 | _closeButton.y = MagicValues.BORDER_OFFSET; 31 | addChild( _closeButton ); 32 | 33 | var commands:TextField = StarlingFactory.getTextField( 300, Texts.COMMANDS, Colors.DYNAMIC_TEXT ); 34 | commands.x = ViewPortSize.HALF_WIDTH; 35 | commands.y = ViewPortSize.HALF_HEIGHT; 36 | commands.height = 300; 37 | commands.alignPivot(); 38 | addChild( commands ); 39 | } 40 | 41 | public function get closeButton():Button 42 | { 43 | return _closeButton; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/views/IntroView.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.views 2 | { 3 | import com.greensock.TimelineLite; 4 | import com.greensock.TweenLite; 5 | 6 | import setzer.tetris.assets.Embeds; 7 | 8 | import setzer.tetris.factories.StarlingFactory; 9 | import setzer.tetris.utils.Colors; 10 | import setzer.tetris.utils.Texts; 11 | import setzer.tetris.utils.ViewPortSize; 12 | 13 | import starling.display.Image; 14 | 15 | import starling.display.Sprite; 16 | import starling.text.TextField; 17 | import starling.textures.Texture; 18 | 19 | public class IntroView extends Sprite 20 | { 21 | private var _txt:TextField; 22 | private var _logo:Image; 23 | 24 | public function IntroView() 25 | { 26 | addChild( StarlingFactory.getColorBackground() ); 27 | 28 | _txt = StarlingFactory.getTextField( 300, Texts.DEVELOPER, Colors.DYNAMIC_TEXT ); 29 | _txt.alignPivot(); 30 | _txt.x = ViewPortSize.HALF_WIDTH; 31 | _txt.y = ViewPortSize.HALF_HEIGHT; 32 | _txt.alpha = 0; 33 | addChild( _txt ); 34 | 35 | _logo = new Image( Texture.fromEmbeddedAsset( Embeds.LANGUAGE_IMAGE ) ); 36 | _logo.pivotX = _logo.width * .5; 37 | _logo.x = ViewPortSize.HALF_WIDTH; 38 | _logo.y = ViewPortSize.MAX_HEIGHT - _logo.height; 39 | _logo.alpha = 0; 40 | addChild( _logo ); 41 | 42 | } 43 | 44 | public function playAnimation():void 45 | { 46 | var timeline:TimelineLite = new TimelineLite(); 47 | timeline.append( new TweenLite( _txt, .8, {scaleX:1.2, scaleY:1.2, alpha:1} ) ); 48 | timeline.append( new TweenLite( _txt, 2, {scaleX:1.2, scaleY:1.2, alpha:1} ) ); 49 | timeline.append( new TweenLite( _txt, .3, {scaleX:1, scaleY:1, alpha:0} ) ); 50 | 51 | var timelineLogo:TimelineLite = new TimelineLite(); 52 | timelineLogo.append( new TweenLite( _logo, .8, {alpha:1} ) ); 53 | timelineLogo.append( new TweenLite( _logo, 2, {alpha:1} ) ); 54 | timelineLogo.append( new TweenLite( _logo, .3, {alpha:0} ) ); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/views/MainStarlingView.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.views 2 | { 3 | import starling.display.Sprite; 4 | 5 | public class MainStarlingView extends Sprite 6 | { 7 | public function MainStarlingView() 8 | { 9 | super(); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/views/PausePopup.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.views 2 | { 3 | import setzer.tetris.assets.AssetsInfo; 4 | import setzer.tetris.factories.StarlingFactory; 5 | import setzer.tetris.utils.Colors; 6 | import setzer.tetris.utils.MagicValues; 7 | import setzer.tetris.utils.Texts; 8 | import setzer.tetris.utils.ViewPortSize; 9 | 10 | import starling.display.Button; 11 | import starling.display.Sprite; 12 | import starling.text.TextField; 13 | 14 | public class PausePopup extends Sprite 15 | { 16 | private var _resumeButton:Button; 17 | private var _homeButton:Button; 18 | 19 | public function PausePopup() 20 | { 21 | addChild( StarlingFactory.getShadowBackground() ); 22 | 23 | var boardBackground:Sprite = StarlingFactory.getBoardBackground(); 24 | boardBackground.x = ViewPortSize.HALF_WIDTH; 25 | boardBackground.y = ViewPortSize.HALF_HEIGHT; 26 | addChild( boardBackground ); 27 | 28 | _resumeButton = StarlingFactory.getButton( AssetsInfo.BUTTON_RESUME ); 29 | _resumeButton.pivotX = _resumeButton.width; 30 | _resumeButton.x = ViewPortSize.MAX_WIDTH - MagicValues.BORDER_OFFSET; 31 | _resumeButton.y = MagicValues.BORDER_OFFSET; 32 | addChild( _resumeButton ); 33 | 34 | _homeButton = StarlingFactory.getButton( AssetsInfo.BUTTON_HOME ); 35 | _homeButton.pivotX = _homeButton.width; 36 | _homeButton.x = ViewPortSize.MAX_WIDTH - _resumeButton.width - MagicValues.BORDER_OFFSET - 4; 37 | _homeButton.y = MagicValues.BORDER_OFFSET; 38 | addChild( _homeButton ); 39 | 40 | var pause:TextField = StarlingFactory.getTextField( 300, Texts.PAUSED, Colors.DYNAMIC_TEXT ); 41 | pause.x = ViewPortSize.HALF_WIDTH; 42 | pause.y = ViewPortSize.HALF_HEIGHT; 43 | pause.scaleX = 1.2; 44 | pause.scaleY = 1.2; 45 | pause.alignPivot(); 46 | addChild( pause ); 47 | } 48 | 49 | public function get resumeButton():Button 50 | { 51 | return _resumeButton; 52 | } 53 | 54 | public function get homeButton():Button 55 | { 56 | return _homeButton; 57 | } 58 | } 59 | } -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/views/ResetConfimPopup.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.views 2 | { 3 | import setzer.tetris.assets.AssetsInfo; 4 | import setzer.tetris.factories.StarlingFactory; 5 | import setzer.tetris.utils.Colors; 6 | import setzer.tetris.utils.Texts; 7 | import setzer.tetris.utils.ViewPortSize; 8 | 9 | import starling.display.Button; 10 | 11 | import starling.display.Sprite; 12 | import starling.text.TextField; 13 | 14 | public class ResetConfimPopup extends Sprite 15 | { 16 | private var _cancelButton:Button; 17 | private var _confirmButton:Button; 18 | 19 | public function ResetConfimPopup() 20 | { 21 | addChild( StarlingFactory.getShadowBackground() ); 22 | 23 | var boardBackground:Sprite = StarlingFactory.getBoardBackground(); 24 | boardBackground.x = ViewPortSize.HALF_WIDTH; 25 | boardBackground.y = ViewPortSize.HALF_HEIGHT; 26 | addChild( boardBackground ); 27 | 28 | var question:TextField = StarlingFactory.getTextField( 400, Texts.CONFIRM_RESET, Colors.DYNAMIC_TEXT ); 29 | question.x = ViewPortSize.HALF_WIDTH; 30 | question.y = ViewPortSize.HALF_HEIGHT - 25; 31 | question.height = 150; 32 | question.scaleX = .9; 33 | question.scaleY = .9; 34 | question.alignPivot(); 35 | addChild( question ); 36 | 37 | _confirmButton = StarlingFactory.getButton( AssetsInfo.BUTTON_CONFIRM ); 38 | _confirmButton.alignPivot(); 39 | _confirmButton.x = ViewPortSize.HALF_WIDTH - _confirmButton.width * .5 - 4; 40 | _confirmButton.y = ViewPortSize.HALF_HEIGHT + 25; 41 | addChild( _confirmButton ); 42 | 43 | _cancelButton = StarlingFactory.getButton( AssetsInfo.BUTTON_CANCEL ); 44 | _cancelButton.alignPivot(); 45 | _cancelButton.x = ViewPortSize.HALF_WIDTH + _cancelButton.width * .5 + 4; 46 | _cancelButton.y = ViewPortSize.HALF_HEIGHT + 25; 47 | addChild( _cancelButton ); 48 | } 49 | 50 | public function get confirmButton():Button 51 | { 52 | return _confirmButton; 53 | } 54 | 55 | public function get cancelButton():Button 56 | { 57 | return _cancelButton; 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/views/StartingPopup.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.views 2 | { 3 | import com.greensock.TimelineLite; 4 | import com.greensock.TweenLite; 5 | import com.greensock.easing.Bounce; 6 | 7 | import setzer.tetris.factories.StarlingFactory; 8 | import setzer.tetris.utils.Colors; 9 | import setzer.tetris.utils.ViewPortSize; 10 | 11 | import starling.display.DisplayObject; 12 | import starling.display.Sprite; 13 | import starling.events.Event; 14 | import starling.text.TextField; 15 | 16 | public class StartingPopup extends Sprite 17 | { 18 | private var decreasingNumber:TextField; 19 | private var timeline:TimelineLite; 20 | private var count:int; 21 | private var background:DisplayObject; 22 | 23 | public function StartingPopup() 24 | { 25 | background = StarlingFactory.getShadowBackground(); 26 | addChild( background ); 27 | 28 | count = 3; 29 | 30 | decreasingNumber = StarlingFactory.getTextField( 40, "3", Colors.DYNAMIC_TEXT ); 31 | decreasingNumber.alignPivot(); 32 | decreasingNumber.x = ViewPortSize.HALF_WIDTH; 33 | decreasingNumber.y = ViewPortSize.HALF_HEIGHT; 34 | addChild( decreasingNumber ); 35 | 36 | timeline = new TimelineLite( {onComplete:timeLineComplete} ); 37 | timeline.append( new TweenLite( decreasingNumber, .4, { 38 | scaleX:1.5, scaleY:1.5, ease:Bounce.easeOut, onComplete:changeNumber 39 | } ) ); 40 | timeline.append( new TweenLite( decreasingNumber, .4, { 41 | scaleX:1.5, scaleY:1.5, ease:Bounce.easeOut, onComplete:changeNumber 42 | } ) ); 43 | timeline.append( new TweenLite( decreasingNumber, .4, { 44 | scaleX:1.5, scaleY:1.5, ease:Bounce.easeOut 45 | } ) ); 46 | timeline.stop(); 47 | } 48 | 49 | private function changeNumber():void 50 | { 51 | background.alpha -= .1; 52 | count -= 1; 53 | decreasingNumber.scaleX = .1; 54 | decreasingNumber.scaleY = .1; 55 | decreasingNumber.text = String( count ); 56 | } 57 | 58 | private function timeLineComplete():void 59 | { 60 | timeline.stop(); 61 | timeline = null; 62 | dispatchEvent( new Event( Event.COMPLETE ) ); 63 | } 64 | 65 | public function destroy():void 66 | { 67 | removeFromParent(); 68 | } 69 | 70 | public function start():void 71 | { 72 | timeline.restart(); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/views/components/DoubleTextField.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.views.components 2 | { 3 | import setzer.tetris.factories.StarlingFactory; 4 | import setzer.tetris.utils.Colors; 5 | 6 | import starling.display.Sprite; 7 | import starling.text.TextField; 8 | 9 | public class DoubleTextField extends Sprite 10 | { 11 | private var _text:TextField; 12 | 13 | public function DoubleTextField( label:String ) 14 | { 15 | var labelField:TextField = StarlingFactory.getTextField( 120, label, Colors.STATIC_TEXT, "right" ); 16 | labelField.pivotX = labelField.width; 17 | addChild( labelField ); 18 | 19 | _text = StarlingFactory.getTextField( 80, "0", Colors.DYNAMIC_TEXT, "right" ); 20 | _text.pivotX = _text.width; 21 | _text.y = labelField.y + 30; 22 | addChild( _text ); 23 | } 24 | 25 | public function get text():String 26 | { 27 | return _text.text; 28 | } 29 | 30 | public function set text( value:String ):void 31 | { 32 | _text.text = value; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/views/components/GridComponent.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.views.components 2 | { 3 | import setzer.tetris.utils.TilePool; 4 | 5 | import starling.display.Sprite; 6 | 7 | public class GridComponent extends Sprite 8 | { 9 | public function clear():void 10 | { 11 | while ( numChildren > 0 ) 12 | { 13 | if ( getChildAt( 0 ) is TileDisplay ) 14 | TilePool.back( TileDisplay( getChildAt( 0 ) ) ); 15 | removeChildAt( 0 ); 16 | } 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/views/components/HUDGameComponent.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.views.components 2 | { 3 | import setzer.tetris.assets.AssetsInfo; 4 | import setzer.tetris.factories.StarlingFactory; 5 | import setzer.tetris.models.GameModel; 6 | import setzer.tetris.utils.Colors; 7 | import setzer.tetris.utils.MagicValues; 8 | import setzer.tetris.utils.Texts; 9 | import setzer.tetris.utils.ViewPortSize; 10 | 11 | import starling.display.Button; 12 | import starling.display.Sprite; 13 | import starling.text.TextField; 14 | 15 | public class HUDGameComponent extends Sprite 16 | { 17 | private var _linesText:DoubleTextField; 18 | private var _scoreText:DoubleTextField; 19 | private var _levelText:DoubleTextField; 20 | private var _hiScoreText:DoubleTextField; 21 | 22 | private var _pauseButton:Button; 23 | 24 | public function HUDGameComponent() 25 | { 26 | createTextFields(); 27 | createButtons(); 28 | } 29 | 30 | private function createButtons():void 31 | { 32 | _pauseButton = StarlingFactory.getButton( AssetsInfo.BUTTON_PAUSE ); 33 | _pauseButton.pivotX = _pauseButton.width; 34 | _pauseButton.x = ViewPortSize.MAX_WIDTH - MagicValues.BORDER_OFFSET; 35 | _pauseButton.y = MagicValues.BORDER_OFFSET; 36 | addChild( _pauseButton ); 37 | } 38 | 39 | private function createTextFields():void 40 | { 41 | var gameLabel:TextField = StarlingFactory.getTextField( 100, Texts.TETRIS, Colors.DYNAMIC_TEXT, "left" ); 42 | gameLabel.x = MagicValues.BORDER_OFFSET; 43 | gameLabel.y = 24; 44 | addChild( gameLabel ); 45 | 46 | _levelText = new DoubleTextField( Texts.LEVEL ); 47 | _levelText.x = ViewPortSize.MAX_WIDTH - MagicValues.BORDER_OFFSET; 48 | _levelText.y = 184; 49 | addChild( _levelText ); 50 | 51 | _scoreText = new DoubleTextField( Texts.SCORE ); 52 | _scoreText.x = ViewPortSize.MAX_WIDTH - MagicValues.BORDER_OFFSET; 53 | _scoreText.y = 252; 54 | addChild( _scoreText ); 55 | 56 | _linesText = new DoubleTextField( Texts.LINES ); 57 | _linesText.x = ViewPortSize.MAX_WIDTH - MagicValues.BORDER_OFFSET; 58 | _linesText.y = 318; 59 | addChild( _linesText ); 60 | 61 | _hiScoreText = new DoubleTextField( Texts.HI_SCORE ); 62 | _hiScoreText.x = ViewPortSize.MAX_WIDTH - MagicValues.BORDER_OFFSET; 63 | _hiScoreText.y = 386; 64 | addChild( _hiScoreText ); 65 | } 66 | 67 | public function updateData( model:GameModel ):void 68 | { 69 | _linesText.text = model.lines.toString(); 70 | _scoreText.text = model.score.toString(); 71 | _levelText.text = model.level.toString(); 72 | _hiScoreText.text = model.hiScore.toString(); 73 | } 74 | 75 | public function get pauseButton():Button 76 | { 77 | return _pauseButton; 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/views/components/NextPieceComponent.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.views.components 2 | { 3 | import starling.display.Sprite; 4 | 5 | public class NextPieceComponent extends Sprite 6 | { 7 | public function NextPieceComponent() 8 | { 9 | super(); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /actionscript-tetris/src/setzer/tetris/views/components/TileDisplay.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.views.components 2 | { 3 | import starling.display.Image; 4 | import starling.textures.Texture; 5 | 6 | public class TileDisplay extends Image 7 | { 8 | private var _typeId:int; 9 | 10 | public function TileDisplay( texture:Texture, typeId:int ) 11 | { 12 | super( texture ); 13 | 14 | _typeId = typeId; 15 | } 16 | 17 | public function get typeId():int 18 | { 19 | return _typeId; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /actionscript-tetris/test/setzer/tetris/factories/TileGroupFactoryTest.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.factories 2 | { 3 | import org.flexunit.asserts.assertEquals; 4 | import org.flexunit.asserts.assertNotNull; 5 | 6 | import setzer.tetris.models.Tile; 7 | import setzer.tetris.models.TileGroup; 8 | import setzer.tetris.models.TileGroupType; 9 | 10 | public class TileGroupFactoryTest 11 | { 12 | [Test] 13 | public function testGetTileGroup():void 14 | { 15 | var tileGroup:TileGroup = TileGroupFactory.getTileGroup( TileGroupType.TYPE_I ); 16 | assertNotNull( tileGroup ); 17 | assertEquals( tileGroup.typeId, TileGroupType.TYPE_I ); 18 | assertEquals( tileGroup.tiles.length, 4 ) 19 | } 20 | 21 | [Test] 22 | public function testGetRandomTileGroup():void 23 | { 24 | var tileGroup:TileGroup = TileGroupFactory.getRandomTileGroup(); 25 | assertNotNull( tileGroup ); 26 | } 27 | 28 | [Test] 29 | public function testGetTilesByTypeArray():void 30 | { 31 | var array:Array = TileGroupType.getTypeArray( TileGroupType.TYPE_L ); 32 | var tiles:Vector. = TileGroupFactory.getTilesByTypeArray( array ); 33 | 34 | assertNotNull( tiles ); 35 | assertEquals( tiles.length, array.length ); 36 | assertEquals( tiles[0].col, array[0] % 2 ); 37 | assertEquals( tiles[0].row, int( array[0] / 2 ) ); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /actionscript-tetris/test/setzer/tetris/models/GameModelTest.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.models 2 | { 3 | import org.flexunit.asserts.assertEquals; 4 | 5 | public class GameModelTest 6 | { 7 | [Test] 8 | public function testClear():void 9 | { 10 | var model:GameModel = new GameModel(); 11 | model.clear(); 12 | 13 | assertEquals( model.level, 1 ); 14 | assertEquals( model.lines, 0 ); 15 | assertEquals( model.score, 0 ); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /actionscript-tetris/test/setzer/tetris/models/GridTest.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.models 2 | { 3 | import org.flexunit.Assert; 4 | 5 | public class GridTest 6 | { 7 | private var grid:Grid; 8 | 9 | [Before] 10 | public function setup():void 11 | { 12 | grid = new Grid(); 13 | } 14 | 15 | 16 | [Test] 17 | public function testRemoveRow():void 18 | { 19 | var tile:Tile = new Tile( 0, 5 ); 20 | grid.setTile( tile, 0, 5 ); 21 | 22 | var removedRow:Vector. = grid.removeRow( 5 ); 23 | 24 | Assert.assertNull( grid.getTile( 0, 5 ) ); 25 | Assert.assertEquals( tile, removedRow[0] ); 26 | Assert.assertNull( grid.getTile( 0, grid.maxRows - 1 ) ); 27 | } 28 | 29 | [Test] 30 | public function testGetRow():void 31 | { 32 | var tile:Tile = new Tile(); 33 | grid.setTile( tile, 0, 0 ); 34 | 35 | var gridRow:Vector. = grid.getRow( 0 ); 36 | 37 | Assert.assertEquals( tile, gridRow[0] ); 38 | } 39 | 40 | [Test] 41 | public function testGenerateEmptyGrid():void 42 | { 43 | Assert.assertNotNull( grid.isEmptyTile( 0, 0 ) ); 44 | Assert.assertNotNull( grid.isEmptyTile( grid.maxCols - 1, grid.maxRows - 1 ) ); 45 | } 46 | 47 | [Test] 48 | public function testIsTileEmpty():void 49 | { 50 | grid.setTile( null, 0, 0 ); 51 | Assert.assertTrue( grid.isEmptyTile( 0, 0 ) ); 52 | 53 | grid.setTile( new Tile(), 0, 0 ); 54 | Assert.assertFalse( grid.isEmptyTile( 0, 0 ) ); 55 | } 56 | 57 | [Test] 58 | public function testGetTile():void 59 | { 60 | grid.setTile( null, 0, 0 ); 61 | grid.setTile( new Tile( 1, 1 ), 1, 1 ); 62 | 63 | Assert.assertNull( grid.getTile( 0, 0 ) ); 64 | Assert.assertNotNull( grid.getTile( 1, 1 ) ); 65 | } 66 | 67 | [Test] 68 | public function testSetTile():void 69 | { 70 | Assert.assertNull( grid.getTile( 0, 0 ) ); 71 | 72 | var tile:Tile = new Tile(); 73 | grid.setTile( tile, 0, 0 ); 74 | 75 | Assert.assertEquals( tile, grid.getTile( 0, 0 ) ); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /actionscript-tetris/test/setzer/tetris/models/TileGroupTest.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.models 2 | { 3 | import org.flexunit.asserts.assertEquals; 4 | 5 | import setzer.tetris.factories.TileGroupFactory; 6 | 7 | public class TileGroupTest 8 | { 9 | private var tileGroup:TileGroup; 10 | private var tilesBackup:Vector.; 11 | 12 | [Before] 13 | public function setup():void 14 | { 15 | tileGroup = TileGroupFactory.getRandomTileGroup(); 16 | tilesBackup = generateTilesBackup( tileGroup.tiles ); 17 | } 18 | 19 | private function generateTilesBackup( tiles:Vector. ):Vector. 20 | { 21 | var bkpTiles:Vector. = new Vector.(); 22 | for ( var i:int = 0; i < tiles.length; i++ ) 23 | { 24 | bkpTiles.push( tiles[i].clone() ); 25 | 26 | } 27 | return bkpTiles; 28 | } 29 | 30 | [Test] 31 | public function testMoveVertical_down():void 32 | { 33 | tileGroup.moveVertical( 1 ); 34 | 35 | var tile:Tile; 36 | var tileBefore:Tile; 37 | for ( var i:int = 0; i < tileGroup.tiles.length; i++ ) 38 | { 39 | tile = tileGroup.tiles[i]; 40 | tileBefore = tilesBackup[i]; 41 | tileBefore.row += 1; 42 | 43 | assertEquals( tileBefore.col, tile.col ); 44 | assertEquals( tileBefore.row, tile.row ); 45 | } 46 | } 47 | 48 | [Test] 49 | public function testMoveVertical_up():void 50 | { 51 | tileGroup.moveVertical( -1 ); 52 | 53 | var tile:Tile; 54 | var tileBefore:Tile; 55 | for ( var i:int = 0; i < tileGroup.tiles.length; i++ ) 56 | { 57 | tile = tileGroup.tiles[i]; 58 | tileBefore = tilesBackup[i]; 59 | tileBefore.row -= 1; 60 | 61 | assertEquals( tileBefore.col, tile.col ); 62 | assertEquals( tileBefore.row, tile.row ); 63 | } 64 | } 65 | 66 | [Test] 67 | public function testMoveHorizontal_right():void 68 | { 69 | tileGroup.moveHorizontal( 1 ); 70 | 71 | var tile:Tile; 72 | var tileBefore:Tile; 73 | for ( var i:int = 0; i < tileGroup.tiles.length; i++ ) 74 | { 75 | tile = tileGroup.tiles[i]; 76 | tileBefore = tilesBackup[i]; 77 | tileBefore.col += 1; 78 | 79 | assertEquals( tileBefore.col, tile.col ); 80 | assertEquals( tileBefore.row, tile.row ); 81 | } 82 | } 83 | 84 | [Test] 85 | public function testMoveHorizontal_left():void 86 | { 87 | tileGroup.moveHorizontal( -1 ); 88 | 89 | var tile:Tile; 90 | var tileBefore:Tile; 91 | for ( var i:int = 0; i < tileGroup.tiles.length; i++ ) 92 | { 93 | tile = tileGroup.tiles[i]; 94 | tileBefore = tilesBackup[i]; 95 | tileBefore.col -= 1; 96 | 97 | assertEquals( tileBefore.col, tile.col ); 98 | assertEquals( tileBefore.row, tile.row ); 99 | } 100 | } 101 | 102 | [Test] 103 | public function testRotate():void 104 | { 105 | tileGroup.rotate(); 106 | 107 | var tile:Tile; 108 | var tileBefore:Tile; 109 | 110 | var tempAnchor:Tile = tilesBackup[1]; 111 | var tempTile:Tile = new Tile(); 112 | for ( var i:int = 0; i < tileGroup.tiles.length; i++ ) 113 | { 114 | tile = tileGroup.tiles[i]; 115 | tileBefore = tilesBackup[i]; 116 | 117 | tempTile.col = tileBefore.row - tempAnchor.row; 118 | tempTile.row = tileBefore.col - tempAnchor.col; 119 | tileBefore.col = tempAnchor.col - tempTile.col; 120 | tileBefore.row = tempAnchor.row + tempTile.row; 121 | 122 | assertEquals( tileBefore.col, tile.col ); 123 | assertEquals( tileBefore.row, tile.row ); 124 | } 125 | } 126 | 127 | [Test] 128 | public function testRollbackX():void 129 | { 130 | tileGroup.moveHorizontal( 1 ); 131 | tileGroup.rollbackX(); 132 | 133 | var tile:Tile; 134 | var tileBefore:Tile; 135 | 136 | for ( var i:int = 0; i < tileGroup.tiles.length; i++ ) 137 | { 138 | tile = tileGroup.tiles[i]; 139 | tileBefore = tilesBackup[i]; 140 | 141 | assertEquals( tile.col, tileBefore.col ); 142 | assertEquals( tile.col, tile.bCol ); 143 | } 144 | } 145 | 146 | [Test] 147 | public function testRollbackY():void 148 | { 149 | tileGroup.moveVertical( 1 ); 150 | tileGroup.rollbackY(); 151 | 152 | var tile:Tile; 153 | var tileBefore:Tile; 154 | 155 | for ( var i:int = 0; i < tileGroup.tiles.length; i++ ) 156 | { 157 | tile = tileGroup.tiles[i]; 158 | tileBefore = tilesBackup[i]; 159 | 160 | assertEquals( tile.row, tileBefore.row ); 161 | assertEquals( tile.row, tile.bRow ); 162 | } 163 | } 164 | 165 | [Test] 166 | public function testRollback():void 167 | { 168 | tileGroup.moveVertical( 1 ); 169 | tileGroup.moveHorizontal( 1 ); 170 | tileGroup.rollback(); 171 | 172 | var tile:Tile; 173 | var tileBefore:Tile; 174 | 175 | for ( var i:int = 0; i < tileGroup.tiles.length; i++ ) 176 | { 177 | tile = tileGroup.tiles[i]; 178 | tileBefore = tilesBackup[i]; 179 | 180 | assertEquals( tile.col, tileBefore.col ); 181 | assertEquals( tile.row, tileBefore.row ); 182 | 183 | assertEquals( tile.col, tile.bCol ); 184 | assertEquals( tile.row, tile.bRow ); 185 | } 186 | } 187 | } 188 | } 189 | -------------------------------------------------------------------------------- /actionscript-tetris/test/setzer/tetris/models/TileTest.as: -------------------------------------------------------------------------------- 1 | package setzer.tetris.models 2 | { 3 | import org.flexunit.Assert; 4 | 5 | public class TileTest 6 | { 7 | private var tile:Tile; 8 | 9 | [Before] 10 | public function setup():void 11 | { 12 | tile = new Tile(); 13 | } 14 | 15 | [Test] 16 | public function testClone():void 17 | { 18 | tile.row = 1; 19 | tile.col = 1; 20 | 21 | var clone:Tile = tile.clone(); 22 | 23 | Assert.assertEquals( tile.row, clone.row ); 24 | Assert.assertEquals( tile.col, clone.col ); 25 | } 26 | 27 | [Test] 28 | public function testToString():void 29 | { 30 | tile.row = 5; 31 | tile.col = 5; 32 | 33 | var str:String = tile.toString(); 34 | 35 | Assert.assertEquals( str, "tile_col_5_row_5" ); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /gif_tetris_as_demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/gif_tetris_as_demo.gif -------------------------------------------------------------------------------- /gif_tetris_ts_demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/gif_tetris_ts_demo.gif -------------------------------------------------------------------------------- /img_cover_tetris.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/img_cover_tetris.png -------------------------------------------------------------------------------- /img_game_tetris.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/img_game_tetris.png -------------------------------------------------------------------------------- /typescript-tetris/.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | end_of_line = lf 5 | charset = utf-8 6 | trim_trailing_whitespace = true 7 | indent_style = space 8 | indent_size = 4 9 | 10 | [*.{ts}] 11 | indent_style = space 12 | indent_size = 4 13 | 14 | [*.{js,json}] 15 | indent_style = space 16 | indent_size = 2 17 | 18 | [{.codeclimate.yml,.eslintignore,.eslintrc,.istanbul.yml,.publishrc,.travis.yml}] 19 | indent_style = space 20 | indent_size = 2 21 | -------------------------------------------------------------------------------- /typescript-tetris/.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "printWidth": 120, 3 | "tabWidth": 2, 4 | "overrides": [ 5 | { 6 | "files": "*.ts", 7 | "options": { 8 | "tabWidth": 4 9 | } 10 | } 11 | ], 12 | "useTabs": false, 13 | "semi": true, 14 | "singleQuote": false, 15 | "trailingComma": "none", 16 | "bracketSpacing": true 17 | } 18 | -------------------------------------------------------------------------------- /typescript-tetris/README.md: -------------------------------------------------------------------------------- 1 | ![cover](media/img_cover_tetris_ts.png) 2 | 3 | This is a TypeScript open-source game which is a port of the ActionScript version, following the same concepts, graphics, and architecture. 4 | 5 | + **Category:** Puzzle. 6 | + **Platform:** Web. 7 | + **Language:** TypeScript. 8 | + **Technologies:** PixiJs, RobotlegsJS, RobotlegsJS-Pixi, RobotlegsJS-Pixi-Palidor. 9 | 10 | * * * 11 | 12 | ### Gameplay 13 | 14 | ![gif_ts](media/gif_tetris_ts_demo.gif) 15 | 16 | * * * 17 | 18 | ### Dependencies 19 | 20 | + [PixiJs](http://www.pixijs.com/) 21 | + [RobotlegsJs](https://github.com/RobotlegsJS/RobotlegsJS) 22 | + [Robotlegs-Pixi](https://github.com/RobotlegsJS/RobotlegsJS-Pixi) 23 | + [Robotlegs-Pixi-Palidor](https://github.com/RobotlegsJS/RobotlegsJS-Pixi-Palidor) 24 | 25 | * * * 26 | 27 | ### Demo 28 | + **[ActionScript](https://ronaldosetzer.github.io/portfolio/open_source/tetris_as/)** 29 | + **[TypeScript](https://ronaldosetzer.github.io/portfolio/open_source/tetris_ts/)** 30 | 31 | * * * 32 | 33 | ### Game 34 | 35 | #### Game Flow 36 | 37 | ![screenshot01](media/img_ss_tetris_ts_01.png) 38 | 39 | * * * 40 | 41 | ### Screenshots 42 | ![screenshot01](media/img_game_tetris_ts.png) 43 | * * * 44 | 45 | **Ronaldo Santiago** - Game Developer [ [portfolio](https://ronaldosetzer.github.io/portfolio/) ] 46 | -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/buttons/button_cancel_over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/buttons/button_cancel_over.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/buttons/button_cancel_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/buttons/button_cancel_up.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/buttons/button_config_over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/buttons/button_config_over.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/buttons/button_config_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/buttons/button_config_up.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/buttons/button_confirm_over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/buttons/button_confirm_over.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/buttons/button_confirm_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/buttons/button_confirm_up.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/buttons/button_home_over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/buttons/button_home_over.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/buttons/button_home_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/buttons/button_home_up.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/buttons/button_pause_over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/buttons/button_pause_over.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/buttons/button_pause_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/buttons/button_pause_up.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/buttons/button_reset_over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/buttons/button_reset_over.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/buttons/button_reset_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/buttons/button_reset_up.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/buttons/button_resume_over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/buttons/button_resume_over.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/buttons/button_resume_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/buttons/button_resume_up.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/buttons/button_retry_over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/buttons/button_retry_over.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/buttons/button_retry_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/buttons/button_retry_up.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/buttons/button_start_over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/buttons/button_start_over.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/buttons/button_start_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/buttons/button_start_up.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/displays/grid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/displays/grid.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/displays/logo_setzer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/displays/logo_setzer.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/displays/logo_tetris.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/displays/logo_tetris.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/displays/next_tile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/displays/next_tile.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/tiles.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/tiles.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/tiles/tile_01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/tiles/tile_01.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/tiles/tile_02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/tiles/tile_02.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/tiles/tile_03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/tiles/tile_03.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/tiles/tile_04.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/tiles/tile_04.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/tiles/tile_05.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/tiles/tile_05.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/tiles/tile_06.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/tiles/tile_06.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/tiles/tile_07.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/tiles/tile_07.png -------------------------------------------------------------------------------- /typescript-tetris/assets/atlas/tiles/tile_08.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/atlas/tiles/tile_08.png -------------------------------------------------------------------------------- /typescript-tetris/assets/fonts/SimpleSmalPixel7_.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/fonts/SimpleSmalPixel7_.png -------------------------------------------------------------------------------- /typescript-tetris/assets/fonts/SimpleSmallPixel7_0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/fonts/SimpleSmallPixel7_0.png -------------------------------------------------------------------------------- /typescript-tetris/assets/logo_typescript.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/logo_typescript.png -------------------------------------------------------------------------------- /typescript-tetris/assets/tetris-pixijs-atlas.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/assets/tetris-pixijs-atlas.png -------------------------------------------------------------------------------- /typescript-tetris/media/gif_tetris_ts_demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/media/gif_tetris_ts_demo.gif -------------------------------------------------------------------------------- /typescript-tetris/media/img_cover_tetris_ts.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/media/img_cover_tetris_ts.png -------------------------------------------------------------------------------- /typescript-tetris/media/img_game_tetris_ts.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/media/img_game_tetris_ts.png -------------------------------------------------------------------------------- /typescript-tetris/media/img_ss_tetris_ts_01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RonaldoSetzer/GAME-Tetris/5126431cfa21c3c1616a8e595eb2b70995389488/typescript-tetris/media/img_ss_tetris_ts_01.png -------------------------------------------------------------------------------- /typescript-tetris/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "typescript-tetris", 3 | "version": "1.0.0", 4 | "description": "OpenSource Game", 5 | "main": "src/index.ts", 6 | "author": "Ronaldo Santiago", 7 | "license": "MIT", 8 | "dependencies": { 9 | "@robotlegsjs/core": "^0.1.1", 10 | "@robotlegsjs/pixi": "^0.1.2", 11 | "@robotlegsjs/pixi-palidor": "^0.1.1", 12 | "pixi.js": "^4.6.1", 13 | "reflect-metadata": "^0.1.10" 14 | }, 15 | "devDependencies": { 16 | "@types/chai": "^4.0.5", 17 | "@types/mocha": "^2.2.44", 18 | "@types/pixi.js": "^4.6.0", 19 | "chai": "^4.1.2", 20 | "html-webpack-plugin": "^2.30.1", 21 | "http-server": "^0.10.0", 22 | "mocha": "^4.0.1", 23 | "prettier": "^1.8.2", 24 | "ts-loader": "^3.1.1", 25 | "ts-node": "^3.3.0", 26 | "tslint": "^5.8.0", 27 | "tslint-config-prettier": "^1.6.0", 28 | "typescript": "^2.6.1", 29 | "webpack": "^3.8.1", 30 | "webpack-dev-server": "^2.9.4" 31 | }, 32 | "scripts": { 33 | "autoformat": "prettier --config .prettierrc --write '{src,test}/**/*.ts'", 34 | "start": "webpack-dev-server --config ./webpack.setzer.config.js", 35 | "test": "mocha test/**/*.test.ts --require ts-node/register" 36 | }, 37 | "keywords": [ 38 | "RobotlegsJS", 39 | "RobotlegsJS-Pixi", 40 | "RobotlegsJS-Pixi-Palidor", 41 | "TypeScript", 42 | "PixiJs", 43 | "Game" 44 | ] 45 | } -------------------------------------------------------------------------------- /typescript-tetris/src/commands/CreateLevelCommand.ts: -------------------------------------------------------------------------------- 1 | import { ICommand, inject, injectable } from "@robotlegsjs/core"; 2 | 3 | import { GameModel } from "./../models/GameModel"; 4 | import { GameStatus } from "./../models/GameStatus"; 5 | import { FlowService } from "./../services/FlowService"; 6 | import { GameService } from "./../services/GameService"; 7 | import { TileGroupFactory } from "./../utils/TileGroupFactory"; 8 | 9 | @injectable() 10 | export class CreateLevelCommand implements ICommand { 11 | @inject(GameModel) private model: GameModel; 12 | @inject(GameService) private gameService: GameService; 13 | @inject(FlowService) private flowService: FlowService; 14 | 15 | public execute(): void { 16 | this.model.clear(); 17 | this.model.currentPiece = TileGroupFactory.getRandomTileGroup(); 18 | this.model.nextPiece = TileGroupFactory.getRandomTileGroup(); 19 | 20 | this.gameService.clearGrid(); 21 | this.gameService.updateNextPiece(); 22 | this.gameService.updateData(); 23 | 24 | if (this.model.status) { 25 | this.flowService.showStartingPopup(); 26 | } else { 27 | this.flowService.showInfoPopup(); 28 | } 29 | this.model.status = GameStatus.GAME; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /typescript-tetris/src/commands/GameOverCommand.ts: -------------------------------------------------------------------------------- 1 | import { ICommand, inject, injectable } from "@robotlegsjs/core"; 2 | 3 | import { GameModel } from "./../models/GameModel"; 4 | import { GameStatus } from "./../models/GameStatus"; 5 | import { FlowService } from "./../services/FlowService"; 6 | 7 | @injectable() 8 | export class GameOverCommand implements ICommand { 9 | @inject(GameModel) private model: GameModel; 10 | @inject(FlowService) private flowService: FlowService; 11 | 12 | public execute(): void { 13 | this.model.status = GameStatus.GAMEOVER; 14 | this.flowService.showGameOverPopup(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /typescript-tetris/src/commands/GetNextPieceCommand.ts: -------------------------------------------------------------------------------- 1 | import { ICommand, inject, injectable } from "@robotlegsjs/core"; 2 | 3 | import { GameModel } from "./../models/GameModel"; 4 | import { GameService } from "./../services/GameService"; 5 | import { TileGroupFactory } from "./../utils/TileGroupFactory"; 6 | 7 | @injectable() 8 | export class GetNextPieceCommand implements ICommand { 9 | @inject(GameModel) private model: GameModel; 10 | @inject(GameService) private gameService: GameService; 11 | 12 | public execute(): void { 13 | this.model.currentPiece = this.model.nextPiece; 14 | this.model.nextPiece = TileGroupFactory.getRandomTileGroup(); 15 | 16 | this.gameService.updateNextPiece(); 17 | this.gameService.updateData(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /typescript-tetris/src/commands/IncreasePointsCommand.ts: -------------------------------------------------------------------------------- 1 | import { ICommand, inject, injectable } from "@robotlegsjs/core"; 2 | 3 | import { GameEvent } from "./../events/GameEvent"; 4 | import { GameModel } from "./../models/GameModel"; 5 | import { GameService } from "./../services/GameService"; 6 | import { GameUtils } from "./../utils/GameUtils"; 7 | 8 | @injectable() 9 | export class IncreasePointsCommand implements ICommand { 10 | @inject(GameModel) private model: GameModel; 11 | @inject(GameEvent) private event: GameEvent; 12 | @inject(GameService) private gameService: GameService; 13 | 14 | public execute(): void { 15 | this.model.score += GameUtils.getPointsByLines(this.event.lines); 16 | this.model.level = GameUtils.getCurrentLevel(this.model.lines); 17 | this.model.lines += this.event.lines; 18 | 19 | this.gameService.updateData(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /typescript-tetris/src/configs/GameConfig.ts: -------------------------------------------------------------------------------- 1 | import { IConfig, IContext, IEventCommandMap, inject, injectable } from "@robotlegsjs/core"; 2 | 3 | import { CreateLevelCommand } from "./../commands/CreateLevelCommand"; 4 | import { GameOverCommand } from "./../commands/GameOverCommand"; 5 | import { GetNextPieceCommand } from "./../commands/GetNextPieceCommand"; 6 | import { IncreasePointsCommand } from "./../commands/IncreasePointsCommand"; 7 | import { GameEvent } from "./../events/GameEvent"; 8 | import { GameManager } from "./../managers/GameManager"; 9 | import { GameModel } from "./../models/GameModel"; 10 | import { GameService } from "./../services/GameService"; 11 | import { TilePool } from "./../utils/TilePool"; 12 | 13 | @injectable() 14 | export class GameConfig implements IConfig { 15 | @inject(IContext) private context: IContext; 16 | @inject(IEventCommandMap) private commandMap: IEventCommandMap; 17 | 18 | public configure(): void { 19 | TilePool.init(); 20 | 21 | this.mapCommands(); 22 | this.mapManager(); 23 | this.mapModels(); 24 | } 25 | private mapCommands(): void { 26 | this.commandMap.map(GameEvent.CREATE_LEVEL).toCommand(CreateLevelCommand); 27 | this.commandMap.map(GameEvent.GAME_OVER).toCommand(GameOverCommand); 28 | this.commandMap.map(GameEvent.GET_NEXT_PIECE).toCommand(GetNextPieceCommand); 29 | this.commandMap.map(GameEvent.INCREASE_POINTS).toCommand(IncreasePointsCommand); 30 | } 31 | private mapManager(): void { 32 | this.context.injector 33 | .bind(GameService) 34 | .to(GameService) 35 | .inSingletonScope(); 36 | this.context.injector 37 | .bind(GameManager) 38 | .to(GameManager) 39 | .inSingletonScope(); 40 | // this.context.injector.bind( SharedObjectManager ).to(SharedObjectManager).inSingletonScope();* 41 | } 42 | private mapModels(): void { 43 | this.context.injector 44 | .bind(GameModel) 45 | .to(GameModel) 46 | .inSingletonScope(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /typescript-tetris/src/configs/PalidorConfig.ts: -------------------------------------------------------------------------------- 1 | import { IConfig, IContext, IEventDispatcher, inject, injectable } from "@robotlegsjs/core"; 2 | import { IFlowManager } from "@robotlegsjs/pixi-palidor"; 3 | 4 | import { FlowEvent } from "./../events/FlowEvent"; 5 | import { FlowService } from "./../services/FlowService"; 6 | import { GameOverPopup } from "./../views/GameOverPopup"; 7 | import { GameView } from "./../views/GameView"; 8 | import { HomeView } from "./../views/HomeView"; 9 | import { InfoPopup } from "./../views/InfoPopup"; 10 | import { IntroView } from "./../views/IntroView"; 11 | import { OptionsView } from "./../views/OptionsView"; 12 | import { PausePopup } from "./../views/PausePopup"; 13 | import { ResetConfirmPopup } from "./../views/ResetConfirmPopup"; 14 | import { StartingPopup } from "./../views/StartingPopup"; 15 | 16 | @injectable() 17 | export class PalidorConfig implements IConfig { 18 | @inject(IContext) private context: IContext; 19 | @inject(IFlowManager) private flowManager: IFlowManager; 20 | @inject(IEventDispatcher) private eventDispatcher: IEventDispatcher; 21 | 22 | public configure(): void { 23 | this.mapPalidor(); 24 | this.eventDispatcher.dispatchEvent(new FlowEvent(FlowEvent.SHOW_INTRO_VIEW)); 25 | } 26 | private mapPalidor(): void { 27 | this.context.injector 28 | .bind(FlowService) 29 | .to(FlowService) 30 | .inSingletonScope(); 31 | 32 | this.flowManager.map(FlowEvent.SHOW_GAME_VIEW).toView(GameView); 33 | this.flowManager.map(FlowEvent.SHOW_HOME_VIEW).toView(HomeView); 34 | this.flowManager.map(FlowEvent.SHOW_INTRO_VIEW).toView(IntroView); 35 | this.flowManager.map(FlowEvent.SHOW_OPTIONS_VIEW).toView(OptionsView); 36 | 37 | this.flowManager.map(FlowEvent.SHOW_GAME_OVER_POPUP).toFloatingView(GameOverPopup); 38 | this.flowManager.map(FlowEvent.SHOW_INFO_POPUP).toFloatingView(InfoPopup); 39 | this.flowManager.map(FlowEvent.SHOW_PAUSE_POPUP).toFloatingView(PausePopup); 40 | this.flowManager.map(FlowEvent.SHOW_RESET_CONFIRM_POPUP).toFloatingView(ResetConfirmPopup); 41 | this.flowManager.map(FlowEvent.SHOW_STARTING_POPUP).toFloatingView(StartingPopup); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /typescript-tetris/src/configs/ViewsConfig.ts: -------------------------------------------------------------------------------- 1 | import { IConfig, inject, injectable } from "@robotlegsjs/core"; 2 | import { IMediatorMap } from "@robotlegsjs/pixi"; 3 | 4 | import { GameOverPopupMediator } from "./../mediators/GameOverPopupMediator"; 5 | import { GameViewMediator } from "./../mediators/GameViewMediator"; 6 | import { GridComponentMediator } from "./../mediators/GridComponentMediator"; 7 | import { HomeViewMediator } from "./../mediators/HomeViewMediator"; 8 | import { HUDGameComponentMediator } from "./../mediators/HUDGameComponentMediator"; 9 | import { InfoPopupMediator } from "./../mediators/InfoPopupMediator"; 10 | import { IntroViewMediator } from "./../mediators/IntroViewMediator"; 11 | import { NextPieceComponentMediator } from "./../mediators/NextPieceComponentMediator"; 12 | import { OptionsViewMediator } from "./../mediators/OptionsViewMediator"; 13 | import { PausePopupMediator } from "./../mediators/PausePopupMediator"; 14 | import { ResetConfirmPopupMediator } from "./../mediators/ResetConfirmPopupMediator"; 15 | import { StartingPopupMediator } from "./../mediators/StartingPopupMediator"; 16 | import { GridComponent } from "./../views/components/GridComponent"; 17 | import { HUDGameComponent } from "./../views/components/HUDGameComponent"; 18 | import { NextPieceComponent } from "./../views/components/NextPieceComponent"; 19 | import { GameOverPopup } from "./../views/GameOverPopup"; 20 | import { GameView } from "./../views/GameView"; 21 | import { HomeView } from "./../views/HomeView"; 22 | import { InfoPopup } from "./../views/InfoPopup"; 23 | import { IntroView } from "./../views/IntroView"; 24 | import { OptionsView } from "./../views/OptionsView"; 25 | import { PausePopup } from "./../views/PausePopup"; 26 | import { ResetConfirmPopup } from "./../views/ResetConfirmPopup"; 27 | import { StartingPopup } from "./../views/StartingPopup"; 28 | 29 | @injectable() 30 | export class ViewsConfig implements IConfig { 31 | @inject(IMediatorMap) private mediatorMap: IMediatorMap; 32 | 33 | public configure(): void { 34 | this.mapMediators(); 35 | } 36 | private mapMediators(): void { 37 | this.mediatorMap.map(GameView).toMediator(GameViewMediator); 38 | this.mediatorMap.map(HomeView).toMediator(HomeViewMediator); 39 | this.mediatorMap.map(IntroView).toMediator(IntroViewMediator); 40 | this.mediatorMap.map(OptionsView).toMediator(OptionsViewMediator); 41 | 42 | this.mediatorMap.map(GridComponent).toMediator(GridComponentMediator); 43 | this.mediatorMap.map(HUDGameComponent).toMediator(HUDGameComponentMediator); 44 | this.mediatorMap.map(NextPieceComponent).toMediator(NextPieceComponentMediator); 45 | 46 | this.mediatorMap.map(GameOverPopup).toMediator(GameOverPopupMediator); 47 | this.mediatorMap.map(InfoPopup).toMediator(InfoPopupMediator); 48 | this.mediatorMap.map(PausePopup).toMediator(PausePopupMediator); 49 | this.mediatorMap.map(ResetConfirmPopup).toMediator(ResetConfirmPopupMediator); 50 | this.mediatorMap.map(StartingPopup).toMediator(StartingPopupMediator); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /typescript-tetris/src/events/FlowEvent.ts: -------------------------------------------------------------------------------- 1 | import { Event } from "@robotlegsjs/core"; 2 | 3 | export class FlowEvent extends Event { 4 | public static SHOW_GAME_VIEW = "showGameView"; 5 | public static SHOW_HOME_VIEW = "showHomeView"; 6 | public static SHOW_INTRO_VIEW = "showIntroView"; 7 | public static SHOW_OPTIONS_VIEW = "showOptionsView"; 8 | 9 | public static SHOW_GAME_OVER_POPUP = "showGameOverPopup"; 10 | public static SHOW_INFO_POPUP = "showInfoPopup"; 11 | public static SHOW_PAUSE_POPUP = "showPausePopup"; 12 | public static SHOW_RESET_CONFIRM_POPUP = "showResetConfirmPopup"; 13 | public static SHOW_STARTING_POPUP = "showStartingPopup"; 14 | 15 | public static CLOSE_POPUP = "closePopup"; 16 | 17 | constructor(type: string) { 18 | super(type); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /typescript-tetris/src/events/GameEvent.ts: -------------------------------------------------------------------------------- 1 | import { Event } from "@robotlegsjs/core"; 2 | 3 | export class GameEvent extends Event { 4 | public static CREATE_LEVEL = "createLevel"; 5 | public static CLEAR_GRID = "clearGrid"; 6 | public static GAME_OVER = "gameOver"; 7 | 8 | public static PAUSE = "pause"; 9 | public static RESUME = "resume"; 10 | 11 | public static GET_NEXT_PIECE = "getNextPiece"; 12 | public static INCREASE_POINTS = "increasePoints"; 13 | 14 | public static UPDATE_DATA = "updateData"; 15 | public static UPDATE_NEXT_PIECE = "updateNextPiece"; 16 | 17 | public lines: number; 18 | 19 | constructor(type: string) { 20 | super(type); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /typescript-tetris/src/index.ts: -------------------------------------------------------------------------------- 1 | // tslint:disable-next-line:no-reference 2 | /// 3 | import "reflect-metadata"; 4 | 5 | import { Context, MVCSBundle } from "@robotlegsjs/core"; 6 | import { ContextView, PixiBundle } from "@robotlegsjs/pixi"; 7 | import { PalidorPixiExtension } from "@robotlegsjs/pixi-palidor"; 8 | import PIXI = require("pixi.js"); 9 | 10 | import { GameConfig } from "./configs/GameConfig"; 11 | import { PalidorConfig } from "./configs/PalidorConfig"; 12 | import { ViewsConfig } from "./configs/ViewsConfig"; 13 | 14 | class Main { 15 | private stage: PIXI.Container; 16 | private renderer: PIXI.CanvasRenderer | PIXI.WebGLRenderer; 17 | private context: Context; 18 | 19 | constructor() { 20 | this.renderer = PIXI.autoDetectRenderer(340, 480, {}); 21 | this.stage = new PIXI.Container(); 22 | this.context = new Context(); 23 | // this.context.logLevel = LogLevel.DEBUG; 24 | this.context 25 | .install(MVCSBundle, PixiBundle) 26 | .install(PalidorPixiExtension) 27 | .configure(new ContextView(this.stage)) 28 | .configure(ViewsConfig, GameConfig, PalidorConfig) 29 | .initialize(); 30 | 31 | document.body.appendChild(this.renderer.view); 32 | } 33 | public render = () => { 34 | this.renderer.render(this.stage); 35 | window.requestAnimationFrame(this.render); 36 | }; 37 | } 38 | const main = new Main(); 39 | main.render(); 40 | -------------------------------------------------------------------------------- /typescript-tetris/src/managers/GameManager.ts: -------------------------------------------------------------------------------- 1 | import { inject, injectable } from "@robotlegsjs/core"; 2 | 3 | import { Grid } from "../models/Grid"; 4 | import { TileGroup } from "../models/TileGroup"; 5 | import { GameService } from "../services/GameService"; 6 | import { Tile } from "./../models/Tile"; 7 | 8 | @injectable() 9 | export class GameManager { 10 | @inject(GameService) private gameService: GameService; 11 | 12 | private _grid: Grid; 13 | private _currentPiece: TileGroup; 14 | private _tilesToUpdate: Tile[]; 15 | private _tilesToRemove: Tile[]; 16 | 17 | public createEmpytGrid(cols = 10, rows = 20): void { 18 | this._grid = new Grid(cols, rows); 19 | this._tilesToUpdate = new Array(); 20 | this._tilesToRemove = new Array(); 21 | } 22 | public addPiece(currentPiece: TileGroup): void { 23 | this._currentPiece = currentPiece; 24 | this._currentPiece.moveHorizontal(4); 25 | this.addTilesToUpdate(this._currentPiece.tiles); 26 | 27 | this.validateGameOver(); 28 | } 29 | public tickUpdate(): void { 30 | this.moveCurrentPieceDown(); 31 | } 32 | public rotateCurrentPiece(): void { 33 | this._currentPiece.rotate(); 34 | 35 | if (this.isMovementValid() === false) { 36 | this._currentPiece.rollback(); 37 | } else { 38 | this.addTilesToUpdate(this._currentPiece.tiles); 39 | } 40 | } 41 | public getTilesToUpdate(): Tile[] { 42 | return this._tilesToUpdate; 43 | } 44 | public getTilesToRemove(): Tile[] { 45 | return this._tilesToRemove; 46 | } 47 | public moveCurrentPieceLeft(): void { 48 | this._currentPiece.moveHorizontal(-1); 49 | 50 | if (this.isMovementValid() === false) { 51 | this._currentPiece.rollbackX(); 52 | } else { 53 | this.addTilesToUpdate(this._currentPiece.tiles); 54 | } 55 | } 56 | public moveCurrentPieceRight(): void { 57 | this._currentPiece.moveHorizontal(1); 58 | 59 | if (this.isMovementValid() === false) { 60 | this._currentPiece.rollbackX(); 61 | } else { 62 | this.addTilesToUpdate(this._currentPiece.tiles); 63 | } 64 | } 65 | public moveCurrentPieceDown(): void { 66 | this._currentPiece.moveVertical(); 67 | 68 | if (this.isMovementValid() === false) { 69 | this._currentPiece.rollbackY(); 70 | 71 | const tiles: Tile[] = this._currentPiece.tiles; 72 | for (let i = 0; i < tiles.length; i++) { 73 | this._grid.setTile(tiles[i], tiles[i].col, tiles[i].row); 74 | } 75 | this.solveCompletedRows(); 76 | this.gameService.getNextPiece(); 77 | } else { 78 | this.addTilesToUpdate(this._currentPiece.tiles); 79 | } 80 | } 81 | private solveCompletedRows(): void { 82 | let linesRemoved = 0; 83 | 84 | const updateTiles: Tile[] = new Array(); 85 | let removeTiles: Tile[] = new Array(); 86 | 87 | let isToRemove: boolean; 88 | let hasToUpdate: boolean; 89 | 90 | let tile: Tile; 91 | 92 | for (let row = this._grid.maxRows - 1; row > 0; row--) { 93 | isToRemove = true; 94 | for (let col = 0; col < this._grid.maxCols; col++) { 95 | tile = this._grid.getTile(col, row); 96 | 97 | if (tile && hasToUpdate) { 98 | tile.setPosition(col, row); 99 | this._tilesToUpdate.push(tile); 100 | } 101 | isToRemove = isToRemove && tile !== null; 102 | } 103 | if (isToRemove) { 104 | removeTiles = removeTiles.concat(this._grid.removeRow(row)); 105 | hasToUpdate = true; 106 | row++; 107 | linesRemoved++; 108 | } 109 | } 110 | 111 | if (linesRemoved > 0) { 112 | this.gameService.increasePoints(linesRemoved); 113 | } 114 | 115 | this._tilesToUpdate = this._tilesToUpdate.concat(updateTiles); 116 | this._tilesToRemove = this._tilesToRemove.concat(removeTiles); 117 | } 118 | private isMovementValid(): boolean { 119 | for (let i = 0; i < this._currentPiece.tiles.length; i++) { 120 | const tile = this._currentPiece.tiles[i]; 121 | if (this.isOffBounds(tile) || this.isNotEmptyTile(tile)) { 122 | return false; 123 | } 124 | } 125 | return true; 126 | } 127 | private addTilesToUpdate(tiles: Tile[]): void { 128 | this._tilesToUpdate = this._tilesToUpdate.concat(tiles); 129 | } 130 | private validateGameOver(): void { 131 | if (this.isMovementValid() === false) { 132 | this.gameService.gameOver(); 133 | } 134 | } 135 | private isNotEmptyTile(tile: Tile): boolean { 136 | return !this._grid.isEmptyTile(tile.col, tile.row); 137 | } 138 | private isOffBounds(tile: Tile): boolean { 139 | return tile.col < 0 || tile.col >= this._grid.maxCols || tile.row < 0 || tile.row >= this._grid.maxRows; 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /typescript-tetris/src/mediators/GameOverPopupMediator.ts: -------------------------------------------------------------------------------- 1 | import { inject, injectable } from "@robotlegsjs/core"; 2 | import { Mediator } from "@robotlegsjs/pixi"; 3 | 4 | import { FlowService } from "./../services/FlowService"; 5 | import { GameService } from "./../services/GameService"; 6 | import { GameOverPopup } from "./../views/GameOverPopup"; 7 | 8 | @injectable() 9 | export class GameOverPopupMediator extends Mediator { 10 | @inject(FlowService) private flowService: FlowService; 11 | @inject(GameService) private gameService: GameService; 12 | 13 | public initialize(): void { 14 | this.eventMap.mapListener(this.view.homeButton, "click", this.homeButton_onClick, this); 15 | this.eventMap.mapListener(this.view.retryButton, "click", this.retryButton_onClick, this); 16 | } 17 | public destroy(): void { 18 | this.eventMap.unmapListeners(); 19 | } 20 | private homeButton_onClick(e: any): void { 21 | this.flowService.setHomeView(); 22 | this.flowService.closePopup(); 23 | } 24 | private retryButton_onClick(e: any): void { 25 | this.flowService.closePopup(); 26 | this.gameService.createLevel(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /typescript-tetris/src/mediators/GameViewMediator.ts: -------------------------------------------------------------------------------- 1 | import { inject, injectable } from "@robotlegsjs/core"; 2 | import { Mediator } from "@robotlegsjs/pixi"; 3 | 4 | import { GameService } from "./../services/GameService"; 5 | import { GameView } from "./../views/GameView"; 6 | 7 | @injectable() 8 | export class GameViewMediator extends Mediator { 9 | @inject(GameService) private gameService: GameService; 10 | 11 | public initialize(): void { 12 | this.view.createComponents(); 13 | 14 | this.gameService.createLevel(); 15 | } 16 | public destroy(): void { 17 | this.view.destroy(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /typescript-tetris/src/mediators/HUDGameComponentMediator.ts: -------------------------------------------------------------------------------- 1 | import { inject, injectable } from "@robotlegsjs/core"; 2 | import { Mediator } from "@robotlegsjs/pixi"; 3 | 4 | import { GameEvent } from "./../events/GameEvent"; 5 | import { GameModel } from "./../models/GameModel"; 6 | import { FlowService } from "./../services/FlowService"; 7 | import { GameService } from "./../services/GameService"; 8 | import { HUDGameComponent } from "./../views/components/HUDGameComponent"; 9 | 10 | @injectable() 11 | export class HUDGameComponentMediator extends Mediator { 12 | @inject(GameModel) private model: GameModel; 13 | @inject(GameService) private gameService: GameService; 14 | @inject(FlowService) private flowService: FlowService; 15 | 16 | public initialize(): void { 17 | this.eventMap.mapListener(this.view.pauseButton, "click", this.pauseButton_onClick, this); 18 | this.eventMap.mapListener(this.eventDispatcher, GameEvent.UPDATE_DATA, this.game_onUpdate, this); 19 | } 20 | public destroy(): void { 21 | this.eventMap.unmapListeners(); 22 | } 23 | private game_onUpdate(e: any): void { 24 | this.view.updateData(this.model); 25 | } 26 | private pauseButton_onClick(e: any): void { 27 | this.gameService.pause(); 28 | this.flowService.showPausePopup(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /typescript-tetris/src/mediators/HomeViewMediator.ts: -------------------------------------------------------------------------------- 1 | import { inject, injectable } from "@robotlegsjs/core"; 2 | import { Mediator } from "@robotlegsjs/pixi"; 3 | 4 | import { FlowService } from "./../services/FlowService"; 5 | import { HomeView } from "./../views/HomeView"; 6 | 7 | @injectable() 8 | export class HomeViewMediator extends Mediator { 9 | @inject(FlowService) private flowService: FlowService; 10 | 11 | public initialize(): void { 12 | this.eventMap.mapListener(this.view.startButton, "click", this.startButton_onClick, this); 13 | this.eventMap.mapListener(this.view.optionButton, "click", this.optionsButton_onClick, this); 14 | } 15 | public destroy(): void { 16 | this.eventMap.unmapListeners(); 17 | } 18 | private startButton_onClick(e: any): void { 19 | this.flowService.setGameView(); 20 | } 21 | private optionsButton_onClick(e: any): void { 22 | this.flowService.setOptionsView(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /typescript-tetris/src/mediators/InfoPopupMediator.ts: -------------------------------------------------------------------------------- 1 | import { inject, injectable } from "@robotlegsjs/core"; 2 | import { Mediator } from "@robotlegsjs/pixi"; 3 | 4 | import { FlowService } from "./../services/FlowService"; 5 | import { InfoPopup } from "./../views/InfoPopup"; 6 | 7 | @injectable() 8 | export class InfoPopupMediator extends Mediator { 9 | @inject(FlowService) private service: FlowService; 10 | 11 | public initialize(): void { 12 | this.eventMap.mapListener(this.view.closeButton, "click", this.closeButton_onClick, this); 13 | } 14 | public destroy(): void { 15 | this.eventMap.unmapListeners(); 16 | } 17 | private closeButton_onClick(e: any): void { 18 | this.service.closePopup(); 19 | this.service.showStartingPopup(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /typescript-tetris/src/mediators/IntroViewMediator.ts: -------------------------------------------------------------------------------- 1 | import { inject, injectable } from "@robotlegsjs/core"; 2 | import { Mediator } from "@robotlegsjs/pixi"; 3 | 4 | import { IntroView } from "../views/IntroView"; 5 | import { FlowService } from "./../services/FlowService"; 6 | import { AtlasKeys } from "./../utils/AtlasKeys"; 7 | 8 | @injectable() 9 | export class IntroViewMediator extends Mediator { 10 | @inject(FlowService) private flowService: FlowService; 11 | 12 | public initialize(): void { 13 | setTimeout(this.onTimerOut.bind(this), 3000, this); 14 | 15 | const loader = PIXI.loader 16 | .add(AtlasKeys.ATLAS_PNG) 17 | .add(AtlasKeys.ATLAS_XML) 18 | .load(this.onLoad); 19 | } 20 | public destroy(): void { 21 | this.eventMap.unmapListeners(); 22 | } 23 | private onLoad() { 24 | AtlasKeys.update(); 25 | } 26 | private onTimerOut() { 27 | this.flowService.setHomeView(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /typescript-tetris/src/mediators/NextPieceComponentMediator.ts: -------------------------------------------------------------------------------- 1 | import { inject, injectable } from "@robotlegsjs/core"; 2 | import { Mediator } from "@robotlegsjs/pixi"; 3 | import { Sprite } from "pixi.js"; 4 | 5 | import { GameEvent } from "./../events/GameEvent"; 6 | import { GameModel } from "./../models/GameModel"; 7 | import { Tile } from "./../models/Tile"; 8 | import { GameService } from "./../services/GameService"; 9 | import { PixiFactory } from "./../utils/PixiFactory"; 10 | import { NextPieceComponent } from "./../views/components/NextPieceComponent"; 11 | 12 | @injectable() 13 | export class NextPieceComponentMediator extends Mediator { 14 | @inject(GameModel) private model: GameModel; 15 | @inject(GameService) private gameService: GameService; 16 | 17 | public initialize(): void { 18 | this.eventMap.mapListener(this.eventDispatcher, GameEvent.UPDATE_NEXT_PIECE, this.game_updateNextPiece, this); 19 | } 20 | public destroy(): void { 21 | this.eventMap.unmapListeners(); 22 | } 23 | private game_updateNextPiece(e: any): void { 24 | this.view.removeChildren(); 25 | 26 | let display: Sprite; 27 | for (let i = 0; i < this.model.nextPiece.tiles.length; i++) { 28 | display = this.createDisplay(this.model.nextPiece.typeId); 29 | display.x = (this.model.nextPiece.tiles[i].col + 2) * Tile.TILE_WIDTH; 30 | display.y = this.model.nextPiece.tiles[i].row * Tile.TILE_WIDTH; 31 | display.anchor.x = 0.5; 32 | this.view.addChild(display); 33 | } 34 | } 35 | private createDisplay(assetId: number): Sprite { 36 | return PixiFactory.getTileDisplay(assetId); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /typescript-tetris/src/mediators/OptionsViewMediator.ts: -------------------------------------------------------------------------------- 1 | import { inject, injectable } from "@robotlegsjs/core"; 2 | import { Mediator } from "@robotlegsjs/pixi"; 3 | 4 | import { FlowService } from "./../services/FlowService"; 5 | import { OptionsView } from "./../views/OptionsView"; 6 | 7 | @injectable() 8 | export class OptionsViewMediator extends Mediator { 9 | @inject(FlowService) private flowService: FlowService; 10 | 11 | public initialize(): void { 12 | this.eventMap.mapListener(this.view.homeButton, "click", this.homeButton_onClick, this); 13 | this.eventMap.mapListener(this.view.resetButton, "click", this.resetButton_onClick, this); 14 | } 15 | public destroy(): void { 16 | this.eventMap.unmapListeners(); 17 | } 18 | private homeButton_onClick(e: any, thisObject: any): void { 19 | this.flowService.setHomeView(); 20 | } 21 | private resetButton_onClick(e: any): void { 22 | this.flowService.showResetConfirmPopup(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /typescript-tetris/src/mediators/PausePopupMediator.ts: -------------------------------------------------------------------------------- 1 | import { inject, injectable } from "@robotlegsjs/core"; 2 | import { Mediator } from "@robotlegsjs/pixi"; 3 | 4 | import { FlowService } from "./../services/FlowService"; 5 | import { GameService } from "./../services/GameService"; 6 | import { PausePopup } from "./../views/PausePopup"; 7 | 8 | @injectable() 9 | export class PausePopupMediator extends Mediator { 10 | @inject(FlowService) private flowService: FlowService; 11 | @inject(GameService) private gameService: GameService; 12 | 13 | public initialize(): void { 14 | this.eventMap.mapListener(this.view.homeButton, "click", this.homeButton_onClick, this); 15 | this.eventMap.mapListener(this.view.resumeButton, "click", this.resumeButton_onClick, this); 16 | this.eventMap.mapListener(this.view.retryButton, "click", this.retryButton_onClick, this); 17 | } 18 | public destroy(): void { 19 | this.eventMap.unmapListeners(); 20 | } 21 | private homeButton_onClick(e: any): void { 22 | this.flowService.setHomeView(); 23 | this.flowService.closePopup(); 24 | } 25 | private resumeButton_onClick(e: any): void { 26 | this.flowService.closePopup(); 27 | this.flowService.showStartingPopup(); 28 | } 29 | private retryButton_onClick(e: any): void { 30 | this.flowService.closePopup(); 31 | this.gameService.createLevel(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /typescript-tetris/src/mediators/ResetConfirmPopupMediator.ts: -------------------------------------------------------------------------------- 1 | import { inject, injectable } from "@robotlegsjs/core"; 2 | import { Mediator } from "@robotlegsjs/pixi"; 3 | 4 | import { FlowService } from "./../services/FlowService"; 5 | import { ResetConfirmPopup } from "./../views/ResetConfirmPopup"; 6 | 7 | @injectable() 8 | export class ResetConfirmPopupMediator extends Mediator { 9 | @inject(FlowService) private flowService: FlowService; 10 | 11 | public initialize(): void { 12 | this.eventMap.mapListener(this.view.confirmButton, "click", this.confirmButton_onClick, this); 13 | this.eventMap.mapListener(this.view.cancelButton, "click", this.cancelButton_onClick, this); 14 | } 15 | public destroy(): void { 16 | this.eventMap.unmapListeners(); 17 | } 18 | private confirmButton_onClick(e: any): void { 19 | this.flowService.closePopup(); 20 | } 21 | private cancelButton_onClick(e: any): void { 22 | this.flowService.closePopup(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /typescript-tetris/src/mediators/StartingPopupMediator.ts: -------------------------------------------------------------------------------- 1 | import { inject, injectable } from "@robotlegsjs/core"; 2 | import { Mediator } from "@robotlegsjs/pixi"; 3 | 4 | import { FlowService } from "./../services/FlowService"; 5 | import { GameService } from "./../services/GameService"; 6 | import { StartingPopup } from "./../views/StartingPopup"; 7 | 8 | @injectable() 9 | export class StartingPopupMediator extends Mediator { 10 | @inject(GameService) private gameService: GameService; 11 | @inject(FlowService) private flowService: FlowService; 12 | 13 | private _count: number; 14 | 15 | public initialize(): void { 16 | this._count = 4; 17 | 18 | this.tick(this); 19 | } 20 | public destroy(): void { 21 | this.eventMap.unmapListeners(); 22 | } 23 | private tick(obThis: any) { 24 | obThis._count -= 1; 25 | if (obThis._count > 0) { 26 | obThis.view.changeNumber(obThis._count); 27 | setTimeout(obThis.tick, 300, obThis); 28 | } else { 29 | obThis.tick_onComplete(); 30 | } 31 | } 32 | private tick_onComplete(): void { 33 | this.gameService.resume(); 34 | this.flowService.closePopup(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /typescript-tetris/src/models/GameModel.ts: -------------------------------------------------------------------------------- 1 | import { injectable } from "@robotlegsjs/core"; 2 | 3 | import { TileGroup } from "./TileGroup"; 4 | 5 | @injectable() 6 | export class GameModel { 7 | public score: number; 8 | public level: number; 9 | public lines: number; 10 | public hiScore: number; 11 | 12 | public currentPiece: TileGroup; 13 | public nextPiece: TileGroup; 14 | 15 | public status: string; 16 | 17 | constructor() { 18 | this.clear(); 19 | } 20 | 21 | public clear(): void { 22 | this.score = 0; 23 | this.level = 1; 24 | this.lines = 0; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /typescript-tetris/src/models/GameStatus.ts: -------------------------------------------------------------------------------- 1 | export class GameStatus { 2 | public static GAME = "game"; 3 | public static GAMEOVER = "gameOver"; 4 | } 5 | -------------------------------------------------------------------------------- /typescript-tetris/src/models/Grid.ts: -------------------------------------------------------------------------------- 1 | import { Tile } from "./Tile"; 2 | 3 | export class Grid { 4 | private _maxCols: number; 5 | private _maxRows: number; 6 | private _grid: Tile[][]; 7 | 8 | constructor(cols = 10, rows = 20) { 9 | this._grid = new Array(); 10 | this._maxCols = cols; 11 | this._maxRows = rows; 12 | 13 | this.generateEmptyGrid(); 14 | } 15 | public removeRow(index: number): Tile[] { 16 | const removedRow: Tile[] = this.getRow(index); 17 | this._grid.splice(index, 1); 18 | 19 | this.insertNewEmpytRow(0); 20 | 21 | return removedRow; 22 | } 23 | public getRow(index: number): Tile[] { 24 | return this._grid[index]; 25 | } 26 | public get maxCols(): number { 27 | return this._maxCols; 28 | } 29 | public get maxRows(): number { 30 | return this._maxRows; 31 | } 32 | public isEmptyTile(col: number, row: number): Boolean { 33 | return this._grid[row][col] == null; 34 | } 35 | public setTile(tile: Tile, col: number, row: number): void { 36 | this._grid[row][col] = tile; 37 | } 38 | public getTile(col: number, row: number): Tile { 39 | return this._grid[row][col] || null; 40 | } 41 | private generateEmptyGrid(): void { 42 | let line: Tile[]; 43 | for (let row = 0; row < this._maxRows; row++) { 44 | line = new Array(); 45 | for (let col = 0; col < this._maxCols; col++) { 46 | line.push(null); 47 | } 48 | this._grid.push(line); 49 | } 50 | } 51 | private insertNewEmpytRow(index: number): void { 52 | const newRow: Tile[] = new Array(); 53 | for (let i = 0; i < this._maxCols; i++) { 54 | newRow.push(null); 55 | } 56 | this._grid.splice(index, 0, newRow); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /typescript-tetris/src/models/Tile.ts: -------------------------------------------------------------------------------- 1 | export class Tile { 2 | public static TILE_WIDTH = 18; 3 | 4 | public row: number; 5 | public col: number; 6 | public bRow: number; 7 | public bCol: number; 8 | 9 | constructor(col = 0, row = 0) { 10 | this.col = col; 11 | this.row = row; 12 | this.bCol = col; 13 | this.bRow = row; 14 | } 15 | public clone(): Tile { 16 | const tile: Tile = new Tile(); 17 | tile.col = this.col; 18 | tile.row = this.row; 19 | tile.bCol = this.bCol; 20 | tile.bRow = this.bRow; 21 | 22 | return tile; 23 | } 24 | public setPosition(col: number, row: number): void { 25 | this.col = col; 26 | this.row = row; 27 | } 28 | public toString(): String { 29 | return "tile_col_" + this.col + "_row_" + this.row; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /typescript-tetris/src/models/TileGroup.ts: -------------------------------------------------------------------------------- 1 | import { Tile } from "./Tile"; 2 | 3 | export class TileGroup { 4 | private _tiles: Tile[]; 5 | private _typeId: number; 6 | 7 | constructor(typeId: number, tiles: Tile[]) { 8 | this._typeId = typeId; 9 | this._tiles = tiles; 10 | } 11 | public moveVertical(direction = 1): void { 12 | for (let i = 0; i < this.tiles.length; i++) { 13 | this._tiles[i].bRow = this._tiles[i].row; 14 | this._tiles[i].row += direction; 15 | } 16 | } 17 | public moveHorizontal(direction: number): void { 18 | for (let i = 0; i < this.tiles.length; i++) { 19 | this._tiles[i].bCol = this._tiles[i].col; 20 | this._tiles[i].col += direction; 21 | } 22 | } 23 | public rotate(): void { 24 | const anchorTile: Tile = this._tiles[1]; 25 | const newTile: Tile = new Tile(); 26 | for (let i = 0; i < this._tiles.length; i++) { 27 | this._tiles[i].bCol = this._tiles[i].col; 28 | this._tiles[i].bRow = this._tiles[i].row; 29 | newTile.col = this._tiles[i].row - anchorTile.row; 30 | newTile.row = this._tiles[i].col - anchorTile.col; 31 | this._tiles[i].col = anchorTile.col - newTile.col; 32 | this._tiles[i].row = anchorTile.row + newTile.row; 33 | } 34 | } 35 | public rollback(): void { 36 | for (let i = 0; i < this._tiles.length; i++) { 37 | this._tiles[i].col = this._tiles[i].bCol; 38 | this._tiles[i].row = this._tiles[i].bRow; 39 | } 40 | } 41 | public rollbackX(): void { 42 | for (let i = 0; i < this._tiles.length; i++) { 43 | this._tiles[i].col = this._tiles[i].bCol; 44 | } 45 | } 46 | public rollbackY(): void { 47 | for (let i = 0; i < this._tiles.length; i++) { 48 | this._tiles[i].row = this._tiles[i].bRow; 49 | } 50 | } 51 | public get tiles(): Tile[] { 52 | return this._tiles; 53 | } 54 | public get typeId(): number { 55 | return this._typeId; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /typescript-tetris/src/models/TileGroupType.ts: -------------------------------------------------------------------------------- 1 | export class TileGroupType { 2 | public static TYPE_I = 0; 3 | public static TYPE_Z = 1; 4 | public static TYPE_S = 2; 5 | public static TYPE_T = 3; 6 | public static TYPE_L = 4; 7 | public static TYPE_J = 5; 8 | public static TYPE_O = 6; 9 | 10 | private static TYPE_I_ARRAY: number[] = [1, 3, 5, 7]; 11 | private static TYPE_Z_ARRAY: number[] = [2, 4, 5, 7]; 12 | private static TYPE_S_ARRAY: number[] = [3, 5, 4, 6]; 13 | private static TYPE_T_ARRAY: number[] = [3, 5, 4, 7]; 14 | private static TYPE_L_ARRAY: number[] = [2, 3, 5, 7]; 15 | private static TYPE_J_ARRAY: number[] = [3, 5, 7, 6]; 16 | private static TYPE_O_ARRAY: number[] = [2, 3, 4, 5]; 17 | 18 | public static getTypeArray(type: number): number[] { 19 | const dic: Map = new Map(); 20 | dic.set(this.TYPE_I, this.TYPE_I_ARRAY); 21 | dic.set(this.TYPE_Z, this.TYPE_Z_ARRAY); 22 | dic.set(this.TYPE_S, this.TYPE_S_ARRAY); 23 | dic.set(this.TYPE_T, this.TYPE_T_ARRAY); 24 | dic.set(this.TYPE_L, this.TYPE_L_ARRAY); 25 | dic.set(this.TYPE_J, this.TYPE_J_ARRAY); 26 | dic.set(this.TYPE_O, this.TYPE_O_ARRAY); 27 | 28 | return dic.get(type); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /typescript-tetris/src/services/FlowService.ts: -------------------------------------------------------------------------------- 1 | import { EventDispatcher, IEventDispatcher, inject, injectable } from "@robotlegsjs/core"; 2 | import { PalidorEvent } from "@robotlegsjs/pixi-palidor"; 3 | 4 | import { FlowEvent } from "./../events/FlowEvent"; 5 | 6 | @injectable() 7 | export class FlowService { 8 | @inject(IEventDispatcher) private eventDispatcher: IEventDispatcher; 9 | 10 | // Views 11 | public setGameView(): void { 12 | this.dispatchEventWith(FlowEvent.SHOW_GAME_VIEW); 13 | } 14 | public setHomeView(): void { 15 | this.dispatchEventWith(FlowEvent.SHOW_HOME_VIEW); 16 | } 17 | public setOptionsView(): void { 18 | this.dispatchEventWith(FlowEvent.SHOW_OPTIONS_VIEW); 19 | } 20 | // Floating Views 21 | public showGameOverPopup(): void { 22 | this.dispatchEventWith(FlowEvent.SHOW_GAME_OVER_POPUP); 23 | } 24 | public showInfoPopup(): void { 25 | this.dispatchEventWith(FlowEvent.SHOW_INFO_POPUP); 26 | } 27 | public showPausePopup(): void { 28 | this.dispatchEventWith(FlowEvent.SHOW_PAUSE_POPUP); 29 | } 30 | public showResetConfirmPopup(): void { 31 | this.dispatchEventWith(FlowEvent.SHOW_RESET_CONFIRM_POPUP); 32 | } 33 | public showStartingPopup(): void { 34 | this.dispatchEventWith(FlowEvent.SHOW_STARTING_POPUP); 35 | } 36 | // extras 37 | public closePopup(): void { 38 | this.dispatchEventWith(PalidorEvent.REMOVE_LAST_FLOATING_VIEW_ADDED); 39 | } 40 | public dispatchEventWith(type: string): void { 41 | (this.eventDispatcher).dispatchEventWith(type); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /typescript-tetris/src/services/GameService.ts: -------------------------------------------------------------------------------- 1 | import { EventDispatcher, IEventDispatcher, inject, injectable } from "@robotlegsjs/core"; 2 | 3 | import { GameEvent } from "./../events/GameEvent"; 4 | 5 | @injectable() 6 | export class GameService { 7 | @inject(IEventDispatcher) private eventDispatcher: IEventDispatcher; 8 | 9 | public createLevel(): void { 10 | this.dispatchEventWith(GameEvent.CREATE_LEVEL); 11 | } 12 | public pause(): void { 13 | this.dispatchEventWith(GameEvent.PAUSE); 14 | } 15 | public resume(): void { 16 | this.dispatchEventWith(GameEvent.RESUME); 17 | } 18 | public gameOver(): void { 19 | this.dispatchEventWith(GameEvent.GAME_OVER); 20 | } 21 | public clearGrid(): void { 22 | this.dispatchEventWith(GameEvent.CLEAR_GRID); 23 | } 24 | public getNextPiece(): void { 25 | this.dispatchEventWith(GameEvent.GET_NEXT_PIECE); 26 | } 27 | public increasePoints(linesRemoved: number): void { 28 | const event: GameEvent = new GameEvent(GameEvent.INCREASE_POINTS); 29 | event.lines = linesRemoved; 30 | this.eventDispatcher.dispatchEvent(event); 31 | } 32 | public updateData(): void { 33 | this.eventDispatcher.dispatchEvent(new GameEvent(GameEvent.UPDATE_DATA)); 34 | } 35 | public updateNextPiece(): void { 36 | this.eventDispatcher.dispatchEvent(new GameEvent(GameEvent.UPDATE_NEXT_PIECE)); 37 | } 38 | public dispatchEventWith(type: string): void { 39 | (this.eventDispatcher).dispatchEventWith(type); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /typescript-tetris/src/utils/AtlasKeys.ts: -------------------------------------------------------------------------------- 1 | import { Texture } from "pixi.js"; 2 | 3 | export class AtlasKeys { 4 | public static BUTTON_CANCEL = "button_cancel"; 5 | public static BUTTON_CONFIG = "button_config"; 6 | public static BUTTON_CONFIRM = "button_confirm"; 7 | public static BUTTON_HOME = "button_home"; 8 | public static BUTTON_PAUSE = "button_pause"; 9 | public static BUTTON_RESET = "button_reset"; 10 | public static BUTTON_RESUME = "button_resume"; 11 | public static BUTTON_RETRY = "button_retry"; 12 | public static BUTTON_START = "button_start"; 13 | 14 | public static LOGO = "logo_tetris.png"; 15 | public static LOGO_SETZER = "logo_setzer.png"; 16 | public static LOGO_TYPESCRIPT = "./assets/logo_typescript.png"; 17 | 18 | public static GRID = "grid.png"; 19 | public static NEXT_TILE = "next_tile.png"; 20 | 21 | /* TILES */ 22 | public static TILE_01 = "tile_01.png"; 23 | public static TILE_02 = "tile_02.png"; 24 | public static TILE_03 = "tile_03.png"; 25 | public static TILE_04 = "tile_04.png"; 26 | public static TILE_05 = "tile_05.png"; 27 | public static TILE_06 = "tile_06.png"; 28 | public static TILE_07 = "tile_07.png"; 29 | public static TILE_08 = "tile_08.png"; 30 | 31 | public static ATLAS_XML = "./assets/tetris-pixijs-atlas.json"; 32 | public static ATLAS_PNG = "./assets/tetris-pixijs-atlas.png"; 33 | 34 | private static resources: any; 35 | private static textureCache: any; 36 | 37 | public static update(): void { 38 | this.resources = PIXI.loader.resources; 39 | this.textureCache = PIXI.utils.TextureCache; 40 | } 41 | public static getTexture(atlasKey): Texture { 42 | return this.textureCache[atlasKey]; 43 | } 44 | public static getTileTexture(id): Texture { 45 | const ids: string[] = [ 46 | this.TILE_01, 47 | this.TILE_02, 48 | this.TILE_03, 49 | this.TILE_04, 50 | this.TILE_05, 51 | this.TILE_06, 52 | this.TILE_07, 53 | this.TILE_08 54 | ]; 55 | return this.textureCache[ids[id] || ids[0]]; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /typescript-tetris/src/utils/Colors.ts: -------------------------------------------------------------------------------- 1 | export class Colors { 2 | public static BACKGROUND = 0x307291; 3 | public static BACKGROUND_DARK = 0x204d63; 4 | 5 | public static DYNAMIC_TEXT = 0xc1ddea; 6 | public static STATIC_TEXT = 0x6fb0cf; 7 | 8 | public static GAME_ITEMS = 0xb5d6e6; 9 | } 10 | -------------------------------------------------------------------------------- /typescript-tetris/src/utils/GameUtils.ts: -------------------------------------------------------------------------------- 1 | import { Sprite } from "pixi.js"; 2 | 3 | import { Tile } from "../models/Tile"; 4 | 5 | export class GameUtils { 6 | public static getCurrentLevel(lines: number): number { 7 | return Math.floor(lines / 10 + 1); 8 | } 9 | public static getCurrentSpeed(level: number): number { 10 | return Math.max(4, 19 - level); 11 | } 12 | public static getPointsByLines(lines: number): number { 13 | const bonus: number = Math.floor(lines * 0.9); 14 | return (lines + bonus) * 100; 15 | } 16 | public static updateDisplayPositionByTile(tile: Tile, display: Sprite): void { 17 | if (display == null || tile == null) { 18 | return; 19 | } 20 | display.x = tile.col * Tile.TILE_WIDTH; 21 | display.y = tile.row * Tile.TILE_WIDTH; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /typescript-tetris/src/utils/MagicValues.ts: -------------------------------------------------------------------------------- 1 | export class MagicValues { 2 | public static BORDER_OFFSET = 18; 3 | public static SHARED_OBJECT_NAME: String = "TetrisPalidor"; 4 | } 5 | -------------------------------------------------------------------------------- /typescript-tetris/src/utils/PixiFactory.ts: -------------------------------------------------------------------------------- 1 | import { Container, Graphics, Sprite, Text, Texture } from "pixi.js"; 2 | 3 | import { CustomButton } from "./../views/components/CustomButton"; 4 | import { TileDisplay } from "./../views/components/TileDisplay"; 5 | import { AtlasKeys } from "./AtlasKeys"; 6 | import { Colors } from "./Colors"; 7 | import { Texts } from "./Texts"; 8 | import { TilePool } from "./TilePool"; 9 | import { ViewPortSize } from "./ViewPortSize"; 10 | 11 | export class PixiFactory { 12 | public static getColorBackground(color: number = Colors.BACKGROUND_DARK): Graphics { 13 | return this.getColorBox(ViewPortSize.MAX_WIDTH, ViewPortSize.MAX_HEIGHT, color); 14 | } 15 | public static getShadowBackground(): Graphics { 16 | const background: Graphics = this.getColorBox(ViewPortSize.MAX_WIDTH, ViewPortSize.MAX_HEIGHT); 17 | background.alpha = 0.6; 18 | return background; 19 | } 20 | public static getBoardBackground(): Container { 21 | const boardBackground: Graphics = this.getColorBox(ViewPortSize.MAX_WIDTH, 102, Colors.STATIC_TEXT); 22 | boardBackground.beginFill(Colors.BACKGROUND); 23 | boardBackground.drawRect(0, 1, ViewPortSize.MAX_WIDTH, 100); 24 | 25 | const board: Container = new Container(); 26 | board.y = ViewPortSize.HALF_HEIGHT - 60; 27 | board.addChild(boardBackground); 28 | return board; 29 | } 30 | public static getButton(atlasKey: string): CustomButton { 31 | return new CustomButton(atlasKey); 32 | } 33 | public static getColorBox(width: number, heigth: number, color = 0x00000): Graphics { 34 | const background: Graphics = new Graphics(); 35 | background.beginFill(color); 36 | background.drawRect(0, 0, width, heigth); 37 | return background; 38 | } 39 | public static getImage(atlasKey: string): Sprite { 40 | const texture: Texture = AtlasKeys.getTexture(atlasKey); 41 | return new Sprite(texture); 42 | } 43 | public static getText( 44 | text: string, 45 | color: number = Colors.DYNAMIC_TEXT, 46 | fontSize: number = Texts.FONT_SIZE_DEFAULT 47 | ): Text { 48 | const style = new PIXI.TextStyle({ 49 | align: "center", 50 | fill: color, 51 | fontFamily: "Arial", 52 | fontSize, 53 | fontWeight: "bold" 54 | }); 55 | 56 | return new PIXI.Text(text, style); 57 | } 58 | 59 | public static getTileDisplay(typeId: number): TileDisplay { 60 | return TilePool.getTileDisplay(typeId); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /typescript-tetris/src/utils/Texts.ts: -------------------------------------------------------------------------------- 1 | export class Texts { 2 | public static FONT_SIZE_DEFAULT = 22; 3 | public static FONT_SIZE_HUD = 14; 4 | 5 | public static TETRIS = "TETRIS"; 6 | 7 | public static DEVELOPER = "RONALDO SANTIAGO"; 8 | public static CONFIG = "CONFIG"; 9 | public static GAME_OVER = "GAME OVER"; 10 | public static PAUSED = "PAUSED"; 11 | 12 | public static LINES = "lines"; 13 | public static SCORE = "score"; 14 | public static LEVEL = "level"; 15 | public static HI_SCORE = "hiScore"; 16 | 17 | public static COMMANDS = "USE THE ARROWS\nTO MOVE\nAND ROTATE"; 18 | public static CONFIRM_RESET = "WOULD LIKE TO RESET\nTHE HI-SCORE?"; 19 | } 20 | -------------------------------------------------------------------------------- /typescript-tetris/src/utils/TileGroupFactory.ts: -------------------------------------------------------------------------------- 1 | import { TileGroup } from "../models/TileGroup"; 2 | import { TileGroupType } from "../models/TileGroupType"; 3 | import { Tile } from "./../models/Tile"; 4 | 5 | export class TileGroupFactory { 6 | public static getTileGroup(typeId: number): TileGroup { 7 | const tiles: Tile[] = this.getTilesByTypeArray(TileGroupType.getTypeArray(typeId)); 8 | return new TileGroup(typeId, tiles); 9 | } 10 | 11 | public static getRandomTileGroup(): TileGroup { 12 | const types: number[] = [ 13 | TileGroupType.TYPE_I, 14 | TileGroupType.TYPE_Z, 15 | TileGroupType.TYPE_S, 16 | TileGroupType.TYPE_T, 17 | TileGroupType.TYPE_L, 18 | TileGroupType.TYPE_J, 19 | TileGroupType.TYPE_O 20 | ]; 21 | const rndType: number = Math.floor(Math.random() * types.length); 22 | return this.getTileGroup(types[rndType]); 23 | } 24 | 25 | public static getTilesByTypeArray(typeArray: number[]): Tile[] { 26 | const tiles: Tile[] = new Array(); 27 | let tile: Tile; 28 | 29 | for (let i = 0; i < typeArray.length; i++) { 30 | tile = new Tile(); 31 | tile.col = Math.floor(typeArray[i] % 2); 32 | tile.row = Math.floor(typeArray[i] / 2); 33 | tiles.push(tile); 34 | } 35 | return tiles; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /typescript-tetris/src/utils/TilePool.ts: -------------------------------------------------------------------------------- 1 | import { TileDisplay } from "./../views/components/TileDisplay"; 2 | import { AtlasKeys } from "./AtlasKeys"; 3 | 4 | export class TilePool { 5 | private static _dictionary: Map; 6 | 7 | public static init(): void { 8 | this._dictionary = new Map(); 9 | } 10 | public static getTileDisplay(typeId: number): TileDisplay { 11 | if (this._dictionary.get(typeId) === undefined) { 12 | this._dictionary.set(typeId, new Array()); 13 | } 14 | const list: TileDisplay[] = this._dictionary.get(typeId); 15 | let tileDisplay: TileDisplay; 16 | if (list.length === 0) { 17 | tileDisplay = new TileDisplay(AtlasKeys.getTileTexture(typeId), typeId); 18 | } else { 19 | tileDisplay = list.shift(); 20 | } 21 | tileDisplay.visible = true; 22 | 23 | return tileDisplay; 24 | } 25 | public static back(tile: TileDisplay): void { 26 | const list: TileDisplay[] = this._dictionary.get(tile.typeId); 27 | tile.visible = false; 28 | 29 | if (list.indexOf(tile) === -1) { 30 | list.push(tile); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /typescript-tetris/src/utils/ViewPortSize.ts: -------------------------------------------------------------------------------- 1 | export class ViewPortSize { 2 | public static MAX_WIDTH = 340; 3 | public static MAX_HEIGHT = 480; 4 | public static HALF_WIDTH = 170; 5 | public static HALF_HEIGHT = 240; 6 | } 7 | -------------------------------------------------------------------------------- /typescript-tetris/src/views/GameOverPopup.ts: -------------------------------------------------------------------------------- 1 | import { Container, Text } from "pixi.js"; 2 | 3 | import { AtlasKeys } from "./../utils/AtlasKeys"; 4 | import { PixiFactory } from "./../utils/PixiFactory"; 5 | import { Texts } from "./../utils/Texts"; 6 | import { ViewPortSize } from "./../utils/ViewPortSize"; 7 | import { CustomButton } from "./components/CustomButton"; 8 | 9 | export class GameOverPopup extends Container { 10 | private _homeButton: CustomButton; 11 | public get homeButton(): CustomButton { 12 | return this._homeButton; 13 | } 14 | 15 | private _retryButton: CustomButton; 16 | public get retryButton(): CustomButton { 17 | return this._retryButton; 18 | } 19 | 20 | constructor() { 21 | super(); 22 | 23 | this.interactive = true; 24 | 25 | this.setupBackgrounds(); 26 | this.setupTexts(); 27 | this.setupButtons(); 28 | } 29 | private setupBackgrounds(): void { 30 | this.addChild(PixiFactory.getShadowBackground()); 31 | this.addChild(PixiFactory.getBoardBackground()); 32 | } 33 | private setupTexts(): void { 34 | const title: Text = PixiFactory.getText(Texts.GAME_OVER); 35 | title.x = ViewPortSize.HALF_WIDTH; 36 | title.y = ViewPortSize.HALF_HEIGHT - 30; 37 | title.anchor.set(0.5); 38 | this.addChild(title); 39 | } 40 | private setupButtons(): void { 41 | this._homeButton = PixiFactory.getButton(AtlasKeys.BUTTON_HOME); 42 | this._homeButton.x = ViewPortSize.HALF_WIDTH + 25; 43 | this._homeButton.y = ViewPortSize.HALF_HEIGHT + 15; 44 | this.addChild(this._homeButton); 45 | 46 | this._retryButton = PixiFactory.getButton(AtlasKeys.BUTTON_RETRY); 47 | this._retryButton.x = ViewPortSize.HALF_WIDTH - 25; 48 | this._retryButton.y = ViewPortSize.HALF_HEIGHT + 15; 49 | this.addChild(this._retryButton); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /typescript-tetris/src/views/GameView.ts: -------------------------------------------------------------------------------- 1 | import { Container, Sprite } from "pixi.js"; 2 | 3 | import { AtlasKeys } from "./../utils/AtlasKeys"; 4 | import { MagicValues } from "./../utils/MagicValues"; 5 | import { PixiFactory } from "./../utils/PixiFactory"; 6 | import { ViewPortSize } from "./../utils/ViewPortSize"; 7 | import { GridComponent } from "./components/GridComponent"; 8 | import { HUDGameComponent } from "./components/HUDGameComponent"; 9 | import { NextPieceComponent } from "./components/NextPieceComponent"; 10 | 11 | export class GameView extends Container { 12 | private _gridComponent: GridComponent; 13 | private _nextPieceComponent: NextPieceComponent; 14 | private _hudComponent: HUDGameComponent; 15 | 16 | constructor() { 17 | super(); 18 | this.createBackgrounds(); 19 | } 20 | 21 | public destroy(): void { 22 | this.removeChild(this._gridComponent); 23 | this.removeChild(this._nextPieceComponent); 24 | this.removeChild(this._hudComponent); 25 | 26 | this._gridComponent = null; 27 | this._nextPieceComponent = null; 28 | this._hudComponent = null; 29 | } 30 | 31 | public createComponents(): void { 32 | this._nextPieceComponent = new NextPieceComponent(); 33 | this._nextPieceComponent.x = ViewPortSize.MAX_WIDTH - 91 - MagicValues.BORDER_OFFSET + 1; 34 | this._nextPieceComponent.y = 76 + 1; 35 | this.addChild(this._nextPieceComponent); 36 | 37 | this._gridComponent = new GridComponent(); 38 | this._gridComponent.x = MagicValues.BORDER_OFFSET; 39 | this._gridComponent.y = 76; 40 | this.addChild(this._gridComponent); 41 | 42 | this._hudComponent = new HUDGameComponent(); 43 | this.addChild(this._hudComponent); 44 | } 45 | private createBackgrounds(): void { 46 | this.addChild(PixiFactory.getColorBackground()); 47 | 48 | const grid: Sprite = PixiFactory.getImage(AtlasKeys.GRID); 49 | grid.x = MagicValues.BORDER_OFFSET; 50 | grid.y = 76; 51 | this.addChild(grid); 52 | 53 | const nextPieceBg: Sprite = PixiFactory.getImage(AtlasKeys.NEXT_TILE); 54 | nextPieceBg.x = ViewPortSize.MAX_WIDTH - nextPieceBg.width - MagicValues.BORDER_OFFSET; 55 | nextPieceBg.y = 76; 56 | this.addChild(nextPieceBg); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /typescript-tetris/src/views/HomeView.ts: -------------------------------------------------------------------------------- 1 | import { Container, Sprite } from "pixi.js"; 2 | 3 | import { AtlasKeys } from "./../utils/AtlasKeys"; 4 | import { PixiFactory } from "./../utils/PixiFactory"; 5 | import { ViewPortSize } from "./../utils/ViewPortSize"; 6 | import { CustomButton } from "./components/CustomButton"; 7 | 8 | export class HomeView extends Container { 9 | private _startButton: CustomButton; 10 | public get startButton(): CustomButton { 11 | return this._startButton; 12 | } 13 | 14 | private _optionButton: CustomButton; 15 | public get optionButton(): CustomButton { 16 | return this._optionButton; 17 | } 18 | 19 | constructor() { 20 | super(); 21 | 22 | this.setupBackground(); 23 | this.setupImages(); 24 | this.setupButtons(); 25 | } 26 | private setupBackground(): void { 27 | this.addChild(PixiFactory.getColorBackground()); 28 | } 29 | private setupImages(): void { 30 | const logo: Sprite = PixiFactory.getImage(AtlasKeys.LOGO); 31 | logo.anchor.set(0.5); 32 | logo.x = ViewPortSize.HALF_WIDTH; 33 | logo.y = ViewPortSize.MAX_HEIGHT * 0.3; 34 | this.addChild(logo); 35 | 36 | const setzer: Sprite = PixiFactory.getImage(AtlasKeys.LOGO_SETZER); 37 | setzer.anchor.set(0.5); 38 | setzer.x = 10; 39 | setzer.y = ViewPortSize.MAX_HEIGHT - 15; 40 | this.addChild(setzer); 41 | } 42 | private setupButtons(): void { 43 | this._startButton = PixiFactory.getButton(AtlasKeys.BUTTON_START); 44 | this._startButton.x = ViewPortSize.HALF_WIDTH; 45 | this._startButton.y = ViewPortSize.MAX_HEIGHT * 0.7; 46 | this.addChild(this._startButton); 47 | 48 | this._optionButton = PixiFactory.getButton(AtlasKeys.BUTTON_CONFIG); 49 | this._optionButton.x = ViewPortSize.HALF_WIDTH; 50 | this._optionButton.y = ViewPortSize.MAX_HEIGHT * 0.8; 51 | this.addChild(this._optionButton); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /typescript-tetris/src/views/InfoPopup.ts: -------------------------------------------------------------------------------- 1 | import { Container, Text } from "pixi.js"; 2 | 3 | import { AtlasKeys } from "./../utils/AtlasKeys"; 4 | import { MagicValues } from "./../utils/MagicValues"; 5 | import { PixiFactory } from "./../utils/PixiFactory"; 6 | import { Texts } from "./../utils/Texts"; 7 | import { ViewPortSize } from "./../utils/ViewPortSize"; 8 | import { CustomButton } from "./components/CustomButton"; 9 | 10 | export class InfoPopup extends Container { 11 | private _closeButton: CustomButton; 12 | public get closeButton(): CustomButton { 13 | return this._closeButton; 14 | } 15 | 16 | constructor() { 17 | super(); 18 | 19 | this.setupBackgrounds(); 20 | this.setupText(); 21 | this.setupButtons(); 22 | } 23 | private setupBackgrounds(): void { 24 | this.addChild(PixiFactory.getShadowBackground()); 25 | this.addChild(PixiFactory.getBoardBackground()); 26 | } 27 | private setupButtons(): void { 28 | this._closeButton = PixiFactory.getButton(AtlasKeys.BUTTON_CANCEL); 29 | this._closeButton.x = ViewPortSize.MAX_WIDTH - 32; 30 | this._closeButton.y = MagicValues.BORDER_OFFSET + 15; 31 | this.addChild(this._closeButton); 32 | } 33 | private setupText(): void { 34 | const msg: Text = PixiFactory.getText(Texts.COMMANDS); 35 | msg.x = ViewPortSize.HALF_WIDTH; 36 | msg.y = ViewPortSize.HALF_HEIGHT - 10; 37 | msg.anchor.set(0.5); 38 | this.addChild(msg); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /typescript-tetris/src/views/IntroView.ts: -------------------------------------------------------------------------------- 1 | import { Container, Sprite, Text } from "pixi.js"; 2 | 3 | import { AtlasKeys } from "./../utils/AtlasKeys"; 4 | import { Colors } from "./../utils/Colors"; 5 | import { PixiFactory } from "./../utils/PixiFactory"; 6 | import { Texts } from "./../utils/Texts"; 7 | import { ViewPortSize } from "./../utils/ViewPortSize"; 8 | 9 | export class IntroView extends Container { 10 | constructor() { 11 | super(); 12 | 13 | this.setupBackground(); 14 | this.setupImages(); 15 | this.setupText(); 16 | } 17 | 18 | private setupBackground(): void { 19 | this.addChild(PixiFactory.getColorBackground()); 20 | } 21 | private setupImages(): void { 22 | const logo: Sprite = PIXI.Sprite.fromImage(AtlasKeys.LOGO_TYPESCRIPT); 23 | logo.anchor.x = 0.5; 24 | logo.x = ViewPortSize.HALF_WIDTH; 25 | logo.y = ViewPortSize.MAX_HEIGHT - 64; 26 | this.addChild(logo); 27 | } 28 | private setupText(): void { 29 | const title: Text = PixiFactory.getText(Texts.DEVELOPER, Colors.GAME_ITEMS, Texts.FONT_SIZE_DEFAULT + 6); 30 | title.anchor.set(0.5); 31 | title.x = ViewPortSize.HALF_WIDTH; 32 | title.y = ViewPortSize.HALF_HEIGHT; 33 | this.addChild(title); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /typescript-tetris/src/views/OptionsView.ts: -------------------------------------------------------------------------------- 1 | import { Container, Text } from "pixi.js"; 2 | 3 | import { AtlasKeys } from "./../utils/AtlasKeys"; 4 | import { Colors } from "./../utils/Colors"; 5 | import { PixiFactory } from "./../utils/PixiFactory"; 6 | import { Texts } from "./../utils/Texts"; 7 | import { ViewPortSize } from "./../utils/ViewPortSize"; 8 | import { CustomButton } from "./components/CustomButton"; 9 | 10 | export class OptionsView extends Container { 11 | private _homeButton: CustomButton; 12 | public get homeButton(): CustomButton { 13 | return this._homeButton; 14 | } 15 | 16 | private _resetButton: CustomButton; 17 | public get resetButton(): CustomButton { 18 | return this._resetButton; 19 | } 20 | 21 | constructor() { 22 | super(); 23 | 24 | this.setupBackground(); 25 | this.setupButtons(); 26 | this.setupTexts(); 27 | } 28 | private setupBackground(): void { 29 | this.addChild(PixiFactory.getColorBackground()); 30 | } 31 | private setupButtons(): void { 32 | this._homeButton = PixiFactory.getButton(AtlasKeys.BUTTON_HOME); 33 | this._homeButton.x = ViewPortSize.MAX_WIDTH - 30; 34 | this._homeButton.y = 30; 35 | this.addChild(this._homeButton); 36 | 37 | this._resetButton = PixiFactory.getButton(AtlasKeys.BUTTON_RESET); 38 | this._resetButton.x = ViewPortSize.MAX_WIDTH - 76; 39 | this._resetButton.y = 116; 40 | this.addChild(this._resetButton); 41 | } 42 | private setupTexts(): void { 43 | const title: Text = PixiFactory.getText(Texts.CONFIG); 44 | title.x = 15; 45 | title.y = 18; 46 | this.addChild(title); 47 | 48 | const hiScore: Text = PixiFactory.getText(Texts.HI_SCORE, Colors.STATIC_TEXT); 49 | hiScore.x = 15; 50 | hiScore.y = 105; 51 | this.addChild(hiScore); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /typescript-tetris/src/views/PausePopup.ts: -------------------------------------------------------------------------------- 1 | import { Container, Text } from "pixi.js"; 2 | 3 | import { AtlasKeys } from "./../utils/AtlasKeys"; 4 | import { Colors } from "./../utils/Colors"; 5 | import { MagicValues } from "./../utils/MagicValues"; 6 | import { PixiFactory } from "./../utils/PixiFactory"; 7 | import { Texts } from "./../utils/Texts"; 8 | import { ViewPortSize } from "./../utils/ViewPortSize"; 9 | import { CustomButton } from "./components/CustomButton"; 10 | 11 | export class PausePopup extends Container { 12 | private _homeButton: CustomButton; 13 | public get homeButton(): CustomButton { 14 | return this._homeButton; 15 | } 16 | 17 | private _resumeButton: CustomButton; 18 | public get resumeButton(): CustomButton { 19 | return this._resumeButton; 20 | } 21 | 22 | private _retryButton: CustomButton; 23 | public get retryButton(): CustomButton { 24 | return this._retryButton; 25 | } 26 | 27 | constructor() { 28 | super(); 29 | 30 | this.interactive = true; 31 | 32 | this.setupBackgrounds(); 33 | this.setupButtons(); 34 | this.setupText(); 35 | } 36 | private setupBackgrounds(): void { 37 | this.addChild(PixiFactory.getShadowBackground()); 38 | this.addChild(PixiFactory.getBoardBackground()); 39 | } 40 | private setupButtons(): void { 41 | this._homeButton = PixiFactory.getButton(AtlasKeys.BUTTON_HOME); 42 | this._homeButton.x = ViewPortSize.HALF_WIDTH + 25; 43 | this._homeButton.y = ViewPortSize.HALF_HEIGHT + 15; 44 | this.addChild(this._homeButton); 45 | 46 | this._resumeButton = PixiFactory.getButton(AtlasKeys.BUTTON_RESUME); 47 | this._resumeButton.x = ViewPortSize.MAX_WIDTH - 32; 48 | this._resumeButton.y = MagicValues.BORDER_OFFSET + 15; 49 | this.addChild(this._resumeButton); 50 | 51 | this._retryButton = PixiFactory.getButton(AtlasKeys.BUTTON_RETRY); 52 | this._retryButton.x = ViewPortSize.HALF_WIDTH - 25; 53 | this._retryButton.y = ViewPortSize.HALF_HEIGHT + 15; 54 | this.addChild(this._retryButton); 55 | } 56 | private setupText(): void { 57 | const tilte: Text = PixiFactory.getText(Texts.PAUSED, Colors.DYNAMIC_TEXT, Texts.FONT_SIZE_DEFAULT + 8); 58 | tilte.x = ViewPortSize.HALF_WIDTH; 59 | tilte.y = ViewPortSize.HALF_HEIGHT - 35; 60 | tilte.anchor.set(0.5); 61 | this.addChild(tilte); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /typescript-tetris/src/views/ResetConfirmPopup.ts: -------------------------------------------------------------------------------- 1 | import { Container, Text } from "pixi.js"; 2 | 3 | import { AtlasKeys } from "./../utils/AtlasKeys"; 4 | import { PixiFactory } from "./../utils/PixiFactory"; 5 | import { Texts } from "./../utils/Texts"; 6 | import { ViewPortSize } from "./../utils/ViewPortSize"; 7 | import { CustomButton } from "./components/CustomButton"; 8 | 9 | export class ResetConfirmPopup extends Container { 10 | private _cancelButton: CustomButton; 11 | public get cancelButton(): CustomButton { 12 | return this._cancelButton; 13 | } 14 | 15 | private _confirmButton: CustomButton; 16 | public get confirmButton(): CustomButton { 17 | return this._confirmButton; 18 | } 19 | 20 | constructor() { 21 | super(); 22 | 23 | this.interactive = true; 24 | 25 | this.setupBackground(); 26 | this.setupTexts(); 27 | this.setupButtons(); 28 | } 29 | private setupBackground(): void { 30 | this.addChild(PixiFactory.getShadowBackground()); 31 | this.addChild(PixiFactory.getBoardBackground()); 32 | } 33 | private setupTexts(): void { 34 | const msg: Text = PixiFactory.getText(Texts.CONFIRM_RESET); 35 | msg.anchor.set(0.5); 36 | msg.x = ViewPortSize.HALF_WIDTH; 37 | msg.y = ViewPortSize.HALF_HEIGHT - 30; 38 | this.addChild(msg); 39 | } 40 | private setupButtons(): void { 41 | this._cancelButton = PixiFactory.getButton(AtlasKeys.BUTTON_CANCEL); 42 | this._cancelButton.x = ViewPortSize.HALF_WIDTH + 25; 43 | this._cancelButton.y = ViewPortSize.HALF_HEIGHT + 15; 44 | this.addChild(this._cancelButton); 45 | 46 | this._confirmButton = PixiFactory.getButton(AtlasKeys.BUTTON_CONFIRM); 47 | this._confirmButton.x = ViewPortSize.HALF_WIDTH - 25; 48 | this._confirmButton.y = ViewPortSize.HALF_HEIGHT + 15; 49 | this.addChild(this._confirmButton); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /typescript-tetris/src/views/StartingPopup.ts: -------------------------------------------------------------------------------- 1 | import { Container, Graphics, Text } from "pixi.js"; 2 | 3 | import { Colors } from "./../utils/Colors"; 4 | import { PixiFactory } from "./../utils/PixiFactory"; 5 | import { Texts } from "./../utils/Texts"; 6 | import { ViewPortSize } from "./../utils/ViewPortSize"; 7 | 8 | export class StartingPopup extends Container { 9 | private _decreasingNumber: Text; 10 | private _background: Graphics; 11 | 12 | constructor() { 13 | super(); 14 | 15 | this.interactive = true; 16 | 17 | this.setupBackgrounds(); 18 | this.setupTexts(); 19 | } 20 | public changeNumber(n: number): void { 21 | this._background.alpha -= 0.1; 22 | this._decreasingNumber.text = String(n); 23 | } 24 | private setupBackgrounds(): void { 25 | this._background = PixiFactory.getShadowBackground(); 26 | this.addChild(this._background); 27 | } 28 | private setupTexts(): void { 29 | this._decreasingNumber = PixiFactory.getText("3", Colors.DYNAMIC_TEXT, Texts.FONT_SIZE_DEFAULT + 6); 30 | this._decreasingNumber.anchor.set(0.5); 31 | this._decreasingNumber.scale.set(1.2); 32 | this._decreasingNumber.x = ViewPortSize.HALF_WIDTH; 33 | this._decreasingNumber.y = ViewPortSize.HALF_HEIGHT; 34 | this.addChild(this._decreasingNumber); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /typescript-tetris/src/views/components/CustomButton.ts: -------------------------------------------------------------------------------- 1 | import { Sprite, Texture } from "pixi.js"; 2 | 3 | import { AtlasKeys } from "./../../utils/AtlasKeys"; 4 | 5 | export class CustomButton extends Sprite { 6 | private _downState: Texture; 7 | private _overState: Texture; 8 | private _upState: Texture; 9 | 10 | private _isDown: boolean; 11 | private _isOver: boolean; 12 | 13 | constructor(atlasKey: string) { 14 | super(AtlasKeys.getTexture(atlasKey + "_up.png")); 15 | 16 | const downStateTexture: Texture = AtlasKeys.getTexture(atlasKey + "_over.png"); 17 | const upStateTexture: Texture = AtlasKeys.getTexture(atlasKey + "_up.png"); 18 | 19 | this._downState = downStateTexture; 20 | this._overState = downStateTexture; 21 | this._upState = upStateTexture; 22 | 23 | this.setInitialValues(); 24 | this.setupInteractions(); 25 | } 26 | private setInitialValues(): void { 27 | this.anchor.set(0.5); 28 | this.interactive = true; 29 | this.buttonMode = true; 30 | } 31 | private setupInteractions(): void { 32 | this.on("pointerup", this.onButtonUp); 33 | this.on("pointerupoutside", this.onButtonUp); 34 | this.on("pointerdown", this.onButtonDown); 35 | this.on("pointerover", this.onButtonOver); 36 | this.on("pointerout", this.onButtonOut); 37 | } 38 | private onButtonDown(): void { 39 | this._isDown = true; 40 | this.texture = this._downState; 41 | this.scale.set(0.95, 0.95); 42 | } 43 | private onButtonOut(): void { 44 | this._isOver = false; 45 | this.texture = this._upState; 46 | this.scale.set(1, 1); 47 | } 48 | private onButtonOver(): void { 49 | this._isOver = true; 50 | this.texture = this._overState; 51 | } 52 | private onButtonUp(): void { 53 | this._isDown = false; 54 | this.scale.set(1, 1); 55 | 56 | if (this._isOver) { 57 | this.texture = this._overState; 58 | } else { 59 | this.texture = this._upState; 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /typescript-tetris/src/views/components/DoubleTextField.ts: -------------------------------------------------------------------------------- 1 | import { Container, Text } from "pixi.js"; 2 | 3 | import { Colors } from "./../../utils/Colors"; 4 | import { PixiFactory } from "./../../utils/PixiFactory"; 5 | 6 | export class DoubleTextField extends Container { 7 | private _text: Text; 8 | public get text(): string { 9 | return this._text.text; 10 | } 11 | public set text(value: string) { 12 | this._text.text = value; 13 | } 14 | 15 | constructor(label: string) { 16 | super(); 17 | 18 | const labelField: Text = PixiFactory.getText(label, Colors.STATIC_TEXT); 19 | labelField.anchor.x = 1; 20 | this.addChild(labelField); 21 | 22 | this._text = PixiFactory.getText("0", Colors.DYNAMIC_TEXT); 23 | this._text.anchor.x = 1; 24 | this._text.y = labelField.y + 30; 25 | this.addChild(this._text); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /typescript-tetris/src/views/components/GridComponent.ts: -------------------------------------------------------------------------------- 1 | import { TilePool } from "./../../utils/TilePool"; 2 | import { TileDisplay } from "./TileDisplay"; 3 | 4 | import { Container } from "pixi.js"; 5 | 6 | export class GridComponent extends Container { 7 | public clear(): void { 8 | while (this.children.length > 0) { 9 | if (this.getChildAt(0) instanceof TileDisplay) { 10 | TilePool.back(this.getChildAt(0)); 11 | } 12 | this.removeChildAt(0); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /typescript-tetris/src/views/components/HUDGameComponent.ts: -------------------------------------------------------------------------------- 1 | import { Container, Text } from "pixi.js"; 2 | 3 | import { GameModel } from "./../../models/GameModel"; 4 | import { AtlasKeys } from "./../../utils/AtlasKeys"; 5 | import { Colors } from "./../../utils/Colors"; 6 | import { MagicValues } from "./../../utils/MagicValues"; 7 | import { PixiFactory } from "./../../utils/PixiFactory"; 8 | import { Texts } from "./../../utils/Texts"; 9 | import { ViewPortSize } from "./../../utils/ViewPortSize"; 10 | import { CustomButton } from "./CustomButton"; 11 | import { DoubleTextField } from "./DoubleTextField"; 12 | 13 | export class HUDGameComponent extends Container { 14 | private _linesText: DoubleTextField; 15 | private _scoreText: DoubleTextField; 16 | private _levelText: DoubleTextField; 17 | private _hiScoreText: DoubleTextField; 18 | 19 | private _pauseButton: CustomButton; 20 | public get pauseButton(): CustomButton { 21 | return this._pauseButton; 22 | } 23 | 24 | constructor() { 25 | super(); 26 | 27 | this.createTextFields(); 28 | this.createButtons(); 29 | } 30 | public updateData(model: GameModel): void { 31 | this._linesText.text = String(model.lines); 32 | this._scoreText.text = String(model.score); 33 | this._levelText.text = String(model.level); 34 | this._hiScoreText.text = "0"; // String(model.hiScore); 35 | } 36 | private createButtons(): void { 37 | this._pauseButton = PixiFactory.getButton(AtlasKeys.BUTTON_PAUSE); 38 | this._pauseButton.x = ViewPortSize.MAX_WIDTH - 32; 39 | this._pauseButton.y = MagicValues.BORDER_OFFSET + 15; 40 | this.addChild(this._pauseButton); 41 | } 42 | private createTextFields(): void { 43 | const gameLabel: Text = PixiFactory.getText(Texts.TETRIS, Colors.DYNAMIC_TEXT); 44 | gameLabel.x = MagicValues.BORDER_OFFSET; 45 | gameLabel.y = 24; 46 | this.addChild(gameLabel); 47 | 48 | this._levelText = new DoubleTextField(Texts.LEVEL); 49 | this._levelText.x = ViewPortSize.MAX_WIDTH - MagicValues.BORDER_OFFSET; 50 | this._levelText.y = 184; 51 | this.addChild(this._levelText); 52 | 53 | this._scoreText = new DoubleTextField(Texts.SCORE); 54 | this._scoreText.x = ViewPortSize.MAX_WIDTH - MagicValues.BORDER_OFFSET; 55 | this._scoreText.y = 252; 56 | this.addChild(this._scoreText); 57 | 58 | this._linesText = new DoubleTextField(Texts.LINES); 59 | this._linesText.x = ViewPortSize.MAX_WIDTH - MagicValues.BORDER_OFFSET; 60 | this._linesText.y = 318; 61 | this.addChild(this._linesText); 62 | 63 | this._hiScoreText = new DoubleTextField(Texts.HI_SCORE); 64 | this._hiScoreText.x = ViewPortSize.MAX_WIDTH - MagicValues.BORDER_OFFSET; 65 | this._hiScoreText.y = 386; 66 | this.addChild(this._hiScoreText); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /typescript-tetris/src/views/components/NextPieceComponent.ts: -------------------------------------------------------------------------------- 1 | import { Container } from "pixi.js"; 2 | 3 | export class NextPieceComponent extends Container { 4 | constructor() { 5 | super(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /typescript-tetris/src/views/components/TileDisplay.ts: -------------------------------------------------------------------------------- 1 | import { Sprite, Texture } from "pixi.js"; 2 | 3 | export class TileDisplay extends Sprite { 4 | private _typeId: number; 5 | 6 | public get typeId(): number { 7 | return this._typeId; 8 | } 9 | 10 | constructor(texture: Texture, typeId: number) { 11 | super(texture); 12 | 13 | this._typeId = typeId; 14 | } 15 | public removeFromParent(): void { 16 | this.parent.removeChild(this); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /typescript-tetris/test/entry.test.ts: -------------------------------------------------------------------------------- 1 | import "reflect-metadata"; 2 | -------------------------------------------------------------------------------- /typescript-tetris/test/models/GameModel.test.ts: -------------------------------------------------------------------------------- 1 | import "reflect-metadata"; 2 | import { GameModel } from "./../../src/models/GameModel"; 3 | import { assert } from "chai"; 4 | 5 | describe("GameModel", () => { 6 | it("Clear: Returns the values to default", () => { 7 | let model: GameModel = new GameModel(); 8 | model.clear(); 9 | let result = model.level === 1 && model.lines === 0 && model.score === 0; 10 | assert.isTrue(result); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /typescript-tetris/test/models/Grid.test.ts: -------------------------------------------------------------------------------- 1 | import { Tile } from "./../../src/models/Tile"; 2 | import { Grid } from "./../../src/models/Grid"; 3 | import { assert } from "chai"; 4 | 5 | describe("Grid", () => { 6 | let grid: Grid; 7 | 8 | beforeEach(() => { 9 | grid = new Grid(); 10 | }); 11 | 12 | afterEach(() => { 13 | grid = null; 14 | }); 15 | 16 | it("RemoveRow: The values must be null", () => { 17 | let row = 5; 18 | for (let col = 0; col < grid.maxCols; col++) { 19 | grid.setTile(new Tile(col, row), col, row); 20 | } 21 | let removedRow: Array = grid.removeRow(row); 22 | let result = true; 23 | for (let col = 0; col < grid.maxCols; col++) { 24 | result = result && grid.getTile(col, row) === null; 25 | } 26 | assert.isTrue(result); 27 | }); 28 | 29 | it("RemoveRow: The method has to return the removed tiles", () => { 30 | let tiles: Array = new Array(); 31 | let row = 5; 32 | for (let col = 0; col < grid.maxCols; col++) { 33 | let tile = new Tile(col, row); 34 | grid.setTile(tile, col, row); 35 | tiles.push(tile); 36 | } 37 | let removedRow: Array = grid.removeRow(row); 38 | let result = true; 39 | for (let i = 0; i < grid.maxCols; i++) { 40 | result = result && removedRow[i] === tiles[i]; 41 | } 42 | 43 | assert.isTrue(result); 44 | }); 45 | 46 | it("GetRow: Get the list of tiles from the row", () => { 47 | let tiles: Array = new Array(); 48 | let row = 5; 49 | for (let col = 0; col < grid.maxCols; col++) { 50 | let tile = new Tile(col, row); 51 | grid.setTile(tile, col, row); 52 | tiles.push(tile); 53 | } 54 | let getRow: Array = grid.getRow(row); 55 | let result = true; 56 | for (let i = 0; i < grid.maxCols; i++) { 57 | result = result && getRow[i] === tiles[i]; 58 | } 59 | assert.isTrue(result); 60 | }); 61 | 62 | it("Initial values: The grid must start with null values", () => { 63 | let result = true; 64 | for (let row = 0; row < grid.maxRows; row++) { 65 | for (let col = 0; col < grid.maxCols; col++) { 66 | result = result && grid.getTile(col, row) === null; 67 | } 68 | } 69 | assert.isTrue(result); 70 | }); 71 | 72 | it("IsEmptyTile: When the tile is null, then the Tile is Empty", () => { 73 | let col = 0; 74 | let row = 0; 75 | grid.setTile(null, col, row); 76 | assert.isTrue(grid.isEmptyTile(col, row)); 77 | }); 78 | 79 | it("IsEmptyTile: When the tile is not null, then the Tile is not Empty", () => { 80 | let col = 0; 81 | let row = 0; 82 | grid.setTile(new Tile(col, row), col, row); 83 | assert.isFalse(grid.isEmptyTile(col, row)); 84 | }); 85 | 86 | it("GetTile: Returns the Tile", () => { 87 | let col = 2; 88 | let row = 2; 89 | let tile = new Tile(col, row); 90 | grid.setTile(tile, col, row); 91 | tile = grid.getTile(col, row); 92 | 93 | assert.isTrue(tile.col === col && tile.row === row); 94 | }); 95 | 96 | it("SetTile: Set the Tile in the position", () => { 97 | let col = 0; 98 | let row = 0; 99 | let tile = new Tile(col, row); 100 | grid.setTile(tile, col, row); 101 | assert.deepEqual(tile, grid.getTile(col, row)); 102 | }); 103 | }); 104 | -------------------------------------------------------------------------------- /typescript-tetris/test/models/Tile.test.ts: -------------------------------------------------------------------------------- 1 | import { Tile } from "./../../src/models/Tile"; 2 | import { assert } from "chai"; 3 | 4 | describe("Tile", () => { 5 | let tile: Tile; 6 | 7 | beforeEach(() => { 8 | tile = new Tile(); 9 | }); 10 | 11 | afterEach(() => { 12 | tile = null; 13 | }); 14 | 15 | it("Clone: Returns a new Tile with the same values", () => { 16 | tile.row = 1; 17 | tile.col = 1; 18 | 19 | let clone: Tile = tile.clone(); 20 | let result = tile.col === clone.col && tile.row === clone.row; 21 | assert.isTrue(result); 22 | }); 23 | 24 | it("SetPosition: Changes the Tile position", () => { 25 | let row = 1; 26 | let col = 1; 27 | 28 | tile.setPosition(col, row); 29 | let result = tile.col === col && tile.row === row; 30 | assert.isTrue(result); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /typescript-tetris/test/utils/TileGroupFactory.test.ts: -------------------------------------------------------------------------------- 1 | import { Equal } from "tslint/lib/utils"; 2 | import { Tile } from "./../../src/models/Tile"; 3 | import { TileGroup } from "./../../src/models/TileGroup"; 4 | import { TileGroupType } from "./../../src/models/TileGroupType"; 5 | import { TileGroupFactory } from "./../../src/utils/TileGroupFactory"; 6 | import { assert } from "chai"; 7 | 8 | describe("TileGroupFactory", () => { 9 | it("testGetTileGroup: notNull", () => { 10 | let tileGroup: TileGroup = TileGroupFactory.getTileGroup(TileGroupType.TYPE_I); 11 | assert.isNotNull(tileGroup); 12 | }); 13 | 14 | it("testGetTileGroup: right values", () => { 15 | let tileGroup: TileGroup = TileGroupFactory.getTileGroup(TileGroupType.TYPE_I); 16 | let result = true; 17 | result = result && tileGroup.typeId === TileGroupType.TYPE_I; 18 | result = result && tileGroup.tiles.length === 4; 19 | assert.equal(tileGroup.typeId, TileGroupType.TYPE_I); 20 | assert.isTrue(result); 21 | }); 22 | 23 | it("testGetRandomTileGroup: ", () => { 24 | let tileGroup: TileGroup = TileGroupFactory.getRandomTileGroup(); 25 | assert.isNotNull(tileGroup); 26 | }); 27 | 28 | it("testGetTilesByTypeArray: Is not Null", () => { 29 | let array: Array = TileGroupType.getTypeArray(TileGroupType.TYPE_L); 30 | let tiles: Array = TileGroupFactory.getTilesByTypeArray(array); 31 | assert.isNotNull(tiles); 32 | }); 33 | 34 | it("testGetTilesByTypeArray: right values", () => { 35 | let array: Array = TileGroupType.getTypeArray(TileGroupType.TYPE_L); 36 | let tiles: Array = TileGroupFactory.getTilesByTypeArray(array); 37 | let result = true; 38 | 39 | result = result && tiles.length === array.length; 40 | result = result && tiles[0].col === Math.floor(array[0] % 2); 41 | result = result && tiles[0].row === Math.floor(array[0] / 2); 42 | assert.isTrue(result); 43 | }); 44 | }); 45 | -------------------------------------------------------------------------------- /typescript-tetris/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "outDir": "dist/", 5 | "strictNullChecks": false, 6 | "baseUrl": "src/", 7 | "noImplicitAny": false, 8 | "removeComments": true, 9 | "preserveConstEnums": true, 10 | "target": "es5", 11 | "lib": ["es6", "dom"], 12 | "types": ["reflect-metadata", "mocha"], 13 | "declaration": true, 14 | "moduleResolution": "node", 15 | "sourceMap": true, 16 | "watch": true, 17 | "experimentalDecorators": true, 18 | "emitDecoratorMetadata": true 19 | }, 20 | "include": [ 21 | "src/*", 22 | "test/*", 23 | "*" 24 | ], 25 | "exclude": [ 26 | "node_modules", 27 | "**/*.spec.ts" 28 | ] 29 | } 30 | -------------------------------------------------------------------------------- /typescript-tetris/tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "tslint:latest", 4 | "tslint-config-prettier" 5 | ], 6 | "rules": { 7 | "ban-types": [ false ], 8 | "class-name": true, 9 | "comment-format": [ 10 | true, 11 | "check-space" 12 | ], 13 | "curly": true, 14 | "forin": true, 15 | "label-position": true, 16 | "member-access": true, 17 | "member-ordering": [ 18 | true, { 19 | "order": [ 20 | "static-field", 21 | "instance-field", 22 | "constructor", 23 | "public-instance-method", 24 | "protected-instance-method", 25 | "private-instance-method" 26 | ] 27 | } 28 | ], 29 | "no-angle-bracket-type-assertion": false, 30 | "no-arg": true, 31 | "no-bitwise": true, 32 | "no-console": [ 33 | true, 34 | "debug", 35 | "info", 36 | "time", 37 | "timeEnd", 38 | "trace" 39 | ], 40 | "no-construct": true, 41 | "no-debugger": true, 42 | "no-duplicate-variable": true, 43 | "no-empty": true, 44 | "no-eval": true, 45 | "no-inferrable-types": false, 46 | "no-shadowed-variable": true, 47 | "no-string-literal": true, 48 | "no-switch-case-fall-through": false, 49 | "no-this-assignment": false, 50 | "no-unused-expression": true, 51 | "no-unused-variable": true, 52 | "no-unreachable": true, 53 | "no-use-before-declare": true, 54 | "no-var-keyword": true, 55 | "object-literal-sort-keys": true, 56 | "ordered-imports": false, 57 | "only-arrow-functions": [ false ], 58 | "prefer-const": true, 59 | "radix":true, 60 | "trailing-comma": [ 61 | false 62 | ], 63 | "triple-equals": [ 64 | true, 65 | "allow-null-check" 66 | ], 67 | "variable-name": false 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /typescript-tetris/webpack.setzer.config.js: -------------------------------------------------------------------------------- 1 | const webpack = require('webpack'); 2 | const path = require('path'); 3 | const HtmlWebpackPlugin = require('html-webpack-plugin'); 4 | 5 | module.exports = (function(options) { 6 | return { 7 | entry: { 8 | main: path.resolve("src/index.ts") 9 | }, 10 | output: { 11 | path: __dirname + "/dist", 12 | filename: "bundle.js" 13 | }, 14 | devtool: 'source-map', 15 | module: { 16 | rules: [ 17 | { test: /\.ts$/, loader: "ts-loader" } 18 | ] 19 | }, 20 | plugins: [ new HtmlWebpackPlugin() ], 21 | resolve: { 22 | extensions: ['.ts', '.js', '.json'] 23 | } 24 | } 25 | })(); --------------------------------------------------------------------------------