Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 21.36 KB | None | 0 0
  1. import Drag;
  2.  
  3. import luxe.Vector;
  4. import luxe.Text;
  5. import luxe.Color;
  6. import luxe.Entity;
  7. import luxe.Sprite;
  8. import luxe.tween.Actuate;
  9.  
  10. // something on a tile with a geom
  11. class Actor {
  12.     public function new() {}
  13.     public var tile : Tile;
  14.     public var geom : Sprite;
  15. }
  16.  
  17. class Piece {
  18.     public function new() {}
  19.     public var actor : Actor;
  20.     public var kind : PieceKind;
  21.     public var team : Int;
  22. }
  23.  
  24. class Structure {
  25.     public function new() {}
  26.     public var actor : Actor;
  27. }
  28.  
  29. enum PieceKind {
  30.     Rook;
  31.     Pawn;
  32.     King;
  33.     Queen;
  34.     Bishop;
  35.     Knight;
  36. }
  37.  
  38. class Puzzle {
  39.  
  40.     var root : Entity;
  41.     var tile_map : TileMap;
  42.  
  43.     var drag : Drag;
  44.     var can_move : Bool;
  45.  
  46.     var turns : Int;
  47.  
  48.     var player : Piece;
  49.     var structures : Array<Structure>;
  50.     var pieces : Array<Piece>;
  51.  
  52.     var ADJACENT_NEIGHBORS : Array<Direction> = [
  53.         North,
  54.         East,
  55.         South,
  56.         West,
  57.  
  58.     ];
  59.     var MOORE_NEIGHBORS : Array<Direction> = [
  60.         North,
  61.         East,
  62.         South,
  63.         West,
  64.         NorthEast,
  65.         NorthWest,
  66.         SouthEast,
  67.         SouthWest,
  68.     ];
  69.  
  70.     public function new() {
  71.         Luxe.events.listen('resize', onresize);
  72.         Luxe.events.listen('dragdown', ondragdown);
  73.         Luxe.events.listen('dragmove', ondragmove);
  74.         Luxe.events.listen('dragup', ondragup);
  75.     }
  76.  
  77.     public function play() {
  78.  
  79.         root = new Entity();
  80.         turns = 0;
  81.  
  82.         var w : Int = 5;
  83.         var h : Int = 6;
  84.  
  85.         tile_map = new TileMap();
  86.         tile_map.init_tiles_to_size(w, h);
  87.         tile_map.tile_size = new Vector(128, 128);
  88.  
  89.         var texture_info = Main.texture_atlas.get_texture('water');
  90.         for (tile in tile_map.tiles_array) {
  91.             var geom : Sprite = tile.geom;
  92.             geom.texture = texture_info.texture;
  93.             geom.uv = texture_info.uv;
  94.             geom.depth = -1;
  95.             geom.parent = root;
  96.             geom.size.copy_from(tile_map.tile_size);
  97.             var i : Int = tile.i;
  98.             var j : Int = tile.j;
  99.             if ((i % 2 == 0 && j % 2 != 0)
  100.                     || (i % 2 != 0 && j % 2 == 0)) {
  101.                 geom.color.rgb(0xe3ded3);
  102.             } else {
  103.                 geom.visible = false;
  104.             }
  105.             var x : Float = (i - w * 0.5 + 0.5) * tile_map.tile_size.x;
  106.             var y : Float = (j - h * 0.5 + 0.5) * tile_map.tile_size.y;
  107.             geom.pos.set_xy(x, y);
  108.             geom.active = false;
  109.         }
  110.  
  111.         can_move = true;
  112.            
  113.         player = make_player(Queen);
  114.         move_actor(player.actor, tile_map.tiles_array[3]);
  115.  
  116.         pieces = [];
  117.  
  118.         structures = [];
  119.         for (i in 0...w) {
  120.             var tile : Tile = tile_map.get_tile_at_index(i, 0);
  121.             var structure = make_structure();
  122.             move_actor(structure.actor, tile);
  123.             tile.geom.visible = false;
  124.             structures.push(structure);
  125.         }
  126.  
  127.         arrange();
  128.  
  129.     }
  130.  
  131.     public function reset() {
  132.         Luxe.scene.empty();
  133.     }
  134.  
  135.     function set_sprite_texture(sprite:Sprite, texture_name:String) {
  136.         var texture_info = Main.texture_atlas.get_texture(texture_name);
  137.         sprite.texture = texture_info.texture;
  138.         sprite.size = texture_info.size.clone();
  139.         sprite.uv = texture_info.uv.clone();
  140.     }
  141.  
  142.     function make_actor() {
  143.         var actor = new Actor();
  144.         actor.geom = new Sprite({
  145.             parent: root,
  146.         });
  147.         actor.geom.color.rgb(0xffffff);
  148.         Utils.fit_vector_to_size(actor.geom.size, tile_map.tile_size.x * 0.9, tile_map.tile_size.y * 0.9);
  149.         return actor;
  150.     }
  151.  
  152.     function make_piece(piece_kind:PieceKind, team:Int) {
  153.         var piece = new Piece();
  154.         piece.actor = make_actor();
  155.         set_piece_kind(piece, piece_kind);
  156.         piece.team = team;
  157.         if (team > 0) {
  158.             piece.actor.geom.color.rgb(0xff0000);
  159.         }
  160.         return piece;
  161.     }
  162.  
  163.     function set_piece_kind(piece:Piece, piece_kind:PieceKind) {
  164.         var textures : Map<PieceKind,String> = [
  165.             Pawn => 'pawn',
  166.             Rook => 'rook',
  167.             King => 'king',
  168.             Queen => 'queen',
  169.             Bishop => 'bishop',
  170.             Knight => 'knight',
  171.         ];
  172.         set_sprite_texture(piece.actor.geom, textures.get(piece_kind));
  173.         piece.actor.geom.name = textures.get(piece_kind);
  174.         piece.kind = piece_kind;
  175.     }
  176.  
  177.     function make_player(piece_kind:PieceKind) {
  178.         var piece = make_piece(piece_kind, 0);
  179.         piece.actor.geom.name = 'player';
  180.         return piece;
  181.     }
  182.  
  183.     function make_structure() {
  184.         var structure = new Structure();
  185.         var actor = make_actor();
  186.         set_sprite_texture(actor.geom, 'gate');
  187.         actor.geom.name = 'structure';
  188.         actor.geom.size.copy_from(tile_map.tile_size);
  189.         actor.geom.size.y *= 0.8;
  190.         actor.geom.color.rgb(0x969184);
  191.         actor.geom.depth = 10;
  192.         structure.actor = actor;
  193.         return structure;
  194.     }
  195.  
  196.     function spawn_piece() {
  197.         var available_structures : Array<Structure>= [];
  198.         for (structure in structures) {
  199.             var tile : Tile = structure.actor.tile;
  200.             var piece : Piece = query_piece_by_tile(tile);
  201.             if (piece == null) available_structures.push(structure);
  202.         }
  203.         if (available_structures.length == 0) return trace('no available structures to spawn');
  204.         var random_index : Int = Math.floor(Math.random() * available_structures.length);
  205.         var structure = available_structures[random_index];
  206.         var tile : Tile = structure.actor.tile;
  207.         if (tile == null) return trace('no tile on structure?');
  208.  
  209.         var piece = make_piece(Pawn, 1);
  210.         move_actor(piece.actor, tile);
  211.         piece.actor.geom.pos.y -= tile_map.tile_size.y;
  212.         move_actor(piece.actor, tile);
  213.         pieces.push(piece);
  214.  
  215.     }
  216.  
  217.     function move_actor(actor:Actor, new_tile:Tile) {
  218.         var old_tile : Tile = actor.tile;
  219.         if (old_tile != null) {
  220.             var anim = Animate.animate_vector(actor.geom.pos);
  221.             anim.duration = 0.2;
  222.             anim.dst.copy_from(new_tile.geom.pos);
  223.             anim.easing = luxe.tween.easing.Cubic.easeInOut;
  224.         } else {
  225.             actor.geom.pos.copy_from(new_tile.geom.pos);
  226.         }
  227.         actor.tile = new_tile;
  228.         trace('moved ${actor.geom.name} from $old_tile to $new_tile');
  229.     }
  230.  
  231.     // gameplay code
  232.     function get_direction_from_components(di:Int, dj:Int):Direction {
  233.         if (di == 0 && dj == 1) return South;
  234.         if (di == 0 && dj == -1) return North;
  235.         if (di == 1 && dj == 0) return East;
  236.         if (di == -1 && dj == 0) return West;
  237.         if (di == 1 && dj == -1) return NorthEast;
  238.         if (di == -1 && dj == -1) return NorthWest;
  239.         if (di == 1 && dj == 1) return SouthEast;
  240.         if (di == -1 && dj == 1) return SouthWest;
  241.         return null;
  242.     }
  243.  
  244.     function query_tile_in_direction(tile:Tile, direction:Direction) {
  245.         var i : Int = tile.i;
  246.         var j : Int = tile.j;
  247.         switch (direction) {
  248.             case North: { j -= 1; };
  249.             case South: { j += 1; };
  250.             case East: { i += 1; };
  251.             case West: { i -= 1; };
  252.             case NorthEast: { i += 1; j -= 1; };
  253.             case NorthWest: { i -= 1; j -= 1; };
  254.             case SouthEast: { i += 1; j += 1; };
  255.             case SouthWest: { i -= 1; j += 1; };
  256.         }
  257.         return tile_map.get_tile_at_index(i, j);
  258.     }
  259.  
  260.     function query_tiles_in_direction(tile:Tile, direction:Direction, ?limit:Int = 0) {
  261.         var tiles : Array<Tile> = [];
  262.         var curr_tile : Tile = tile;
  263.         while (tiles.length < limit || limit <= 0) {
  264.             var next_tile : Tile = query_tile_in_direction(curr_tile, direction);
  265.             if (next_tile == null) break;
  266.             tiles.push(next_tile);
  267.             curr_tile = next_tile;
  268.         }
  269.         return tiles;
  270.     }
  271.  
  272.     function query_structure_by_tile(tile:Tile) {
  273.         for (structure in structures) {
  274.             if (structure.actor.tile == tile) return structure;
  275.         }
  276.         return null;
  277.     }
  278.  
  279.     function query_piece_by_tile(tile:Tile) {
  280.         if (player.actor.tile == tile) return player;
  281.         for (piece in pieces) {
  282.             if (piece.actor.tile == tile) return piece;
  283.         }
  284.         return null;
  285.     }
  286.  
  287.     function is_piece_in_structure(piece:Piece) {
  288.         var tile : Tile = piece.actor.tile;
  289.         var structure : Structure = query_structure_by_tile(tile);
  290.         return structure != null;
  291.     }
  292.  
  293.     function get_valid_moves_for_piece(piece:Piece) {
  294.  
  295.         var valid_moves : Array<Tile> = [];
  296.         var kind : PieceKind = piece.kind;
  297.         var team : Int = piece.team;
  298.         var actor : Actor = piece.actor;
  299.         var curr_tile : Tile = piece.actor.tile;
  300.  
  301.         function get_possible_moves_for_directions(directions:Array<Direction>, limit:Int) {
  302.             var possible_moves : Array<Tile> = [];
  303.             for (direction in directions) {
  304.                 var tiles : Array<Tile> = query_tiles_in_direction(curr_tile, direction, limit);
  305.                 for (tile in tiles) {
  306.                     var hit_piece : Piece = query_piece_by_tile(tile);
  307.                     if (hit_piece != null) {
  308.                         if (hit_piece.team != team) possible_moves.push(tile);
  309.                         break;
  310.                     }
  311.                     var hit_structure : Structure = query_structure_by_tile(tile);
  312.                     if (hit_structure != null) break;
  313.                     possible_moves.push(tile);
  314.                 }
  315.             }
  316.             return possible_moves;
  317.         }
  318.        
  319.         switch(kind) {
  320.             case Pawn: {
  321.                 var possible_moves : Array<Tile> = [];
  322.                 var attackable_tiles : Array<Tile> = [];
  323.                 attackable_tiles.push(tile_map.get_tile_at_offset(curr_tile, 1, 1));
  324.                 attackable_tiles.push(tile_map.get_tile_at_offset(curr_tile, -1, 1));
  325.                 for (tile in attackable_tiles) {
  326.                     var attackable_piece = query_piece_by_tile(tile);
  327.                     if (attackable_piece == null) continue;
  328.                     if (attackable_piece.team == team) continue;
  329.                     possible_moves.push(tile);
  330.                 }
  331.                 var traversable_tile = tile_map.get_tile_at_offset(curr_tile, 0, 1);
  332.                 if (traversable_tile != null) {
  333.                     if (query_piece_by_tile(traversable_tile) == null) {
  334.                         possible_moves.push(traversable_tile);
  335.                     }
  336.                 }
  337.                 valid_moves = Lambda.array(Lambda.filter(possible_moves, function(tile:Tile) {
  338.                     var piece = query_piece_by_tile(tile);
  339.                     if (piece == null) return true;
  340.                     if (piece.team == team) return false;
  341.                     return true;
  342.                 }));
  343.             };
  344.             case Knight: {
  345.                 var possible_moves : Array<Tile> = [];
  346.                 for (direction in [North, South, East, West]) {
  347.                     var tiles : Array<Tile> = query_tiles_in_direction(curr_tile, direction, 2);
  348.                     if (tiles.length < 2) continue;
  349.                     var pivot_tile : Tile = tiles[1];
  350.                     var tile_a : Tile = null;
  351.                     var tile_b : Tile = null;
  352.                     if (direction == North || direction == South) {
  353.                         tile_a = query_tile_in_direction(pivot_tile, East);
  354.                         tile_b = query_tile_in_direction(pivot_tile, West);
  355.                     } else {
  356.                         tile_a = query_tile_in_direction(pivot_tile, North);
  357.                         tile_b = query_tile_in_direction(pivot_tile, South);
  358.                     }
  359.                     if (tile_a != null) possible_moves.push(tile_a);
  360.                     if (tile_b != null) possible_moves.push(tile_b);
  361.                 }
  362.                 valid_moves = possible_moves;
  363.             };
  364.             case Rook: {
  365.                 valid_moves = get_possible_moves_for_directions(ADJACENT_NEIGHBORS, -1);
  366.             };
  367.             case Bishop: {
  368.                 valid_moves = get_possible_moves_for_directions(ADJACENT_NEIGHBORS, -1);
  369.             };
  370.             case Queen: {
  371.                 valid_moves = get_possible_moves_for_directions(MOORE_NEIGHBORS, -1);
  372.             };
  373.             case King: {
  374.                 valid_moves = get_possible_moves_for_directions(MOORE_NEIGHBORS, 1);
  375.             };
  376.             default: {
  377.                 trace('no movement pattern defined for $kind');
  378.             };
  379.         }
  380.  
  381.         // you can only move out of a structure
  382.         valid_moves = Lambda.array(Lambda.filter(valid_moves, function(tile:Tile) {
  383.             var structure = query_structure_by_tile(tile);
  384.             return structure == null;
  385.         }));
  386.  
  387.         return valid_moves;
  388.  
  389.     }
  390.  
  391.     function sim_forward() {
  392.  
  393.         trace('sim forward for turn $turns');
  394.  
  395.         var piece_values : Map<PieceKind,Int> = [
  396.             Pawn => 1,
  397.             Rook => 5,
  398.         ];
  399.  
  400.         function compare_pieces(a:Piece, b:Piece) {
  401.             // units in structures move last
  402.             var a_in_structure = is_piece_in_structure(a);
  403.             var b_in_structure = is_piece_in_structure(b);
  404.             if (a_in_structure && !b_in_structure) return 1;
  405.             else if (!a_in_structure && b_in_structure) return -1;
  406.  
  407.             // units of greatest value and then map y height
  408.             var a_value : Int = piece_values.get(a.kind);
  409.             var b_value : Int = piece_values.get(b.kind);
  410.             if (a_value > b_value) return 1;
  411.             else if (a_value == b_value) {
  412.                 if (a.actor.tile.j < b.actor.tile.j) return 1
  413.                 else if (a.actor.tile.j == b.actor.tile.j) return 0;
  414.                 else return -1;
  415.             } else return -1;
  416.         }
  417.  
  418.         haxe.ds.ArraySort.sort(pieces, compare_pieces);
  419.  
  420.         var player_tile : Tile = player.actor.tile;
  421.  
  422.         for (piece in pieces) {
  423.  
  424.             var kind : PieceKind = piece.kind;
  425.             var team : Int = piece.team;
  426.             var actor : Actor = piece.actor;
  427.  
  428.             var curr_tile : Tile = piece.actor.tile;
  429.             var next_tile : Tile = null;
  430.  
  431.             var inside_structure : Bool = query_structure_by_tile(curr_tile) != null;
  432.             if (inside_structure) {
  433.                 // no move if we need to exit our structure and
  434.                 // the player is blocking us
  435.                 var entrance_tile = tile_map.get_tile_at_offset(curr_tile, 0, 1);
  436.                 var hit_piece = query_piece_by_tile(entrance_tile);
  437.                 if (entrance_tile != player_tile && hit_piece == null) {
  438.                     next_tile = entrance_tile;
  439.                 } else {
  440.                     var src_y : Float = curr_tile.geom.pos.y;
  441.                     var dst_y : Float = src_y + (entrance_tile.geom.pos.y - src_y) * 0.2;
  442.                     Actuate.tween(actor.geom.pos, 0.1, { y: dst_y })
  443.                         .ease(luxe.tween.easing.Cubic.easeInOut)
  444.                         .onComplete(function() {
  445.                             Actuate.tween(actor.geom.pos, 0.1, { y: src_y })
  446.                                 .ease(luxe.tween.easing.Cubic.easeInOut);
  447.                         });
  448.                 }
  449.             } else {
  450.  
  451.                 var possible_moves : Array<Tile> = get_valid_moves_for_piece(piece);
  452.                 if (possible_moves.length == 0) {
  453.                     trace('no possible moves for $kind on $curr_tile');
  454.                     continue;
  455.                 }
  456.  
  457.                 possible_moves = Lambda.array(Lambda.filter(possible_moves, function(tile:Tile) {
  458.                     if (player_tile == tile && !can_move) return false;
  459.                     return true;
  460.                 }));
  461.  
  462.                 function compare_moves(a:Tile, b:Tile) {
  463.                     if (player_tile == a) return 1;
  464.                     if (player_tile == b) return -1;
  465.                     var dist_a = tile_map.get_dist_between_tiles(player_tile, a);
  466.                     var dist_b = tile_map.get_dist_between_tiles(player_tile, b);
  467.                     if (dist_a < dist_b) return 1;
  468.                     else if (dist_a == dist_b) return 0;
  469.                     else return -1;
  470.                 }
  471.                 haxe.ds.ArraySort.sort(possible_moves, compare_moves);
  472.  
  473.                 next_tile = possible_moves.pop();
  474.  
  475.             }
  476.  
  477.             if (next_tile == null) {
  478.                 trace('no move for $kind on $curr_tile');
  479.                 continue;
  480.             }
  481.  
  482.             move_actor(actor, next_tile);
  483.  
  484.             var on_end_tile : Bool = next_tile.j == tile_map.h - 1;
  485.             if (on_end_tile && kind == Pawn) {
  486.                 Luxe.timer.schedule(0.2, function() {
  487.                     set_piece_kind(piece, Queen);
  488.                     trace('promoted!');
  489.                 });
  490.             }
  491.  
  492.             // TODO: improve game over...
  493.             if (next_tile == player_tile) {
  494.                 can_move = false;
  495.                 Luxe.timer.schedule(0.2, function() {
  496.                     trace('game over');
  497.                     player.actor.geom.visible = false;
  498.                 });
  499.             }
  500.  
  501.         }
  502.  
  503.         spawn_piece();
  504.     }
  505.  
  506.     public function visualize_player_moves() {
  507.         var valid_moves = get_valid_moves_for_piece(player);
  508.         var markers = [];
  509.         for (tile in valid_moves) {
  510.             var marker = new Sprite({
  511.                 parent: root,
  512.             });
  513.             marker.color.rgb(0xff0000);
  514.             marker.size.copy_from(tile_map.tile_size);
  515.             marker.pos.copy_from(tile.geom.pos);
  516.             markers.push(marker);
  517.         }
  518.         Luxe.timer.schedule(0.5, function() {
  519.             for (marker in markers) marker.destroy();
  520.         });
  521.     }
  522.  
  523.     // input
  524.     function ondragdown(e:DragEvent) {
  525.  
  526.         // if i want to move to tapping tiles and dragging...
  527.         // lets start by only supporting tap (less fatigueing)
  528.         // so you tap on a tile and if it is valid then
  529.         // it works
  530.  
  531.         if (!can_move) return;
  532.         if (drag != null) return;
  533.  
  534.         var valid_moves = get_valid_moves_for_piece(player);
  535.         // check if we are focusing any particular tile..
  536.  
  537.         drag = {
  538.             id: e.id,
  539.             pos: e.pos,
  540.             origin: e.pos,
  541.             delta: new Vector(),
  542.             elapsed: 0,
  543.             data: valid_moves,
  544.         }
  545.     }
  546.  
  547.     function ondragmove(e:DragEvent) {
  548.  
  549.         if (drag == null) return;
  550.  
  551.         var delta : Vector = Vector.Subtract(e.pos, drag.pos);
  552.         var moved : Vector = Vector.Subtract(e.pos, drag.origin);
  553.         drag.pos = e.pos;
  554.  
  555.         var deadzone : Float = 32;
  556.         if (moved.length < deadzone) return;
  557.  
  558.         var theta : Float = moved.angle2D;
  559.         var cardinal : Float = Utils.nearest_cardinal_radians(theta);
  560.         var i : Int = player.actor.tile.i;
  561.         var j : Int = player.actor.tile.j;
  562.         var di : Int = Math.round(Math.cos(cardinal));
  563.         var dj : Int = Math.round(Math.sin(cardinal));
  564.  
  565.         var new_tile : Tile = null;
  566.         var direction = get_direction_from_components(di, dj);
  567.         var tiles = query_tiles_in_direction(player.actor.tile, direction);
  568.         for (tile in tiles) {
  569.             if (query_structure_by_tile(tile) != null) {
  570.                 break;
  571.             }
  572.             if (query_piece_by_tile(tile) != null) {
  573.                 new_tile = tile;
  574.                 break;
  575.             }
  576.             new_tile = tile;
  577.         }
  578.  
  579.         var t : Float = Math.min((moved.length - deadzone) / 170, 1);
  580.         t = Math.pow(t, 0.5);
  581.         var vec_to_tile : Vector = new Vector(di * 128, dj * 128);
  582.         var displacement : Vector = Vector.Multiply(vec_to_tile, t * 0.1);
  583.  
  584.         /*
  585.         player.actor.geom.pos = Vector.Add(player.actor.tile.geom.pos, displacement);
  586.  
  587.         if (new_tile == null) {
  588.             var squash : Vector = Vector.Multiply(vec_to_tile.normalized, 0.2 * t);
  589.             player.actor.geom.scale = new Vector(1 - Math.abs(squash.x), 1 - Math.abs(squash.y));
  590.         } else {
  591.             Actuate.tween(player.actor.geom.scale, 0.2, new Vector(1.0, 1.0));
  592.         }
  593.         */
  594.  
  595.     }
  596.     function ondragup(e:DragEvent) {
  597.        
  598.         if (drag == null) return;
  599.  
  600.         Actuate.tween(player.actor.geom.scale, 0.2, new Vector(1.0, 1.0));
  601.  
  602.         var moved : Vector = Vector.Subtract(e.pos, drag.origin);
  603.         var elapsed : Float = drag.elapsed;
  604.  
  605.  
  606.    
  607.         var valid_moves : Array<Tile> = cast drag.data;
  608.         var hit_tile : Tile = tile_map.get_tile_at_pos(drag.pos);
  609.         var new_tile : Tile = null;
  610.         for (tile in valid_moves) {
  611.             if (tile == hit_tile) {
  612.                 new_tile = hit_tile;
  613.                 break;
  614.             }
  615.         }
  616.  
  617.         drag = null;
  618.  
  619.         if (new_tile == null) return;
  620.  
  621.         var hit_piece : Piece = query_piece_by_tile(new_tile);
  622.         if (hit_piece != null) {
  623.             pieces.remove(hit_piece);
  624.             Luxe.timer.schedule(0.2, function() {
  625.                 hit_piece.actor.geom.visible = false;
  626.             });
  627.         }
  628.  
  629.         move_actor(player.actor, new_tile);
  630.         turns++;
  631.         Luxe.timer.schedule(0.3, sim_forward);
  632.  
  633.  
  634.  
  635.  
  636.  
  637.         var deadzone : Float = 32;
  638.         if (elapsed < 0.1) deadzone = 64;
  639.         if (elapsed > 0.3) deadzone = 128;
  640.  
  641.         if (moved.length < deadzone) {
  642.  
  643.             return;
  644.  
  645.             /*
  646.             var tile : Tile = tile_map.get_tile_at_pos(drag.pos);
  647.             if (tile == null) {
  648.                 Actuate.tween(player.actor.geom.pos, 0.1, player.actor.tile.geom.pos);
  649.                 return;
  650.             }
  651.  
  652.  
  653.             var hit_piece : Piece = query_piece_by_tile(tile);
  654.             if (hit_piece != null && hit_piece != player) {
  655.                 pieces.remove(hit_piece);
  656.                 Luxe.timer.schedule(0.2, function() {
  657.                     hit_piece.actor.geom.visible = false;
  658.                 });
  659.             }
  660.  
  661.  
  662.             turns++;
  663.             Luxe.timer.schedule(0.2, sim_forward);
  664.             move_actor(player.actor, tile);
  665.                     return;
  666.  
  667.                     */
  668.         }
  669.  
  670.         /*
  671.  
  672.  
  673.         var theta : Float = moved.angle2D;
  674.         var cardinal : Float = Utils.nearest_cardinal_radians(theta);
  675.  
  676.         var i : Int = player.actor.tile.i;
  677.         var j : Int = player.actor.tile.j;
  678.         var di : Int = Math.round(Math.cos(cardinal));
  679.         var dj : Int = Math.round(Math.sin(cardinal));
  680.  
  681.         var new_tile : Tile = null;
  682.         var direction = get_direction_from_components(di, dj);
  683.         var tiles = query_tiles_in_direction(player.actor.tile, direction);
  684.         for (tile in tiles) {
  685.             if (query_structure_by_tile(tile) != null) {
  686.                 break;
  687.             }
  688.             var hit_piece : Piece = query_piece_by_tile(tile);
  689.             if (hit_piece != null) {
  690.                 new_tile = tile;
  691.                 break;
  692.             }
  693.             new_tile = tile;
  694.         }
  695.  
  696.         if (new_tile == null) {
  697.             Actuate.tween(player.actor.geom.pos, 0.1, player.actor.tile.geom.pos);
  698.             return;
  699.         }
  700.  
  701.         var hit_piece : Piece = query_piece_by_tile(new_tile);
  702.         if (hit_piece != null) {
  703.             pieces.remove(hit_piece);
  704.             Luxe.timer.schedule(0.2, function() {
  705.                 hit_piece.actor.geom.visible = false;
  706.             });
  707.         }
  708.  
  709.         // sometimes we may want this to be slower?
  710.         move_actor(player.actor, new_tile);
  711.         turns++;
  712.         Luxe.timer.schedule(0.3, sim_forward);
  713.  
  714.         */
  715.  
  716.     }
  717.  
  718.     // upkeep
  719.     public function update(dt:Float) {
  720.         if (drag != null) drag.elapsed += dt;
  721.     }
  722.  
  723.     function arrange() {
  724.         var tile_map_w : Float = tile_map.w * tile_map.tile_size.x;
  725.         var tile_map_h : Float = tile_map.h * tile_map.tile_size.y;
  726.         var w : Float = Luxe.screen.width;
  727.         var h : Float = Luxe.screen.height;
  728.         var pad : Float = Math.min(w, h) * 0.1;
  729.         var s : Float = Math.min((w - pad) /tile_map_w, (h - pad) / tile_map_h);
  730.         root.scale.set_xy(s, s);
  731.  
  732.     }
  733.  
  734.     function onresize(_) arrange();
  735.  
  736. }
  737.  
  738. class Tile {
  739.     public function new() {}
  740.     public function toString() return '($i, $j)';
  741.     public var tile_map : TileMap;
  742.     public var i : Int;
  743.     public var j : Int;
  744.     public var geom : Sprite;
  745. }
  746.  
  747. enum Direction {
  748.     North;
  749.     South;
  750.     East;
  751.     West;
  752.     NorthEast;
  753.     NorthWest;
  754.     SouthEast;
  755.     SouthWest;
  756. }
  757.  
  758. class TileMap {
  759.  
  760.     public var tiles_array : Array<Tile>;
  761.     public var tiles_indexed : Array<Array<Tile>>;
  762.     public var tile_size : Vector;
  763.     public var w : Int;
  764.     public var h : Int;
  765.  
  766.     public function new() {}
  767.  
  768.     public function init_tiles_to_size(w:Int, h:Int) {
  769.         this.w = w;
  770.         this.h = h;
  771.         tiles_indexed = [];
  772.         tiles_array = [];
  773.         for (i in 0...w) {
  774.             var row : Array<Tile> = [];
  775.             for (j in 0...h) {
  776.                 var tile = new Tile();
  777.                 var pos = new Vector();
  778.                 tile.i = i;
  779.                 tile.j = j;
  780.                 tiles_array.push(tile);
  781.                 row.push(tile);
  782.             }
  783.             tiles_indexed.push(row);
  784.         }
  785.         for (tile in tiles_array) {
  786.             var i : Int = tile.i;
  787.             var j : Int = tile.j;
  788.             var geom = new Sprite({
  789.                 name: '$i,$j}',
  790.             });
  791.             tile.geom = geom;
  792.         }
  793.     }
  794.  
  795.     public function get_tile_at_pos(pos:Vector):Tile {
  796.         for (tile in tiles_array) {
  797.             if (tile.geom.point_inside(pos))  return tile;
  798.         }
  799.         return null;
  800.     }
  801.  
  802.     public function get_tile_at_index(i:Int, j:Int):Tile {
  803.         if (i < 0 || j < 0) return null;
  804.         if (i > w - 1 || j > h - 1) return null;
  805.         var row : Array<Tile> = tiles_indexed[i];
  806.         return row[j];
  807.     }
  808.     public function get_tile_at_offset(tile:Tile, di:Int, dj:Int) {
  809.         return get_tile_at_index(tile.i + di, tile.j + dj);
  810.     }
  811.     public function get_dist_between_tiles(a:Tile, b:Tile):Float {
  812.         var dist : Float = Math.sqrt((a.i - b.i)^2 + (a.j - b.j)^2);
  813.         return dist;
  814.     }
  815. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement