public void goPath(Timer timer) { update(timer); if (position.equals(target) && (pCount == p.getLength() - 1)) { pathing = false; } else if (position.equals(target)) { pCount++; target.set(p.getX(pCount), p.getY(pCount)); } }
public void update(GameContainer gc, int frame) { timr += frame; if (timr > 500) { if (path != null) { pathIdx = Math.min(path.getLength(), pathIdx + 1); pathStep = path.getStep(pathIdx - 1); start.setX(pathStep.getX()); start.setY(pathStep.getY()); } timr = 0; } }
private Path updatePath() { // playMap.clearVisit(); System.out.println("Crawler = " + start); path = pathFinder.findPath(this, start.getX(), start.getY(), finish.getX(), finish.getY()); System.out.println( "UpdatePath : " + (path == null ? "none" : " path length " + path.getLength())); pathIdx = 0; return path; }
public void render(GameContainer gc, Graphics gfx) throws SlickException { Point pt = playMap.getPos(); if (path != null) { for (int i = 0; i < path.getLength(); i++) { Path.Step step = path.getStep(i); gfx.setColor(Color.white); gfx.drawRect( pt.getX() + step.getX() * playMap.getTileWidth(), pt.getY() + step.getY() * playMap.getTileHeight(), playMap.getTileWidth(), playMap.getTileHeight()); } } if (pathStep != null) { Point foe = new Point(pt); gfx.drawOval( foe.getX() + pathStep.getX() * playMap.getTileWidth() + 4, foe.getY() + pathStep.getY() * playMap.getTileHeight() + 4, 9, 9); } }
private Pos popPath(MobileEntity me) { int px = 0, py = 0; if (this.currentPath == null || currentPathIndex >= this.currentPath.getLength() || this.currentPath.getLength() < 1) { this.currentPath = null; return null; } Step s = currentPath.getStep(currentPathIndex); px = s.getX(); py = s.getY(); Pos nextCell = new Pos(px, py); me.desiredSubcell = me.currentSubcell; if (!me.world.blockingEntityMap.isSubcellFree(nextCell, me.desiredSubcell)) { me.desiredSubcell = me.world.blockingEntityMap.getFreeSubCell(nextCell, me.currentSubcell); if (me.desiredSubcell == null) { me.desiredSubcell = me.currentSubcell; } } if (!me.canEnterCell(nextCell)) { // This building we ignore if (this.ignoreBuilding != null && me.world.getBuildingInCell(nextCell) == this.ignoreBuilding) { this.hasNotifiedBlocker = false; this.hasWaited = false; return null; } // See if we close enough float dx = destCell.getX() - nextCell.getX(); float dy = destCell.getY() - nextCell.getY(); if (dx * dx + dy * dy <= this.destRange * this.destRange) { // System.out.println("We're close enough. Range: " + Math.sqrt(dx * dx + dy * dy) + " <= " // + this.destRange); this.currentPathIndex = this.currentPath.getLength(); // stop and skip all path this.currentPath = null; return null; } // Notify all friendly blockers inside cell if (!this.hasNotifiedBlocker) { for (Influence i : me.world.blockingEntityMap.getCellInfluences(nextCell)) { Entity blocker = i.entity; if (blocker instanceof MobileEntity) { // Notify blocker if (blocker != null && ((MobileEntity) blocker).isFrendlyTo(me)) { ((MobileEntity) blocker).notifyBlocking(me); } } } this.hasNotifiedBlocker = true; } // Wait a bit if (!this.hasWaited) { this.waitTicksRemaining = me.getWaitAverageTime() + me.world.getRandomInt(-me.getWaitSpreadTime(), me.getWaitSpreadTime()); // System.out.println("Waiting time: " + this.waitTicksRemaining); this.hasWaited = true; ((EntityInfantry) me).setCurrentAnimationState(AnimationState.WAITING); } if (--this.waitTicksRemaining >= 0) { // We're waiting now return null; } // We're totally blocked, try to calculate new path chooseNewPath(me); return null; } ((EntityInfantry) me).setCurrentAnimationState(AnimationState.MOVING); if (--this.ticksBeforeRepath <= 0) { this.ticksBeforeRepath = this.REPATHING_INTERVAL_TICKS; chooseNewPath(me); } this.currentPathIndex++; this.hasNotifiedBlocker = false; this.hasWaited = false; return nextCell; }
public void setPath(Path p) { pCount = 0; this.p = p; target.set(p.getX(pCount), p.getY(pCount)); pathing = true; }