public void followHuman() { int dist = 400; Actor kill = null; if (!getObjectsInRange(dist, Player.class).isEmpty()) { for (Object obj : getObjectsInRange(dist, Player.class)) { Actor guy = (Actor) obj; int guyDist = (int) Math.hypot(guy.getX() - getX(), guy.getY() - getY()); if (kill == null || guyDist < dist) { kill = guy; dist = guyDist; } } turnTowards(kill.getX(), kill.getY()); } }
public boolean checkCollision(Actor actor) { int actorX = actor.toPixel(actor.getX()); int actorY = actor.toPixel(actor.getY()); int dx = actorX - this.x; int dy = actorY - this.y; int dist = (int) Math.sqrt((double) (dx * dx + dy * dy)); return dist <= this.r; }
/** * If a target exist, status would appear above the target until the count down duration is 0. If * target is removed the status is removed as well. */ public void act() { if (target != null) // if a target is assigned to a status { if (target.getWorld() != null) { setLocation(target.getX(), target.getY() - OFFSET); // offset myImage above the actor update(1); // update the image with the actual countdown(not controlled by keyboard) } else removeStatus(); // remove status along with actor } }
/** * Method climbRope * * @param num Amount to move. Positive for up, negative for down. */ public void climbRope(int num) { Actor rope = getOneIntersectingObject(Rope.class); if (rope != null) { curSpeed = 0; // Prevents player from flying off rope from momentum onRope = true; if (num > 0 && (rope.getY() - rope.getImage().getHeight() / 2) - getY() < -30) { // Only allows up movement if the player is under the top of the rope setLocation(getX(), getY() - num); } // If the player moves down the rope, off the length of the rope, the player // is considered off the rope else if (num < 0) { setLocation(getX(), getY() - num); if (getY() - (rope.getY() + rope.getImage().getHeight() / 2) > 0) { onRope = false; } } } }
@Override public void run() { // TODO Auto-generated method stub while (runner.isAlive()) { actors = one.getActors(); // one.go(); for (int z = 0; z < AList.size(); z++) { Actor a = AList.get(z); if (a.equals(background) && a.topy() >= 350) { a.setLocation(-5, -360); } if (a.equals(background2) && a.topy() >= 350) { a.setLocation(-5, -360); } if (a.equals(ammo) && a.topy() >= 350) { a.setLocation((int) (330 * Math.random()) + 30, (int) (-1000 * Math.random()) + -1); } if (a.equals(r) && a.topy() >= 350) { a.setLocation(-85, -361); } if (a.equals(r2) && a.topy() >= 350) { a.setLocation(-85, -361); } if (a.equals(r3) && a.topy() >= 350) { a.setLocation(355, -361); } if (a.equals(r4) && a.topy() >= 350) { a.setLocation(355, -361); } for (int ctr = 0; ctr < rs.length; ctr++) { if (a.equals(rs[ctr]) && a.topy() >= 350) { a.setLocation((int) (330 * Math.random()) + 30, (int) (-370 * Math.random()) + -1); } } if (a instanceof Ammo) { if (a.getRect().intersects(one.getLeft()) == true || a.getRect().intersects(one.getRight()) == true || a.getRect().intersects(one.getBottom()) == true) { a.setLocation((int) (330 * Math.random()) + 30, (int) (-370 * Math.random()) + -1); Laser laser = new Laser(one.getX(), one.getY(), "images/laser2.png"); one.arsenal.add(laser); // a.removeSelf(); one.ammohit = true; } } if (a instanceof Rock) { // System.out.println("PodR: "+one.getRect()); // System.out.println("Rock :"+a.getRect()); if (a.getRect().intersects(one.getLeft()) == true || a.getRect().intersects(one.getRight()) == true) { // System.out.println("collision"); one.collision = true; one.newImage("images/Explosion1.gif"); one.y -= 60; tstop = true; speed = one.backSpeed; runner.stop(); } // if(one.collision(a)==true){ // System.out.println("collide"); // } } if (a instanceof PodRacer) { while (a.getY() <= 0) { a.y += 1; } while (a.getY() >= 288) { a.y += -1; } } else { a.move(); } if (a instanceof Laser) { Laser temp = (Laser) a; for (int ctr = 0; ctr < rs.length; ctr++) { if (a.getRect().intersects(rs[ctr].getRect()) == false || a.boty() >= 0) { // System.out.println("test"); temp.Shot(); } if (a.boty() <= 0) { mtemp = temp; laserremove = true; } if (a.getRect().intersects(rs[ctr].getRect())) { mtemp = temp; laserremove = true; rs[ctr].setLocation( (int) (330 * Math.random()) + 30, (int) (-370 * Math.random()) + -1); one.rockhit = true; } } } } if (laserremove == true) { laserremove = false; // System.out.println("test"); // one.arsenal.remove(0); mtemp.removeSelf(); } try { speed = one.backSpeed; runner.sleep(speed); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // run(); } }