| 1 | package net.suztomo.FPazzle |
|---|
| 2 | { |
|---|
| 3 | import flash.display.*; |
|---|
| 4 | |
|---|
| 5 | import mx.containers.Panel; |
|---|
| 6 | import mx.core.UIComponent; |
|---|
| 7 | |
|---|
| 8 | public class FBoard extends UIComponent |
|---|
| 9 | { |
|---|
| 10 | public static var pazzleImage:BitmapData; |
|---|
| 11 | [Embed(source='flower.jpg')] private var EmbedImage:Class; |
|---|
| 12 | |
|---|
| 13 | private var x_num:uint, y_num:uint; |
|---|
| 14 | private var p_width:Number, p_height:Number; |
|---|
| 15 | private static var answerDiff:Number = 2; |
|---|
| 16 | |
|---|
| 17 | public var pieces:PazzleMap; |
|---|
| 18 | public var keyword:String; |
|---|
| 19 | public var infoPanel:Panel; |
|---|
| 20 | |
|---|
| 21 | public function FBoard(_x_num:uint, _y_num:uint, _pazzleImage:BitmapData = null) |
|---|
| 22 | { |
|---|
| 23 | x_num = _x_num; |
|---|
| 24 | y_num = _y_num; |
|---|
| 25 | pazzleImage = _pazzleImage; |
|---|
| 26 | |
|---|
| 27 | setPazzleImage(); |
|---|
| 28 | |
|---|
| 29 | p_width = pazzleImage.width / x_num; |
|---|
| 30 | p_height = pazzleImage.height / y_num; |
|---|
| 31 | super(); |
|---|
| 32 | |
|---|
| 33 | pieces = new PazzleMap(x_num, y_num); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | public function setPazzleImage() :void |
|---|
| 37 | { |
|---|
| 38 | if (pazzleImage == null) { |
|---|
| 39 | var img:Bitmap = new EmbedImage() as Bitmap; |
|---|
| 40 | pazzleImage = img.bitmapData; |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | public function putPiece(_x:Number = 0, _y:Number = 0, _x_index:uint = 0, _y_index:uint = 0):void |
|---|
| 45 | { |
|---|
| 46 | var piece:FPiece = new FPiece(pazzleImage, p_width, p_height, _x_index, _y_index); |
|---|
| 47 | piece.x = _x; |
|---|
| 48 | piece.y = _y; |
|---|
| 49 | if (pazzleImage == null) { |
|---|
| 50 | trace("pazzle Image is not setted"); |
|---|
| 51 | } else { |
|---|
| 52 | addChild(piece); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | pieces.setElement(_x_index, _y_index, piece); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | public function createPieces():void |
|---|
| 59 | { |
|---|
| 60 | var min_x:Number = p_width, min_y:Number = p_height; |
|---|
| 61 | var range:Number = 400; |
|---|
| 62 | for (var i:uint=0; i < x_num; i++) { |
|---|
| 63 | for (var j:uint=0; j < y_num; j++) { |
|---|
| 64 | putPiece(min_x + range*Math.random(), min_y + range*Math.random(), j, i); |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | public function isAnswer():Boolean |
|---|
| 70 | { |
|---|
| 71 | var tmp1:FPiece, tmp2:FPiece; |
|---|
| 72 | |
|---|
| 73 | for (var i:uint = 0; i<y_num; i++) { |
|---|
| 74 | for (var j:uint = 0; j<x_num; j++) { |
|---|
| 75 | tmp1 = pieces.getElement(j, i) as FPiece; |
|---|
| 76 | if (tmp1.rotation != 0) return false; |
|---|
| 77 | |
|---|
| 78 | if (j != x_num-1) { |
|---|
| 79 | tmp2 = pieces.getElement(j+1, i) as FPiece; |
|---|
| 80 | if (Math.abs(tmp1.x + p_width - tmp2.x) > answerDiff |
|---|
| 81 | || Math.abs(tmp1.y - tmp2.y) > answerDiff){ |
|---|
| 82 | return false; |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | if (i != y_num-1) { |
|---|
| 87 | tmp2 = pieces.getElement(j, i+1) as FPiece; |
|---|
| 88 | if (Math.abs(tmp1.x - tmp2.x) > answerDiff |
|---|
| 89 | || Math.abs(tmp1.y + p_height - tmp2.y) > answerDiff){ |
|---|
| 90 | return false; |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | return true; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | public function deactivate():void |
|---|
| 99 | { |
|---|
| 100 | for (var i:uint=0; i<pieces.length; i++) { |
|---|
| 101 | pieces.getInline(i).deactivate(); |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | } |
|---|