示例#1
0
  public boolean isFreeTile(int x, int y) {
    for (Entity entity : entities) {
      if (entity.getBump(true).intersects(new Rectangle(x, y, Tile.SIZE, Tile.SIZE))) return false;
    }

    return true;
  }
示例#2
0
  @Override
  public void update(int tick) {
    sortEntities();

    @SuppressWarnings("unchecked")
    List<Entity> sorted = (List<Entity>) entities.clone();
    Collections.reverse(sorted);
    for (Entity entity : sorted) {
      entity.update(tick);
      if (entity.isDead()) {
        if (entity.equals(selectedEntity)) selectedEntity = null;
        entities.remove(entity);
      }
    }

    for (Projectile p : projectiles) {
      p.update(tick);
      if (p.isDead()) projectiles.remove(p);
    }

    for (Animation a : animations) {
      a.update(tick);
      if (a.isDead()) animations.remove(a);
    }
  }
示例#3
0
  @Override
  public void mouseMoved(MouseEvent e) {
    e.translatePoint(-x, -y);
    for (Entity entity : entities) entity.setHovered(false);
    for (Entity entity : entities) if (entity.mouseMoved(e)) break;

    e.translatePoint(x, y);
  }
示例#4
0
  public boolean addEntity(Entity e, boolean initial) {
    for (Entity entity : entities) {
      if (e.getBump(true).intersects(entity.getBump(true))) return false;
    }
    entities.add(e);
    e.onSpawn(initial);

    render();

    return true;
  }
示例#5
0
  @Override
  protected void onDeath() {
    for (Entity e : Game.world.entities) {

      if ((e instanceof Creature)
          && ((Creature) e).getOrigin() != null
          && ((Creature) e).getOrigin().equals(this)) {
        e.kill();
      }
    }
    dead = true;
  }
示例#6
0
  private static void handleAdjacentTiles(Node node) {
    byte[][] neighbors = Game.world.getNeighbors((int) node.t.x, (int) node.t.y);

    for (int i = 0; i < neighbors.length; i++) {
      for (int j = 0; j < neighbors[0].length; j++) {
        if (i == j
            || neighbors[i][j] == Tile.empty.getId()
            || (i == 0 && j == 2)
            || (i == 2 && j == 0)) continue;

        Vector tile = new Vector(node.t.x + i - 1, node.t.y + j - 1);
        Node n =
            new Node(
                node.G + Tile.getTileForId(neighbors[i][j]).G,
                tile.getDistance(target),
                tile,
                node);

        if (closedList.contains(n)) continue;

        if (openList.contains(n) && openList.get(openList.indexOf(n)).G > n.G) {
          openList.get(openList.indexOf(n)).G = n.G;
          openList.get(openList.indexOf(n)).p = node;
        } else if (!openList.contains(n)) {
          boolean free = true;
          for (Entity e : Game.world.entities) {
            if (e.getBump(true)
                    .intersects(
                        tile.x * Tile.SIZE + 8,
                        tile.y * Tile.SIZE + 8,
                        Tile.SIZE - 16,
                        Tile.SIZE - 16)
                && e.isMassive()) {
              free = false;
              break;
            }
          }

          if (free || n.H == 0) openList.add(n);
        }
      }
    }
  }
示例#7
0
  @Override
  public void mousePressed(MouseEvent e) {
    if (Game.currentGame.placedStruct || Game.currentGame.activeStruct != null) return;

    e.translatePoint(-x, -y);
    if (e.getButton() == MouseEvent.BUTTON1) // LMB
    {
      if (selectedEntity != null
          && selectedEntity instanceof Struct
          && ((Struct) selectedEntity).guiPoint != null
          && ((Struct) selectedEntity).components.size() > 0) return;

      selectedEntity = null;
      for (Entity entity : entities) entity.setClicked(false);
      for (Entity entity : entities) {
        if (entity.mousePressed(e)) {
          selectedEntity = entity;
          break;
        }
      }
    } else if (e.getButton() == MouseEvent.BUTTON3
        && selectedEntity != null
        && selectedEntity instanceof Villager) {
      Entity target = null;
      for (Entity entity : entities) {
        if (entity.mousePressed(e)) {
          target = entity;
          break;
        }
      }

      setVillagerTarget((Villager) selectedEntity, e.getX(), e.getY(), target);
    } else if (e.getButton() == MouseEvent.BUTTON3) // after Drag
    {
      Entity target = null;
      for (Entity entity : entities) {
        if (entity.isClicked() && entity instanceof Villager) continue;

        if (entity.mousePressed(e)) {
          target = entity;
          break;
        }
      }

      for (Entity entity : entities) {
        if (entity.isClicked() && entity instanceof Villager) {
          setVillagerTarget((Villager) entity, e.getX(), e.getY(), target);
        }
      }
    }

    e.translatePoint(x, y);
  }
示例#8
0
  public void addEntity2(Entity e, boolean initial) {
    entities.add(e);
    e.onSpawn(initial);

    render();
  }
示例#9
0
  @Override
  public void draw(Graphics2D g) {
    AffineTransform old = g.getTransform();
    AffineTransform at = g.getTransform();
    at.translate(x, y);
    g.setTransform(at);

    Rectangle visible = new Rectangle(-x, -y, Game.getWidth(), Game.getHeight());
    try {
      for (int i = 0; i < chunks.length; i++) {
        for (int j = 0; j < chunks[0].length; j++) {
          if (new Rectangle(
                  i * Chunk.SIZE * Tile.SIZE,
                  j * Chunk.SIZE * Tile.SIZE,
                  Chunk.SIZE * Tile.SIZE,
                  Chunk.SIZE * Tile.SIZE)
              .intersects(visible)) chunks[i][j].draw(g);
        }
      }
    } catch (NullPointerException e) {
      return;
    }

    for (Entity e : entities) {
      if (e.getArea(false).intersects(visible)) e.drawEntity(g);
    }

    for (Projectile p : projectiles) p.draw(g);

    for (Animation a : animations) a.draw(g);

    // try
    // {
    // if (AStar.openList != null)
    // {
    // for (Node n : AStar.openList)
    // n.draw(Tile.SIZE, Color.white, g);
    // for (Node n : AStar.closedList)
    // n.draw(Tile.SIZE, Color.blue, g);
    // }
    // }
    // catch (ConcurrentModificationException e)
    // {}
    //
    // for (Entity e : entities)
    // {
    // if (e instanceof Creature)
    // {
    // if (((Creature) e).path != null) ((Creature) e).path.draw(g);
    // }
    // }

    // Color c = g.getColor();
    // g.setColor(Color.darkGray);
    // g.drawRect(Math.round((Game.currentGame.mouse.x - x - Tile.SIZE / 2) / (float) Tile.SIZE) *
    // Tile.SIZE, Math.round((Game.currentGame.mouse.y - y - Tile.SIZE / 2) / (float) Tile.SIZE) *
    // Tile.SIZE, Tile.SIZE, Tile.SIZE);
    // g.setColor(c);

    g.setTransform(old);
  }