Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hu.cig.vob;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.Collections;
- import java.util.Iterator;
- import java.util.List;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Paint.Align;
- import android.graphics.Point;
- import android.graphics.Rect;
- import android.graphics.Typeface;
- import android.view.Display;
- import android.view.View;
- import android.view.WindowManager;
- import cug.hu.vob.R;
- public class LevelView extends View {
- public static double intScreenWidth;
- public static double screenWidth,screenHeight;
- public static float screenBottom;
- private Paint p;
- private int level;
- private List<Block> bgBlocks = Collections
- .synchronizedList(new ArrayList<Block>());
- private List<Block> fgBlocks = Collections
- .synchronizedList(new ArrayList<Block>());
- private List<Enemy> enemys = Collections
- .synchronizedList(new ArrayList<Enemy>());
- private Context context;
- private Robot robot;
- private boolean isRunning = true, gameOver = false;
- private Rect r = new Rect();
- /*** --------------------------------------------------------------- ***/
- public LevelView(Context context, int level, WindowManager wmg) {
- super(context);
- p = new Paint();
- Bitmap robotIcon = BitmapFactory.decodeResource(context.getResources(),
- R.drawable.char1);
- Display display = wmg.getDefaultDisplay();
- Point size = new Point();
- display.getSize(size);
- intScreenWidth = size.x / 2.5;
- screenBottom = size.y - Helper.convertDpi_Px(context, 40);
- screenWidth = size.x;
- screenHeight = size.y;
- robot = new Robot(2, 2, robotIcon, screenBottom, context);
- MyThread th = new MyThread();
- th.start();
- this.level = level;
- this.context = context;
- try {
- loadMap();
- } catch (IOException e) {
- }
- try {
- loadEntity();
- } catch (Exception e) {
- }
- }
- /*** --------------------------------------------------------------- ***/
- private void loadMap() throws IOException {
- ArrayList<String> lines = new ArrayList<String>();
- InputStream iStream;
- switch (level) {
- case 0:
- iStream = context.getResources().openRawResource(R.raw.level0);
- break;
- default:
- iStream = null;
- break;
- }
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- iStream));
- while (true) {
- String line = reader.readLine();
- if (line == null) {
- break;
- }
- if (!line.startsWith("!")) {
- lines.add(line);
- }
- }
- for (int i = 0; i < lines.size(); i++) {
- String currLine = lines.get(i);
- for (int j = 0; j < currLine.length(); j++) {
- int type = Character.getNumericValue(currLine.charAt(j));
- boolean foreground = false;
- Bitmap icon = null;
- switch (type) {
- case 1:
- icon = BitmapFactory.decodeResource(context.getResources(),
- R.drawable.block1);
- foreground = true;
- break;
- case 2:
- icon = BitmapFactory.decodeResource(context.getResources(),
- R.drawable.block2);
- foreground = true;
- break;
- case 3:
- icon = BitmapFactory.decodeResource(context.getResources(),
- R.drawable.block3);
- foreground = false;
- break;
- case 4:
- icon = BitmapFactory.decodeResource(context.getResources(),
- R.drawable.block4);
- foreground = false;
- break;
- }
- Block b = new Block(j, i, type, icon, foreground);
- if (!foreground) {
- bgBlocks.add(b);
- } else {
- fgBlocks.add(b);
- }
- }
- }
- }
- /*** --------------------------------------------------------------- ***/
- private void loadEntity() throws IOException {
- ArrayList<String> lines = new ArrayList<String>();
- InputStream iStream = null;
- switch (level) {
- case 0:
- iStream = context.getResources().openRawResource(R.raw.entity0);
- break;
- }
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- iStream));
- while (true) {
- String line = reader.readLine();
- if (line == null) {
- break;
- }
- if (!line.startsWith("!")) {
- lines.add(line);
- }
- }
- for (int i = 0; i < lines.size(); i++) {
- String currLine = lines.get(i);
- for (int j = 0; j < currLine.length(); j++) {
- int type = Character.getNumericValue(currLine.charAt(j));
- Bitmap icon = null;
- switch (type) {
- case 1:
- icon = BitmapFactory.decodeResource(context.getResources(),
- R.drawable.enemy);
- break;
- }
- if (icon != null) {
- Enemy e = new Enemy(j, i, icon, screenBottom, context);
- enemys.add(e);
- }
- }
- }
- }
- /*** --------------------------------------------------------------- ***/
- public void update(long elpsedTime) {
- if (!gameOver) {
- robot.update(fgBlocks);
- if(robot.getY()>screenBottom){
- robot.degreeseHealth(100);
- isRunning = false;
- }
- for (int i = 0; i < bgBlocks.size(); i++) {
- bgBlocks.get(i).update(robot);
- }
- for (int i = 0; i < fgBlocks.size(); i++) {
- fgBlocks.get(i).update(robot);
- }
- for (Enemy e : enemys) {
- e.update(robot, Calendar.getInstance().getTimeInMillis());
- }
- List<Bullet> blts = robot.getBullets();
- if (blts.size() > 0 && enemys.size() > 0) {
- Iterator<Bullet> it_b = blts.iterator();
- Iterator<Enemy> it_e = enemys.iterator();
- while (it_e.hasNext()) {
- Enemy e = it_e.next();
- it: while (it_b.hasNext()) {
- Bullet b = it_b.next();
- if (b.getRect().intersect(e.getRect())) {
- e.degreesHealth(20);
- it_b.remove();
- if (e.getHealth() <= 0) {
- it_e.remove();
- break it;
- }
- }
- }
- }
- }
- if (enemys.size() > 0) {
- Iterator<Enemy> it_e = enemys.iterator();
- itt: while (it_e.hasNext()) {
- Enemy e = it_e.next();
- blts = e.getBullets();
- Iterator<Bullet> it_b = blts.iterator();
- while (it_b.hasNext()) {
- Bullet b = it_b.next();
- if (b.getRect().intersect(robot.getHRect())) {
- it_b.remove();
- robot.degreeseHealth(10);
- if (robot.getHealth() <= 0) {
- isRunning = false;
- break itt;
- }
- }
- }
- }
- }
- }
- postInvalidate();
- }
- /*** --------------------------------------------------------------- ***/
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- if (!gameOver) {
- paintBgBlock(canvas);
- paintBullets(canvas);
- canvas.drawBitmap(robot.getIcon(), robot.getX(), robot.getY(), p);
- paintEnemy(canvas);
- paintFgBlock(canvas);
- }else{
- canvas.drawPaint(p);
- p.setColor(Color.BLACK);
- canvas.drawRect(0, 0,(float)screenWidth,(float)screenHeight,p);
- p.setColor(Color.GREEN);
- p.setTextSize(70f);
- p.setTypeface(Typeface.DEFAULT_BOLD);
- p.setTextAlign(Align.LEFT);
- String text = "Game Over";
- p.getTextBounds(text,0,text.length(),r);
- canvas.drawText(text, r.left+20, -2*r.top+20, p);
- p.setTextSize(30);
- canvas.drawText("Tap to continue", r.left+20, -3*r.top+20, p);
- }
- }
- /*** --------------------------------------------------------------- ***/
- private void paintEnemy(Canvas c) {
- for (Enemy e : enemys) {
- c.drawBitmap(e.getIcon(), e.getX(), e.getY(), p);
- }
- }
- /*** --------------------------------------------------------------- ***/
- private void paintBullets(Canvas c) {
- List<Bullet> blts = robot.getBullets();
- p.setColor(Color.YELLOW);
- for (Bullet b : blts) {
- c.drawCircle(b.getCx(), b.getCy(), b.getRad(), p);
- }
- for (Enemy e : enemys) {
- List<Bullet> bltss = e.getBullets();
- p.setColor(Color.BLUE);
- for (Bullet b : bltss) {
- c.drawCircle(b.getCx(), b.getCy(), b.getRad(), p);
- }
- }
- }
- /*** --------------------------------------------------------------- ***/
- private void paintBgBlock(Canvas c) {
- for (int i = 0; i < bgBlocks.size(); i++) {
- Block b = (Block) bgBlocks.get(i);
- if (b.getIcon() != null) {
- c.drawBitmap(b.getIcon(), b.getX(), b.getY(), p);
- }
- }
- }
- /*** --------------------------------------------------------------- ***/
- private void paintFgBlock(Canvas c) {
- for (int i = 0; i < fgBlocks.size(); i++) {
- Block b = (Block) fgBlocks.get(i);
- if (b.getIcon() != null) {
- c.drawBitmap(b.getIcon(), b.getX(), b.getY(), p);
- }
- }
- }
- /*** --------------------------------------------------------------- ***/
- public Robot getRobot() {
- return robot;
- }
- /*** --------------------------------------------------------------- ***/
- public boolean getGameState(){
- return gameOver;
- }
- /*** --------------------------------------------------------------- ***/
- private class MyThread extends Thread {
- @Override
- public void run() {
- while (isRunning) {
- update(1);
- try {
- Thread.sleep(17);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- try {
- Thread.sleep(1);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- gameOver = true;
- update(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement