Advertisement
xerpi

ParticeLib v1.2 (c) xerpi 2011

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