Advertisement
kemkriszt

Circuits

Jun 30th, 2014
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.24 KB | None | 0 0
  1. package hu.cig.vob;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.util.ArrayList;
  8. import java.util.Calendar;
  9. import java.util.Collections;
  10. import java.util.Iterator;
  11. import java.util.List;
  12.  
  13. import android.content.Context;
  14. import android.graphics.Bitmap;
  15. import android.graphics.BitmapFactory;
  16. import android.graphics.Canvas;
  17. import android.graphics.Color;
  18. import android.graphics.Paint;
  19. import android.graphics.Paint.Align;
  20. import android.graphics.Point;
  21. import android.graphics.Rect;
  22. import android.graphics.Typeface;
  23. import android.view.Display;
  24. import android.view.View;
  25. import android.view.WindowManager;
  26. import cug.hu.vob.R;
  27.  
  28. public class LevelView extends View {
  29.  
  30.     public static double intScreenWidth;
  31.     public static double screenWidth,screenHeight;
  32.     public static float screenBottom;
  33.  
  34.     private Paint p;
  35.     private int level;
  36.     private List<Block> bgBlocks = Collections
  37.             .synchronizedList(new ArrayList<Block>());
  38.     private List<Block> fgBlocks = Collections
  39.             .synchronizedList(new ArrayList<Block>());
  40.     private List<Enemy> enemys = Collections
  41.             .synchronizedList(new ArrayList<Enemy>());
  42.     private Context context;
  43.     private Robot robot;
  44.     private boolean isRunning = true, gameOver = false;
  45.     private Rect r = new Rect();
  46.  
  47.     /*** --------------------------------------------------------------- ***/
  48.     public LevelView(Context context, int level, WindowManager wmg) {
  49.         super(context);
  50.         p = new Paint();
  51.  
  52.         Bitmap robotIcon = BitmapFactory.decodeResource(context.getResources(),
  53.                 R.drawable.char1);
  54.  
  55.         Display display = wmg.getDefaultDisplay();
  56.         Point size = new Point();
  57.         display.getSize(size);
  58.         intScreenWidth = size.x / 2.5;
  59.         screenBottom = size.y - Helper.convertDpi_Px(context, 40);
  60.         screenWidth = size.x;
  61.         screenHeight = size.y;
  62.  
  63.         robot = new Robot(2, 2, robotIcon, screenBottom, context);
  64.  
  65.         MyThread th = new MyThread();
  66.         th.start();
  67.  
  68.         this.level = level;
  69.         this.context = context;
  70.  
  71.         try {
  72.             loadMap();
  73.  
  74.         } catch (IOException e) {
  75.         }
  76.         try {
  77.             loadEntity();
  78.         } catch (Exception e) {
  79.         }
  80.     }
  81.  
  82.     /*** --------------------------------------------------------------- ***/
  83.     private void loadMap() throws IOException {
  84.         ArrayList<String> lines = new ArrayList<String>();
  85.  
  86.         InputStream iStream;
  87.         switch (level) {
  88.         case 0:
  89.             iStream = context.getResources().openRawResource(R.raw.level0);
  90.             break;
  91.         default:
  92.             iStream = null;
  93.             break;
  94.         }
  95.  
  96.         BufferedReader reader = new BufferedReader(new InputStreamReader(
  97.                 iStream));
  98.         while (true) {
  99.             String line = reader.readLine();
  100.             if (line == null) {
  101.                 break;
  102.             }
  103.             if (!line.startsWith("!")) {
  104.                 lines.add(line);
  105.  
  106.             }
  107.         }
  108.  
  109.         for (int i = 0; i < lines.size(); i++) {
  110.             String currLine = lines.get(i);
  111.  
  112.             for (int j = 0; j < currLine.length(); j++) {
  113.  
  114.                 int type = Character.getNumericValue(currLine.charAt(j));
  115.                 boolean foreground = false;
  116.  
  117.                 Bitmap icon = null;
  118.  
  119.                 switch (type) {
  120.                 case 1:
  121.                     icon = BitmapFactory.decodeResource(context.getResources(),
  122.                             R.drawable.block1);
  123.                     foreground = true;
  124.                     break;
  125.                 case 2:
  126.                     icon = BitmapFactory.decodeResource(context.getResources(),
  127.                             R.drawable.block2);
  128.                     foreground = true;
  129.                     break;
  130.                 case 3:
  131.                     icon = BitmapFactory.decodeResource(context.getResources(),
  132.                             R.drawable.block3);
  133.                     foreground = false;
  134.                     break;
  135.                 case 4:
  136.                     icon = BitmapFactory.decodeResource(context.getResources(),
  137.                             R.drawable.block4);
  138.                     foreground = false;
  139.                     break;
  140.  
  141.                 }
  142.  
  143.                 Block b = new Block(j, i, type, icon, foreground);
  144.                 if (!foreground) {
  145.                     bgBlocks.add(b);
  146.                 } else {
  147.                     fgBlocks.add(b);
  148.                 }
  149.             }
  150.         }
  151.     }
  152.  
  153.     /*** --------------------------------------------------------------- ***/
  154.    
  155.  
  156.  
  157.     private void loadEntity() throws IOException {
  158.         ArrayList<String> lines = new ArrayList<String>();
  159.         InputStream iStream = null;
  160.         switch (level) {
  161.         case 0:
  162.             iStream = context.getResources().openRawResource(R.raw.entity0);
  163.             break;
  164.         }
  165.  
  166.         BufferedReader reader = new BufferedReader(new InputStreamReader(
  167.                 iStream));
  168.         while (true) {
  169.             String line = reader.readLine();
  170.             if (line == null) {
  171.                 break;
  172.             }
  173.             if (!line.startsWith("!")) {
  174.                 lines.add(line);
  175.  
  176.             }
  177.         }
  178.  
  179.         for (int i = 0; i < lines.size(); i++) {
  180.             String currLine = lines.get(i);
  181.  
  182.             for (int j = 0; j < currLine.length(); j++) {
  183.                 int type = Character.getNumericValue(currLine.charAt(j));
  184.                 Bitmap icon = null;
  185.  
  186.                 switch (type) {
  187.                 case 1:
  188.                     icon = BitmapFactory.decodeResource(context.getResources(),
  189.                             R.drawable.enemy);
  190.                     break;
  191.                 }
  192.  
  193.                 if (icon != null) {
  194.                     Enemy e = new Enemy(j, i, icon, screenBottom, context);
  195.                     enemys.add(e);
  196.                 }
  197.             }
  198.         }
  199.     }
  200.  
  201.     /*** --------------------------------------------------------------- ***/
  202.  
  203.     public void update(long elpsedTime) {
  204.         if (!gameOver) {
  205.             robot.update(fgBlocks);
  206.            
  207.             if(robot.getY()>screenBottom){
  208.                 robot.degreeseHealth(100);
  209.                 isRunning = false;
  210.             }
  211.  
  212.             for (int i = 0; i < bgBlocks.size(); i++) {
  213.                 bgBlocks.get(i).update(robot);
  214.             }
  215.  
  216.             for (int i = 0; i < fgBlocks.size(); i++) {
  217.                 fgBlocks.get(i).update(robot);
  218.             }
  219.  
  220.             for (Enemy e : enemys) {
  221.                 e.update(robot, Calendar.getInstance().getTimeInMillis());
  222.             }
  223.  
  224.             List<Bullet> blts = robot.getBullets();
  225.             if (blts.size() > 0 && enemys.size() > 0) {
  226.                 Iterator<Bullet> it_b = blts.iterator();
  227.                 Iterator<Enemy> it_e = enemys.iterator();
  228.                 while (it_e.hasNext()) {
  229.                     Enemy e = it_e.next();
  230.                     it: while (it_b.hasNext()) {
  231.                         Bullet b = it_b.next();
  232.                         if (b.getRect().intersect(e.getRect())) {
  233.                             e.degreesHealth(20);
  234.                             it_b.remove();
  235.                             if (e.getHealth() <= 0) {
  236.                                 it_e.remove();
  237.                                 break it;
  238.                             }
  239.                         }
  240.                     }
  241.                 }
  242.             }
  243.  
  244.             if (enemys.size() > 0) {
  245.                 Iterator<Enemy> it_e = enemys.iterator();
  246.                 itt: while (it_e.hasNext()) {
  247.                     Enemy e = it_e.next();
  248.                     blts = e.getBullets();
  249.                     Iterator<Bullet> it_b = blts.iterator();
  250.                     while (it_b.hasNext()) {
  251.                         Bullet b = it_b.next();
  252.                         if (b.getRect().intersect(robot.getHRect())) {
  253.                             it_b.remove();
  254.                             robot.degreeseHealth(10);
  255.                             if (robot.getHealth() <= 0) {
  256.                                 isRunning = false;
  257.                                 break itt;
  258.                             }
  259.                         }
  260.                     }
  261.                 }
  262.             }
  263.         }
  264.         postInvalidate();
  265.     }
  266.  
  267.     /*** --------------------------------------------------------------- ***/
  268.  
  269.     @Override
  270.     protected void onDraw(Canvas canvas) {
  271.         super.onDraw(canvas);
  272.         if (!gameOver) {
  273.  
  274.             paintBgBlock(canvas);
  275.             paintBullets(canvas);
  276.             canvas.drawBitmap(robot.getIcon(), robot.getX(), robot.getY(), p);
  277.  
  278.             paintEnemy(canvas);
  279.             paintFgBlock(canvas);
  280.         }else{
  281.             canvas.drawPaint(p);
  282.             p.setColor(Color.BLACK);
  283.             canvas.drawRect(0, 0,(float)screenWidth,(float)screenHeight,p);
  284.            
  285.             p.setColor(Color.GREEN);
  286.             p.setTextSize(70f);
  287.             p.setTypeface(Typeface.DEFAULT_BOLD);
  288.             p.setTextAlign(Align.LEFT);
  289.            
  290.             String text = "Game Over";
  291.             p.getTextBounds(text,0,text.length(),r);
  292.            
  293.             canvas.drawText(text, r.left+20, -2*r.top+20, p);
  294.             p.setTextSize(30);
  295.             canvas.drawText("Tap to continue", r.left+20, -3*r.top+20, p);
  296.            
  297.         }
  298.     }
  299.  
  300.     /*** --------------------------------------------------------------- ***/
  301.     private void paintEnemy(Canvas c) {
  302.         for (Enemy e : enemys) {
  303.             c.drawBitmap(e.getIcon(), e.getX(), e.getY(), p);
  304.  
  305.         }
  306.     }
  307.  
  308.     /*** --------------------------------------------------------------- ***/
  309.     private void paintBullets(Canvas c) {
  310.         List<Bullet> blts = robot.getBullets();
  311.         p.setColor(Color.YELLOW);
  312.         for (Bullet b : blts) {
  313.             c.drawCircle(b.getCx(), b.getCy(), b.getRad(), p);
  314.         }
  315.  
  316.         for (Enemy e : enemys) {
  317.             List<Bullet> bltss = e.getBullets();
  318.             p.setColor(Color.BLUE);
  319.             for (Bullet b : bltss) {
  320.                 c.drawCircle(b.getCx(), b.getCy(), b.getRad(), p);
  321.             }
  322.         }
  323.     }
  324.  
  325.     /*** --------------------------------------------------------------- ***/
  326.     private void paintBgBlock(Canvas c) {
  327.         for (int i = 0; i < bgBlocks.size(); i++) {
  328.             Block b = (Block) bgBlocks.get(i);
  329.             if (b.getIcon() != null) {
  330.                 c.drawBitmap(b.getIcon(), b.getX(), b.getY(), p);
  331.             }
  332.         }
  333.     }
  334.  
  335.     /*** --------------------------------------------------------------- ***/
  336.     private void paintFgBlock(Canvas c) {
  337.  
  338.         for (int i = 0; i < fgBlocks.size(); i++) {
  339.             Block b = (Block) fgBlocks.get(i);
  340.             if (b.getIcon() != null) {
  341.                 c.drawBitmap(b.getIcon(), b.getX(), b.getY(), p);
  342.             }
  343.         }
  344.  
  345.     }
  346.  
  347.     /*** --------------------------------------------------------------- ***/
  348.     public Robot getRobot() {
  349.         return robot;
  350.     }
  351.     /*** --------------------------------------------------------------- ***/
  352.    
  353.     public boolean getGameState(){
  354.         return gameOver;
  355.     }
  356.    
  357.     /*** --------------------------------------------------------------- ***/
  358.    
  359.     private class MyThread extends Thread {
  360.  
  361.         @Override
  362.         public void run() {
  363.             while (isRunning) {
  364.  
  365.                 update(1);
  366.  
  367.                 try {
  368.                     Thread.sleep(17);
  369.                 } catch (Exception e) {
  370.                     e.printStackTrace();
  371.                 }
  372.             }
  373.             try {
  374.                 Thread.sleep(1);
  375.             } catch (InterruptedException e) {
  376.                 e.printStackTrace();
  377.             }
  378.            
  379.             gameOver = true;
  380.             update(1);
  381.            
  382.         }
  383.     }
  384.  
  385. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement