Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- 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 checkCollision(Bullet bullet, float x, float y, float w, float h){
- if ( bullet.x >= x && bullet.x <= x+w && bullet.y >= y && bullet.y <= y+h){
- return true;
- }
- return false;
- }
- 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){
- ListIterator<Bullet> a = bullets.listIterator();
- while(a.hasNext()) {
- Bullet i = (Bullet) a.next();
- canvas.save();
- canvas.rotate((float) Math.toDegrees(i.ang),i.x,i.y);
- canvas.drawBitmap(this.img,i.x,i.y,paint);
- canvas.restore();
- }
- }
- }
- public void move(){
- synchronized(this){
- this.c_time++;
- ListIterator<Bullet> a = bullets.listIterator();
- while(a.hasNext()) {
- Bullet i = (Bullet) a.next();
- if (outOfScreen(i) ){
- a.remove();
- }
- i.y += i.inc_y * i.vel;
- i.x += i.inc_x * i.vel;
- }
- }
- }
- public boolean collison(float x, float y, float w, float h, boolean deadcollision){
- synchronized(this){
- this.c_time++;
- ListIterator<Bullet> a = bullets.listIterator();
- while(a.hasNext()) {
- Bullet i = (Bullet) a.next();
- if (checkCollision(i, x, y, w, h)){
- if (deadcollision){
- a.remove();
- }
- return true;
- }
- }
- }
- return false;
- }
- 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;
- 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);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement