Advertisement
xerpi

ParticeLib v1.3 [Java] (c) xerpi 2011

Oct 24th, 2011
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.ListIterator;
  3. import java.util.Random;
  4.  
  5. import android.graphics.Bitmap;
  6. import android.graphics.Canvas;
  7.  
  8. public class Particles {
  9.     Random random = new Random();
  10.     private float speed1,speed2,initX,initY;
  11.     int screenW,screenH;
  12.     boolean run = false;
  13.     ArrayList <Particle> particles = new ArrayList<Particle>();
  14.    
  15.     public Particles(int screenW,int screenH,int total,double speed1,double speed2){
  16.         this.speed1 = (float) speed1;
  17.         this.speed2 = (float) speed2;
  18.         for (int i =0; i<total;i++){
  19.             this.add(speed1,speed2);
  20.         }
  21.         this.screenW = screenW;
  22.         this.screenH = screenH;
  23.     }
  24.     public class Particle{
  25.         double x,y,inc_x,inc_y,w,h,angle,speed;
  26.         public Particle(){
  27.             this.angle = random.nextFloat() * 6.28;
  28.             this.inc_x = Math.cos(this.angle);
  29.             this.inc_y = Math.sin(this.angle);
  30.             this.speed = getSpeedValue(speed1,speed2);
  31.         }
  32.     }
  33.     public boolean outOfScreen(Particle particle){
  34.         if (this.initX+particle.x > this.screenW  || this.initX+particle.x< 0  || this.initY+particle.y < 0 || this.initY+particle.y > this.screenH){
  35.             return true;
  36.         }
  37.         return false;
  38.     }
  39.     public void blit(Bitmap img,Canvas canvas){
  40.         if (this.run){
  41.             ListIterator<Particle> a = particles.listIterator();
  42.             float fX,fY;
  43.             while(a.hasNext()) {
  44.                 Particle i = (Particle) a.next();
  45.                 fX = (float) (this.initX+i.x);
  46.                 fY = (float) (this.initY+i.y);
  47.                 canvas.save();
  48.                 canvas.rotate((float) Math.toDegrees(i.angle),fX,fY);
  49.                 canvas.drawBitmap(img, fX,fY,null);
  50.                 canvas.restore();
  51.             }
  52.         }
  53.     }
  54.    
  55.     public boolean move() {
  56.         if (this.run){
  57.             ListIterator<Particle> a = particles.listIterator();
  58.             while(a.hasNext()) {
  59.                 Particle i = (Particle) a.next();
  60.                 i.y += i.inc_y * i.speed;
  61.                 i.x += i.inc_x * i.speed;
  62.                 if (outOfScreen(i)){
  63.                     a.remove();
  64.                 }
  65.             }  
  66.             return (a.nextIndex()<1);
  67.         }
  68.         return false;
  69.     }
  70.    
  71.     public void reset() {
  72.         for (int i = 0; i < this.particles.size(); i++) {
  73.             this.particles.set(i,new Particle());
  74.         }
  75.     }
  76.     public void init( float x, float y){
  77.         this.initX = x;
  78.         this.initY = y;
  79.         this.run = true;
  80.     }
  81.     public void resume(){
  82.         this.run = true;
  83.     }
  84.     public void pause(){
  85.         this.run = false;
  86.     }
  87.     public void changeXY( float x, float y){
  88.         this.initX = x;
  89.         this.initY = y;    
  90.     }
  91.    
  92.     public void add(double speed1,double speed2){
  93.         this.particles.add(new Particle());
  94.     }
  95.     public double getSpeedValue(double num1,double num2){
  96.         return num1 + ((num2-num1)*this.random.nextDouble());
  97.     }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement