Hello, (java noob warning)
I was reading about generics(works like templates in c++) and decided to write a generic LinkedArray Class (I plan to use this not only for bullets but particles and enemies).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| import java.util.ArrayList;
public class LinkedArray<T> {
private ArrayList<T> elements = new ArrayList<T>(); private int size; private int freeElements = 0; private int activeElements = 0; private int[] freeElementIndex; public LinkedArray( int size, T e ) { this.size = size; this.activeElements = 0; this.freeElements = size; this.freeElementIndex = new int[size]; elements.ensureCapacity(size); for( int i = 0; i < size; i++ ) { elements.add(e); } for( int i = 0; i < size; i++ ) { freeElementIndex[i] = i; } } public int getSize() { return size; } public int getActiveElements() { return activeElements; } public void add( T e ) { if( freeElements == 0 ) { return; } freeElements--; int index = freeElementIndex[freeElements]; elements.set( index, e ); activeElements++; } void remove( int index ) { freeElementIndex[freeElements] = index; freeElements++; activeElements--; }
public T get( int index ) { return elements.get( index ); } } |
And here's how it is used...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
import java.awt.Graphics2D;
public class BulletFactory { private LinkedArray<Bullet> bullets; public BulletFactory( int size ) { Bullet b = new Bullet(); bullets = new LinkedArray<Bullet>( size, b ); } public int getSize() { return bullets.getSize(); } public int getActiveBullets() { return bullets.getActiveElements(); } public void addBullet( Bullet bullet ) { bullets.add( bullet ); } void removeBullet( int index ) { bullets.remove( index ); } public void updateBullets() { for( int i = 0; i < bullets.getSize(); i++ ) { Bullet b = bullets.get(i); if( bullets.get(i).isActive() ) { b.update(); if( (b.position.x < 0) || (b.position.x > Globals.SCREEN_WIDTH) || (b.position.y < 0) || (b.position.y > Globals.SCREEN_HEIGHT) ) { b.kill() ; bullets.remove(i); } } } } public void renderBullets( Screen screen, Graphics2D g, ImageAtlas imageAtlas ) { for( int i = 0; i < bullets.getSize(); i++ ) { Bullet b = bullets.get(i); if( b.isActive() ) { int frame = b.getFrame(); g.drawImage( imageAtlas.getSprite(frame), (int)b.position.x - imageAtlas.getSprite(frame).getWidth()/2, (int)b.position.y - imageAtlas.getSprite(frame).getHeight()/2, screen ); } } } }
|
My questions are:
1. Why does updateBullets() and renderBullets() work? Since I made a local copy of the bullet b from bullets.get(index). Does Bullet b and bullets.get(i) share the same reference and is "deep-copied"?
*Note that I did that code out of curiosity and crossed my fingers it works.
2. Why can't I use normal arrays (vs arrayList ) when doing generic classes?
Thanks!