Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.93 KB | None | 0 0
  1. package com.arsaki.deadescape.views.menu;
  2.  
  3.  
  4. import com.arsaki.deadescape.actors.Block;
  5. import com.arsaki.deadescape.ext.DarkGroup;
  6. import com.arsaki.deadescape.xam.extension.XBackground;
  7. import com.arsaki.deadescape.xam.extension.XGroup;
  8. import com.arsaki.deadescape.xam.extension.XImage;
  9. import com.arsaki.deadescape.xam.extension.XStage;
  10. import com.arsaki.deadescape.xam.extension.XUtils;
  11. import com.badlogic.gdx.graphics.Color;
  12. import com.badlogic.gdx.math.MathUtils;
  13. import com.badlogic.gdx.scenes.scene2d.InputEvent;
  14. import com.badlogic.gdx.scenes.scene2d.ui.Table;
  15. import com.badlogic.gdx.scenes.scene2d.utils.DragListener;
  16. import com.badlogic.gdx.utils.Align;
  17. import com.badlogic.gdx.utils.Array;
  18.  
  19.  
  20. /**
  21.  * Created by Kharin on 07.12.17.
  22.  */
  23.  
  24. public class MenuView extends XGroup {
  25.  
  26.     Block[][] matrix;
  27.     MenuView instance;
  28.     XBackground background = new XBackground("main_menu_back");
  29.     int GAME_FIELD_WIDTH = 4;
  30.     int GAME_FIELD_HEIGHT = 5;
  31.  
  32.  
  33.     XGroup map = new XGroup(XStage.w, XStage.h);
  34.     Array<Block> pickBlocks = new Array<Block>();
  35.  
  36.     public MenuView() {
  37.         super(XStage.w, XStage.h);
  38.         instance = this;
  39.         addActor(background);
  40.         background.toCenter();
  41.  
  42.         matrix = new Block[GAME_FIELD_WIDTH][GAME_FIELD_HEIGHT];
  43.  
  44.  
  45.         for (int i = 0; i < GAME_FIELD_WIDTH; i++) {
  46.             for (int j = 0; j < GAME_FIELD_HEIGHT; j++) {
  47.                 Block cell = new Block(i+":"+j);
  48.                 matrix[i][j] = cell;
  49.                 map.addActor(cell);
  50.                 cell.setPosition(cell.getWidth()/2 + i * cell.getWidth(), cell.getWidth()/2 + j * cell.getHeight(), Align.center);
  51.             }
  52.         }
  53.  
  54.  
  55.         addActor(map);
  56.         map.setSize(128 * 4, 128*5);
  57.         map.toCenter();
  58.  
  59.         map.addListener(new DragListener() {
  60.             @Override
  61.             public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
  62.                 int indexX = (int) x / 128;
  63.                 int indexY = (int) y / 128;
  64.                 indexX = MathUtils.clamp(indexX,0,GAME_FIELD_WIDTH-1);
  65.                 indexY = MathUtils.clamp(indexY,0,GAME_FIELD_HEIGHT-1);
  66.                 addBlock(matrix[indexX][indexY]);
  67.                 return super.touchDown(event, x, y, pointer, button);
  68.             }
  69.  
  70.             @Override
  71.             public void touchDragged(InputEvent event, float x, float y, int pointer) {
  72.                 int indexX = (int) x / 128;
  73.                 int indexY = (int) y / 128;
  74.                 indexX = MathUtils.clamp(indexX,0,GAME_FIELD_WIDTH-1);
  75.                 indexY = MathUtils.clamp(indexY,0,GAME_FIELD_HEIGHT-1);
  76.                 addBlock(matrix[indexX][indexY]);
  77.                 super.dragStart(event, x, y, pointer);
  78.                 super.touchDragged(event, x, y, pointer);
  79.             }
  80.  
  81.             @Override
  82.             public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
  83.                 onTouchUp();
  84.                 super.touchUp(event, x, y, pointer, button);
  85.             }
  86.         });
  87.  
  88.     }
  89.  
  90.     private void onTouchUp() {
  91.         for(Block[] blocs: matrix) {
  92.             for(Block bloc: blocs)
  93.                 bloc.setColor(Color.RED);
  94.         }
  95.         pickBlocks.clear();
  96.     }
  97.  
  98.     private void addBlock(Block block) {
  99.         if(pickBlocks.contains(block, true)) {
  100.                 resetBlocks(block);
  101.         }else {
  102.             pickBlocks.add(block);
  103.             block.setColor(Color.GREEN);
  104.             XUtils.log("pickBlocks "+pickBlocks.size);
  105.         }
  106.     }
  107.  
  108.     private void resetBlocks(Block block) {
  109.         int index = pickBlocks.indexOf(block, true);
  110.         for(int i = index+1; i < pickBlocks.size; i++) {
  111.             pickBlocks.get(i).setColor(Color.WHITE);
  112.             pickBlocks.removeValue(pickBlocks.get(i), true);
  113.             XUtils.log("pickBlocks "+pickBlocks.size);
  114.         }
  115.     }
  116.    
  117.     public void dispose() {
  118.  
  119.     }
  120.  
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement