-
-
Save dearenot/222ffe602e59577aaadda4748b5d670d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package; | |
| import openfl.Assets; | |
| import openfl.display.Sprite; | |
| import openfl.Lib; | |
| import openfl.geom.Point; | |
| import openfl.text.TextField; | |
| import openfl.text.TextFieldAutoSize; | |
| import openfl.text.TextFormat; | |
| class TestObject { | |
| public var testField:Int; | |
| public function new(testField:Int) { | |
| this.testField = testField; | |
| } | |
| } | |
| class Main extends Sprite | |
| { | |
| var fieldModel:Array<Array<Int>>; | |
| var WIDTH:Int = 10; | |
| var HEIGHT:Int = 10; | |
| var displayedCells:Array<Sprite>; | |
| public function new() | |
| { | |
| super(); | |
| fieldModel = []; | |
| for (i in 0...WIDTH) { | |
| var newArr:Array<Int> = []; | |
| fieldModel.push(newArr); | |
| for (j in 0...HEIGHT) { | |
| newArr.push(1); | |
| } | |
| } | |
| displayedCells = []; | |
| renderModel(fieldModel); | |
| var ar:Array<TestObject> = [for (i in 0...10) new TestObject(Std.int(Math.random() * 100))]; | |
| var tr1:String = " before "; | |
| for (i in 0...ar.length) { | |
| tr1 += " " + ar[i].testField + " "; | |
| } | |
| trace(tr1); | |
| ar.sort(function(a:TestObject,b:TestObject):Int { | |
| if (a.testField == b.testField) | |
| return 0; | |
| if (a.testField > b.testField) | |
| return 1; | |
| else | |
| return -1; | |
| }); | |
| var tr2:String = " after "; | |
| for (i in 0...ar.length) { | |
| tr2 += " " + ar[i].testField + " "; | |
| } | |
| trace(tr2); | |
| } | |
| function renderModel(model:Array<Array<Int>>) { | |
| clearScreen(); | |
| for (i in 0...model.length) { | |
| for (j in 0...model[i].length) { | |
| var cell:Cell = new Cell(); | |
| displayedCells.push(cell); | |
| cell.x = i * (cell.width ); | |
| cell.y = j * (cell.height); | |
| cell.tf.text = Std.string(model[i][j]); | |
| addChild(cell); | |
| var isoPoint:Point = twoDToIso(new Point(cell.x, cell.y)); | |
| cell.x = isoPoint.x + 100; | |
| cell.y = isoPoint.y + 50; | |
| } | |
| } | |
| } | |
| function clearScreen() { | |
| for (i in 0...displayedCells.length) { | |
| displayedCells[i] = null; | |
| displayedCells.slice(0, displayedCells.length); | |
| } | |
| while (numChildren > 0) removeChildAt(0); | |
| } | |
| function twoDToIso(pt:Point):Point{ | |
| var tempPt:Point = new Point(0,0); | |
| tempPt.x = ((pt.x - pt.y)/2); | |
| tempPt.y = ((pt.x + pt.y)/2) / 2; | |
| return(tempPt); | |
| } | |
| } | |
| class Cell extends Sprite { | |
| public var tf:TextField; | |
| public function new(w:Int = 50, h:Int = 50) { | |
| super(); | |
| this.x = 50; | |
| this.y = 25; | |
| //graphics.beginFill(0xFF99F2); | |
| //graphics.drawRect(0, 0, 20, 20); | |
| //graphics.endFill(); | |
| graphics.beginFill(0x008000); | |
| graphics.lineStyle(5, 0x000000, 0.7); | |
| graphics.lineTo(0, 0); | |
| graphics.lineTo(w, h/2); | |
| graphics.lineTo(w * 2, 0); | |
| graphics.lineTo(w, -h/2); | |
| graphics.lineTo(0, 0); | |
| graphics.endFill(); | |
| //graphics.beginFill(0x804000); | |
| //graphics.lineTo(0, 0); | |
| //graphics.lineTo(0, h); | |
| //graphics.lineTo(2, h * 2); | |
| //graphics.lineTo(w * 2, h); | |
| //graphics.lineTo(w * 2, 0); | |
| //graphics.lineTo(w, h); | |
| //graphics.lineTo(w, h * 2); | |
| graphics.endFill(); | |
| tf = new TextField(); | |
| tf.autoSize = TextFieldAutoSize.CENTER; | |
| tf.defaultTextFormat = new TextFormat("Verdana", 20, 0x000000); | |
| tf.text = "0"; | |
| tf.x = 0; | |
| tf.y -= 5; | |
| //addChild(tf); | |
| } | |
| } | |
| enum CellModelType { | |
| Passable; | |
| Impassable; | |
| } | |
| class CellModel { | |
| public var type:CellModelType; | |
| public function new(type:CellModelType) { | |
| this.type = type; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment