In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: forked from: Iso terrain

Get Adobe Flash player
by baudon.thomas 14 Apr 2012
    Embed
/**
 * Copyright baudon.thomas ( http://wonderfl.net/user/baudon.thomas )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/wsbq
 */

// forked from baudon.thomas's forked from: Iso terrain
// forked from baudon.thomas's Iso terrain
package 
{
    import flash.text.TextField;
    import flash.ui.Mouse;
    import flash.events.MouseEvent;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.display.Shape;
    import flash.events.MouseEvent;
    import flash.geom.Matrix;
    import flash.geom.Point;
    
    public class Main extends Sprite 
    {
        
        /**
        * @author blabla
        */
        private var myPoints:Vector.<Point>;
        
        private var myWidth:uint = 20;
        private var myHeight:uint = 20;
        
        private var myIsoTransform:Matrix;
        
        "string"
        'a'
        
        private var mSelectection:Shape;
        
        private var myTrace:TextField;
        
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            
            myPoints = new Vector.<Point>();
            
               mSelectection = new Shape();
            
            mSelectection.graphics.lineStyle(3, 0xffffff, 0.8);
            mSelectection.graphics.beginFill(0xffffff, 0.4);
            mSelectection.graphics.drawRect(0, 0, 32, 32);
            mSelectection.graphics.endFill();
            
            myTrace = new TextField();
            
            for (var i:uint = 0; i < myHeight +1; ++i) {
                for (var j:uint = 0; j <  myWidth +1; ++j) {
                    var temp:Point = new Point();
                    temp.x = j * 32;
                    temp.y = i * 32;
                    myPoints.push(temp );
                }
            }            
            myIsoTransform = new Matrix();
            myIsoTransform.rotate(Math.PI / 4);
            myIsoTransform.scale(1, 0.5);
            myIsoTransform.translate(stage.stageWidth / 2, 0);
            for (var a:uint = 0; a < myPoints.length; ++a){
                myPoints[a] = myIsoTransform.transformPoint(myPoints[a]);
                myPoints[a].y += Math.random() * 50 -25;
            }
            
            graphics.lineStyle(2, 0x333366, 0.8);
            
            for (var k:uint = 0; k < myHeight; ++k) {
                for (var l:uint = 0; l < myWidth; ++l) {
                     if ((k+l)%2 > 0) 
                        graphics.beginFill(0x666699);
                    else 
                        graphics.beginFill(0x9999cc);
                    var r:uint = l % (myHeight + 1) + k * (myWidth + 1);
                    graphics.moveTo(myPoints[r].x, myPoints[r].y);
                    graphics.lineTo(myPoints[r + 1].x, myPoints[r + 1].y);
                    graphics.lineTo(myPoints[r + 2+  myWidth].x, myPoints[r + 2 + myWidth].y);
                    graphics.lineTo(myPoints[r + 1 +  myWidth].x, myPoints[r + 1 + myWidth].y);
                    graphics.lineTo(myPoints[r].x, myPoints[r].y);
                }
            }
            
              addChild(mSelectection);
              addChild(myTrace);
              
              myIsoTransform.invert();
            
            stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
            
        }
        
        private function mouseMove(e:MouseEvent):void{
            var mousePos:Point = new Point(mouseX, mouseY);
            
            mousePos = myIsoTransform.transformPoint(mousePos);
            
            var x:int = mousePos.x / 32;
            var y:int = mousePos.y / 32;
            
            var i:uint = x + y*(myWidth+1); 
            
            var p1:uint = i;
            var p2:uint = i+1;   
            var p3:uint = i + myWidth+1;
            var p4:uint = i + myWidth + 2;
            
            var a:Point = myPoints[p1];
            var b:Point = myPoints[p2];
            var c:Point = myPoints[p4];
            var d:Point = myPoints[p3];
            
            mSelectection.graphics.clear();
            mSelectection.graphics.lineStyle(3, 0xffffff, 0.8);
            mSelectection.graphics.beginFill(0xffffff, 0.4);
            mSelectection.graphics.moveTo(a.x,a.y);
            mSelectection.graphics.lineTo(b.x,b.y);
            mSelectection.graphics.lineTo(c.x,c.y);
            mSelectection.graphics.lineTo(d.x,d.y);
            mSelectection.graphics.lineTo(a.x,a.y);
            mSelectection.graphics.endFill();
            
            myTrace.text = "tile: " +i;
            
        }

        
    }
    
}