Advertisement
JuiceBoxx

Untitled

Feb 11th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 4.83 KB | None | 0 0
  1. package;
  2.  
  3. import flixel.FlxG;
  4. import flixel.FlxObject;
  5. import flixel.FlxSprite;
  6. import flixel.FlxState;
  7. import flixel.math.FlxAngle;
  8. import flixel.math.FlxPoint;
  9. import flixel.text.FlxText;
  10. import flixel.util.FlxColor;
  11. import flixel.math.FlxMath;
  12. import flixel.FlxCamera;
  13. import openfl.Lib;
  14. import openfl.net.URLRequest;
  15. import haxe.io.Path;
  16. import flixel.graphics.FlxGraphic;
  17.  
  18. import flixel.addons.display.FlxGridOverlay;
  19.  
  20. class MainState extends FlxState
  21. {
  22.     var mapCam:FlxCamera; //The camera that renders the tilemap being drawn
  23.     var uiCam:FlxCamera; //The camera that renders the UI elements
  24.     var bgCam:FlxCamera; //The camera that renders background elements
  25.     var grabbedPos:FlxPoint = new FlxPoint( -1, -1); //For camera scrolling
  26.     var initialScroll:FlxPoint = new FlxPoint(0, 0); //Ditto ^
  27.     var editing:Bool = false; //Bool that determines whether or not the user is editing something
  28.     var backgroundImage:FlxSprite; //Background
  29.    
  30.     var grid:FlxSprite;
  31.     var file:Dropdown;
  32.     var help:Dropdown;
  33.    
  34.     var saved:String;
  35.    
  36.    
  37.     public function new(Editing:Bool = false){
  38.         super();
  39.         editing = Editing;
  40.     }
  41.    
  42.     override public function create():Void
  43.     {  
  44.         super.create();
  45.         FlxG.mouse.useSystemCursor = true;
  46.        
  47.        
  48.         //Camera
  49.         uiCam = new FlxCamera(0, 0, FlxG.width, FlxG.height);
  50.         uiCam.bgColor = FlxColor.TRANSPARENT;
  51.         uiCam.setScrollBounds(0, FlxG.width, 0, FlxG.height);
  52.         uiCam.antialiasing = true;
  53.        
  54.         bgCam = new FlxCamera(0, 0, FlxG.width, FlxG.height);
  55.         bgCam.setScrollBounds(0, FlxG.width, 0, FlxG.height);
  56.         bgCam.antialiasing = true;
  57.        
  58.         if(editing){
  59.             //Create map camera
  60.             if((Current.MAP_SIZE.x * Current.TILE_SIZE) >= 1920 && (Current.MAP_SIZE.y * Current.TILE_SIZE) >= 1080){
  61.                 mapCam = new FlxCamera(Std.int((Current.MAP_SIZE.x * Current.TILE_SIZE)*-0.25), Std.int((Current.MAP_SIZE.y * Current.TILE_SIZE)*-0.25), Std.int((Current.MAP_SIZE.x * Current.TILE_SIZE)*1.5), Std.int((Current.MAP_SIZE.y * Current.TILE_SIZE)*1.5), 1);
  62.                 mapCam.scroll.x = ((Current.MAP_SIZE.x * Current.TILE_SIZE) * -0.23);
  63.                 mapCam.scroll.y = ((Current.MAP_SIZE.y * Current.TILE_SIZE) * -0.125);
  64.             }else{
  65.                 mapCam = new FlxCamera(-480, -270, 2880, 1620, 1);
  66.                 mapCam.scroll.set((2880 * -0.5)+(Current.TILE_SIZE*Current.MAP_SIZE.x/2), (1620 * -0.5)+(Current.TILE_SIZE*Current.MAP_SIZE.y/2));
  67.             }
  68.             mapCam.bgColor = FlxColor.TRANSPARENT;
  69.             FlxG.cameras.reset(bgCam);
  70.             FlxG.cameras.add(mapCam);
  71.             mapCam.antialiasing = true;
  72.         }
  73.         FlxG.camera.antialiasing = true;
  74.         FlxG.cameras.add(uiCam);
  75.        
  76.         //BG
  77.         backgroundImage = new FlxSprite(0, 0, "assets/sprites/background.png");
  78.         backgroundImage.scrollFactor.set(0, 0);
  79.         if (editing){
  80.             backgroundImage.alpha = 0.25;
  81.             backgroundImage.cameras = [bgCam];
  82.         }
  83.        
  84.         //Grid
  85.         grid = new FlxSprite(0, 0, FlxGraphic.fromBitmapData(FlxGridOverlay.createGrid(Current.TILE_SIZE, Current.TILE_SIZE, Std.int(Current.MAP_SIZE.x * Current.TILE_SIZE), Std.int(Current.MAP_SIZE.y * Current.TILE_SIZE), true, FlxColor.WHITE, FlxColor.fromRGB(204, 204, 204))));
  86.         if (editing){
  87.             grid.cameras = [mapCam];
  88.             grid.alpha = 0.25;
  89.         }
  90.        
  91.         //Toolbar
  92.         file = new Dropdown(' File... ', ["New..", "Save..", "Save as..", "Open..", "Config"], new FlxPoint(0, 0), uiCam, fileEvent);
  93.         help = new Dropdown(' Help... ', ["About", "Manual"], new FlxPoint(file.items[0].x + file.items[0].width, 0), uiCam, helpEvent);
  94.        
  95.         //Add things
  96.         add(backgroundImage);
  97.         if(editing){
  98.             add(grid);
  99.             //Add tiles
  100.         }
  101.        
  102.         //Toolbar
  103.         add(file);
  104.         add(help);
  105.     }
  106.  
  107.     override public function update(elapsed:Float):Void
  108.     {
  109.         super.update(elapsed);
  110.         if(editing){
  111.            
  112.             //Zoom in and out with the scrollwheel
  113.             mapCam.zoom += 0.2 * FlxG.mouse.wheel;
  114.            
  115.             if (mapCam.zoom < 0.75)
  116.                 mapCam.zoom = 0.75;
  117.             if (mapCam.zoom > 4)
  118.                 mapCam.zoom = 4;
  119.            
  120.             //Scroll the camera by pressing the middle button
  121.             if (FlxG.mouse.justPressedMiddle){
  122.                 grabbedPos = FlxG.mouse.getWorldPosition(mapCam);
  123.                 initialScroll = mapCam.scroll;
  124.             }
  125.             if (FlxG.mouse.pressedMiddle){
  126.                 var mousePosChange:FlxPoint = FlxG.mouse.getWorldPosition(mapCam).subtractPoint(grabbedPos);
  127.                 mapCam.scroll.subtractPoint(mousePosChange);
  128.             }
  129.         }
  130.     }
  131.    
  132.     function getPath(File:String){
  133.         return Path.join([Path.directory(Sys.executablePath()), File]);
  134.     }
  135.    
  136.     function fileEvent(ID:String):Void{
  137.         switch(ID){
  138.             case "New..":
  139.                 FlxG.switchState(new MainState(true)); //Needs a dialogue for setting map/tile sizes and whether or not to make it a nape map
  140.             case "Save..":
  141.                
  142.             case "Save as..":
  143.                
  144.             case "Open..":
  145.                
  146.             case "Config":
  147.                
  148.         }
  149.     }
  150.    
  151.     function helpEvent(ID:String):Void{
  152.         switch(ID){
  153.             case 'About':
  154.                 Lib.getURL(new URLRequest("file:///" + getPath("assets/doc/About.pdf")));
  155.             case 'Manual':
  156.                 trace("file:///" + getPath("docs/About.pdf"));
  157.         }
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement