Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.xerpi.particleweapon;
- import java.util.LinkedList;
- import java.util.ListIterator;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- public class WeaponLib{
- Bitmap img;
- float time,w,h,c_time;
- LinkedList <Bullet> bullets = new LinkedList<Bullet>();
- int screenW,screenH;
- static final int ALIVE = 1;
- static final int EXPLODING = 0;
- public WeaponLib(int screenW, int screenH,Bitmap img, float time){
- this.img = img;
- this.w = img.getWidth();
- this.h = img.getHeight();
- this.time = time;
- this.screenW = screenW;
- this.screenH = screenH;
- }
- public boolean outOfScreen(Bullet bullet){
- if (bullet.x > this.screenW || bullet.x < 0 || bullet.y > this.screenH || bullet.y < 0){
- return true;
- }
- // if (bullet.x > width || bullet.x < 0 ){
- // bullet.inc_x *= -1;
- // }
- // if (bullet.y > height || bullet.y < 0 ){
- // bullet.inc_y *= -1;
- // }
- return false;
- }
- public void blit(Canvas canvas, Paint paint, Bitmap img){
- synchronized(this){
- // for( ListIterator<Bullet> a = bullets.listIterator(); a.hasNext(); ) {
- ListIterator<Bullet> a = bullets.listIterator();
- while(a.hasNext()) {
- Bullet i = (Bullet) a.next();
- if (i.status == WeaponLib.ALIVE){
- canvas.save();
- canvas.rotate((float) Math.toDegrees(i.ang),i.x,i.y);
- canvas.drawBitmap(this.img,i.x,i.y,paint);
- canvas.restore();
- }else if (i.status == WeaponLib.EXPLODING){
- i.particle.blit(img ,canvas);
- }
- }
- }
- }
- public void move(){
- synchronized(this){
- this.c_time++;
- // for( ListIterator<Bullet> a = bullets.listIterator(); a.hasNext(); ) {
- ListIterator<Bullet> a = bullets.listIterator();
- while(a.hasNext()) {
- Bullet i = (Bullet) a.next();
- if (i.status == WeaponLib.ALIVE){
- if (outOfScreen(i) ){
- i.status = WeaponLib.EXPLODING;
- i.particle.init(i.x,i.y);
- }
- i.y += i.inc_y * i.vel;
- i.x += i.inc_x * i.vel;
- }else if (i.status == WeaponLib.EXPLODING){
- if (i.particle.move()){
- a.remove();
- }
- }
- }
- }
- }
- public void insertBullet(float vel, float x, float y, float ang){
- this.bullets.add(new Bullet(vel, x, y, ang));
- }
- public void shoot(float vel, float x, float y, float ang){
- synchronized(this){
- if (this.c_time >= this.time){
- this.bullets.add(new Bullet(vel, x, y, ang));
- this.c_time = 0;
- }
- }
- }
- public class Bullet{
- float vel,x,y,ang,inc_x,inc_y;
- int status = WeaponLib.ALIVE;
- Particles particle;
- public Bullet(float vel, float x, float y, float ang){
- this.vel = vel; this.x = x; this.y = y; this.ang = ang;
- this.inc_x = (float) Math.cos(ang);
- this.inc_y = (float) Math.sin(ang);
- this.particle = new Particles(screenW,screenH,25,10,20);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement