コード例 #1
1
ファイル: EdgePortal.java プロジェクト: BackupTheBerlios/ty2
  public void arrive(Thing t) {

    Map map = getMap();

    if (map == null) throw new Error("EdgePortal Error - no map specified");

    int tx = -1;
    int ty = -1;
    int i = 0;

    while (tx == -1) {
      switch (RPG.d(5)) {
        case 1:
          if ((side & NORTH) > 0) {
            ty = 0;
            tx = RPG.r(map.getWidth());
          }
          ;
          break;
        case 2:
          if ((side & SOUTH) > 0) {
            ty = map.getHeight() - 1;
            tx = RPG.r(map.getWidth());
          }
          ;
          break;
        case 3:
          if ((side & WEST) > 0) {
            tx = 0;
            ty = RPG.r(map.getHeight());
          }
          ;
          break;
        case 4:
          if ((side & EAST) > 0) {
            tx = map.getWidth() - 1;
            ty = RPG.r(map.getHeight());
          }
          ;
          break;
        case 5:
          if ((side & ZONE) > 0) {
            tx = RPG.r(map.getWidth());
            ty = RPG.r(map.getHeight());
          }
          ;
          break;
      }

      i++;
      if (i > 5000)
        throw new Error("EdgePortal Error - unable to find space to add " + t.getName());

      if ((tx >= 0) && (map.isBlocked(tx, ty))) tx = -1;
    }
    ;

    t.moveTo(map, tx, ty);
  }
コード例 #2
0
  public void moveForward(int spaces) {
    // change y +
    if (me.direction.equals("N")) {
      int i = 1;
      while (!map.isBlocked(me.x, me.y + 1) && i <= spaces) {
        me.y += 1;
        i++;
      }
      // went too far
      if (i < spaces) {
        explode();
      }
    }

    // change y -
    else if (me.direction.equals("S")) {
      int i = 1;
      while (!map.isBlocked(me.x, me.y - 1) && i <= spaces) {
        me.y -= 1;
        i++;
      }

      // went too far
      if (i < spaces) {
        explode();
      }
    }

    // change x +
    else if (me.direction.equals("E")) {
      int i = 1;
      while (!map.isBlocked(me.x + 1, me.y) && i <= spaces) {
        me.x += 1;
        i++;
      }

      // went too far
      if (i < spaces) {
        explode();
      }
    }

    // change x -
    else if (me.direction.equals("W")) {
      int i = 1;

      while (!map.isBlocked(me.x - 1, me.y) && i <= spaces) {
        me.x -= 1;
        i++;
      }

      // went too far
      if (i < spaces) {
        explode();
      }
    } else {
      // error - do nothing
    }
  }