Advertisement
Guest User

Untitled

a guest
Mar 27th, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.91 KB | None | 0 0
  1. 03-27 01:14:59.567 19696-19696/com.example.sam.matrix D/ViewRootImpl﹕ ViewPostImeInputStage ACTION_DOWN
  2. 03-27 01:14:59.567 19696-19696/com.example.sam.matrix I/System.out﹕ TOUCHED BOARD X: 206
  3. 03-27 01:14:59.567 19696-19696/com.example.sam.matrix I/System.out﹕ TOUCHED BOARD Y: 190
  4. 03-27 01:14:59.567 19696-19696/com.example.sam.matrix I/System.out﹕ POSITION OF off X = 3
  5. 03-27 01:14:59.567 19696-19696/com.example.sam.matrix I/System.out﹕ POSITION OF off Y = 0
  6. 03-27 01:14:59.567 19696-19696/com.example.sam.matrix I/System.out﹕ POSITION OF pix X = 165
  7. 03-27 01:14:59.567 19696-19696/com.example.sam.matrix I/System.out﹕ POSITION OF pix Y = 0
  8. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ _____________CELL TEMP_____________
  9. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ CELL Shape = 6
  10. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ CELL pixX = 165
  11. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ CELL pixY = 0
  12. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ CELL offX = 3
  13. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ CELL offY = 0
  14. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ _____________BEFORE SWAP_____________
  15. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ _____________CELL TOUCH_____________
  16. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ board[col][row] Shape = 6
  17. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ board[col][row] pixX = 165
  18. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ board[col][row] pixY = 0
  19. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ board[col][row] offX = 3
  20. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ board[col][row] offY = 0
  21. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ _____________CELL TOUCH SWAP_____________
  22. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ board[col+horzDelta][row] Shape = 6
  23. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ board[col+horzDelta][row] pixX = 110
  24. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ board[col+horzDelta][row] pixY = 0
  25. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ board[col+horzDelta][row] offX = 2
  26. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ board[col+horzDelta][row] offY = 0
  27. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ _____________AFTER SWAP_____________
  28. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ _____________CELL TOUCH_____________
  29. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ board[col][row] Shape = 6
  30. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ board[col][row] pixX = 110
  31. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ board[col][row] pixY = 0
  32. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ board[col][row] offX = 2
  33. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ board[col][row] offY = 0
  34. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ _____________CELL TOUCH SWAP_____________
  35. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ board[col+horzDelta][row] Shape = 6
  36. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ board[col+horzDelta][row] pixX = 165
  37. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ board[col+horzDelta][row] pixY = 0
  38. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ board[col+horzDelta][row] offX = 3
  39. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ board[col+horzDelta][row] offY = 0
  40.  
  41. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ ____________ RENDERING ____________
  42. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ _____________CELL RENDER swapped left_____________
  43. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ BOARD Shape = 6
  44. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ BOARD PIX X START = 110
  45. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ BOARD PIX Y START = 0
  46. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ BOARD offX = 2
  47. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ BOARD offY = 0
  48. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ _____________CELL RENDER swapped right_____________
  49. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ BOARD Shape = 6
  50. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ BOARD PIX X START = 165
  51. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ BOARD PIX Y START = 0
  52. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ BOARD offX = 3
  53. 03-27 01:14:59.817 19696-19717/com.example.sam.matrix I/System.out﹕ BOARD offY = 0
  54.  
  55. public class Cell {
  56. //which shape the cell will consist
  57. private int shape;
  58. //offset of where the cell is located by cell number -> need to translate the given coordinates to pixel
  59. private int pixX, pixY;
  60. private int offX, offY;
  61.  
  62. public Cell(int shape, int pixX, int pixY, int offX, int offY){
  63. this.shape = shape;
  64. this.pixX = pixX;
  65. this.pixY = pixY;
  66. this.offX = offX;
  67. this.offY = offY;
  68. }
  69.  
  70. public class Board {
  71. private int col = 8, row= 1;
  72. private Cell[][] board;
  73.  
  74. public void randomBoard(){
  75. board = new Cell[col][row];
  76. //Initialize board with cells
  77. for (int r = 0 ; r<row; r++){
  78. for(int c = 0; c< col; c++){
  79. board[c][r] = new Cell(rand.getRandIntBetween(0,6), translateOffsetToPixel(c,r).getX(), translateOffsetToPixel(c,r).getY(),c,r);
  80. }
  81. }
  82. }
  83.  
  84. public void swap( int col, int row, int horzDelta, int vertDelta ){
  85. Cell temp;
  86. temp = board[col][row];
  87.  
  88. //left swap
  89. if(horzDelta == -1 || horzDelta == 1){
  90. board[col][row] = board[col+horzDelta][row];
  91. board[col+horzDelta][row] =temp;
  92. }
  93.  
  94. }
  95.  
  96. public class PlayState extends State{
  97. private Board board;
  98.  
  99. @Override
  100. public void render(Painter g) {
  101. // g.setBackground(Color.WHITE);
  102. g.setColor(Color.rgb(255, 255, 255));
  103. g.fillRect(0, 0, GameMainActivity.GAME_WIDTH, GameMainActivity.GAME_HEIGHT);
  104. renderBoard(g);
  105.  
  106. }
  107.  
  108.  
  109. public void renderBoard(Painter g) {
  110. // g.drawImage(Assets.circle50 ,board.getBoard()[0][0].getX(), board.getBoard()[0][0].getY(), board.getBoard()[0][0].getX()+25,board.getBoard()[0][0].getY()+25 );
  111.  
  112. for(int r = 0; r<board.getRow(); r++){
  113. for(int c = 0; c<board.getCol(); c++){
  114. renderCell(g,c,r);
  115. }
  116. }
  117.  
  118.  
  119. }
  120.  
  121. public void renderCell( Painter g, int col, int row){
  122. if(board.getSwapLeft() &&
  123. col == touchedCell.getOffX() &&
  124. row == touchedCell.getOffY() &&
  125. touchedCell.getPixX() != 10 )
  126. {
  127. //LEFT SWIPE
  128. board.swap(col, row, -1,0);
  129. board.setSwapLeft(false);
  130.  
  131. }
  132.  
  133.  
  134. switch(board.getBoard()[col][row].getShape()){
  135. case(0):
  136. g.drawImage(Assets.circle50 ,board.getBoard()[col][row].getPixX()+SHIFT_RIGHT, board.getBoard()[col][row].getPixY()+SHIFT_DOWN,
  137. board.getBoard()[0][0].getPixX()+SHAPE_WIDTH,board.getBoard()[0][0].getPixY()+SHAPE_HEIGHT );
  138. break;
  139. case(1):
  140. g.drawImage(Assets.diamond50 ,board.getBoard()[col][row].getPixX()+SHIFT_RIGHT, board.getBoard()[col][row].getPixY()+SHIFT_DOWN,
  141. board.getBoard()[0][0].getPixX()+SHAPE_WIDTH,board.getBoard()[0][0].getPixY()+SHAPE_HEIGHT );
  142. break;
  143. case(2):
  144. g.drawImage(Assets.heart50 ,board.getBoard()[col][row].getPixX()+SHIFT_RIGHT, board.getBoard()[col][row].getPixY()+SHIFT_DOWN,
  145. board.getBoard()[0][0].getPixX()+SHAPE_WIDTH,board.getBoard()[0][0].getPixY()+SHAPE_HEIGHT );
  146. break;
  147. case(3):
  148. g.drawImage(Assets.pentagon50 ,board.getBoard()[col][row].getPixX()+SHIFT_RIGHT, board.getBoard()[col][row].getPixY()+SHIFT_DOWN,
  149. board.getBoard()[0][0].getPixX()+SHAPE_WIDTH,board.getBoard()[0][0].getPixY()+SHAPE_HEIGHT );
  150. break;
  151.  
  152.  
  153. @Override
  154. public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
  155.  
  156. if (e.getAction() == MotionEvent.ACTION_DOWN && board.touchBoard(scaledX, scaledY) ) {
  157. recentTouchY = scaledY;
  158. recentTouchX = scaledX;
  159. System.out.println("TOUCHED BOARD X: "+ scaledX);
  160. System.out.println("TOUCHED BOARD Y: "+ scaledY);
  161. board.whichCell(scaledX,scaledY);
  162.  
  163. touchedCell = board.whichCellTouched(scaledX,scaledY);
  164. } else if (e.getAction() == MotionEvent.ACTION_UP) {
  165.  
  166. //swipe event
  167. if (scaledX - recentTouchX < -25) {
  168. board.setSwapLeft(true);
  169. }
  170. /*
  171. else if (scaledY - recentTouchY > 50) {
  172. player.duck();
  173. }
  174. */
  175. }
  176. return true;
  177. }
  178.  
  179. /*
  180. * GameView:
  181. * The central class for our game, replaces the Game class
  182. * GameView will host our game loop, and will have methods to start and exit out of our game
  183. */
  184.  
  185. /*
  186. * Inherit from SurfaceView to have on activity for the entire program -> have whichever state
  187. */
  188. public class GameView extends SurfaceView implements Runnable {
  189.  
  190. private Bitmap gameImage;
  191. private Rect gameImageSrc;
  192. private Rect gameImageDst;
  193. private Canvas gameCanvas;
  194. private Painter graphics;
  195.  
  196. private Thread gameThread;
  197. private volatile boolean running = false;
  198. private volatile State currentState;
  199.  
  200. private InputHandler inputHandler;
  201.  
  202. public GameView(Context context, int gameWidth, int gameHeight) {
  203. super(context);
  204. gameImage = Bitmap.createBitmap(gameWidth, gameHeight,
  205. Bitmap.Config.RGB_565);
  206. gameImageSrc = new Rect(0, 0, gameImage.getWidth(),
  207. gameImage.getHeight());
  208. gameImageDst = new Rect();
  209. gameCanvas = new Canvas(gameImage);
  210. graphics = new Painter(gameCanvas);
  211. SurfaceHolder holder = getHolder();
  212. holder.addCallback(new Callback() {
  213.  
  214. @Override
  215. public void surfaceCreated(SurfaceHolder holder) {
  216. initInput();
  217. if (currentState == null) {
  218. setCurrentState(new LoadState());
  219. }
  220. initGame();
  221. }
  222.  
  223. @Override
  224. public void surfaceChanged(SurfaceHolder holder, int format,
  225. int width, int height) {
  226. // TODO Auto-generated method stub
  227. }
  228.  
  229. @Override
  230. public void surfaceDestroyed(SurfaceHolder holder) {
  231. pauseGame();
  232. }
  233.  
  234. });
  235. }
  236.  
  237. public void setCurrentState(State newState) {
  238. System.gc();
  239. newState.init();
  240. currentState = newState;
  241. inputHandler.setCurrentState(currentState);
  242. }
  243.  
  244. private void initInput() {
  245. if (inputHandler == null) {
  246. inputHandler = new InputHandler();
  247. }
  248. setOnTouchListener(inputHandler);
  249. }
  250.  
  251. private void initGame() {
  252. running = true;
  253. gameThread = new Thread(this, "Game Thread");
  254. gameThread.start();
  255. }
  256.  
  257. private void pauseGame() {
  258. running = false;
  259. while (gameThread.isAlive()) {
  260. try {
  261. gameThread.join();
  262. break;
  263. } catch (InterruptedException e) {
  264. }
  265. }
  266. }
  267.  
  268. private void updateAndRender(long delta) {
  269. currentState.update(delta / 1000f);
  270. currentState.render(graphics);
  271. renderGameImage();
  272. }
  273.  
  274. private void renderGameImage() {
  275. Canvas screen = getHolder().lockCanvas();
  276. if (screen != null) {
  277. screen.getClipBounds(gameImageDst);
  278. screen.drawBitmap(gameImage, gameImageSrc, gameImageDst, null);
  279. getHolder().unlockCanvasAndPost(screen);
  280. }
  281. }
  282. /*
  283. * Implementing the Game Loop Thread
  284. */
  285. public void run() {
  286. long updateDurationMillis = 0;
  287. long sleepDurationMillis = 0;
  288. while (running) {
  289. long beforeUpdateRender = System.nanoTime();
  290. long deltaMillis = sleepDurationMillis + updateDurationMillis;
  291. updateAndRender(deltaMillis);
  292. updateDurationMillis = (System.nanoTime() - beforeUpdateRender) / 1000000L;
  293. sleepDurationMillis = Math.max(2, 17 - updateDurationMillis);
  294. try {
  295. Thread.sleep(sleepDurationMillis);
  296. } catch (Exception e) {
  297. e.printStackTrace();
  298. }
  299. }
  300. }
  301.  
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement