Advertisement
xerpi

WeaponLib 3.0 Java Android (c) xerpi 2011

Oct 9th, 2011
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. import java.util.LinkedList;
  2. import java.util.ListIterator;
  3.  
  4. import android.graphics.Bitmap;
  5. import android.graphics.Canvas;
  6. import android.graphics.Paint;
  7.  
  8. public class WeaponLib{
  9.     Bitmap img;
  10.     float time,w,h,c_time;
  11.     LinkedList <Bullet> bullets = new LinkedList<Bullet>();
  12.     int screenW,screenH;
  13.    
  14.     public WeaponLib(int screenW, int screenH, Bitmap img, float time){
  15.         this.img = img;
  16.         this.w = img.getWidth();
  17.         this.h = img.getHeight();
  18.         this.time = time;
  19.         this.screenW = screenW;
  20.         this.screenH = screenH;
  21.     }
  22.     public boolean checkCollision(Bullet bullet, float x, float y, float w, float h){
  23.         if ( bullet.x >= x && bullet.x <= x+w && bullet.y >= y && bullet.y <= y+h){
  24.             return true;
  25.         }  
  26.         return false;
  27.     }
  28.     public boolean outOfScreen(Bullet bullet){
  29.         if (bullet.x > this.screenW  || bullet.x < 0 || bullet.y > this.screenH || bullet.y < 0){
  30.             return true;
  31.         }
  32. //      if (bullet.x > width  || bullet.x < 0 ){
  33. //          bullet.inc_x *= -1;
  34. //      }  
  35. //      if (bullet.y > height  || bullet.y < 0 ){
  36. //          bullet.inc_y *= -1;
  37. //      }
  38.         return false;
  39.     }  
  40.     public void blit(Canvas canvas, Paint paint, Bitmap img){
  41.         synchronized(this){
  42.             ListIterator<Bullet> a = bullets.listIterator();
  43.             while(a.hasNext()) {
  44.                 Bullet i = (Bullet) a.next();          
  45.                 canvas.save();
  46.                 canvas.rotate((float) Math.toDegrees(i.ang),i.x,i.y);
  47.                 canvas.drawBitmap(this.img,i.x,i.y,paint);
  48.                 canvas.restore();                      
  49.             }
  50.         }
  51.     }
  52.     public void move(){
  53.         synchronized(this){
  54.             this.c_time++;
  55.             ListIterator<Bullet> a = bullets.listIterator();
  56.             while(a.hasNext()) {
  57.                 Bullet i = (Bullet) a.next();
  58.                 if (outOfScreen(i) ){
  59.                     a.remove();
  60.                 }
  61.                 i.y += i.inc_y * i.vel;
  62.                 i.x += i.inc_x * i.vel;    
  63.             }
  64.         }
  65.     }  
  66.     public boolean collison(float x, float y, float w, float h, boolean deadcollision){
  67.         synchronized(this){
  68.             this.c_time++;
  69.             ListIterator<Bullet> a = bullets.listIterator();
  70.             while(a.hasNext()) {
  71.                 Bullet i = (Bullet) a.next();
  72.                 if (checkCollision(i, x, y, w, h)){
  73.                     if (deadcollision){
  74.                         a.remove();
  75.                     }
  76.                     return true;
  77.                 }
  78.             }
  79.         }
  80.         return false;
  81.     }
  82.     public void insertBullet(float vel, float x, float y, float ang){
  83.         this.bullets.add(new Bullet(vel, x, y, ang));
  84.     }  
  85.     public void shoot(float vel, float x, float y, float ang){
  86.         synchronized(this){
  87.             if (this.c_time >= this.time){
  88.                 this.bullets.add(new Bullet(vel, x, y, ang));
  89.                 this.c_time = 0;
  90.             }
  91.         }
  92.     }  
  93.     public class Bullet{
  94.         float vel,x,y,ang,inc_x,inc_y;
  95.         public Bullet(float vel, float x, float y, float ang){
  96.             this.vel = vel; this.x = x; this.y = y; this.ang = ang;
  97.             this.inc_x = (float) Math.cos(ang);
  98.             this.inc_y = (float) Math.sin(ang);
  99.         }
  100.  
  101.     }
  102. }
  103.  
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement