@Override public void tankMove(Coordinates delta) { for (int i = 0; i < this.elements.length; i++) { Movable toMove = null; try { toMove = (Movable) this.elements[i]; } catch (ClassCastException e) { continue; } if (toMove == null) continue; if (toMove.getType() != Type.TANK) continue; if (!(toMove.isAlive())) continue; try { BoundingBox toMoveNextBb = toMove.getArea().translate(delta); BoundingBox gridBb = new BoundingBox(new Coordinates(0, 0), this.maxSize); BoundingBox intersection = toMoveNextBb.intersection(gridBb); if (!(intersection.equals(toMoveNextBb))) return; toMove.move(delta); } catch (NegativeSizeException e) { // ignore this, nothing is moved } } }
protected void dragWholeShape(DragSelectEvent dragEvent, Movable dragObject) { View view = getWwd().getView(); EllipsoidalGlobe globe = (EllipsoidalGlobe) getWwd().getModel().getGlobe(); // Compute ref-point position in screen coordinates. Position refPos = dragObject.getReferencePosition(); if (refPos == null) return; Vec4 refPoint = globe.computePointFromPosition(refPos); Vec4 screenRefPoint = view.project(refPoint); // Compute screen-coord delta since last event. int dx = dragEvent.getPickPoint().x - dragEvent.getPreviousPickPoint().x; int dy = dragEvent.getPickPoint().y - dragEvent.getPreviousPickPoint().y; // Find intersection of screen coord ref-point with globe. double x = screenRefPoint.x + dx; double y = dragEvent.getMouseEvent().getComponent().getSize().height - screenRefPoint.y + dy - 1; Line ray = view.computeRayFromScreenPoint(x, y); Intersection inters[] = globe.intersect(ray, refPos.getElevation()); if (inters != null) { // Intersection with globe. Move reference point to the intersection point. Position p = globe.computePositionFromPoint(inters[0].getIntersectionPoint()); dragObject.moveTo(p); } }
protected void doMoveAirspaceLaterally( WorldWindow wwd, Airspace airspace, Point mousePoint, Point previousMousePoint) { // Intersect a ray throuh each mouse point, with a geoid passing through the reference // elevation. Since // most airspace control points follow a fixed altitude, this will track close to the intended // mouse position. // If either ray fails to intersect the geoid, then ignore this event. Use the difference // between the two // intersected positions to move the control point's location. if (!(airspace instanceof Movable)) { return; } Movable movable = (Movable) airspace; View view = wwd.getView(); Globe globe = wwd.getModel().getGlobe(); Position refPos = movable.getReferencePosition(); if (refPos == null) return; // Convert the reference position into a cartesian point. This assumes that the reference // elevation is defined // by the airspace's lower altitude. Vec4 refPoint = null; if (airspace.isTerrainConforming()[LOWER_ALTITUDE]) refPoint = wwd.getSceneController().getTerrain().getSurfacePoint(refPos); if (refPoint == null) refPoint = globe.computePointFromPosition(refPos); // Convert back to a position. refPos = globe.computePositionFromPoint(refPoint); Line ray = view.computeRayFromScreenPoint(mousePoint.getX(), mousePoint.getY()); Line previousRay = view.computeRayFromScreenPoint(previousMousePoint.getX(), previousMousePoint.getY()); Vec4 vec = AirspaceEditorUtil.intersectGlobeAt(wwd, refPos.getElevation(), ray); Vec4 previousVec = AirspaceEditorUtil.intersectGlobeAt(wwd, refPos.getElevation(), previousRay); if (vec == null || previousVec == null) { return; } Position pos = globe.computePositionFromPoint(vec); Position previousPos = globe.computePositionFromPoint(previousVec); LatLon change = pos.subtract(previousPos); movable.move(new Position(change.getLatitude(), change.getLongitude(), 0.0)); this.fireAirspaceMoved(new AirspaceEditEvent(wwd, airspace, this)); }
private MovableTestWindow() throws InterruptedException { MatchConstants.init(new NetworkTimer(true), 1000); MatchConstants.clock().startExecution(); MovableTestsMap grid = new MovableTestsMap(100, 100, PLAYER_0); MapInterfaceConnector connector = TestUtils.openTestWindow(grid); movable = new Movable( grid.getMovableGrid(), EMovableType.PIONEER, new ShortPoint2D(49, 50), PLAYER_0); movable.setSelected(true); connector.setSelection(new SelectionSet(movable)); connector.addListener( new IMapInterfaceListener() { @Override public void action(Action action) { switch (action.getActionType()) { case MOVE_TO: movable.moveTo(((PointAction) action).getPosition()); break; case SPEED_FASTER: MatchConstants.clock().multiplyGameSpeed(1.2f); break; case SPEED_SLOWER: MatchConstants.clock().multiplyGameSpeed(1 / 1.2f); break; case FAST_FORWARD: MatchConstants.clock().fastForward(); break; default: break; } } }); grid.getMovableGrid().dropMaterial(new ShortPoint2D(40, 40), EMaterialType.PLANK, true); grid.getMovableGrid().dropMaterial(new ShortPoint2D(60, 60), EMaterialType.STONE, true); new Movable(grid.getMovableGrid(), EMovableType.BEARER, new ShortPoint2D(30, 30), PLAYER_0); new Movable(grid.getMovableGrid(), EMovableType.BEARER, new ShortPoint2D(31, 31), PLAYER_0); new Movable(grid.getMovableGrid(), EMovableType.BEARER, new ShortPoint2D(32, 32), PLAYER_0); new Movable(grid.getMovableGrid(), EMovableType.BEARER, new ShortPoint2D(33, 33), PLAYER_0); new Movable(grid.getMovableGrid(), EMovableType.BEARER, new ShortPoint2D(50, 50), PLAYER_0); { // test automatic distribution of many movables next to each other for (int x = 30; x < 40; x++) { for (int y = 80; y < 90; y++) { new Movable(grid.getMovableGrid(), EMovableType.BEARER, new ShortPoint2D(x, y), PLAYER_0); } } } { Thread.sleep(3000); // circle of three movables blocking each others path Movable m1 = new Movable( grid.getMovableGrid(), EMovableType.PIONEER, new ShortPoint2D(50, 65), PLAYER_0); Movable m2 = new Movable( grid.getMovableGrid(), EMovableType.PIONEER, new ShortPoint2D(51, 65), PLAYER_0); Movable m3 = new Movable( grid.getMovableGrid(), EMovableType.PIONEER, new ShortPoint2D(50, 64), PLAYER_0); m1.moveTo(new ShortPoint2D(52, 65)); m2.moveTo(new ShortPoint2D(49, 63)); m3.moveTo(new ShortPoint2D(50, 66)); } }
protected void doMoveAirspaceVertically( WorldWindow wwd, Airspace airspace, Point mousePoint, Point previousMousePoint) { // Find the closest points between the rays through each screen point, and the ray from the // control point // and in the direction of the globe's surface normal. Compute the elevation difference between // these two // points, and use that as the change in airspace altitude. // // If the state keepControlPointsAboveTerrain is set, we prevent the control point from passing // any lower than // the terrain elevation beneath it. if (!(airspace instanceof Movable)) { return; } Movable movable = (Movable) airspace; Position referencePos = movable.getReferencePosition(); if (referencePos == null) return; Vec4 referencePoint = wwd.getModel().getGlobe().computePointFromPosition(referencePos); Vec4 surfaceNormal = wwd.getModel() .getGlobe() .computeSurfaceNormalAtLocation( referencePos.getLatitude(), referencePos.getLongitude()); Line verticalRay = new Line(referencePoint, surfaceNormal); Line screenRay = wwd.getView() .computeRayFromScreenPoint(previousMousePoint.getX(), previousMousePoint.getY()); Line previousScreenRay = wwd.getView().computeRayFromScreenPoint(mousePoint.getX(), mousePoint.getY()); Vec4 pointOnLine = AirspaceEditorUtil.nearestPointOnLine(verticalRay, screenRay); Vec4 previousPointOnLine = AirspaceEditorUtil.nearestPointOnLine(verticalRay, previousScreenRay); Position pos = wwd.getModel().getGlobe().computePositionFromPoint(pointOnLine); Position previousPos = wwd.getModel().getGlobe().computePositionFromPoint(previousPointOnLine); double elevationChange = previousPos.getElevation() - pos.getElevation(); double[] altitudes = this.getAirspace().getAltitudes(); boolean[] terrainConformance = this.getAirspace().isTerrainConforming(); if (this.isKeepControlPointsAboveTerrain()) { if (terrainConformance[LOWER_ALTITUDE]) { if (altitudes[LOWER_ALTITUDE] + elevationChange < 0.0) elevationChange = 0.0 - altitudes[LOWER_ALTITUDE]; } else { double height = AirspaceEditorUtil.computeLowestHeightAboveSurface( wwd, this.getCurrentControlPoints(), LOWER_ALTITUDE); if (elevationChange <= -height) { elevationChange = -height; } } } altitudes[LOWER_ALTITUDE] += elevationChange; altitudes[UPPER_ALTITUDE] += elevationChange; this.getAirspace().setAltitudes(altitudes[LOWER_ALTITUDE], altitudes[UPPER_ALTITUDE]); this.fireAirspaceMoved(new AirspaceEditEvent(wwd, airspace, this)); }