package shootergame.util.math.map; import java.util.ArrayList; import java.util.Iterator; import shootergame.util.math.vec.Vec2i; public class Map2D implements Iterable> { private ArrayList> elements; private Map2DElement element_last = null; private IMap2D map2d_i; public Map2D(IMap2D map2d_i) { // Store some values this.map2d_i = map2d_i; this.elements = new ArrayList>(); } public void set(Vec2i pos, T o) { if(element_last != null) { if(element_last.pos.equal(pos)) { element_last.o = o; return; } } // Loop over the elements for(Map2DElement e : this.elements) { // Set the object if these positions are the same if(e.pos.equal(pos)) { element_last = e; e.o = o; return; } } // Create an element and add it Map2DElement e = new Map2DElement(pos, o); this.elements.add(e); element_last = e; } public T get(Vec2i pos) { if(element_last != null) { if(element_last.pos.equal(pos)) { return element_last.o; } } // Loop over the elements for(Map2DElement e : this.elements) { // Send back the object if these positions are the same if(e.pos.equal(pos)) { element_last = e; return e.o; } } // Send back an empty object return this.map2d_i.getEmpty(pos); } public void remove(Vec2i pos) { if(element_last != null) { if(element_last.pos.equal(pos)) { this.elements.remove(element_last); } } // Loop over the elements for(int i=0;i e = this.elements.get(i); // Delete the item if the position is the same if(e.pos.equal(pos)) { this.elements.remove(e); } } element_last = null; } public boolean contains(Vec2i pos) { if(element_last != null) { if(element_last.pos.equal(pos)) { return true; } } // Loop over the elements for(int i=0;i e = this.elements.get(i); // Return true if the position is the same if(e.pos.equal(pos)) { return true; } } // Return false if nothing was found return false; } @Override public Iterator> iterator() { return new Iterator>() { private int i = 0; @Override public boolean hasNext() { return i < elements.size(); } @Override public Map2DElement next() { Map2DElement o = elements.get(i); i++; return o; } }; } }