Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.xerpi.particleweapon;
- import java.util.ArrayList;
- import java.util.ListIterator;
- import java.util.Random;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- public class Particles {
- Random random = new Random();
- private float speed1,speed2,initX,initY;
- int screenW,screenH;
- boolean finished = false;
- ArrayList <Particle> particles = new ArrayList<Particle>();
- public Particles(int screenW,int screenH,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);
- }
- this.screenW = screenW;
- this.screenH = screenH;
- }
- public class Particle{
- double x,y,inc_x,inc_y,w,h,angle,speed;
- public Particle(){
- this.angle = random.nextFloat() * 6.28;
- this.inc_x = Math.cos(this.angle);
- this.inc_y = Math.sin(this.angle);
- this.speed = getSpeedValue(speed1,speed2);
- }
- }
- public boolean outOfScreen(Particle particle){
- if (this.initX+particle.x > this.screenW || this.initX+particle.x< 0 || this.initY+particle.y < 0 || this.initY+particle.y > this.screenH){
- return true;
- }
- return false;
- }
- public void blit(Bitmap img,Canvas canvas){
- ListIterator<Particle> a = particles.listIterator();
- float fX,fY;
- while(a.hasNext()) {
- Particle i = (Particle) a.next();
- fX = (float) (this.initX+i.x);
- fY = (float) (this.initY+i.y);
- canvas.save();
- canvas.rotate((float) Math.toDegrees(i.angle),fX,fY);
- canvas.drawBitmap(img, fX,fY,null);
- canvas.restore();
- }
- }
- public boolean move() {
- ListIterator<Particle> a = particles.listIterator();
- while(a.hasNext()) {
- Particle i = (Particle) a.next();
- i.y += i.inc_y * i.speed;
- i.x += i.inc_x * i.speed;
- if (outOfScreen(i)){
- a.remove();
- }
- }
- return (a.nextIndex()<1);
- }
- public void reset() {
- for (int i = 0; i < this.particles.size(); i++) {
- this.particles.set(i,new Particle());
- }
- }
- public void init( float x, float y){
- this.initX = x;
- this.initY = y;
- }
- public void changeXY( float x, float y){
- this.initX = x;
- this.initY = y;
- }
- 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());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement