Advertisement
xerpi

particlelib alpha1

Sep 17th, 2011
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. package com.particleshooter;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5.  
  6. import android.graphics.Bitmap;
  7. import android.graphics.Canvas;
  8. import android.graphics.Color;
  9. import android.graphics.Paint;
  10.  
  11.  
  12.  
  13. public class ParticleLib {
  14.     public static Random random = new Random();
  15.     private float MIN_VEL = 4;
  16.     private float MAX_VEL = 8;
  17.     private ArrayList particles = new ArrayList();
  18.     private String status = "stop";
  19.     private double initX,initY;
  20.     private int number_particles =5;
  21.     private double radius;
  22.    
  23.     public ParticleLib(int total, float min_vel, float max_vel){
  24.         this.number_particles = total;
  25.         this.MIN_VEL = min_vel;
  26.         this.MAX_VEL = max_vel;
  27.         for (int i =0;i<number_particles;i++){
  28.             this.particles.add(new Particle());
  29.         }
  30.     }
  31.    
  32.     public void setRadius(double newRadius){
  33.         this.radius = newRadius;
  34.     }
  35.    
  36.     public void setXY(double newX,double newY){
  37.         this.initX = newX;
  38.         this.initY = newY;
  39.     }
  40.    
  41.     public void blit(Bitmap img,Canvas canvas,Paint mPaint){
  42.         for (int i =0;i<this.number_particles;i++){
  43.             canvas.drawBitmap(img, (float) (this.initX+((Particle) this.particles.get(i)).x),(float) (this.initY+((Particle) this.particles.get(i)).y),mPaint);
  44.         }
  45.     }
  46.     public void move(){
  47.         double x,y,hypotenuse;
  48.         if(this.status != "stop"){
  49.             for (int i =0;i<this.number_particles;i++){
  50.                 if (((Particle) this.particles.get(i)).status == "alive"){
  51.                    
  52.                     ((Particle) this.particles.get(i)).x += Math.cos(((Particle) this.particles.get(i)).ang)*((Particle) this.particles.get(i)).vel;
  53.                     ((Particle) this.particles.get(i)).y += Math.sin(((Particle) this.particles.get(i)).ang)*((Particle) this.particles.get(i)).vel;
  54.                     x = (double) (((Particle) this.particles.get(i)).x+this.initX);
  55.                     y = (double) (((Particle) this.particles.get(i)).y+this.initY);    
  56.                     hypotenuse = Math.sqrt(Math.pow(((Particle) this.particles.get(i)).x,2)+Math.pow(((Particle) this.particles.get(i)).y,2));
  57. //                  if (x >= 480 || x <= 0 || y >= 854 || y <= 0 || hypotenuse >= this.radius){
  58. //                      ((Particle) this.particles.get(i)).status = "dead";
  59. //                  }
  60. //                  part.particles[i].alpha = 255;
  61. //                  if (hypotenuse/rad*100) > 40 then
  62. //                      part.particles[i].alpha = 255-((hypotenuse/rad*80)*255)/100
  63. //                  end                                    
  64.                 }
  65.             }          
  66.         }
  67.     }  
  68.     public void init(){
  69.         this.status = "run";
  70.     }
  71.     public void pause(){
  72.         this.status = "pause";
  73.     }
  74.     public void stop(){
  75.         this.status = "stop";
  76.     }
  77.    
  78.     public void reset(){
  79.         for (int i =0;i<this.number_particles;i++){
  80.             ((Particle) this.particles.get(i)).x = 0;
  81.             ((Particle) this.particles.get(i)).y = 0;
  82.             ((Particle) this.particles.get(i)).status = "alive";
  83.             ((Particle) this.particles.get(i)).vel = aleatorio(MIN_VEL,MAX_VEL);
  84.             ((Particle) this.particles.get(i)).ang = aleatorio(0,360);
  85.         }
  86.     }
  87.    
  88.     public static float aleatorio(float max,float min){
  89.         return (float)(random.nextFloat()*(max-min))+min;      
  90.     }
  91.    
  92.     public class Particle{
  93.         private String status="alive";
  94.         private double x = 0;
  95.         private double y = 0;
  96.         private float vel = aleatorio(MIN_VEL,MAX_VEL);
  97. //      private int alpha = 255;
  98.         private float ang = aleatorio(0,360);
  99.     }
  100.    
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement