Advertisement
kemkriszt

CLS2

Jul 12th, 2014
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.87 KB | None | 0 0
  1. package hu.cig.vob;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Iterator;
  6. import java.util.List;
  7.  
  8. import android.content.Context;
  9. import android.graphics.Bitmap;
  10. import android.graphics.BitmapFactory;
  11. import android.graphics.Rect;
  12. import cug.hu.vob.R;
  13.  
  14. public class Robot {
  15.     private Bitmap icon, currIcon, iconShot;
  16.     private int x, y, speedX = 0, speedY = 0, health = 100;
  17.     private final int MOVESPEED = 3, JumpSpeed = 15;
  18.     private boolean isMovingLeft = false, isMovingRight = false,
  19.             isFalling = true, isJumping = false, canJump = true;
  20.     private Rect bottom, horizontal;
  21.     private Context context;
  22.     private List<Bullet> bullets = Collections
  23.             .synchronizedList(new ArrayList<Bullet>());
  24.  
  25.  
  26.     public Robot(int in_x, int in_y, Bitmap i, float sc, Context c) {
  27.         context = c;
  28.  
  29.         icon = i;
  30.         currIcon = icon;
  31.         iconShot = BitmapFactory.decodeResource(c.getResources(),
  32.                 R.drawable.shot_right);
  33.  
  34.         x = in_x * (icon.getWidth()/2);
  35.         y = in_y * (icon.getHeight()/2);
  36.         y = (int) (sc - y);
  37.  
  38.         // for colliding
  39.         horizontal = new Rect((int) (x + icon.getWidth() * (31.5 / 100)),
  40.                 y + 5, (int) (x + icon.getWidth() - icon.getWidth()
  41.                         * (31.5 / 100)), y + icon.getHeight() - 5);
  42.  
  43.         bottom = new Rect((int) (x + icon.getWidth() * (31.5 / 100) + 5), y
  44.                 + icon.getHeight() - 10, (int) (x + icon.getWidth()
  45.                 - icon.getWidth() * (31.5 / 100) - 5), y + icon.getHeight());
  46.     }
  47.  
  48.     public void update(List<Block> blocks) {
  49.         int oldX = x, oldY = y;
  50.         if (!(x + speedX < 0 || x + speedX > LevelView.intScreenWidth)) {
  51.             x += speedX;
  52.         }
  53.  
  54.         updateRect();
  55.  
  56.         for (int i = 0; i < blocks.size(); i++) {
  57.             if (horizontal.intersect(blocks.get(i).getRect())) {
  58.                 stopMovingRight();
  59.                 stopMovingLeft();
  60.                 x = oldX;
  61.                 updateRect();
  62.             }
  63.         }
  64.        
  65.        
  66.         if (bullets.size() > 0) {
  67.             Iterator<Bullet> it = bullets.iterator();
  68.             while (it.hasNext()) {
  69.                 Bullet b = it.next();
  70.                 if (b.getCx() > LevelView.screenWidth) {
  71.                     it.remove();
  72.                 } else {
  73.                     b.update();
  74.                 }
  75.                
  76.                 Iterator<Block> itt = blocks.iterator();
  77.                 while (itt.hasNext()) {
  78.                     Block bb = itt.next();
  79.                     if (bb.getRect().intersect(b.getRect())) {
  80.                         it.remove();
  81.                     }
  82.                 }
  83.             }
  84.         }
  85.        
  86.  
  87.         // @graviti"
  88.  
  89.         if (isJumping) {
  90.             y -= speedY;
  91.             speedY--;
  92.             if (speedY == 0) {
  93.                 isFalling = true;
  94.                 isJumping = false;
  95.             }
  96.             updateRect();
  97.         }
  98.  
  99.         if (isFalling) {
  100.             y += JumpSpeed - 5;
  101.             canJump = false;
  102.             updateRect();
  103.         }
  104.  
  105.         for (Block b : blocks) {
  106.             if (bottom.intersect(b.getRect())) {
  107.                 y = oldY;
  108.                 updateRect();
  109.                 canJump = true;
  110.             }
  111.         }
  112.     }
  113.  
  114.     private void updateRect() {
  115.         horizontal = new Rect((int) (x + icon.getWidth() * (31.5 / 100)),
  116.                 y + 10, (int) (x + icon.getWidth() - icon.getWidth()
  117.                         * (31.5 / 100)), y + icon.getHeight() - 10);
  118.         bottom = new Rect((int) (x + icon.getWidth() * (31.5 / 100) + 5), y
  119.                 + icon.getHeight() - 10, (int) (x + icon.getWidth()
  120.                 - icon.getWidth() * (31.5 / 100) - 5), y + icon.getHeight() - 2);
  121.     }
  122.  
  123.     public void moveLeft() {
  124.         speedX = -MOVESPEED;
  125.         isMovingLeft = true;
  126.     }
  127.  
  128.     public void moveRight() {
  129.         speedX = MOVESPEED;
  130.         isMovingRight = true;
  131.     }
  132.  
  133.     public void stopMovingLeft() {
  134.         isMovingLeft = false;
  135.         stop();
  136.     }
  137.  
  138.     public void stopMovingRight() {
  139.         isMovingRight = false;
  140.         stop();
  141.     }
  142.  
  143.     public void stop() {
  144.         if (isMovingRight == false && isMovingLeft == false) {
  145.             speedX = 0;
  146.         }
  147.  
  148.         if (isMovingRight == true && isMovingLeft == false) {
  149.             moveRight();
  150.         }
  151.  
  152.         if (isMovingRight == false && isMovingLeft == true) {
  153.             moveLeft();
  154.         }
  155.     }
  156.  
  157.     public Bitmap getIcon() {
  158.         return currIcon;
  159.     }
  160.  
  161.     public int getX() {
  162.         return x;
  163.     }
  164.  
  165.     public int getY() {
  166.         return y;
  167.     }
  168.  
  169.     public int getSpeed() {
  170.         return speedX;
  171.     }
  172.  
  173.     public boolean isMovingLeft() {
  174.         return isMovingLeft;
  175.     }
  176.  
  177.     public boolean isMovingRight() {
  178.         return isMovingRight;
  179.     }
  180.  
  181.     public Rect getBottomRect() {
  182.         return bottom;
  183.     }
  184.  
  185.     public Rect getHRect() {
  186.         return horizontal;
  187.     }
  188.    
  189.     public int getHealth() {
  190.         return health;
  191.        
  192.     }
  193.  
  194.     public void setX(int x) {
  195.         this.x = x;
  196.     }
  197.  
  198.     public void setY(int y) {
  199.         this.y = y;
  200.     }
  201.  
  202.     public void setHealth(int health) {
  203.         this.health = health;
  204.     }
  205.  
  206.     public void shot() {
  207.         currIcon = iconShot;
  208.         Bullet b = new Bullet((float) (x + iconShot.getWidth()),
  209.                 (float) (y + ((26.31 * iconShot.getHeight()) / 100)),
  210.                 Helper.convertPx_Dpi(10, context), 2);
  211.         bullets.add(b);
  212.     }
  213.  
  214.     public void _shot() {
  215.         currIcon = icon;
  216.     }
  217.  
  218.     public List<Bullet> getBullets() {
  219.         return bullets;
  220.     }
  221.  
  222.     public void jump() {
  223.         if (!isJumping && canJump) {
  224.             isJumping = true;
  225.             isFalling = false;
  226.             speedY = JumpSpeed;
  227.         }
  228.     }
  229.  
  230.  
  231.  
  232.     public void increaseHealth(int x){
  233.         health += x;
  234.         if(health > 100){
  235.             health = 100;
  236.         }
  237.     }
  238.  
  239.     public void degreeseHealth(int a) {
  240.         health -= a;
  241.     }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement