Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.xerpi.particleweapon;
- import java.util.ArrayList;
- import java.util.Random;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- public class Particles {
- Random random = new Random();
- private float speed1,speed2;
- boolean finished = false;
- ArrayList <Particle> particles = new ArrayList<Particle>();
- public Particles(int total,double speed1,double speed2){
- this.speed1 = (float) speed1;
- this.speed2 = (float) speed2;
- for (int i =0; i<total;i++){
- this.add(speed1,speed2);
- }
- }
- public class Particle{
- double x,y,inc_x,inc_y,w,h,angle,speed;
- boolean dead = false;
- public Particle(){
- this.angle = random.nextDouble()*(6.28);
- this.inc_x = Math.cos(this.angle);
- this.inc_y = Math.sin(this.angle);
- this.speed = getSpeedValue(speed1,speed2);
- }
- }
- public void blit(float x,float y,Bitmap img,Canvas canvas){
- for (int i = 0; i < this.particles.size(); i++){
- if (this.particles.get(i).dead){
- continue;
- }
- canvas.save();
- canvas.rotate((float) Math.toDegrees(this.particles.get(i).angle),x+(float) this.particles.get(i).x,y+(float) this.particles.get(i).y);
- canvas.drawBitmap(img, x+(float) this.particles.get(i).x,y+(float) this.particles.get(i).y,null);
- canvas.restore();
- }
- }
- public boolean move() {
- this.finished = true;
- for (int i = 0; i < this.particles.size(); i++) {
- if (this.particles.get(i).dead){
- continue;
- }
- this.particles.get(i).y += this.particles.get(i).inc_y * this.particles.get(i).speed;
- this.particles.get(i).x += this.particles.get(i).inc_x * this.particles.get(i).speed;
- 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){
- // this.particles.get(i).inc_x *=-1;
- // this.particles.get(i).angle+=Math.PI;
- this.finished = false;
- }
- }
- return this.finished;
- }
- public void reset() {
- for (int i = 0; i < this.particles.size(); i++) {
- this.particles.set(i,new Particle());
- }
- }
- public void add(double speed1,double speed2){
- this.particles.add(new Particle());
- }
- public double getSpeedValue(double num1,double num2){
- return num1 + ((num2-num1)*this.random.nextDouble());
- }
- }
Add Comment
Please, Sign In to add comment