Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package;
- import flixel.FlxG;
- import flixel.FlxSprite;
- import flixel.FlxState;
- import flixel.text.FlxText;
- import flixel.ui.FlxButton;
- import flixel.util.FlxMath;
- import flixel.group.FlxGroup;
- import flixel.group.FlxTypedGroup;
- import flixel.util.FlxPoint;
- import flixel.util.FlxRandom;
- import flixel.FlxObject;
- class PlayState extends FlxState
- {
- private var _blocks:FlxTypedGroup<Block>;
- private var _blocksCollide:FlxTypedGroup<Block>;
- private var _spawnTimer:Float;
- private var _spawnInterval:Float = 2.5;
- //Puzzle Array
- public var _blocksArr:Array<Block>;
- public var puzzleState:Array<Array<Int>>;
- public var puzzleBoardWidth:Int = 6;
- public var puzzleBoardHeight:Int = 13;
- //Borders
- public var _barrier:FlxSprite;
- public var _barrier2:FlxSprite;
- public var _barrier3:FlxSprite;
- public var _barrier4:FlxSprite;
- private var random:Float;
- private var random2:Int;
- //Game Over Trigger
- public var GameOver:FlxText; //46,86
- public var GameIsOver:Bool;
- override public function create():Void
- {
- super.create();
- //Utility
- FlxG.cameras.bgColor = 0xffAEAEFF;
- _blocks = new FlxTypedGroup();
- add(_blocks);
- _blocksCollide = new FlxTypedGroup();
- add(_blocksCollide);
- _blocksArr = new Array<Block>();
- //Puzzle Array
- puzzleState = new Array();
- for (y in 0...puzzleBoardHeight)
- {
- var row:Array<Int> = new Array();
- puzzleState.push(row);
- }
- init_puzzle_state();
- ///////////////////////
- //Everything else
- _barrier = new FlxSprite(0, 448);
- _barrier.makeGraphic(640, 32, 0xff000000);
- _barrier.immovable = true;
- //_barrier.solid = true;
- add(_barrier);
- _barrier2 = new FlxSprite(220, 0);
- _barrier2.makeGraphic(32, 480, 0xff000000);
- _barrier2.immovable = true;
- //_barrier.solid = true;
- add(_barrier2);
- _barrier3 = new FlxSprite(444, 0);
- _barrier3.makeGraphic(32, 480, 0xff000000);
- _barrier3.immovable = true;
- add(_barrier3);
- _barrier4 = new FlxSprite(220, 0);
- _barrier4.makeGraphic(256, 16, 0xffFF0000);
- _barrier4.immovable = true;
- _barrier4.visible = false;
- add(_barrier4);
- //Game Over
- GameOver = new FlxText(6, 16, 180, "GAME OVER \n\n\nClick to Restart", 18);
- GameOver.alignment = "center";
- GameOver.color = 0xFF0000;
- GameOver.visible = false;
- add(GameOver);
- spawnBlock();
- }
- function init_puzzle_state():Void
- {
- for (y in 0...puzzleBoardHeight)
- {
- for (x in 0...puzzleBoardWidth)
- {
- puzzleState[y][x] = 0;
- }
- }
- }
- override public function update():Void
- {
- super.update();
- //Spawning blocks from above
- /*
- if (FlxG.mouse.justPressed)
- {
- spawnBlock();
- }*/
- for (b in _blocks.members )
- {
- //Blocks hitting bottom
- if (b.overlaps(_barrier) && b.actor == true )
- {
- if (!FlxG.overlap(_blocksCollide,_barrier4)) spawnBlock();
- b.actor = false;
- _blocksCollide.add(b);
- //b.immovable = true;
- b.justTouched(FlxObject.FLOOR);
- }
- //Blocks hit fallen blocks
- if (b.overlaps(_blocksCollide) && b.actor == true && b.facing == FlxObject.DOWN && !FlxG.keys.justPressed.ANY)
- {
- if (!FlxG.overlap(_blocksCollide,_barrier4)) spawnBlock();
- b.actor = false;
- _blocksCollide.add(b);
- //b.immovable = true;
- if (b.facing != FlxObject.LEFT || b.facing != FlxObject.RIGHT) b.velocity.y == 0;
- b.justTouched(FlxObject.FLOOR);
- }
- if (b.overlaps(_blocksCollide) && b.actor == true && b.facing == FlxObject.LEFT)
- {
- b.velocity.y == 75;
- }
- if (b.overlaps(_blocksCollide) && b.actor == true && b.facing == FlxObject.RIGHT)
- {
- b.velocity.y == 75;
- }
- }
- //Check for matches
- if (any_pieces_have_just_hit_the_floor())
- {
- update_puzzle_array();
- check_and_mark_vertical_matches();
- check_and_mark_horizontal_matches();
- delete_marked_pieces();
- }
- FlxG.watch.addMouse;
- FlxG.collide(_blocks, _barrier);
- FlxG.collide(_blocks, _barrier2);
- FlxG.collide(_blocks, _barrier3);
- FlxG.collide(_blocks, _blocks);
- FlxG.collide(_blocks, _blocksCollide);
- //Game Over
- if (FlxG.overlap(_blocksCollide, _barrier4)) GameIsOver = true;
- if (GameIsOver == true) GameOver.visible = true;
- if (GameIsOver && FlxG.mouse.justPressed) FlxG.switchState(new PlayState());
- if (FlxG.keys.justReleased.SPACE)
- {
- FlxG.switchState(new PlayState());
- }
- }
- function any_pieces_have_just_hit_the_floor():Bool
- {
- //Loop through all of our pieces to see if any have just hit the floor
- for (i in 0..._blocks.length)
- {
- if (_blocks.members[i].justTouched(FlxObject.FLOOR)) {
- return true;
- }
- }
- return false;
- }
- function update_puzzle_array():Void
- {
- init_puzzle_state();
- for (i in 0..._blocks.length) puzzleState[_blocks.members[i].grid_position_y][_blocks.members[i].grid_position_x] = _blocks.members[i].BlockColor;
- }
- function check_and_mark_vertical_matches():Void
- {
- //For every column...
- for (x in 0...puzzleBoardWidth)
- {
- //First we need to get the column data
- var column:Array<Int> = new Array();
- for (y in 0...puzzleBoardHeight)
- {
- column.push(puzzleState[y][x]);
- }
- //Get matched pieces in that column
- var matched_pieces = get_matched_pieces(column);
- for (i in 0...matched_pieces.length)
- {
- //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!!!
- for (p in 0..._blocks.length)
- {
- if (_blocks.members[p].grid_position_x == x && _blocks.members[p].grid_position_y == matched_pieces[i]) _blocks.members[p].marked_for_deletion = true;
- }
- }
- }
- }
- function check_and_mark_horizontal_matches():Void
- {
- //Fore every row...
- for (y in 0...puzzleBoardHeight)
- {
- //Get matched pieces in that row
- var matched_pieces = get_matched_pieces(puzzleState[y]);
- for (i in 0...matched_pieces.length)
- {
- //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!!!
- for (p in 0..._blocks.length)
- {
- if (_blocks.members[p].grid_position_x == matched_pieces[i] && _blocks.members[p].grid_position_y == y) _blocks.members[p].marked_for_deletion = true;
- }
- }
- }
- }
- //Make an array to hold our matched pieces
- function get_matched_pieces(piece_array:Array<Int>):Array<Int>
- {
- var matched_pieces:Array<Int> = new Array();
- for (_i in 0...piece_array.length)
- {
- var this_piece_is_in_a_match = false;
- //Watch this value
- var n = 0;
- //Checking pieces 2 slots behind and in front of current piece...
- for (i in -2...3)
- {
- if (_i + i >= 0 && _i + i < piece_array.length && piece_array[_i + i] == piece_array[_i]) n++;
- else n = 0;
- //If the above checks out, that n value is increased by one, if there are three in a row, it will represent a match...
- if (n >= 3) this_piece_is_in_a_match = true;
- }
- //...and we add the piece to the matched pieces array
- //THIS IS THE SINGLE LINE
- if (this_piece_is_in_a_match) matched_pieces.push[_i];
- }
- return matched_pieces;
- }
- function delete_marked_pieces():Void
- {
- for (i in 0..._blocks.length)
- {
- if (_blocks.members[i].marked_for_deletion && _blocks.members[i].alive) _blocks.members[i].kill();
- }
- }
- public function spawnBlock():Void
- {
- var x:Float = 316;
- var y:Float = 0;
- var gBlock = new Block(x,y);
- FlxG.watch.add(gBlock, "x", "Gx");
- FlxG.watch.add(gBlock, "y", "Gy");
- //var gBlock = new Block(FlxG.mouse.x, FlxG.mouse.y);
- _blocks.add(gBlock);
- _blocksArr.push(gBlock);
- }
- }
- Puzzle Piece
- package;
- import flixel.FlxSprite;
- import flixel.FlxG;
- import flixel.util.FlxSpriteUtil;
- import flixel.util.FlxTimer;
- import flixel.FlxObject;
- class Block extends FlxSprite
- {
- public var actor:Bool;
- public var BlockColor:Int;
- public var grid_position_x:Int;
- public var grid_position_y:Int;
- public var marked_for_deletion:Bool = false;
- var padding_left:Float = 0;
- var padding_top:Float = 0;
- public function new(x:Float, y:Float)
- {
- super(x, y);
- BlockColor = Random.int(0, 2);
- if (BlockColor == 0) loadGraphic("assets/images/BlockRed.png", false, 32, 32);
- if (BlockColor == 1) loadGraphic("assets/images/BlockBlue.png", false, 32, 32);
- if (BlockColor == 2) loadGraphic("assets/images/BlockGreen.png", false, 32, 32);
- velocity.y = 75;
- solid = true;
- //immovable = true;
- actor = true;
- facing = FlxObject.DOWN;
- }
- override public function kill():Void
- {
- super.kill();
- //We set this so we don't try killing the same piece multiple times or something wacky
- alive = false;
- //Now we can do that flickering business (or whatever you want)
- FlxSpriteUtil.flicker(this, 1);
- //I dunno how callbacks work on flicker stuff sooo...
- //new FlxTimer().start(1).onComplete = function(t:FlxTimer):Void { super.kill(); }
- }
- override public function update():Void
- {
- super.update();
- grid_position_x = Math.floor(Std.int(x) - padding_left / width);
- grid_position_y = Math.floor(Std.int(x) - padding_top / height);
- FlxSpriteUtil.bound(this, 0, 640, -32, 480);
- if (!actor) immovable = true;
- if (actor)
- {
- if (FlxG.keys.anyJustPressed(["LEFT", "A"]))
- {
- facing = FlxObject.LEFT;
- x -= 32;
- }
- if (FlxG.keys.anyJustPressed(["RIGHT", "D"]))
- {
- facing = FlxObject.RIGHT;
- x += 32;
- }
- if (FlxG.keys.anyPressed(["DOWN", "S"]))
- {
- facing = FlxObject.DOWN;
- velocity.y += 82;
- } else velocity.y = 75;
- if (!FlxG.keys.justPressed.ANY) facing = FlxObject.DOWN;
- if (velocity.y >= 0) facing = FlxObject.DOWN;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement