Advertisement
Guest User

Untitled

a guest
Jul 14th, 2015
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.96 KB | None | 0 0
  1. package;
  2.  
  3. import flixel.FlxG;
  4. import flixel.FlxSprite;
  5. import flixel.FlxState;
  6. import flixel.text.FlxText;
  7. import flixel.ui.FlxButton;
  8. import flixel.util.FlxMath;
  9. import flixel.group.FlxGroup;
  10. import flixel.group.FlxTypedGroup;
  11. import flixel.util.FlxPoint;
  12. import flixel.util.FlxRandom;
  13. import flixel.FlxObject;
  14.  
  15.  
  16. class PlayState extends FlxState
  17. {
  18.  
  19.  
  20. private var _blocks:FlxTypedGroup<Block>;
  21. private var _blocksCollide:FlxTypedGroup<Block>;
  22. private var _spawnTimer:Float;
  23. private var _spawnInterval:Float = 2.5;
  24.  
  25. //Puzzle Array
  26. public var _blocksArr:Array<Block>;
  27. public var puzzleState:Array<Array<Int>>;
  28. public var puzzleBoardWidth:Int = 6;
  29. public var puzzleBoardHeight:Int = 13;
  30.  
  31.  
  32. //Borders
  33. public var _barrier:FlxSprite;
  34. public var _barrier2:FlxSprite;
  35. public var _barrier3:FlxSprite;
  36. public var _barrier4:FlxSprite;
  37.  
  38. private var random:Float;
  39. private var random2:Int;
  40.  
  41. //Game Over Trigger
  42. public var GameOver:FlxText; //46,86
  43. public var GameIsOver:Bool;
  44.  
  45. override public function create():Void
  46. {
  47. super.create();
  48.  
  49. //Utility
  50. FlxG.cameras.bgColor = 0xffAEAEFF;
  51.  
  52.  
  53. _blocks = new FlxTypedGroup();
  54. add(_blocks);
  55.  
  56. _blocksCollide = new FlxTypedGroup();
  57. add(_blocksCollide);
  58.  
  59. _blocksArr = new Array<Block>();
  60.  
  61. //Puzzle Array
  62. puzzleState = new Array();
  63. for (y in 0...puzzleBoardHeight)
  64. {
  65. var row:Array<Int> = new Array();
  66. puzzleState.push(row);
  67. }
  68. init_puzzle_state();
  69.  
  70.  
  71.  
  72.  
  73. ///////////////////////
  74.  
  75. //Everything else
  76. _barrier = new FlxSprite(0, 448);
  77. _barrier.makeGraphic(640, 32, 0xff000000);
  78. _barrier.immovable = true;
  79. //_barrier.solid = true;
  80. add(_barrier);
  81.  
  82. _barrier2 = new FlxSprite(220, 0);
  83. _barrier2.makeGraphic(32, 480, 0xff000000);
  84. _barrier2.immovable = true;
  85. //_barrier.solid = true;
  86. add(_barrier2);
  87.  
  88. _barrier3 = new FlxSprite(444, 0);
  89. _barrier3.makeGraphic(32, 480, 0xff000000);
  90. _barrier3.immovable = true;
  91. add(_barrier3);
  92.  
  93. _barrier4 = new FlxSprite(220, 0);
  94. _barrier4.makeGraphic(256, 16, 0xffFF0000);
  95. _barrier4.immovable = true;
  96. _barrier4.visible = false;
  97. add(_barrier4);
  98.  
  99. //Game Over
  100. GameOver = new FlxText(6, 16, 180, "GAME OVER \n\n\nClick to Restart", 18);
  101. GameOver.alignment = "center";
  102. GameOver.color = 0xFF0000;
  103. GameOver.visible = false;
  104. add(GameOver);
  105.  
  106.  
  107. spawnBlock();
  108. }
  109.  
  110.  
  111. function init_puzzle_state():Void
  112. {
  113. for (y in 0...puzzleBoardHeight)
  114. {
  115. for (x in 0...puzzleBoardWidth)
  116. {
  117. puzzleState[y][x] = 0;
  118. }
  119. }
  120. }
  121.  
  122. override public function update():Void
  123. {
  124. super.update();
  125.  
  126. //Spawning blocks from above
  127. /*
  128. if (FlxG.mouse.justPressed)
  129. {
  130. spawnBlock();
  131. }*/
  132.  
  133.  
  134.  
  135. for (b in _blocks.members )
  136. {
  137. //Blocks hitting bottom
  138. if (b.overlaps(_barrier) && b.actor == true )
  139. {
  140. if (!FlxG.overlap(_blocksCollide,_barrier4)) spawnBlock();
  141. b.actor = false;
  142. _blocksCollide.add(b);
  143. //b.immovable = true;
  144. b.justTouched(FlxObject.FLOOR);
  145. }
  146.  
  147. //Blocks hit fallen blocks
  148. if (b.overlaps(_blocksCollide) && b.actor == true && b.facing == FlxObject.DOWN && !FlxG.keys.justPressed.ANY)
  149. {
  150.  
  151. if (!FlxG.overlap(_blocksCollide,_barrier4)) spawnBlock();
  152. b.actor = false;
  153. _blocksCollide.add(b);
  154. //b.immovable = true;
  155. if (b.facing != FlxObject.LEFT || b.facing != FlxObject.RIGHT) b.velocity.y == 0;
  156. b.justTouched(FlxObject.FLOOR);
  157. }
  158.  
  159. if (b.overlaps(_blocksCollide) && b.actor == true && b.facing == FlxObject.LEFT)
  160. {
  161. b.velocity.y == 75;
  162.  
  163. }
  164.  
  165. if (b.overlaps(_blocksCollide) && b.actor == true && b.facing == FlxObject.RIGHT)
  166. {
  167. b.velocity.y == 75;
  168.  
  169. }
  170. }
  171.  
  172. //Check for matches
  173. if (any_pieces_have_just_hit_the_floor())
  174. {
  175. update_puzzle_array();
  176. check_and_mark_vertical_matches();
  177. check_and_mark_horizontal_matches();
  178. delete_marked_pieces();
  179. }
  180.  
  181.  
  182.  
  183. FlxG.watch.addMouse;
  184.  
  185. FlxG.collide(_blocks, _barrier);
  186. FlxG.collide(_blocks, _barrier2);
  187. FlxG.collide(_blocks, _barrier3);
  188. FlxG.collide(_blocks, _blocks);
  189. FlxG.collide(_blocks, _blocksCollide);
  190.  
  191.  
  192.  
  193. //Game Over
  194. if (FlxG.overlap(_blocksCollide, _barrier4)) GameIsOver = true;
  195.  
  196. if (GameIsOver == true) GameOver.visible = true;
  197.  
  198. if (GameIsOver && FlxG.mouse.justPressed) FlxG.switchState(new PlayState());
  199.  
  200. if (FlxG.keys.justReleased.SPACE)
  201. {
  202. FlxG.switchState(new PlayState());
  203. }
  204.  
  205.  
  206. }
  207.  
  208. function any_pieces_have_just_hit_the_floor():Bool
  209. {
  210. //Loop through all of our pieces to see if any have just hit the floor
  211. for (i in 0..._blocks.length)
  212. {
  213. if (_blocks.members[i].justTouched(FlxObject.FLOOR)) {
  214. return true;
  215. }
  216. }
  217. return false;
  218. }
  219.  
  220. function update_puzzle_array():Void
  221. {
  222. init_puzzle_state();
  223. for (i in 0..._blocks.length) puzzleState[_blocks.members[i].grid_position_y][_blocks.members[i].grid_position_x] = _blocks.members[i].BlockColor;
  224. }
  225.  
  226. function check_and_mark_vertical_matches():Void
  227. {
  228. //For every column...
  229. for (x in 0...puzzleBoardWidth)
  230. {
  231. //First we need to get the column data
  232. var column:Array<Int> = new Array();
  233. for (y in 0...puzzleBoardHeight)
  234. {
  235. column.push(puzzleState[y][x]);
  236. }
  237.  
  238. //Get matched pieces in that column
  239. var matched_pieces = get_matched_pieces(column);
  240. for (i in 0...matched_pieces.length)
  241. {
  242. //Check every piece to see if its data matches any of the matched pieces' data !!!THIS IS LAZY!!!THERE IS PROBABLY A BETTER WAY TO DO THIS!!!
  243. for (p in 0..._blocks.length)
  244. {
  245. if (_blocks.members[p].grid_position_x == x && _blocks.members[p].grid_position_y == matched_pieces[i]) _blocks.members[p].marked_for_deletion = true;
  246. }
  247. }
  248. }
  249. }
  250.  
  251. function check_and_mark_horizontal_matches():Void
  252. {
  253. //Fore every row...
  254. for (y in 0...puzzleBoardHeight)
  255. {
  256. //Get matched pieces in that row
  257. var matched_pieces = get_matched_pieces(puzzleState[y]);
  258. for (i in 0...matched_pieces.length)
  259. {
  260. //Check every piece to see if its data matches any of the matched pieces' data !!!THIS IS LAZY!!!THERE IS PROBABLY A BETTER WAY TO DO THIS!!!
  261. for (p in 0..._blocks.length)
  262. {
  263. if (_blocks.members[p].grid_position_x == matched_pieces[i] && _blocks.members[p].grid_position_y == y) _blocks.members[p].marked_for_deletion = true;
  264. }
  265. }
  266. }
  267. }
  268.  
  269. //Make an array to hold our matched pieces
  270.  
  271.  
  272. function get_matched_pieces(piece_array:Array<Int>):Array<Int>
  273. {
  274. var matched_pieces:Array<Int> = new Array();
  275. for (_i in 0...piece_array.length)
  276. {
  277. var this_piece_is_in_a_match = false;
  278. //Watch this value
  279. var n = 0;
  280. //Checking pieces 2 slots behind and in front of current piece...
  281. for (i in -2...3)
  282. {
  283. if (_i + i >= 0 && _i + i < piece_array.length && piece_array[_i + i] == piece_array[_i]) n++;
  284. else n = 0;
  285.  
  286. //If the above checks out, that n value is increased by one, if there are three in a row, it will represent a match...
  287. if (n >= 3) this_piece_is_in_a_match = true;
  288. }
  289. //...and we add the piece to the matched pieces array
  290. //THIS IS THE SINGLE LINE
  291. if (this_piece_is_in_a_match) matched_pieces.push[_i];
  292. }
  293.  
  294. return matched_pieces;
  295. }
  296.  
  297. function delete_marked_pieces():Void
  298. {
  299. for (i in 0..._blocks.length)
  300. {
  301. if (_blocks.members[i].marked_for_deletion && _blocks.members[i].alive) _blocks.members[i].kill();
  302. }
  303. }
  304.  
  305. public function spawnBlock():Void
  306. {
  307. var x:Float = 316;
  308. var y:Float = 0;
  309.  
  310. var gBlock = new Block(x,y);
  311.  
  312.  
  313. FlxG.watch.add(gBlock, "x", "Gx");
  314. FlxG.watch.add(gBlock, "y", "Gy");
  315.  
  316. //var gBlock = new Block(FlxG.mouse.x, FlxG.mouse.y);
  317. _blocks.add(gBlock);
  318.  
  319. _blocksArr.push(gBlock);
  320. }
  321.  
  322.  
  323. }
  324.  
  325.  
  326. Puzzle Piece
  327.  
  328. package;
  329. import flixel.FlxSprite;
  330. import flixel.FlxG;
  331. import flixel.util.FlxSpriteUtil;
  332. import flixel.util.FlxTimer;
  333.  
  334. import flixel.FlxObject;
  335.  
  336. class Block extends FlxSprite
  337. {
  338.  
  339. public var actor:Bool;
  340.  
  341. public var BlockColor:Int;
  342.  
  343. public var grid_position_x:Int;
  344. public var grid_position_y:Int;
  345. public var marked_for_deletion:Bool = false;
  346.  
  347. var padding_left:Float = 0;
  348. var padding_top:Float = 0;
  349.  
  350. public function new(x:Float, y:Float)
  351. {
  352. super(x, y);
  353.  
  354.  
  355.  
  356.  
  357. BlockColor = Random.int(0, 2);
  358.  
  359. if (BlockColor == 0) loadGraphic("assets/images/BlockRed.png", false, 32, 32);
  360. if (BlockColor == 1) loadGraphic("assets/images/BlockBlue.png", false, 32, 32);
  361. if (BlockColor == 2) loadGraphic("assets/images/BlockGreen.png", false, 32, 32);
  362.  
  363. velocity.y = 75;
  364.  
  365. solid = true;
  366. //immovable = true;
  367.  
  368. actor = true;
  369. facing = FlxObject.DOWN;
  370.  
  371.  
  372. }
  373.  
  374. override public function kill():Void
  375. {
  376. super.kill();
  377. //We set this so we don't try killing the same piece multiple times or something wacky
  378. alive = false;
  379. //Now we can do that flickering business (or whatever you want)
  380. FlxSpriteUtil.flicker(this, 1);
  381. //I dunno how callbacks work on flicker stuff sooo...
  382. //new FlxTimer().start(1).onComplete = function(t:FlxTimer):Void { super.kill(); }
  383. }
  384.  
  385. override public function update():Void
  386. {
  387. super.update();
  388.  
  389. grid_position_x = Math.floor(Std.int(x) - padding_left / width);
  390. grid_position_y = Math.floor(Std.int(x) - padding_top / height);
  391.  
  392. FlxSpriteUtil.bound(this, 0, 640, -32, 480);
  393.  
  394. if (!actor) immovable = true;
  395.  
  396.  
  397. if (actor)
  398. {
  399. if (FlxG.keys.anyJustPressed(["LEFT", "A"]))
  400. {
  401. facing = FlxObject.LEFT;
  402. x -= 32;
  403. }
  404. if (FlxG.keys.anyJustPressed(["RIGHT", "D"]))
  405. {
  406. facing = FlxObject.RIGHT;
  407. x += 32;
  408. }
  409. if (FlxG.keys.anyPressed(["DOWN", "S"]))
  410. {
  411. facing = FlxObject.DOWN;
  412. velocity.y += 82;
  413. } else velocity.y = 75;
  414.  
  415. if (!FlxG.keys.justPressed.ANY) facing = FlxObject.DOWN;
  416.  
  417. if (velocity.y >= 0) facing = FlxObject.DOWN;
  418. }
  419. }
  420. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement