Exemple #1
0
  @Override
  public void renderGameLoop(Graphics2D g) {
    // TODO Auto-generated method stub
    super.renderGameLoop(g);

    Point levelRenderOffset =
        new Point(
            (int) Math.round((this.getCanvasWidth() / 16) * 0.5),
            (int) Math.round((this.getCanvasHeight() / 16) * 0.5));

    level.highlightTile = new Point(Math.round(player.origin.x), Math.round(player.origin.y));
    level.render(g, player.origin.x - levelRenderOffset.x, player.origin.y - levelRenderOffset.y);

    for (Projectile p : projectiles) {
      Point2f renderPoint =
          new Point2f(
              (p.origin.x - player.origin.x) * 16 - levelRenderOffset.x + 8,
              (p.origin.y - player.origin.y) * 16 - levelRenderOffset.y + 8);
      g.drawImage(
          p.getImage(),
          (this.getCanvasWidth() / 2) + Math.round(renderPoint.x),
          (this.getCanvasHeight() / 2) + Math.round(renderPoint.y),
          null);
    }

    player.render(g, (this.getCanvasWidth() / 2) - 8, (this.getCanvasHeight() / 2));

    for (MobAI mob : mobs) {
      Point2f renderPoint =
          new Point2f(
              (mob.origin.x - player.origin.x) * 16 - levelRenderOffset.x,
              (mob.origin.y - player.origin.y) * 16 - levelRenderOffset.y + 8);
      mob.render(
          g,
          (this.getCanvasWidth() / 2) + Math.round(renderPoint.x),
          (this.getCanvasHeight() / 2) + Math.round(renderPoint.y));

      if (mob.health < mob.maxHealth)
        drawHealth(
            g,
            mob.health / (float) mob.maxHealth,
            (this.getCanvasWidth() / 2) + Math.round(renderPoint.x),
            (this.getCanvasHeight() / 2) + Math.round(renderPoint.y));
    }
  }
Exemple #2
0
  @Override
  public void updateGameLoop(long timeSinceLastFrame) {
    // TODO Auto-generated method stub
    super.updateGameLoop(timeSinceLastFrame);

    time += (timeSinceLastFrame / 1000000);
    realtime += time / 1000;
    if (time > secondsPerDay) {
      time = time % secondsPerDay;

      /* daybreak! spawn more items and mobs */
      for (int i = 0; i < mobSpawnCount; i++) {
        this.mobs.add((MobAI) level.spawnMob());
      }

      mobSpawnCount += 20;

      Item ammo = new AmmoPack();
      ammo.origin = level.getSpawnLocation();
      this.level.addItem(ammo);

      HealthPack h = new HealthPack();
      h.origin = this.level.randomSpawnPoint();
      this.level.addItem(h);
    }

    ArrayList<Projectile> died = new ArrayList<Projectile>();
    ArrayList<Mob> diedMobs = new ArrayList<Mob>();
    for (Projectile p : projectiles) {
      p.update(timeSinceLastFrame);

      for (Mob m : mobs) {
        if (m.hitTest(p) && !diedMobs.contains(m)) {
          died.add(p);
          m.dealDamage(p.damage);
          if (m.health == 0) {
            diedMobs.add(m);
            score++;
          }
        }
      }
      mobs.removeAll(diedMobs);
      diedMobs.clear();

      if (p.ttl < 0) {
        died.add(p);
      }
    }
    projectiles.removeAll(died);

    if (player.weapon != null && player.fireButtonPressed && player.weapon.readyToFire()) {

      Projectile[] p = player.weapon.fire(player);
      for (Projectile projectile : p) {
        projectiles.add(projectile);
      }
    }

    player.update(timeSinceLastFrame);

    for (MobAI mob : mobs) {
      Vector2f dist = new Vector2f(mob.origin.x - player.origin.x, mob.origin.y - player.origin.y);
      if (Math.abs(dist.x) > Math.abs(dist.y)) {
        mob.setDirection((dist.x < 0) ? Sprite.Direction.RIGHT : Sprite.Direction.LEFT);

      } else {
        mob.setDirection((dist.y < 0) ? Sprite.Direction.DOWN : Sprite.Direction.UP);
      }
      mob.update(timeSinceLastFrame);
      if (mob.hitTest(player.origin, 2)) {
        player.dealDamage(1);
      }
    }
    if (player.health == 0) {
      this.launcher.setGameWindow(new ZedGameOverMenu(this.score));
    }

    for (Item item : level.items) {
      if (item != null) item.update(timeSinceLastFrame);
    }
  }