Guest User

Untitled

a guest
Jan 12th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. package com.xerpi.particleweapon;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5.  
  6. import android.graphics.Bitmap;
  7. import android.graphics.Canvas;
  8.  
  9. public class Particles {
  10.     Random random = new Random();
  11.     private float speed1,speed2;
  12.     boolean finished = false;
  13.     ArrayList <Particle> particles = new ArrayList<Particle>();
  14.    
  15.     public Particles(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.     }
  22.    
  23.     public class Particle{
  24.         double x,y,inc_x,inc_y,w,h,angle,speed;
  25.         boolean dead = false;
  26.         public Particle(){
  27.             this.angle = random.nextDouble()*(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 void blit(float x,float y,Bitmap img,Canvas canvas){
  34.         for (int i = 0; i < this.particles.size(); i++){
  35.             if (this.particles.get(i).dead){
  36.                 continue;
  37.             }
  38.             canvas.save();
  39.             canvas.rotate((float) Math.toDegrees(this.particles.get(i).angle),x+(float) this.particles.get(i).x,y+(float) this.particles.get(i).y);
  40.             canvas.drawBitmap(img, x+(float) this.particles.get(i).x,y+(float) this.particles.get(i).y,null);
  41.             canvas.restore();
  42.         }
  43.     }
  44.    
  45.     public boolean move() {
  46.         this.finished = true;
  47.         for (int i = 0; i < this.particles.size(); i++) {
  48.             if (this.particles.get(i).dead){
  49.                 continue;
  50.             }
  51.             this.particles.get(i).y += this.particles.get(i).inc_y * this.particles.get(i).speed;
  52.             this.particles.get(i).x += this.particles.get(i).inc_x * this.particles.get(i).speed;
  53.             if (this.particles.get(i).x < 854 && this.particles.get(i).x > 0 && this.particles.get(i).y < 480 && this.particles.get(i).y > 0){
  54. //              this.particles.get(i).inc_x *=-1;
  55. //              this.particles.get(i).angle+=Math.PI;
  56.                 this.finished = false;
  57.             }
  58.         }
  59.         return this.finished;
  60.     }
  61.    
  62.     public void reset() {
  63.         for (int i = 0; i < this.particles.size(); i++) {
  64.             this.particles.set(i,new Particle());
  65.         }
  66.     }
  67.    
  68.     public void add(double speed1,double speed2){
  69.         this.particles.add(new Particle());
  70.     }
  71.     public double getSpeedValue(double num1,double num2){
  72.         return num1 + ((num2-num1)*this.random.nextDouble());
  73.     }
  74. }
Add Comment
Please, Sign In to add comment