package greenfoot.collision;

import greenfoot.TestUtilDelegate;
import greenfoot.World;
import greenfoot.TestObject;
import greenfoot.WorldCreator;
import greenfoot.core.Simulation;
import greenfoot.core.WorldHandler;
import greenfoot.util.GreenfootUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import junit.framework.TestCase;


| | Tests the method that gets the neighbours. | | @author Poul Henriksen | public class NeighbourTest extends TestCase{ private World world; @Override protected void setUp() throws Exception { GreenfootUtil.initialise(new TestUtilDelegate()); Simulation.initialize(); } @SuppressWarnings("unchecked") public void testNoWrapDiagonal() { int d = 2; world = WorldCreator.createWorld(10, 10, 10); WorldHandler.initialise(); WorldHandler.getInstance().setWorld(world, false); int xStart = 9; int yStart = 9; TestObject me = new TestObject(); world.addObject(me,xStart, yStart); Collection neighbours = createNeigboursWithDiagonal(xStart, yStart, d); Collection c = me.getNeighboursP(d, true, TestObject.class); assertEquals(8, c.size()); assertTrue(c.containsAll(neighbours)); world = WorldCreator.createWorld(10, 10, 10); xStart = 0; yStart = 0; world.addObject(me, xStart, yStart); neighbours = createNeigboursWithDiagonal(xStart, yStart, d); c = me.getNeighboursP(d, true, TestObject.class); assertEquals(8, c.size()); assertTrue(c.containsAll(neighbours)); } @SuppressWarnings("unchecked") private Collection createNeigboursWithDiagonal(int xStart, int yStart, int d) { for (int x = 0; x < world.getWidth(); x++) { for (int y = 0; y < world.getHeight(); y++) { TestObject actor = new TestObject(); world.addObject(actor, x, y); } } Collection neighbours = new ArrayList(); for (int x = xStart - d; x <= xStart + d; x++) { for (int y = yStart - d; y <= yStart + d; y++) { if ( /*!world.isWrapped() && (x < 0 || y < 0 || x >= world.getWidth() || y >= world.getHeight())) { continue; } Collection remove = world.getObjectsAt(x, y, TestObject.class); for (Iterator iter = remove.iterator(); iter.hasNext();) { TestObject element = (TestObject) iter.next(); if (!(x == xStart && y == yStart)) { neighbours.add(element); } } } } return neighbours; } }
top, use, map, class NeighbourTest

.   setUp
.   testNoWrapDiagonal
.   createNeigboursWithDiagonal




103 neLoCode + 3 LoComm