Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.particleshooter;
- import java.util.ArrayList;
- import java.util.Random;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- public class ParticleLib {
- public static Random random = new Random();
- private float MIN_VEL = 4;
- private float MAX_VEL = 8;
- private ArrayList particles = new ArrayList();
- private String status = "stop";
- private double initX,initY;
- private int number_particles =5;
- private double radius;
- public ParticleLib(int total, float min_vel, float max_vel){
- this.number_particles = total;
- this.MIN_VEL = min_vel;
- this.MAX_VEL = max_vel;
- for (int i =0;i<number_particles;i++){
- this.particles.add(new Particle());
- }
- }
- public void setRadius(double newRadius){
- this.radius = newRadius;
- }
- public void setXY(double newX,double newY){
- this.initX = newX;
- this.initY = newY;
- }
- public void blit(Bitmap img,Canvas canvas,Paint mPaint){
- for (int i =0;i<this.number_particles;i++){
- canvas.drawBitmap(img, (float) (this.initX+((Particle) this.particles.get(i)).x),(float) (this.initY+((Particle) this.particles.get(i)).y),mPaint);
- }
- }
- public void move(){
- double x,y,hypotenuse;
- if(this.status != "stop"){
- for (int i =0;i<this.number_particles;i++){
- if (((Particle) this.particles.get(i)).status == "alive"){
- ((Particle) this.particles.get(i)).x += Math.cos(((Particle) this.particles.get(i)).ang)*((Particle) this.particles.get(i)).vel;
- ((Particle) this.particles.get(i)).y += Math.sin(((Particle) this.particles.get(i)).ang)*((Particle) this.particles.get(i)).vel;
- x = (double) (((Particle) this.particles.get(i)).x+this.initX);
- y = (double) (((Particle) this.particles.get(i)).y+this.initY);
- hypotenuse = Math.sqrt(Math.pow(((Particle) this.particles.get(i)).x,2)+Math.pow(((Particle) this.particles.get(i)).y,2));
- // if (x >= 480 || x <= 0 || y >= 854 || y <= 0 || hypotenuse >= this.radius){
- // ((Particle) this.particles.get(i)).status = "dead";
- // }
- // part.particles[i].alpha = 255;
- // if (hypotenuse/rad*100) > 40 then
- // part.particles[i].alpha = 255-((hypotenuse/rad*80)*255)/100
- // end
- }
- }
- }
- }
- public void init(){
- this.status = "run";
- }
- public void pause(){
- this.status = "pause";
- }
- public void stop(){
- this.status = "stop";
- }
- public void reset(){
- for (int i =0;i<this.number_particles;i++){
- ((Particle) this.particles.get(i)).x = 0;
- ((Particle) this.particles.get(i)).y = 0;
- ((Particle) this.particles.get(i)).status = "alive";
- ((Particle) this.particles.get(i)).vel = aleatorio(MIN_VEL,MAX_VEL);
- ((Particle) this.particles.get(i)).ang = aleatorio(0,360);
- }
- }
- public static float aleatorio(float max,float min){
- return (float)(random.nextFloat()*(max-min))+min;
- }
- public class Particle{
- private String status="alive";
- private double x = 0;
- private double y = 0;
- private float vel = aleatorio(MIN_VEL,MAX_VEL);
- // private int alpha = 255;
- private float ang = aleatorio(0,360);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement