Advertisement
xerpi

WeaponLib 2.0 java (c) xerpi 2011

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