Advertisement
xerpi

WeaponLib java (c) xerpi 2011

Oct 6th, 2011
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. package com.xerpi.weaponlib;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.ListIterator;
  5.  
  6. import android.graphics.Bitmap;
  7. import android.graphics.Canvas;
  8. import android.graphics.Paint;
  9.  
  10. public class WeaponLib{
  11.     Bitmap img;
  12.     float time,w,h,c_time;
  13.     String status = "stop";
  14.     LinkedList <Bullet> bullets = new LinkedList<Bullet>();
  15.     public WeaponLib(Bitmap img, float time){
  16.         this.img = img;
  17.         this.w = img.getWidth();
  18.         this.h = img.getHeight();
  19.         this.time = time;
  20.     }
  21.     public boolean outOfScreen(Bullet bullet){
  22.         if (bullet.x > 854  || bullet.x < 0 || bullet.y > 480 || bullet.y < 0){
  23.             return true;
  24.         }      
  25.         return false;
  26.     }  
  27.     public void blit(Canvas canvas, Paint paint){
  28.         for( ListIterator<Bullet> a = bullets.listIterator(); a.hasNext(); ) {
  29.             Bullet i = (Bullet) a.next();      
  30.             canvas.save();
  31.             canvas.rotate((float) Math.toDegrees(i.ang),i.x,i.y);
  32.             canvas.drawBitmap(this.img,i.x,i.y,paint);
  33.             canvas.restore();  
  34.         }
  35.     }
  36.    
  37.     public void move(){
  38.         this.c_time++;
  39.         for( ListIterator<Bullet> a = bullets.listIterator(); a.hasNext(); ) {
  40.             Bullet i = (Bullet) a.next();  
  41.             if (outOfScreen(i) ){
  42.                 a.remove();
  43. //              continue;
  44.             }
  45.             i.y += i.inc_y * i.vel;
  46.             i.x += i.inc_x * i.vel;
  47.         }
  48.     }
  49.     public void insertBullet(float vel, float x, float y, float ang){
  50.         this.bullets.add(new Bullet(vel, x, y, ang));
  51.     }  
  52.     public void shoot(float vel, float x, float y, float ang){
  53.         if (this.c_time >= this.time){
  54.             this.bullets.add(new Bullet(vel, x, y, ang));
  55.             this.c_time = 0;
  56.         }
  57.     }  
  58.     public class Bullet{
  59.         float vel,x,y,ang,inc_x,inc_y; 
  60.         public Bullet(float vel, float x, float y, float ang){
  61.             this.vel = vel; this.x = x; this.y = y; this.ang = ang;
  62.             this.inc_x = (float) Math.cos(ang);
  63.             this.inc_y = (float) Math.sin(ang);
  64.         }
  65.  
  66.     }
  67. }
  68.  
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement